# Using mitmproxy for all HTTP/HTTPS traffic coming from an ipsec vpn server

**URL:** <https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792>\
**Category:** Uncategorized\
**Created:** [January 14, 2018, 11:19pm UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792 "2018-01-14T23:19:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyilo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/tyilo/32/248_2.png) [@Tyilo](https://discourse.mitmproxy.org/u/Tyilo)\
**Post date:** [January 14, 2018, 11:19pm UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/1 "2018-01-14T23:19:35Z")

</div>

Background: I want to use mitmproxy with my iPhone while on 3G. In iOS you can only use a proxy for WiFi connections and not while using mobile data. So instead I can use a VPN server that needs to forward its request through mitmproxy.

I want to create a VPN server that automatically forwards all outgoing HTTP/HTTPS requests to mitmproxy, however I have failed creating an iptables configuration that works.

To do this I’ve used docker-compose with the [hwdsl2/ipsec-vpn-server](https://github.com/hwdsl2/docker-ipsec-vpn-server/) and mitmproxy/mitmproxy images.

I have added the following iptables config to ipsec-vpn-server (where HOST will be the local ip address of the mitmproxy server)

```
HOST=$(host -4 mitmproxy | head -1 | awk '{ print $4 }')
iptables -t nat -A PREROUTING -i eth+ -p tcp --destination-port 80 -j DNAT --to-destination $HOST:8080
iptables -t nat -A PREROUTING -i eth+ -p tcp --destination-port 443 -j DNAT --to-destination $HOST:8080

```

and I run `mitmdump` using `mitmdump -T --host`.

However when I connect my iPhone to the VPN I just get an endless stream of output from `mitmdump` saying

```
172.18.0.2:49801: clientconnect

```

How do I fix this so that mitmproxy actually works?

The full configuration I’ve used is at: [https://gist.github.com/Tyilo/03889ddc651fcf96e1208b65bfc7aa7f](https://gist.github.com/Tyilo/03889ddc651fcf96e1208b65bfc7aa7f)

Here is a diagram of what I want to achieve:

```
       +----------------------+
       | |
       | iPhone |
       | |
       +-----------+----------+
                   |
                   |
+--------------------------------------+
| +-----------v----------+ |
| | | |
| | VPN server +-----+ |
| | | | |
| +----------------------+ | |
| |HTTP/HTTPS | | My server
| | | |
| +----------v-----------+ | |
| | | | |
| | mitmproxy | | |
| | | | |
| +----------------------+ | |
+--------------------------------------+
                  | |
                  | |non-HTTP/HTTPS traffic
                  | |
        +---------v-----------+ |
        | | |
        | Internet +^----+
        | |
        +---------------------+
```

---

<div class="post-metadata">

**Author:** ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)\
**Post date:** [January 18, 2018, 6:48pm UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/2 "2018-01-18T18:48:10Z")

</div>

Hi,

You’re running into the first issue described at [http://docs.mitmproxy.org/en/stable/modes.html#transparent-proxy](http://docs.mitmproxy.org/en/stable/modes.html#transparent-proxy) - don’t DNAT before sending your data to the mitmproxy instance. 🙂

---

<div class="post-metadata">

**Author:** ![Tyilo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/tyilo/32/248_2.png) [@Tyilo](https://discourse.mitmproxy.org/u/Tyilo)\
**Post date:** [February 4, 2018, 7:25pm UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/3 "2018-02-04T19:25:31Z")

</div>

How do I do this without DNAT?

---

<div class="post-metadata">

**Author:** ![drzax](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/drzax/32/310_2.png) [@drzax](https://discourse.mitmproxy.org/u/drzax)\
**Post date:** [May 22, 2018, 12:46am UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/4 "2018-05-22T00:46:25Z")

</div>

Did you get this successfully setup? I’m looking to do the same thing (though I’ve been playing with OpenVPN).

---

<div class="post-metadata">

**Author:** ![netiger1986](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@netiger1986](https://discourse.mitmproxy.org/u/netiger1986)\
**Post date:** [June 5, 2018, 12:26pm UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/5 "2018-06-05T12:26:44Z")

</div>

Maybe try to put vpn server and mitmproxy in the same machine and use REDIRECT instead of DNAT

---

<div class="post-metadata">

**Author:** ![marting](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@marting](https://discourse.mitmproxy.org/u/marting)\
**Post date:** [July 20, 2018, 5:49pm UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/6 "2018-07-20T17:49:27Z")

</div>

Hi,

if you set up your iPhone in supervision mode then you can set Global HTTP Proxy in profile for your iPhone using Apple Configurator 2. Then this proxy is used while 3G as well as while WiFi.  
Only problem is when you use mitmprox with --proxyauth logn:pass then HTTPS communication doesn’t work. ☹

---

<div class="post-metadata">

**Author:** ![uniQ](https://avatars.discourse-cdn.com/v4/letter/u/bc8723/32.png) [@uniQ](https://discourse.mitmproxy.org/u/uniQ)\
**Post date:** [October 3, 2018, 1:22am UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/7 "2018-10-03T01:22:50Z")

</div>

Did you manage to get the VPN and Mitmproxy working.

---

<div class="post-metadata">

**Author:** ![Tyilo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/tyilo/32/248_2.png) [@Tyilo](https://discourse.mitmproxy.org/u/Tyilo)\
**Post date:** [October 3, 2018, 1:54am UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/8 "2018-10-03T01:54:26Z")

</div>

No, I didn’t get it to work

---

<div class="post-metadata">

**Author:** ![drzax](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/drzax/32/310_2.png) [@drzax](https://discourse.mitmproxy.org/u/drzax)\
**Post date:** [October 4, 2018, 2:53am UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/9 "2018-10-04T02:53:18Z")

</div>

Here’s my attempt, which works to my satisfaction: [https://github.com/abcnews/data-life/tree/master/server](https://github.com/abcnews/data-life/tree/master/server)

---

<div class="post-metadata">

**Author:** ![uniQ](https://avatars.discourse-cdn.com/v4/letter/u/bc8723/32.png) [@uniQ](https://discourse.mitmproxy.org/u/uniQ)\
**Post date:** [October 14, 2018, 1:58am UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/10 "2018-10-14T01:58:34Z")

</div>

Thanks for this repo.  
The Transparent proxy setup is really usefull and does a great job.  
I tried to do the mitmproxy in the docker container but that does not seem to be working.

Is it possible to be able to only accept certain domains (IP address) through the transparent mode. Since when i try to do a regex for ignoring all IP addresses, it does not working.

---

<div class="post-metadata">

**Author:** ![drzax](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/drzax/32/310_2.png) [@drzax](https://discourse.mitmproxy.org/u/drzax)\
**Post date:** [October 14, 2018, 3:17am UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/11 "2018-10-14T03:17:01Z")

</div>

If you use the docker-compose file in my repo for both proxy and VPN it should just work\*.

The proxy is only exposed to the local (docker) network so will only accept connections through the VPN.

---

<div class="post-metadata">

**Author:** ![uniQ](https://avatars.discourse-cdn.com/v4/letter/u/bc8723/32.png) [@uniQ](https://discourse.mitmproxy.org/u/uniQ)\
**Post date:** [October 22, 2018, 3:29pm UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/12 "2018-10-22T15:29:42Z")

</div>

When using your docker-compose. I can connect to the VPN but the mitmproxy is not doing anything.

Is there any extra settings I need to do on an Android device so that it forwards to 172.20.128.2  
Currently I just create VPN on android and enter the Server Address, PSK, username and password.

~Thanks

---

<div class="post-metadata">

**Author:** ![drzax](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/drzax/32/310_2.png) [@drzax](https://discourse.mitmproxy.org/u/drzax)\
**Post date:** [October 25, 2018, 4:55am UTC](https://discourse.mitmproxy.org/t/using-mitmproxy-for-all-http-https-traffic-coming-from-an-ipsec-vpn-server/792/13 "2018-10-25T04:55:37Z")

</div>

I haven’t tested on Android, but I’ve heard that it’s difficult to force traffic through a VPN. Maybe someone with some Android knowledge could pipe up here. I’d be really interested too since I’m planning on tackling an Android device soon.
