SoFunction
Updated on 2024-11-13

Guide to using the python proxy tool mitmproxy

preamble

mitmproxy is short for man-in-the-middle proxy, which translates to man-in-the-middle proxy tool that can be used to intercept, modify, and save HTTP/HTTPS requests. Presented in the form of a command line terminal, the operation is similar to Vim, and provides mitmweb plugin, is similar to Chrome developer mode visualization tools.

It is an open source tool based on Python development, the most important thing is that it provides a Python API, you can completely control the request and response through Python code, which is not possible with other tools, this point is one of the reasons why I like this tool.

mounting

sudo pip3 install mitmproxy

activate (a plan)

mitmproxy
# or specified port
mitmproxy -p 8888

After starting mitmproxy, port 8080 will be opened by default. mitmproxy does not support Windows platform, you need to use the mitmdump or mitmweb command instead. you can also download its EXE file from the official website to install it for Windows systems.

After the phone or browser set up a good proxy, you can capture the packet analysis, open the browser to visit a certain URL, mitmproxy see the effect is:

Currently there are a total of 136 requests, the current selection is the 16th request, the request method is GET, the return status code is 200, the agent's port is 8080, through the J, K key can be switched up and down to a different request, enter to see the details of the currently selected request, including three parts, Request and Response and Details

mitmproxy shortcut

? help file 
q come (or go) back/opt-out program 
b save (a file etc) (computing)response body 
f Input Filter Criteria
k first (of multiple parts)
j arrive at (a decision, conclusion etc)
h unorthodox
l right (-hand)
space flip
enter Access to interface details
z clear the screen
e compiler
r renewed request

HTTPS Packet Grabbing Configuration

For HTTPS requests, the certificate needs to be installed first in order to catch the request properly. A request without a certificate installed sees results like this.

Open the web site If you want to install the HTTPS certificate, select the matching platform and download the HTTPS certificate. And follow the corresponding steps to install

mitmweb

$ mitmweb

After launching the mitmweb command, there will be a web page similar to the Chrome Developer Tools, which is similar to mitmroxy in functionality, as well as the ability to view the details of each request, including the request, the response, and the ability to modify the request and response content, including filtering, resending the request, and other common functions.

mitmdump

$ mitmdump -s 

The most important feature of the mitmdump command is that it can be customized as a script, in which you can programmatically control the content of the request or response to achieve the parsing, modification, and storage of data.

# 
from mitmproxy import http

def request(flow: ) -> None:
  # Add a new query parameter to the request
  ["mitmproxy"] = "rocks"

def response(flow: ) -> None:
  # Added a new custom header field to the response header
  ["newheader"] = "foo"
  print()

When you request the/get , see the effects:

You can also refer to these links:

  • official document/stable/
  • GitHub Address/mitmproxy/mitmproxy
  • More scripting examples/mitmproxy/mitmproxy/tree/master/examples/simple

This is the whole content of this article.