This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
rewrite.py
executable file
·87 lines (80 loc) · 3.51 KB
/
rewrite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#####################################################
## Content rewriting script for mitmproxy 4
## Other versions of mitmproxy may not be compatible
#####################################################
#
# BEGIN LICENSE #
#
# CERT Tapioca
#
# Copyright 2018 Carnegie Mellon University. All Rights Reserved.
#
# NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE
# ENGINEERING INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS.
# CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER
# EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED
# TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY,
# OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON
# UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO
# FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
#
# Released under a BSD (SEI)-style license, please see license.txt or
# contact [email protected] for full terms.
#
# [DISTRIBUTION STATEMENT A] This material has been approved for
# public release and unlimited distribution. Please see Copyright
# notice for non-US Government use and distribution.
# CERT(R) is registered in the U.S. Patent and Trademark Office by
# Carnegie Mellon University.
#
# DM18-0637
#
# END LICENSE #
# See https://github.com/mitmproxy/mitmproxy/tree/master/examples for more
# examples as to what you can do with mitmproxy scripts
# This file can be edited while mitmproxy is running. It will pick up changes
# on file save
from mitmproxy import http
req_before = 'Content to find in intercepted requests'
req_after = 'Content to replace the above with'
resp_before = 'Content to find in intercepted responses'
resp_after = 'Content to replace the above with'
#calcbytes = None
#with open("calc.exe", "rb") as f:
# calcbytes = f.read()
def response(flow: http.HTTPFlow) -> None:
try:
# Older mitmproxy version
flow.response.replace(resp_before, resp_after)
except AttributeError:
# Newer mitmproxy version
# https://stackoverflow.com/questions/64111152/issue-converting-older-mitmproxy-scripts-to-work-on-5-2-error-on-replace-and-c
if flow.response.content:
try:
# Try binary replacement first
flow.response.content = flow.response.content.replace(resp_before, resp_after)
except TypeError:
# Then fall back to text replacement
flow.response.text = flow.response.text.replace(resp_before, resp_after)
def request(flow: http.HTTPFlow) -> None:
try:
# Older mitmproxy version
flow.request.replace(req_before, req_after)
except AttributeError:
# Newer mitmproxy version
if flow.request.content:
try:
# Try binary replacement first
flow.request.content = flow.request.content.replace(req_before, req_after)
except TypeError:
# Then fall back to text replacement
flow.request.text = flow.request.text.replace(req_before, req_after)
#flow.request.headers['User-Agent'] = 'Custom User-Agent'
## Below is an example that will answer any question for a URI that ends in '.exe'
## with the bytes from calc.exe (uncomment the above as well)
# if flow.request.method == 'GET' and flow.request.url.endswith('.exe'):
# flow.response = http.HTTPResponse.make(
# 200, # (optional) status code
# calcbytes, # (optional) content
# {'Content-Type': 'application/octet-stream'} # (optional) headers
# )