2015-02-27 11:55:16 +00:00
|
|
|
"""
|
2017-03-09 00:52:58 +00:00
|
|
|
This script makes it possible to use mitmproxy in scenarios where IP spoofing
|
|
|
|
has been used to redirect connections to mitmproxy. The way this works is that
|
|
|
|
we rely on either the TLS Server Name Indication (SNI) or the Host header of the
|
|
|
|
HTTP request. Of course, this is not foolproof - if an HTTPS connection comes
|
|
|
|
without SNI, we don't know the actual target and cannot construct a certificate
|
|
|
|
that looks valid. Similarly, if there's no Host header or a spoofed Host header,
|
|
|
|
we're out of luck as well. Using transparent mode is the better option most of
|
|
|
|
the time.
|
2015-02-27 11:55:16 +00:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
mitmproxy
|
|
|
|
-p 443
|
2015-08-31 11:43:30 +00:00
|
|
|
-s dns_spoofing.py
|
|
|
|
# Used as the target location if neither SNI nor host header are present.
|
2018-06-26 11:09:45 +00:00
|
|
|
--mode reverse:http://example.com/
|
2017-02-23 10:49:18 +00:00
|
|
|
# To avoid auto rewriting of host header by the reverse proxy target.
|
2018-10-25 21:15:55 +00:00
|
|
|
--set keep_host_header
|
2015-08-31 11:43:30 +00:00
|
|
|
mitmdump
|
|
|
|
-p 80
|
2018-06-26 11:09:45 +00:00
|
|
|
--mode reverse:http://localhost:443/
|
2015-02-27 11:55:16 +00:00
|
|
|
|
2015-08-31 11:43:30 +00:00
|
|
|
(Setting up a single proxy instance and using iptables to redirect to it
|
|
|
|
works as well)
|
2015-02-27 11:55:16 +00:00
|
|
|
"""
|
2015-08-31 11:43:30 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
# This regex extracts splits the host header into host and port.
|
|
|
|
# Handles the edge case of IPv6 addresses containing colons.
|
|
|
|
# https://bugzilla.mozilla.org/show_bug.cgi?id=45891
|
|
|
|
parse_host_header = re.compile(r"^(?P<host>[^:]+|\[.+\])(?::(?P<port>\d+))?$")
|
2015-02-27 11:55:16 +00:00
|
|
|
|
2017-01-21 08:39:34 +00:00
|
|
|
|
2017-01-20 22:48:26 +00:00
|
|
|
class Rerouter:
|
2017-01-20 22:43:53 +00:00
|
|
|
def request(self, flow):
|
2018-01-05 21:46:23 +00:00
|
|
|
if flow.client_conn.tls_established:
|
2017-01-20 22:43:53 +00:00
|
|
|
flow.request.scheme = "https"
|
|
|
|
sni = flow.client_conn.connection.get_servername()
|
|
|
|
port = 443
|
|
|
|
else:
|
|
|
|
flow.request.scheme = "http"
|
|
|
|
sni = None
|
|
|
|
port = 80
|
|
|
|
|
2017-02-23 10:49:18 +00:00
|
|
|
host_header = flow.request.host_header
|
2017-01-29 13:33:53 +00:00
|
|
|
m = parse_host_header.match(host_header)
|
2017-01-20 22:43:53 +00:00
|
|
|
if m:
|
2017-01-29 13:33:53 +00:00
|
|
|
host_header = m.group("host").strip("[]")
|
2017-01-20 22:43:53 +00:00
|
|
|
if m.group("port"):
|
|
|
|
port = int(m.group("port"))
|
|
|
|
|
2017-02-17 22:43:18 +00:00
|
|
|
flow.request.host_header = host_header
|
2017-01-29 13:33:53 +00:00
|
|
|
flow.request.host = sni or host_header
|
2017-01-20 22:43:53 +00:00
|
|
|
flow.request.port = port
|
|
|
|
|
2017-01-21 08:39:34 +00:00
|
|
|
|
2017-04-25 07:06:24 +00:00
|
|
|
addons = [Rerouter()]
|