To be able to use mitm for HTTPS you need to have the a custom generated CA certificate in the connection source system/container.
To pre-generate these custom generated CA certificates just execute:
docker run --rm -ti nmaguiar/netutils mitm-gencerts.shThe result will be an unix command that, when executed, will create several files, in the current folder, with custom generated CA certificate in different formats for you to copy to the communication source system.
In some cases (like in MacOS) you might need to open 'vi' paste the copied contents, save to a temporary file and then execute it.
Then, before setting up mitm on the nmaguiar/netutils container execute:
sudo -u mitm bash
mkdir -p ~/.mitmproxy
cd ~/.mitmproxy
# execute the provided unix command from the first mitm-generate.sh executionNow you can start the mitm tool.
When mitm is running you can use the following method to retrieve the current CA certificate in use.
- Set your system proxy to port 8080.
- Access http://mitm.it/
- Obtain the
~/.mitmproxy/mitmproxy-ca-cert.pemand copy it to the target container/usr/local/share/ca-certificates/mitmproxy-ca-cert.crt - Execute
update-ca-certificates
sudo cp ~/.mitmproxy/mitmproxy-ca-cert.pem /usr/local/share/ca-certificates/mitmproxy-ca-cert.crt
sudo update-ca-certificates
export http_proxy=http://127.0.0.1:8080
export https_proxy=http://127.0.0.1:8080Common linux distributions ca certificates location:
| Distribution | Location | Java Location |
|---|---|---|
| Alpine | /etc/ssl/certs/ca-certificates.crt | /etc/ssl/certs/java/cacerts |
| RedHat-based | /etc/pki/tls/certs/ca-bundle.crt | /etc/pki/java/cacerts |
| Debian-based | /etc/ssl/certs/ca-certificates.crt | /etc/ssl/certs/java/cacerts |
You can retrieve these files using OpenAF's oJob ojob.io/docker/expand to extract, append the mitm certificate and then overwrite it on the image (using ojob.io/docker/collapse or a K8s config map or similar method).
You can append /home/mitm/.mitmproxy/mitmproxy-ca-cert.pem to these ca certificates crt files. For Java you need to use key-tool or OpenAF's oJob ojob.io/java/certs. Depending on the image the cacerts file might be located in [java home]/lib/security/cacerts.
Check also: https://docs.mitmproxy.org/stable/concepts-certificates/#installing-the-mitmproxy-ca-certificate-manually
There are several modes of operation for mitmproxy. The default is to act as a HTTP/HTTPS proxy for which you should check, before or after starting, the previous chapter to understand how to setup the generated ca-certificate. Keep in mind that it will be regenerated each time this container restarts.
Some quick guidance on operating modes:
- To set as a reverse proxy just add the command-line argument:
--mode reverse:https://my.target:1234 - To set as a SOCKS proxy just add the command-line argument:
--mode socks5 - To set as a DNS server, expose the port 53 and add the command-line argument:
--mode dns
You can also use a different port from 8080 by adding the command-line argument -p 1234 and adding that port to the container exposed ports.
Execute mitmproxy directly on this container.
- Expose this container port 8081 (which might require restart)
- Execute
mitmweb --web-host 0.0.0.0
You can run mitmproxy in transparent mode using the helper scripts in the scripts/ directory:
- Set up the iptables bypass chain so that mitmproxy’s own traffic isn’t re-intercepted:
mitm-transparent-set.sh- Redirect traffic to the transparent proxy (choose one):
If you need to redirect port 8080 and since this is the port used by mitmproxy you can set the environment variable MITM_PORT to a different port to avoid the collision.
- Outgoing IPv4 TCP:
mitm-transparent-add.sh port [host]- Outgoing IPv6 TCP:
mitm-transparent-add6.sh port [host]- Incoming IPv4 on
eth0:
mitm-transparent-add-incoming.sh port [host]- Incoming IPv6 on
eth0:
mitm-transparent-add6-incoming.sh port [host]- Start mitmproxy in transparent mode (runs as user
mitm, default port8080):
mitm-transparent-start.sh- Remove individual redirection rules when done:
Use one or more of the following:
mitm-transparent-clean.sh port [host]
mitm-transparent-clean6.sh port [host]
mitm-transparent-clean-incoming.sh port [host]
mitm-transparent-clean6-incoming.sh port [host]OR simply,
- Clean up all mitmproxy iptables chains (bypass, NAT, etc.):
mitm-transparent-cleanall.sh