2011-02-17 23:40:45 +00:00
|
|
|
|
2011-08-05 01:26:39 +00:00
|
|
|
__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:
|
|
|
|
|
|
|
|
$!example("examples/add_header.py")!$
|
|
|
|
|
|
|
|
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.
|
2011-08-04 22:47:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Events
|
|
|
|
|
2011-08-05 01:26:39 +00:00
|
|
|
### start(ScriptContext)
|
|
|
|
|
|
|
|
Called once on startup, before any other events.
|
|
|
|
|
|
|
|
|
|
|
|
###clientconnect(ScriptContext, ClientConnect)
|
|
|
|
|
|
|
|
Called when a client initiates a connection to the proxy. Note that
|
|
|
|
a connection can correspond to multiple HTTP requests.
|
|
|
|
|
|
|
|
|
|
|
|
###request(ScriptContext, Flow)
|
|
|
|
|
|
|
|
Called when a client request has been received. The __Flow__ object is
|
|
|
|
guaranteed to have a non-None __request__ attribute.
|
|
|
|
|
|
|
|
|
|
|
|
### response(ScriptContext, Flow)
|
|
|
|
|
|
|
|
Called when a server response has been received. The __Flow__ object is
|
|
|
|
guaranteed to have non-None __request__ and __response__ attributes.
|
|
|
|
|
|
|
|
|
|
|
|
### error(ScriptContext, Flow)
|
|
|
|
|
2012-04-04 04:05:18 +00:00
|
|
|
Called when a flow error has occurred, e.g. invalid server responses, or
|
2011-08-05 01:26:39 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
### clientdisconnect(ScriptContext, ClientDisconnect)
|
|
|
|
|
|
|
|
Called when a client disconnects from the proxy.
|
2011-03-18 22:26:51 +00:00
|
|
|
|
2011-08-05 01:26:39 +00:00
|
|
|
### done(ScriptContext)
|
2011-03-18 22:26:51 +00:00
|
|
|
|
2011-08-05 01:26:39 +00:00
|
|
|
Called once on script shutdown, after any other events.
|
2011-02-17 23:40:45 +00:00
|
|
|
|
|
|
|
|
2011-08-06 01:19:46 +00:00
|
|
|
## API
|
|
|
|
|
|
|
|
The main classes you will deal with in writing mitmproxy scripts are:
|
|
|
|
|
|
|
|
<table class="kvtable">
|
|
|
|
<tr>
|
2012-02-22 04:17:13 +00:00
|
|
|
<th>libmproxy.flow.ClientConnection</th>
|
|
|
|
<td>Describes a client connection.</td>
|
2011-08-06 01:19:46 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2012-02-22 04:17:13 +00:00
|
|
|
<th>libmproxy.flow.ClientDisconnection</th>
|
|
|
|
<td>Describes a client disconnection.</td>
|
2011-08-06 01:19:46 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2012-02-22 04:17:13 +00:00
|
|
|
<th>libmproxy.flow.Error</th>
|
|
|
|
<td>A communications error.</td>
|
2011-08-06 01:19:46 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2012-02-22 04:17:13 +00:00
|
|
|
<th>libmproxy.flow.Flow</th>
|
|
|
|
<td>A collection of objects representing a single HTTP transaction.</td>
|
2011-08-06 01:19:46 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2012-02-22 04:17:13 +00:00
|
|
|
<th>libmproxy.flow.Headers</th>
|
|
|
|
<td>HTTP headers for a request or response.</td>
|
2011-08-06 01:19:46 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2012-02-22 04:17:13 +00:00
|
|
|
<th>libmproxy.flow.ODict</th>
|
|
|
|
|
|
|
|
<td>A dictionary-like object for managing sets of key/value data. There
|
|
|
|
is also a variant called CaselessODict that ignores key case for some
|
2012-04-04 02:17:26 +00:00
|
|
|
calls (used mainly for headers).</td>
|
2011-08-06 01:19:46 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2012-02-22 04:17:13 +00:00
|
|
|
<th>libmproxy.flow.Response</th>
|
|
|
|
<td>An HTTP response.</td>
|
2011-08-06 01:19:46 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2012-02-22 04:17:13 +00:00
|
|
|
<th>libmproxy.flow.Request</th>
|
|
|
|
<td>An HTTP request.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<th>libmproxy.flow.ScriptContext</th>
|
2012-02-23 02:52:01 +00:00
|
|
|
<td> A handle for interacting with mitmproxy's from within scripts. </td>
|
2012-04-04 02:17:26 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<th>libmproxy.certutils.SSLCert</th>
|
|
|
|
<td>Exposes information SSL certificates.</td>
|
2011-08-06 01:19:46 +00:00
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
The canonical API documentation is the code. You can view the API documentation
|
|
|
|
using pydoc (which is installed with Python by default), like this:
|
|
|
|
|
|
|
|
<pre class="terminal">
|
|
|
|
> pydoc libmproxy.flow.Request
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
|
|
## Running scripts on saved flows
|
|
|
|
|
|
|
|
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](@!urlTo("mitmdump.html")!@) page). It also happens when you run a
|
2011-08-05 01:26:39 +00:00
|
|
|
one-shot script on a single flow through the _|_ (pipe) shortcut in mitmproxy.
|
2011-02-17 23:40:45 +00:00
|
|
|
|
2011-08-05 01:26:39 +00:00
|
|
|
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
|
2011-08-06 01:19:46 +00:00
|
|
|
matching event will be skipped.
|