Mitmproxy

12.2.1 · active · verified Thu Apr 09

Mitmproxy is an interactive, SSL/TLS-capable intercepting proxy for HTTP/1, HTTP/2, and WebSockets. It allows developers and security researchers to inspect, modify, and replay network traffic. Currently at version 12.2.1, it receives frequent patch and minor updates, with major versions introducing significant breaking changes less often.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic mitmproxy addon that intercepts and modifies HTTP requests and responses. It adds custom headers for traffic to 'example.com'. To run, save this code as a `.py` file (e.g., `myaddon.py`) and execute `mitmproxy -s myaddon.py` in your terminal, then configure your client to use mitmproxy as a proxy.

import os
from mitmproxy import http

class SimpleModifier:
    def request(self, flow: http.HTTPFlow):
        # Modify all requests to example.com
        if "example.com" in flow.request.pretty_url:
            flow.request.headers["X-Modified-By"] = "Mitmproxy-Addon"
            # To redirect, uncomment and adjust:
            # flow.request.host = "www.google.com"
            # flow.request.port = 443
            # flow.request.scheme = "https"
            
        # Access options via flow.options or mitmproxy.ctx.options
        # print(f"Verbosity: {flow.options.verbosity}")

    def response(self, flow: http.HTTPFlow):
        # Modify all responses from example.com
        if flow.response and "example.com" in flow.request.pretty_url:
            flow.response.headers["X-Response-Modified-By"] = "Mitmproxy-Addon"
            # Modify response content (ensure it's bytes)
            # if flow.response.content:
            #     flow.response.content = flow.response.content.replace(b"example", b"modified")

# To run this addon:
# 1. Save it as e.g., `myaddon.py`
# 2. Run mitmproxy from your terminal: `mitmproxy -s myaddon.py`
# 3. Configure your client (browser, app) to proxy through mitmproxy (default: http://127.0.0.1:8080).
#    For HTTPS, install the mitmproxy CA certificate (navigate to mitm.it from the proxied client).

view raw JSON →