Build a Tor Transparent Proxy: A Bulletproof Gateway for Lab Anonymity

Security tutorial - IT technology blog
Security tutorial - IT technology blog

Why Anonymity Matters in the Lab

I once watched my server logs fill with over 5,000 SSH brute-force attempts in a single night after a minor IP leak. That incident was a wake-up call. It proved that even a momentary exposure of your public IP can invite persistent scanning and targeted attacks from automated bots. When you’re conducting malware analysis or penetration testing, leaking your real-world identity isn’t just a privacy slip—it’s a massive security liability.

While the Tor Browser works for basic web research, it fails to protect CLI tools, background system updates, or custom scripts that ignore browser-level proxy settings. That is why I rely on a Tor Transparent Proxy. By setting up a dedicated Linux gateway, you force every single packet leaving your lab to traverse the Tor network. No leaks, no manual configurations for every single app, and total peace of mind.

The Architecture

This setup works best on a lightweight Linux machine, such as a Debian VM with 1GB of RAM or a Raspberry Pi. You will need two network interfaces:

  • eth0 (WAN): The upstream connection to the internet.
  • eth1 (LAN/Internal): The gateway for your lab machines (VMs or physical hardware).

Our objective is to intercept all incoming traffic on eth1 and pipe it directly into the Tor process before it ever touches the open web via eth0.

Step 1: Installing Tor and Dependencies

Avoid using outdated packages from standard repositories. The official Tor Project repository is the only way to ensure you have the latest security patches and performance improvements.

# Install prerequisites
sudo apt update
sudo apt install apt-transport-https curl lsb-release -y

# Add the official Tor Project repository
cat <<EOF | sudo tee /etc/apt/sources.list.d/tor.list
deb [signed-by=/usr/share/keyrings/tor-archive-keyring.gpg] https://deb.torproject.org/torproject.org $(lsb_release -cs) main
EOF

# Import the GPG key
curl -s https://deb.torproject.org/torproject.org/A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89.asc | gpg --dearmor | sudo tee /usr/share/keyrings/tor-archive-keyring.gpg >/dev/null

# Install Tor, the keyring, and Nyx for monitoring
sudo apt update
sudo apt install tor deb.torproject.org-keyring nyx -y

Step 2: Configuring the Tor Daemon

By default, Tor acts as a SOCKS proxy on port 9050. We need to reconfigure it to function as a transparent gateway and a DNS resolver. Open /etc/tor/torrc and add these lines to the bottom of the file:

# Transparent Proxy Port
TransPort 9040

# DNS Port for the lab clients
DNSPort 5353

# Map .onion addresses to virtual IPs
VirtualAddrNetworkIPv4 10.192.0.0/10
AutomapHostsOnResolve 1

RunAsDaemon 1

Apply the new configuration by restarting the service:

sudo systemctl restart tor

Step 3: Routing Traffic with IPTables

This is where we define the routing logic. We must tell the Linux kernel to grab any traffic arriving on the internal interface and redirect it to Tor’s TransPort. I use a shell script for this to make updates and resets easier.

Create tor-gateway.sh:

#!/bin/bash

# Configuration
_tor_uid=$(id -u debian-tor)
_trans_port="9040"
_dns_port="5353"
_int_if="eth1"

# Flush existing rules to start fresh
iptables -F
iptables -t nat -F

# 1. Redirect DNS traffic
iptables -t nat -A PREROUTING -i $_int_if -p udp --dport 53 -j REDIRECT --to-ports $_dns_port

# 2. Redirect all TCP traffic to Tor
iptables -t nat -A PREROUTING -i $_int_if -p tcp --syn -j REDIRECT --to-ports $_trans_port

# 3. Drop all other non-Tor traffic from the LAN to prevent leaks
iptables -A FORWARD -i $_int_if -j DROP

# 4. Local gateway protection (prevents the gateway itself from leaking)
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
iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $_trans_port

Execute the script with root privileges:

chmod +x tor-gateway.sh
sudo ./tor-gateway.sh

To ensure these rules survive a system reboot, install the persistence package:

sudo apt install iptables-persistent -y
sudo netfilter-persistent save

Step 4: Killing DNS Leaks

DNS leaking is the most common mistake in security setups. Even if your web traffic is encrypted, your ISP can still see every domain you request if you use standard DNS. By setting DNSPort 5353 and redirecting port 53, we force every query through the Tor circuit.

On your client machines, set the static DNS server to the IP address of your gateway’s eth1 interface. This ensures the client doesn’t try to reach out to 8.8.8.8 or your ISP’s resolvers directly.

Step 5: Verification and Monitoring

Never assume the proxy is working until you prove it. From a client machine, run a simple check:

curl https://check.torproject.org

If the page confirms you are using Tor, your pipeline is secure.

For real-time visibility, I recommend Nyx. It is a powerful CLI dashboard for Tor nodes. Run it on the gateway to see your active circuits:

sudo -u debian-tor nyx

This tool helps you visualize bandwidth usage and identify the geographic location of your current exit nodes.

Final Thoughts on Performance

Routing all traffic through three layers of encrypted nodes adds significant latency. You should expect pings to jump from 20ms to 300ms or higher. While this makes downloading large ISO files tedious, the security trade-off is mandatory for high-stakes research. Getting this foundation right ensures that a single misconfigured tool won’t accidentally reveal your location to a malicious actor.

Share: