Last active
July 26, 2018 12:42
-
-
Save honboubao/fa756e1f90deefbce1d9c16db534f3ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mitmproxy addon to serve modified content | |
# how to use: | |
# 1. create replace.txt file in your current working directory | |
# 2. add urls you want to replace to that file, one line per url; urls can be regular expresions; lines starting with # are ignored | |
# 3. start mitmproxy with parameter -s mitmproxy_replace.py to load this addon | |
# 4. configure your OS or browser to use your running mitmproxy instance (default port 8080) | |
# 5. install mitmproxy certificate to allow proxying https by navigating to http://mitm.it/ | |
# 5. navigate to the url you want to replace; a dump of the response content is created in the replace folder in your current working directory | |
# 6. modify the content dump and reload the page | |
from mitmproxy import ctx | |
import re | |
import os | |
# import base64 | |
import errno | |
class Replacer: | |
def __init__(self): | |
self.matchers = [] | |
self.listFilePath = 'replace.txt' | |
self.copiesDir = 'replace' | |
self.loadedFileTimestamp = 0 | |
def isListFileModified(self): | |
try: | |
return self.loadedFileTimestamp < os.path.getmtime(self.listFilePath) | |
except OSError: | |
ctx.log.info("Replacer addon list file could not be opened") | |
return False | |
def parseListFile(self): | |
if self.isListFileModified(): | |
self.loadedFileTimestamp = os.path.getmtime(self.listFilePath) | |
with open(self.listFilePath) as f: | |
content = f.readlines() | |
self.matchers = [re.compile("^{}$".format(x.strip())) for x in content if x.strip() and not x.strip().startswith("#")] | |
def getFileName(self, flow): | |
return flow.request.host + re.sub(r"[^a-zA-Z0-9-+\./_]", "", flow.request.path).replace("/", "__") # + "_" + base64.urlsafe_b64encode(flow.request.url.encode()).decode() | |
def response(self, flow): | |
self.parseListFile() | |
if any(re.match(m, flow.request.url) for m in self.matchers): | |
try: | |
os.makedirs(self.copiesDir) | |
except OSError as e: | |
if e.errno != errno.EEXIST: | |
raise | |
ctx.log.alert("replace: " + flow.request.url) | |
filename = self.getFileName(flow) | |
filepath = self.copiesDir + "/" + filename | |
if os.path.isfile(filepath): | |
with open(filepath, "rb", buffering=0) as f: | |
flow.response.content = f.readall() | |
else: | |
with open(filepath, "wb") as f: | |
f.write(flow.response.content) | |
addons = [ | |
Replacer() | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment