Mitmproxy is a man-in-the-middle proxy tool that can be used to intercept, modify, and save HTTP/HTTPS requests. In the form of a command line terminal, the operation is similar to Vim, but also provides the MITmWeb plug-in, is similar to Chrome browser developer mode visualization tool.

It’s an open source tool based on Python, and most importantly it provides a Python API that allows you to control requests and responses through Python code in a way that no other tool can, which is one of the things I like about this tool.

The installation

sudo pip3 install mitmproxy
Copy the code

Start the

mitmproxy
# or specify a port
mitmproxy -p 8888
Copy the code

After mitmProxy is enabled, port 8080 is enabled by default. The mitmproxy command does not support the Windows platform. You need to run the mitmdump or mitmweb command instead. For Windows, you can download the EXE file from the official website to install it.

After the proxy is set up in the mobile phone or browser, packet capture analysis can be carried out. Open the browser to visit a website and mitmProxy will see the following effect:

The request method is GET, the status code returned is 200, and the port of the proxy is 8080. You can switch to different requests by J and K keys, and press Enter to see the details of the currently selected request, including three parts. Request and Response and Detail

Mitmproxy shortcuts

? Help document Q Return/exit the program B Save response Body F Enter the filter criteria K Up J Down H Left L Right Space Page turning Enter Enter interface details Z Clear the screen E Edit r Request againCopy the code

HTTPS packet capture configuration

To properly catch HTTPS requests, you need to install a certificate. A request without a certificate installed should look like this.

Open mitm.it, select a platform, and download the HTTPS certificate. And follow the corresponding steps to install it

mitmweb

$ mitmweb
Copy the code

After the mitmWeb command is started, a Web page similar to Chrome developer Tools is displayed. You can view the details of each request, including the request and response, and modify the request and response content, including filtering and resending requests.

mitmdump

$ mitmdump -s script.py
Copy the code

The biggest feature of the mitmdump command is that you can customize scripts. You can programmatically control the request or response content in the scripts, and realize data parsing, modification, and storage

# script.py
from mitmproxy import http

def request(flow: http.HTTPFlow) -> None:
	# add a query parameter to the request
    flow.request.query["mitmproxy"] = "rocks"

def response(flow: http.HTTPFlow) -> None:
	Add a custom header field to the response header
    flow.response.headers["newheader"] = "foo"
    print(flow.response.text)
Copy the code

When you request httpbin.org/get in your browser, you will see:

You can also refer to these links:

  • The official document: docs.mitmproxy.org/stable/
  • GitHub address: github.com/mitmproxy/m…
  • More script examples: github.com/mitmproxy/m…
  • How in the Chrome browser setting agent: jingyan.baidu.com/article/e52…

Blog at foofish.net