Hello,
I want to set up a Linux gateway that routes traffic over Tor so that anyone who enters the Linux IP address can access the internet via Tor.
I changed the Tor configuration file as follows:
TransPort 9040
DNSPort 53
VirtualAddrNetwork 10.192.0.0/10
AutomapHostsOnResolve 1
The iptables rules are as follows:
#!/bin/bash
# Clear existing rules
iptables -F
iptables -t nat -F
iptables -t mangle -F
# Enable forwarding and network settings
sysctl -w net.ipv4.ip_forward=1
sysctl -w net.ipv4.conf.all.rp_filter=0
sysctl -w net.ipv4.conf.enp0s3.rp_filter=0
sysctl -w net.ipv4.conf.all.route_localnet=1
# Set default policies
iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
# Allow loopback interface
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# SSH ACCEPT RULES
# Allow established SSH connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow new SSH connections on port 22 from anywhere
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# FORWARD RULES for gateway functionality
# Allow forwarding between interfaces
iptables -A FORWARD -i enp0s3 -o enp0s3 -j ACCEPT
# Allow established connections in FORWARD chain
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow DNS responses back from Google DNS
iptables -A FORWARD -p udp --sport 53 -j ACCEPT
# Allow Tor traffic to forward
iptables -A FORWARD -p tcp --dport 9040 -j ACCEPT
iptables -A FORWARD -p tcp --sport 9040 -j ACCEPT
# OUTPUT RULES
# Allow Tor to send responses
iptables -A OUTPUT -p tcp --dport 9040 -j ACCEPT
iptables -A INPUT -p tcp --sport 9040 -j ACCEPT
# NAT RULES - Traffic redirection for ALL IPs (no restrictions)
# DNS: Forward all DNS queries to Google DNS (not through Tor)
iptables -t nat -A PREROUTING -i enp0s3 -p udp --dport 53 -j DNAT --to-destination 8.8.8.8:53
# TCP: Redirect all other TCP traffic to Tor
iptables -t nat -A PREROUTING -i enp0s3 -p tcp -j REDIRECT --to-port 9040
# Optional: Allow ICMP (ping) for debugging
iptables -A INPUT -p icmp -j ACCEPT
# Log dropped packets for debugging (optional)
iptables -A INPUT -j LOG --log-prefix "INPUT_DROP: " --log-level 4
But I can’t browse anything. My Linux server can see the traffic:
# tcpdump -i enp0s3 -v src host 172.21.50.77
tcpdump: listening on enp0s3, link-type EN10MB (Ethernet), snapshot length 262144 bytes
06:48:26.177486 IP (tos 0x0, ttl 128, id 29909, offset 0, flags [none], proto UDP (17), length 56)
172.21.50.77.54664 > 172.21.50.52.domain: 35298+ A? zone94.com. (28)
06:48:26.177546 IP (tos 0x0, ttl 127, id 29909, offset 0, flags [none], proto UDP (17), length 56)
172.21.50.77.54664 > dns.google.domain: 35298+ A? zone94.com. (28)
06:48:27.079925 IP (tos 0x0, ttl 128, id 29910, offset 0, flags [none], proto UDP (17), length 76)
172.21.50.77.50283 > 172.21.50.52.domain: 37312+ A? self.events.data.microsoft.com. (48)
06:48:27.080035 IP (tos 0x0, ttl 127, id 29910, offset 0, flags [none], proto UDP (17), length 76)
172.21.50.77.50283 > dns.google.domain: 37312+ A? self.events.data.microsoft.com. (48)
06:48:27.180032 IP (tos 0x0, ttl 128, id 29911, offset 0, flags [none], proto UDP (17), length 56)
172.21.50.77.54664 > 172.21.50.52.domain: 35298+ A? zone94.com. (28)
06:48:27.180077 IP (tos 0x0, ttl 127, id 29911, offset 0, flags [none], proto UDP (17), length 56)
172.21.50.77.54664 > dns.google.domain: 35298+ A? zone94.com. (28)
06:48:28.187805 IP (tos 0x0, ttl 128, id 29912, offset 0, flags [none], proto UDP (17), length 56)
172.21.50.77.54664 > 172.21.50.52.domain: 35298+ A? zone94.com. (28)
06:48:28.187856 IP (tos 0x0, ttl 127, id 29912, offset 0, flags [none], proto UDP (17), length 56)
172.21.50.77.54664 > dns.google.domain: 35298+ A? zone94.com. (28)
What is wrong?
Thank you.