Control http traffic in your application development using mitmproxy

In this blog post I want to promote an awesome tool that may be a valuable addition to your IT arsenal - mitmproxy. It is a set of tools that provide an interactive, SSL/TLS-capable intercepting proxy for HTTP and WebSockets. As the name suggests it works as an isolation layer between a program and internet and allows monitoring of all outgoing and incoming http traffic. It is similar to Wireshark and Fiddler (if you’ve ever heard of them), but I found it easier to understand and use.

How is it useful?

mitmproxy can monitor, intercept, modify, redirect, and do pretty much anything with http traffic going to and from your application. This allows you to effectively mock the external (internet) services and isolate the code you work with much in the same way as mock classes allow you to isolate functionality in the unit tests.

Here are a few use cases to illustrate how mitmproxy could be useful in your development work:

Functionality

In short mitmproxy provides the following features:

mitmproxy comes with 3 interfaces:

Man-in-the-Middle attack

mitmproxy takes its name from the so-called man-in-the-middle attack, where the attacker secretly relays and possibly alters the communications between two parties who believe that they are directly communicating with each other, as the attacker has inserted themselves between the two parties. All the possibilities mentioned above, that make mitmproxy so powerful, are a dangerous weapon in the hands of a malicious party. The most common defense against MitM attacks is the use of mutually trusted Certificate Authorities (CA) that authenticate one or both parties and whose root certificates are usually included in all popular OS and browsers.

For this reason mitmproxy generates a new (private to your machine, so that its security is not violated) SSL certificate, which then needs to be explicitly trusted by the program behind mitmproxy.

This can be done either system-wide (for programs that respect it, for example, curl) by adding the generated public certificate to the right system folder (/etc/ssl/certs on Linux), or individually using respective certificate truststores (for java or browsers).