A customizable man-in-the-middle TCP proxy with support for HTTP & HTTPS.
pip install mitm
Note that OpenSSL 1.1.1 or greater is required.
Documentation can be found here.
Using the default values for the MITM
class:
from mitm import MITM, protocol, middleware, crypto
mitm = MITM(
host="127.0.0.1",
port=8888,
protocols=[protocol.HTTP],
middlewares=[middleware.Log], # middleware.HTTPLog used for the example below.
certificate_authority = crypto.CertificateAuthority()
)
mitm.run()
This will start a proxy on port 8888
that is capable of intercepting all HTTP traffic (with support for SSL/TLS) and log all activity.
mitm
can be customized through the implementations of middlewares and protocols.
Middlewares are event-driven hooks that are called when connections are made, requests are sent, responses are received, and connections are closed.
Protocols are implementations on how the data flows between the client and server, and is used to implement application layer protocols and/or more complex extensions.
Using the example above we can send a request to the server via another script:
import requests
proxies = {"http": "http://127.0.0.1:8888", "https": "http://127.0.0.1:8888"}
requests.get("https://httpbin.org/anything", proxies=proxies, verify=False)
Which will lead to the following being logged where mitm
is running in:
2022-06-08 15:07:10 INFO MITM server started on 127.0.0.1:8888.
2022-06-08 15:07:11 INFO Client 127.0.0.1:64638 has connected.
2022-06-08 15:07:11 INFO Client 127.0.0.1:64638 to mitm:
β CONNECT httpbin.org:443 HTTP/1.0
2022-06-08 15:07:12 INFO Client 127.0.0.1:64638 has connected to server 34.206.80.189:443.
2022-06-08 15:07:12 INFO Client 127.0.0.1:64638 to 34.206.80.189:443:
β GET /anything HTTP/1.1
β Host: httpbin.org
β User-Agent: python-requests/2.26.0
β Accept-Encoding: gzip, deflate
β Accept: */*
β Connection: keep-alive
2022-06-08 15:07:12 INFO Server 34.206.80.189:443 to client 127.0.0.1:64638:
β HTTP/1.1 200 OK
β Date: Wed, 08 Jun 2022 19:07:12 GMT
β Content-Type: application/json
β Content-Length: 396
β Connection: keep-alive
β Server: gunicorn/19.9.0
β Access-Control-Allow-Origin: *
β Access-Control-Allow-Credentials: true
β
β {
β "args": {},
β "data": "",
β "files": {},
β "form": {},
β "headers": {
β "Accept": "*/*",
β "Accept-Encoding": "gzip, deflate",
β "Host": "httpbin.org",
β "User-Agent": "python-requests/2.26.0",
β "X-Amzn-Trace-Id": "Root=1-62a0f360-774052c80b60f4ea049f5665"
β },
β "json": null,
β "method": "GET",
β "origin": "xxx.xxx.xxx.xxx",
β "url": "https://httpbin.org/anything"
β }
2022-06-08 15:07:27 INFO Server 34.206.80.189:443 has disconnected.
2022-06-08 15:07:27 INFO Client 127.0.0.1:64638 has disconnected.