Skip to content

Instantly share code, notes, and snippets.

@ks888
Created May 4, 2017 05:02
Show Gist options
  • Save ks888/ab30546a94bb119f823458f9eb286910 to your computer and use it in GitHub Desktop.
Save ks888/ab30546a94bb119f823458f9eb286910 to your computer and use it in GitHub Desktop.
mitmproxy_replace_host.py
import argparse
import sys
from mitmproxy import ctx
class Replacer:
def __init__(self, src, dst):
self.src, self.dst = src, dst
def request(self, flow):
if flow.request.host == self.src:
flow.request.host = self.dst
flow.request.headers["Host"] = self.dst
def response(self, flow):
headers = flow.response.headers
loc_key = "Location"
if loc_key in headers and self.dst in headers[loc_key]:
headers[loc_key] = headers[loc_key].replace(self.dst, self.src)
def start():
if len(sys.argv) < 3:
ctx.log.info('Usage: mitmproxy -s "%s [src domain name] [dst domain name]"' % sys.argv[0])
ctx.log.info('Example: mitmproxy -s "%s example-a.com example-b.com"' % sys.argv[0])
sys.exit(1)
parser = argparse.ArgumentParser()
parser.add_argument("src", type=str)
parser.add_argument("dst", type=str)
args = parser.parse_args()
return Replacer(args.src, args.dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment