dockerfiles icon indicating copy to clipboard operation
dockerfiles copied to clipboard

Routing traffic through Tor using Docker not working

Open dalareo opened this issue 11 years ago • 11 comments

Hi, I followed this steps (https://blog.jessfraz.com/post/routing-traffic-through-tor-docker-container/) to get my traffic routed through a Docker container running Tor but, though everything seems to be working nice (iptables rules are loaded with the script you provide and docker log gives no error at all) when I check my connection at https://check.torproject.org/ my IP is still visible.

What can be wrong???

I tested the solution at Ubuntu 14.04, Arch Linux and Debian Jessie with Docker Engine 1.9

I run the container with the command:

docker run -d --net host --restart always --name tor jess/tor

and executing as sudo this script:

#!/bin/bash
# Most of this is credited to
# https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy
# With a few minor edits

# to run iptables commands you need to be root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root."
return 1
fi

### set variables
# destinations you don't want routed through Tor
_non_tor="192.168.1.0/24 192.168.0.0/24"

# get the UID that Tor runs as
_tor_uid=$(docker exec -u tor tor id -u)

# Tor's TransPort
_trans_port="9040"
_dns_port="5353"

### set iptables *nat
iptables -t nat -A OUTPUT -m owner --uid-owner $_tor_uid -j RETURN
iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports $_dns_port

# allow clearnet access for hosts in $_non_tor
for _clearnet in $_non_tor 127.0.0.0/9 127.128.0.0/10; do
iptables -t nat -A OUTPUT -d $_clearnet -j RETURN
done

# redirect all other output to Tor's TransPort
iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $_trans_port

### set iptables *filter
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# allow clearnet access for hosts in $_non_tor
for _clearnet in $_non_tor 127.0.0.0/8; do
iptables -A OUTPUT -d $_clearnet -j ACCEPT
done

# allow only Tor output
iptables -A OUTPUT -m owner --uid-owner $_tor_uid -j ACCEPT
iptables -A OUTPUT -j REJECT

dalareo avatar Nov 28 '15 20:11 dalareo

What do the logs from the container look like 

jessfraz avatar Nov 29 '15 06:11 jessfraz

Container log:

{"log":"Nov 29 08:49:53.865 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning\n","stream":"stdout","time":"2015-11-29T08:49:53.866307338Z"}
{"log":"Nov 29 08:49:53.865 [notice] This version is not a stable Tor release. Expect more bugs than usual.\n","stream":"stdout","time":"2015-11-29T08:49:53.866334292Z"}
{"log":"Nov 29 08:49:53.866 [notice] Read configuration file \"/etc/torrc\".\n","stream":"stdout","time":"2015-11-29T08:49:53.866373842Z"}
{"log":"Nov 29 08:49:53.874 [notice] Opening Socks listener on 127.0.0.1:9050\n","stream":"stdout","time":"2015-11-29T08:49:53.874769533Z"}
{"log":"Nov 29 08:49:53.874 [notice] Opening DNS listener on 127.0.0.1:5353\n","stream":"stdout","time":"2015-11-29T08:49:53.87483958Z"}
{"log":"Nov 29 08:49:53.874 [notice] Opening Transparent pf/netfilter listener on 127.0.0.1:9040\n","stream":"stdout","time":"2015-11-29T08:49:53.874962276Z"}
{"log":"Nov 29 08:49:53.000 [notice] Parsing GEOIP IPv4 file /usr/local/share/tor/geoip.\n","stream":"stdout","time":"2015-11-29T08:49:53.894726507Z"}
{"log":"Nov 29 08:49:54.000 [notice] Parsing GEOIP IPv6 file /usr/local/share/tor/geoip6.\n","stream":"stdout","time":"2015-11-29T08:49:54.03146925Z"}
{"log":"Nov 29 08:49:54.000 [notice] Bootstrapped 0%: Starting\n","stream":"stdout","time":"2015-11-29T08:49:54.139291897Z"}
{"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 5%: Connecting to directory server\n","stream":"stdout","time":"2015-11-29T08:49:55.140887463Z"}
{"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 10%: Finishing handshake with directory server\n","stream":"stdout","time":"2015-11-29T08:49:55.246870767Z"}
{"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 15%: Establishing an encrypted directory connection\n","stream":"stdout","time":"2015-11-29T08:49:55.527022048Z"}
{"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 20%: Asking for networkstatus consensus\n","stream":"stdout","time":"2015-11-29T08:49:55.602572043Z"}
{"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 25%: Loading networkstatus consensus\n","stream":"stdout","time":"2015-11-29T08:49:55.686462806Z"}
{"log":"Nov 29 08:49:57.000 [notice] I learned some more directory information, but not enough to build a circuit: We have no usable consensus.\n","stream":"stdout","time":"2015-11-29T08:49:57.358370843Z"}
{"log":"Nov 29 08:49:57.000 [notice] Bootstrapped 40%: Loading authority key certs\n","stream":"stdout","time":"2015-11-29T08:49:57.958402385Z"}
{"log":"Nov 29 08:49:58.000 [notice] Bootstrapped 45%: Asking for relay descriptors\n","stream":"stdout","time":"2015-11-29T08:49:58.483017673Z"}
{"log":"Nov 29 08:49:58.000 [notice] I learned some more directory information, but not enough to build a circuit: We need more microdescriptors: we have 0/6720, and can only build 0% of likely paths. (We have 0% of guards bw, 0% of midpoint bw, and 0% of exit bw = 0% of path bw.)\n","stream":"stdout","time":"2015-11-29T08:49:58.483102655Z"}
{"log":"Nov 29 08:49:58.000 [notice] Bootstrapped 50%: Loading relay descriptors\n","stream":"stdout","time":"2015-11-29T08:49:58.872701276Z"}
{"log":"Nov 29 08:50:00.000 [notice] Bootstrapped 55%: Loading relay descriptors\n","stream":"stdout","time":"2015-11-29T08:50:00.873027945Z"}
{"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 62%: Loading relay descriptors\n","stream":"stdout","time":"2015-11-29T08:50:01.075206776Z"}
{"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 67%: Loading relay descriptors\n","stream":"stdout","time":"2015-11-29T08:50:01.089803628Z"}
{"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 73%: Loading relay descriptors\n","stream":"stdout","time":"2015-11-29T08:50:01.104726326Z"}
{"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 80%: Connecting to the Tor network\n","stream":"stdout","time":"2015-11-29T08:50:01.154480038Z"}
{"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 90%: Establishing a Tor circuit\n","stream":"stdout","time":"2015-11-29T08:50:01.167379926Z"}
{"log":"Nov 29 08:50:01.000 [notice] Tor has successfully opened a circuit. Looks like client functionality is working.\n","stream":"stdout","time":"2015-11-29T08:50:01.446848665Z"}
{"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 100%: Done\n","stream":"stdout","time":"2015-11-29T08:50:01.4469231Z"}

Output of executing: iptables -nvL after visiting Tor check webpage and getting bad results (visible IP):

 pkts bytes target     prot opt in     out     source               destination  
 1488  125K ACCEPT     all  --  \*      \*       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  \*      \*       0.0.0.0/0            192.168.1.0/24  
   66  4324 ACCEPT     all  --  \*      \*       0.0.0.0/0            192.168.0.0/24  
    0     0 ACCEPT     all  --  \*      \*       0.0.0.0/0            127.0.0.0/8  
   34  2040 ACCEPT     all  --  \*      \*       0.0.0.0/0            0.0.0.0/0            owner UID match 1000
    8   608 REJECT     all  --  \*      \*       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    0     0 ACCEPT     all  --  \*      \*       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  \*      \*       0.0.0.0/0            192.168.1.0/24  
    0     0 ACCEPT     all  --  \*      \*       0.0.0.0/0            192.168.0.0/24  
    0     0 ACCEPT     all  --  \*      \*       0.0.0.0/0            127.0.0.0/8  
    0     0 ACCEPT     all  --  \*      \*       0.0.0.0/0            0.0.0.0/0            owner UID match 1000
    0     0 REJECT     all  --  \*      \*       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

dalareo avatar Nov 29 '15 08:11 dalareo

Do you have ip forwarding on?

On Sun, Nov 29, 2015 at 1:00 AM -0800, "David A. Lareo" [email protected] wrote:

Container log:

{"log":"Nov 29 08:49:53.865 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning ","stream":"stdout","time":"2015-11-29T08:49:53.866307338Z"} {"log":"Nov 29 08:49:53.865 [notice] This version is not a stable Tor release. Expect more bugs than usual. ","stream":"stdout","time":"2015-11-29T08:49:53.866334292Z"} {"log":"Nov 29 08:49:53.866 [notice] Read configuration file "/etc/torrc". ","stream":"stdout","time":"2015-11-29T08:49:53.866373842Z"} {"log":"Nov 29 08:49:53.874 [notice] Opening Socks listener on 127.0.0.1:9050 ","stream":"stdout","time":"2015-11-29T08:49:53.874769533Z"} {"log":"Nov 29 08:49:53.874 [notice] Opening DNS listener on 127.0.0.1:5353 ","stream":"stdout","time":"2015-11-29T08:49:53.87483958Z"} {"log":"Nov 29 08:49:53.874 [notice] Opening Transparent pf/netfilter listener on 127.0.0.1:9040 ","stream":"stdout","time":"2015-11-29T08:49:53.874962276Z"} {"log":"Nov 29 08:49:53.000 [notice] Parsing GEOIP IPv4 file /usr/local/share/tor/geoip. ","stream":"stdout","time":"2015-11-29T08:49:53.894726507Z"} {"log":"Nov 29 08:49:54.000 [notice] Parsing GEOIP IPv6 file /usr/local/share/tor/geoip6. ","stream":"stdout","time":"2015-11-29T08:49:54.03146925Z"} {"log":"Nov 29 08:49:54.000 [notice] Bootstrapped 0%: Starting ","stream":"stdout","time":"2015-11-29T08:49:54.139291897Z"} {"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 5%: Connecting to directory server ","stream":"stdout","time":"2015-11-29T08:49:55.140887463Z"} {"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 10%: Finishing handshake with directory server ","stream":"stdout","time":"2015-11-29T08:49:55.246870767Z"} {"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 15%: Establishing an encrypted directory connection ","stream":"stdout","time":"2015-11-29T08:49:55.527022048Z"} {"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 20%: Asking for networkstatus consensus ","stream":"stdout","time":"2015-11-29T08:49:55.602572043Z"} {"log":"Nov 29 08:49:55.000 [notice] Bootstrapped 25%: Loading networkstatus consensus ","stream":"stdout","time":"2015-11-29T08:49:55.686462806Z"} {"log":"Nov 29 08:49:57.000 [notice] I learned some more directory information, but not enough to build a circuit: We have no usable consensus. ","stream":"stdout","time":"2015-11-29T08:49:57.358370843Z"} {"log":"Nov 29 08:49:57.000 [notice] Bootstrapped 40%: Loading authority key certs ","stream":"stdout","time":"2015-11-29T08:49:57.958402385Z"} {"log":"Nov 29 08:49:58.000 [notice] Bootstrapped 45%: Asking for relay descriptors ","stream":"stdout","time":"2015-11-29T08:49:58.483017673Z"} {"log":"Nov 29 08:49:58.000 [notice] I learned some more directory information, but not enough to build a circuit: We need more microdescriptors: we have 0/6720, and can only build 0% of likely paths. (We have 0% of guards bw, 0% of midpoint bw, and 0% of exit bw = 0% of path bw.) ","stream":"stdout","time":"2015-11-29T08:49:58.483102655Z"} {"log":"Nov 29 08:49:58.000 [notice] Bootstrapped 50%: Loading relay descriptors ","stream":"stdout","time":"2015-11-29T08:49:58.872701276Z"} {"log":"Nov 29 08:50:00.000 [notice] Bootstrapped 55%: Loading relay descriptors ","stream":"stdout","time":"2015-11-29T08:50:00.873027945Z"} {"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 62%: Loading relay descriptors ","stream":"stdout","time":"2015-11-29T08:50:01.075206776Z"} {"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 67%: Loading relay descriptors ","stream":"stdout","time":"2015-11-29T08:50:01.089803628Z"} {"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 73%: Loading relay descriptors ","stream":"stdout","time":"2015-11-29T08:50:01.104726326Z"} {"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 80%: Connecting to the Tor network ","stream":"stdout","time":"2015-11-29T08:50:01.154480038Z"} {"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 90%: Establishing a Tor circuit ","stream":"stdout","time":"2015-11-29T08:50:01.167379926Z"} {"log":"Nov 29 08:50:01.000 [notice] Tor has successfully opened a circuit. Looks like client functionality is working. ","stream":"stdout","time":"2015-11-29T08:50:01.446848665Z"} {"log":"Nov 29 08:50:01.000 [notice] Bootstrapped 100%: Done ","stream":"stdout","time":"2015-11-29T08:50:01.4469231Z"}

Output of executing: iptables -nvL after visiting Tor check webpage and getting bad results (visible IP):


 pkts bytes target     prot opt in     out     source               destination

 1488  125K ACCEPT     all  --  \*      \*       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

0 0 ACCEPT all -- * * 0.0.0.0/0 192.168.1.0/24


   66  4324 ACCEPT     all  --  \*      \*       0.0.0.0/0            192.168.0.0/24

0 0 ACCEPT all -- * * 0.0.0.0/0 127.0.0.0/8


   34  2040 ACCEPT     all  --  \*      \*       0.0.0.0/0            0.0.0.0/0            owner UID match 1000

8 608 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable

0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

0 0 ACCEPT all -- * * 0.0.0.0/0 192.168.1.0/24

0 0 ACCEPT all -- * * 0.0.0.0/0 192.168.0.0/24

0 0 ACCEPT all -- * * 0.0.0.0/0 127.0.0.0/8

0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 owner UID match 1000

0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable


—
Reply to this email directly or view it on GitHub.

jessfraz avatar Dec 10 '15 17:12 jessfraz

Yes. If I execute:

sysctl net.ipv4.ip_forward

I get:

net.ipv4.ip_forward = 1

dalareo avatar Dec 10 '15 19:12 dalareo

What's the output of iptables-save I feel like other rules must be messing with it

On Friday, December 11, 2015, David A. Lareo [email protected] wrote:

Yes. If I execute:

sysctl net.ipv4.ip_forward

I get:

net.ipv4.ip_forward = 1

— Reply to this email directly or view it on GitHub https://github.com/jfrazelle/dockerfiles/issues/86#issuecomment-163726788 .

Jessie Frazelle 4096R / D4C4 DD60 0D66 F65A 8EFC 511E 18F3 685C 0022 BFF3 pgp.mit.edu http://pgp.mit.edu/pks/lookup?op=get&search=0x18F3685C0022BFF3

jessfraz avatar Dec 10 '15 23:12 jessfraz

Generated by iptables-save v1.4.21 on Mon Jan 4 16:08:55 2016

*nat :PREROUTING ACCEPT [0:0] :INPUT ACCEPT [0:0] :OUTPUT ACCEPT [7:438] :POSTROUTING ACCEPT [16:1023] :DOCKER - [0:0] -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER -A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER -A OUTPUT -m owner --uid-owner 1000 -j RETURN -A OUTPUT -p udp -m udp --dport 53 -j REDIRECT --to-ports 5353 -A OUTPUT -d 192.168.1.0/24 -j RETURN -A OUTPUT -d 192.168.0.0/24 -j RETURN -A OUTPUT -d 127.0.0.0/9 -j RETURN -A OUTPUT -d 127.128.0.0/10 -j RETURN -A OUTPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j REDIRECT --to-ports 9040 -A OUTPUT -m owner --uid-owner 1000 -j RETURN -A OUTPUT -p udp -m udp --dport 53 -j REDIRECT --to-ports 5353 -A OUTPUT -d 192.168.1.0/24 -j RETURN -A OUTPUT -d 192.168.0.0/24 -j RETURN -A OUTPUT -d 127.0.0.0/9 -j RETURN -A OUTPUT -d 127.128.0.0/10 -j RETURN -A OUTPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j REDIRECT --to-ports 9040 -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE COMMIT

Completed on Mon Jan 4 16:08:55 2016

Generated by iptables-save v1.4.21 on Mon Jan 4 16:08:55 2016

*filter :INPUT ACCEPT [110:32327] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :DOCKER - [0:0] -A FORWARD -o docker0 -j DOCKER -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -A FORWARD -i docker0 ! -o docker0 -j ACCEPT -A FORWARD -i docker0 -o docker0 -j ACCEPT -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A OUTPUT -d 192.168.1.0/24 -j ACCEPT -A OUTPUT -d 192.168.0.0/24 -j ACCEPT -A OUTPUT -d 127.0.0.0/8 -j ACCEPT -A OUTPUT -m owner --uid-owner 1000 -j ACCEPT -A OUTPUT -j REJECT --reject-with icmp-port-unreachable -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A OUTPUT -d 192.168.1.0/24 -j ACCEPT -A OUTPUT -d 192.168.0.0/24 -j ACCEPT -A OUTPUT -d 127.0.0.0/8 -j ACCEPT -A OUTPUT -m owner --uid-owner 1000 -j ACCEPT -A OUTPUT -j REJECT --reject-with icmp-port-unreachable COMMIT

Completed on Mon Jan 4 16:08:55 2016

dalareo avatar Jan 04 '16 15:01 dalareo

Do you have a proxy blocking tor

jessfraz avatar Jun 13 '16 09:06 jessfraz

Nop. Tor works when using it with Tor Browser.

dalareo avatar Jul 04 '16 11:07 dalareo

I can kind of reproduce you problem. It seems that the iptable rules only work for root.

If I run curl icanhazip.com as non root I get my actual IP address. Running it as root returns a tor IP.

Will update this issue if (if!) I figure this out.

koep avatar Aug 22 '16 21:08 koep

Great! It was driving me crazy

dalareo avatar Aug 22 '16 21:08 dalareo

this should be fixed or working now but let me know if otherwise

jessfraz avatar Jun 06 '18 21:06 jessfraz