2014-10-22 23:56:31 +00:00
Mitmproxy has four modes of operation that allow you to use mitmproxy in a
variety of scenarios:
- **Regular** (the default)
- **Transparent**
- **Reverse Proxy**
- **Upstream Proxy**
Now, which one should you pick? Use this flow chart:
2015-05-24 02:09:51 +00:00
< img class = "img-responsive" src = "@!urlTo('schematics/proxy-modes-flowchart.png')!@" / >
2014-09-06 22:42:25 +00:00
< div class = "page-header" >
< h1 > Regular Proxy< / h1 >
< / div >
2014-10-22 23:56:31 +00:00
Mitmproxy's regular mode is the simplest and the easiest to set up.
2014-09-06 22:42:25 +00:00
2014-10-22 23:56:31 +00:00
1. Start mitmproxy.
2. Configure your client to use mitmproxy. For instance on IOS, the settings might look like < a href = "@!urlTo('screenshots/ios-manual.png')!@" > this< / a > .
3. Quick Check: You should already be able to visit an unencrypted HTTP site
through the proxy.
4. Open the magic domain < strong > mitm.it< / strong > and install the certificate for your device.
2014-09-06 22:42:25 +00:00
< div class = "well" >
2014-10-22 23:56:31 +00:00
< strong > Heads Up:< / strong > Unfortunately, some applications bypass the
system HTTP proxy settings - Android applications are a common example. In
these cases, you need to use mitmproxy's transparent mode.
2014-09-06 22:42:25 +00:00
< / div >
2014-10-22 23:56:31 +00:00
If you are proxying an external device, your network will probably look like this:
2015-05-24 02:09:51 +00:00
< img class = "img-responsive" src = "@!urlTo('schematics/proxy-modes-regular.png')!@" >
2014-10-22 23:56:31 +00:00
The square brackets signify the source and destination IP addresses. Your
client explicitly connects to mitmproxy and mitmproxy explicitly connects
to the target server.
2014-09-06 22:42:25 +00:00
< div class = "page-header" >
< h1 > Transparent Proxy< / h1 >
< / div >
2014-10-22 23:56:31 +00:00
In transparent mode, traffic is directed into a proxy at the network layer,
without any client configuration required. This makes transparent proxying
ideal for situations where you can't change client behaviour. In the graphic
below, a machine running mitmproxy has been inserted between the router and
the internet:
2014-09-06 22:42:25 +00:00
< a href = "@!urlTo('schematics/proxy-modes-transparent-1.png')!@" >
2015-05-24 02:09:51 +00:00
< img class = "img-responsive" src = "@!urlTo('schematics/proxy-modes-transparent-1.png')!@" >
2014-10-22 23:56:31 +00:00
< / a >
The square brackets signify the source and destination IP addresses. Round
brackets mark the next hop on the *Ethernet/data link* layer. This distinction
is important: when the packet arrives at the mitmproxy machine, it must still
be addressed to the target server. This means that Network Address Translation
should not be applied before the traffic reaches mitmproxy, since this would
remove the target information, leaving mitmproxy unable to determine the real
destination.
2014-09-06 22:42:25 +00:00
< a href = "@!urlTo('schematics/proxy-modes-transparent-wrong.png')!@" >
2015-05-24 02:09:51 +00:00
< img class = "img-responsive" src = "@!urlTo('schematics/proxy-modes-transparent-wrong.png')!@" > < / a >
2014-09-06 22:42:25 +00:00
< h2 > Common Configurations< / h2 >
2014-10-22 23:56:31 +00:00
There are many ways to configure your network for transparent proxying. We'll
look at three common scenarios:
1. Configuring the client to use a custom gateway/router/"next hop"
2. Implementing custom routing on the router
In most cases, the first option is recommended due to its ease of use.
2014-09-06 22:42:25 +00:00
< h3 > (a) Custom Gateway< / h3 >
2014-10-22 23:56:31 +00:00
One simple way to get traffic to the mitmproxy machine with the destination IP
intact, is to simply configure the client with the mitmproxy box as the
default gateway.
2014-09-06 22:42:25 +00:00
< a href = "@!urlTo('schematics/proxy-modes-transparent-2.png')!@" >
2015-05-24 02:09:51 +00:00
< img class = "img-responsive" src = "@!urlTo('schematics/proxy-modes-transparent-2.png')!@" > < / a >
2014-09-06 22:42:25 +00:00
2014-10-22 23:56:31 +00:00
In this scenario, we would:
- Configure the proxy machine for transparent mode. You can find instructions
in the < em > Transparent Proxying< / em > section of the mitmproxy docs.
- Configure the client to use the proxy machine's IP as the default gateway.
< a href = "@!urlTo('screenshots/ios-gateway.png')!@" > Here< / a > is what this would
look like on IOS.
- Quick Check: At this point, you should already be able to visit an
unencrypted HTTP site over the proxy.
- Open the magic domain < strong > mitm.it< / strong > and install the certificate
for your device.
Setting the custom gateway on clients can be automated by serving the settings
out to clients over DHCP. This lets set up an interception network where all
2015-05-24 02:09:51 +00:00
clients are proxied automatically, which can save time and effort.
2014-10-22 23:56:31 +00:00
2014-09-06 22:42:25 +00:00
< div class = "well" >
< strong style = "text-align: center; display: block" > Troubleshooting Transparent Mode< / strong >
2014-10-22 23:56:31 +00:00
< p > Incorrect transparent mode configurations are a frequent source of
2014-09-06 22:42:25 +00:00
error. If it doesn't work for you, try the following things:< / p >
2014-10-22 23:56:31 +00:00
2014-09-06 22:42:25 +00:00
< ul >
2014-10-22 23:56:31 +00:00
< li >
Open mitmproxy's event log (press `e`) - do you see clientconnect
messages? If not, the packets are not arriving at the proxy. One common
cause is the occurrence of ICMP redirects, which means that your
machine is telling the client that there's a faster way to the
internet by contacting your router directly (see the
< em > Transparent Proxying< / em > section on how to disable them). If in
doubt, < a href = "https://wireshark.org/" > Wireshark< / a > may help you
to see whether something arrives at your machine or not.
2014-09-06 22:42:25 +00:00
< / li >
< li >
2014-10-22 23:56:31 +00:00
Make sure you have not explicitly configured an HTTP proxy on the
client. This is not needed in transparent mode.
2014-09-06 22:42:25 +00:00
< / li >
< li >
Re-check the instructions in the < em > Transparent Proxying< / em > section. Anything you missed?
< / li >
< / ul >
2014-10-22 23:56:31 +00:00
2014-09-06 22:42:25 +00:00
If you encounter any other pitfalls that should be listed here, please let us know!
2014-10-22 23:56:31 +00:00
2014-09-06 22:42:25 +00:00
< / div >
< h3 > (b) Custom Routing< / h3 >
2014-10-22 23:56:31 +00:00
In some cases, you may need more fine-grained control of which traffic reaches
the mitmproxy instance, and which doesn't. You may, for instance, choose only
to divert traffic to some hosts into the transparent proxy. There are a huge
number of ways to accomplish this, and much will depend on the router or
packet filter you're using. In most cases, the configuration will look like
this:
2014-09-06 22:42:25 +00:00
< a href = "@!urlTo('schematics/proxy-modes-transparent-3.png')!@" >
2015-05-24 02:09:51 +00:00
< img class = "img-responsive" src = "@!urlTo('schematics/proxy-modes-transparent-3.png')!@" >
2014-10-22 23:56:31 +00:00
< / a >
2014-09-06 22:42:25 +00:00
< div class = "page-header" >
< h1 > Reverse Proxy< / h1 >
< / div >
2015-08-30 11:40:23 +00:00
mitmproxy is usually used with a client that uses the proxy to access the
2014-10-22 23:56:31 +00:00
Internet. Using reverse proxy mode, you can use mitmproxy to act like a normal
HTTP server:
2014-09-06 22:42:25 +00:00
< a href = "@!urlTo('schematics/proxy-modes-reverse.png')!@" >
2015-05-24 02:09:51 +00:00
< img class = "img-responsive" src = "@!urlTo('schematics/proxy-modes-reverse.png')!@" >
2014-10-22 23:56:31 +00:00
< / a >
2014-09-06 22:42:25 +00:00
There are various use-cases:
2014-10-22 23:56:31 +00:00
- Say you have an internal API running at http://example.local/. You could now
set up mitmproxy in reverse proxy mode at http://debug.example.local/ and
dynamically point clients to this new API endpoint, which provides clients
with the same data and you with debug information. Similarly, you could move
your real server to a different IP/port and set up mitmproxy at the original
place to debug all sessions.
- Say you're a web developer working on example.com (with a development
version running on localhost:8000). You can modify your hosts file so that
example.com points to 127.0.0.1 and then run mitmproxy in reverse proxy mode
on port 80. You can test your app on the example.com domain and get all
requests recorded in mitmproxy.
- Say you have some toy project that should get SSL support. Simply set up
2015-09-01 00:35:05 +00:00
mitmproxy as a reverse proxy on port 443 and you're done (< code > mitmdump -p 443 -R
http://localhost:80/< / code > ). mitmproxy auto-detects TLS traffic and intercepts it dynamically.
There are better tools for this specific task, but mitmproxy is very quick and simple way to
set up an SSL-speaking server.
2014-10-22 23:56:31 +00:00
- Want to add a non-SSL-capable compression proxy in front of your server? You
2015-08-30 11:40:23 +00:00
could even spawn a mitmproxy instance that terminates SSL (-R http://...),
2014-10-22 23:56:31 +00:00
point it to the compression proxy and let the compression proxy point to a
2015-08-30 11:40:23 +00:00
SSL-initiating mitmproxy (-R https://...), which then points to the real
2014-10-22 23:56:31 +00:00
server. As you see, it's a fairly flexible thing.
Note that mitmproxy supports either an HTTP or an HTTPS upstream server, not
both at the same time. You can work around this by spawning a second mitmproxy
instance.
< div class = "well" >
< strong style = "text-align: center; display: block" > Caveat: Interactive Use< / strong >
One caveat is that reverse proxy mode is often not sufficient for interactive
browsing. Consider trying to clone Google by using:
< code > mitmproxy -R http://google.com/< / code >
This works for the initial request, but the HTML served to the client remains
unchanged. As soon as the user clicks on an non-relative URL (or downloads a
non-relative image resource), traffic no longer passes through mitmproxy, and
the client connects to Google directly again.
< / div >
2014-09-06 22:42:25 +00:00
< div class = "page-header" >
< h1 > Upstream Proxy< / h1 >
< / div >
2014-10-22 23:56:31 +00:00
If you want to chain proxies by adding mitmproxy in front of a different proxy
appliance, you can use mitmproxy's upstream mode. In upstream mode, all
2014-10-23 14:17:03 +00:00
requests are unconditionally transferred to an upstream proxy of your choice.
2014-09-06 22:42:25 +00:00
< a href = "@!urlTo('schematics/proxy-modes-upstream.png')!@" >
2015-05-24 02:09:51 +00:00
< img class = "img-responsive" src = "@!urlTo('schematics/proxy-modes-upstream.png')!@" > < / a >
2014-09-06 22:42:25 +00:00
2014-10-22 23:56:31 +00:00
mitmproxy supports both explicit HTTP and explicit HTTPS in upstream proxy
mode. You could in theory chain multiple mitmproxy instances in a row, but
2015-05-24 02:09:51 +00:00
that doesn't make any sense in practice (i.e. outside of our tests).