diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/complex/dns_spoofing.py | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/examples/complex/dns_spoofing.py b/examples/complex/dns_spoofing.py index c020047f..1fb59f74 100644 --- a/examples/complex/dns_spoofing.py +++ b/examples/complex/dns_spoofing.py @@ -28,22 +28,35 @@ import re parse_host_header = re.compile(r"^(?P<host>[^:]+|\[.+\])(?::(?P<port>\d+))?$") -def request(flow): - if flow.client_conn.ssl_established: - flow.request.scheme = "https" - sni = flow.client_conn.connection.get_servername() - port = 443 - else: - flow.request.scheme = "http" - sni = None - port = 80 - - host_header = flow.request.pretty_host - m = parse_host_header.match(host_header) - if m: - host_header = m.group("host").strip("[]") - if m.group("port"): - port = int(m.group("port")) - - flow.request.host = sni or host_header - flow.request.port = port +class Rerouter: + def requestheaders(self, flow): + """ + The original host header is retrieved early + before flow.request is replaced by mitmproxy new outgoing request + """ + flow.metadata["original_host"] = flow.request.headers["Host"] + + def request(self, flow): + if flow.client_conn.ssl_established: + flow.request.scheme = "https" + sni = flow.client_conn.connection.get_servername() + port = 443 + else: + flow.request.scheme = "http" + sni = None + port = 80 + + host_header = flow.metadata["original_host"] + m = parse_host_header.match(host_header) + if m: + host_header = m.group("host").strip("[]") + if m.group("port"): + port = int(m.group("port")) + + flow.request.headers["Host"] = host_header + flow.request.host = sni or host_header + flow.request.port = port + + +def start(): + return Rerouter() |