mirror of
https://github.com/zhigang1992/mitmproxy.git
synced 2026-04-27 19:15:33 +08:00
Merge pull request #1948 from amm0nite/fix_dns_spoofing_example
Fix for dns_spoofing.py example
This commit is contained in:
@@ -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
|
||||
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"]
|
||||
|
||||
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"))
|
||||
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
|
||||
|
||||
flow.request.host = sni or host_header
|
||||
flow.request.port = port
|
||||
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()
|
||||
|
||||
@@ -103,6 +103,28 @@ class TestScripts(mastertest.MasterTest):
|
||||
m.request(f)
|
||||
assert f.response.content == b"Hello World"
|
||||
|
||||
def test_dns_spoofing(self):
|
||||
m, sc = tscript("complex/dns_spoofing.py")
|
||||
original_host = "example.com"
|
||||
|
||||
host_header = Headers(host=original_host)
|
||||
f = tflow.tflow(req=tutils.treq(headers=host_header, port=80))
|
||||
|
||||
m.requestheaders(f)
|
||||
|
||||
# Rewrite by reverse proxy mode
|
||||
f.request.scheme = "https"
|
||||
f.request.host = "mitmproxy.org"
|
||||
f.request.port = 443
|
||||
|
||||
m.request(f)
|
||||
|
||||
assert f.request.scheme == "http"
|
||||
assert f.request.host == original_host
|
||||
assert f.request.port == 80
|
||||
|
||||
assert f.request.headers["Host"] == original_host
|
||||
|
||||
|
||||
class TestHARDump:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user