mitmproxy has a powerful scripting API that allows you to modify flows on-the-fly or rewrite previously saved flows locally.
The mitmproxy scripting API is event driven - a script is simply a Python module that exposes a set of event methods. Here's a complete mitmproxy script that adds a new header to every HTTP response before it is returned to the client:
def response(context, flow): flow.response.headers["newheader"] = ["foo"]
The first argument to each event method is an instance of ScriptContext that lets the script interact with the global mitmproxy state. The response event also gets an instance of Flow, which we can use to manipulate the response itself.
Called once on startup, before any other events.
Called when a client initiates a connection to the proxy. Note that a connection can correspond to multiple HTTP requests.
Called when a client request has been received. The Flow object is guaranteed to have a non-None request attribute.
Called when a server response has been received. The Flow object is guaranteed to have non-None request and response attributes.
Called when a flow error has occurred, e.g. invalid server responses, or interrupted connections. This is distinct from a valid server HTTP error response, which is simply a response with an HTTP error code. The Flow object is guaranteed to have non-None request and error attributes.
Called when a client disconnects from the proxy.
Called once on script shutdown, after any other events.
The main classes you will deal with in writing mitmproxy scripts are:
|libmproxy.flow.ClientConnection||Describes a client connection.|
|libmproxy.flow.ClientDisconnection||Describes a client disconnection.|
|libmproxy.flow.Error||A communications error.|
|libmproxy.flow.Flow||A collection of objects representing a single HTTP transaction.|
|libmproxy.flow.Headers||HTTP headers for a request or response.|
|libmproxy.flow.ODict||A dictionary-like object for managing sets of key/value data. There is also a variant called CaselessODict that ignores key case for some calls (used mainly for headers).|
|libmproxy.flow.Response||An HTTP response.|
|libmproxy.flow.Request||An HTTP request.|
|libmproxy.flow.ScriptContext||A handle for interacting with mitmproxy's from within scripts.|
|libmproxy.certutils.SSLCert||Exposes information SSL certificates.|
The canonical API documentation is the code. You can view the API documentation using pydoc (which is installed with Python by default), like this:
> pydoc libmproxy.flow.Request
Sometimes, we want to run a script on Flow objects that are already complete. This happens when you start a script, and then load a saved set of flows from a file (see the "scripted data transformation" example on the mitmdump page). It also happens when you run a one-shot script on a single flow through the | (pipe) shortcut in mitmproxy.
In this case, there are no client connections, and the events are run in the following order: start, request, response, error, done. If the flow doesn't have a response or error associated with it, the matching event will be skipped.
© mitmproxy project, 2012