Transform Your Raspberry Pi Zero W into a Tor Modem
Written on
Chapter 1: Introduction to Raspberry Pi Zero W
The Raspberry Pi Zero W can serve as a versatile Ethernet device capable of routing all internet traffic through the Tor network. This setup transforms the Pi into a Tor modem, allowing you to enjoy the benefits of Tor beyond just the Tor browser. Instead of confining Tor usage to specific applications, you can ensure that all your device’s traffic is transmitted via Tor. This project is not only entertaining but also resource-efficient compared to using a Raspberry Pi 3B+ for creating an access point.
Section 1.1: Why Use a Raspberry Pi Zero W?
Utilizing the Raspberry Pi Zero W as a Tor modem is an innovative way to enhance your privacy online. By routing all traffic through Tor, you can achieve a higher level of anonymity. I have previously provided guides on setting up Tor access points and proxies, but the lightweight nature of the Pi Zero W makes this project particularly appealing.
Subsection 1.1.1: Requirements for the Project
To embark on this project, you will need the following components:
- A Raspberry Pi Zero W
- A Micro SD Card (16 GB is sufficient)
- A USB adapter, such as the Maker Focus USB-A Addon Board V1.1 or the Zero Dongle.
With careful shopping, this project can be completed for around $25.
Section 1.2: Setting Up Your Raspberry Pi
Begin by flashing your Micro SD Card with Raspbian Lite. To enable SSH, create an empty file named ssh in the boot directory. Next, modify config.txt to include dtoverlay=dwc2, and update cmdline.txt to append modules-load=dwc2,g_ether after rootwait. If you're unfamiliar with this process, Circuit Basics offers a detailed guide.
To connect your Pi to your WiFi network automatically, create a wpa_supplicant.conf file in the boot directory with the following configuration:
country=US # Your 2-digit country code
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
ssid="YOUR_NETWORK_NAME"
psk="YOUR_PASSWORD"
key_mgmt=WPA-PSK
}
After inserting the SD Card into your Pi, SSH into your device and run raspi-config to adjust localization settings, GPU memory, and to expand the file system. Reboot your device afterward.
Next, set up NTP by editing the configuration file:
sudo nano /etc/systemd/timesyncd.conf
Uncomment the second line, specify your preferred NTP server (like time.cloudflare.com), and ensure synchronization is enabled:
sudo timedatectl set-ntp true
Check your configuration with:
timedatectl status
Update your system and install necessary packages while saving the current IPv4 and IPv6 rules:
sudo apt-get update && sudo apt-get upgrade -y
sudo apt install dnsmasq iptables-persistent tor dnsutils -y
Configure a static IP address for usb0 by editing the dhcpcd.conf file:
sudo nano /etc/dhcpcd.conf
Add the following lines at the end of the file:
interface usb0
static ip_address=192.168.1.1/24
Now, set up a DHCP server with DNSMASQ to allocate an IP address on usb0 for client computers. Edit the dnsmasq.conf file:
sudo nano /etc/dnsmasq.conf
Insert these lines at the end of the file:
interface=usb0
dhcp-range=192.168.1.11,192.168.1.30,255.255.255.0,24h
Next, we will configure Tor to act as a translation service and enable proxy access for the Tor Browser through the USB modem. First, back up the torrc file:
sudo cp /etc/tor/torrc /etc/tor/torrc.backup
Then, edit the torrc file:
sudo nano /etc/tor/torrc
Uncomment and add the following configurations:
SocksPort 192.168.1.1:9050
SocksPolicy accept 192.168.0.0/16
Log notice file /var/log/tor/notices.log
RunAsDaemon 1
VirtualAddrNetwork 10.192.0.0/10
AutomapHostsSuffixes .onion,.exit
AutomapHostsOnResolve 1
TransPort 192.168.1.1:9040
DNSPort 192.168.1.1:53
Save your changes and create the Tor log file:
sudo touch /var/log/tor/notices.log
sudo chown debian-tor /var/log/tor/notices.log
sudo chmod 644 /var/log/tor/notices.log
Enable the Tor service:
sudo update-rc.d tor enable
Next, configure IPTABLES:
sudo iptables -F
sudo iptables -t nat -F
Set up routing to share your WiFi connection (wlan0) through usb0:
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i wlan0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i usb0 -o wlan0 -j ACCEPT
Block SSH connections from WiFi:
sudo iptables -A INPUT -i wlan0 -p tcp --dport 22 -j DROP
Allow SSH, DNS, Tor proxy, and route TCP traffic over Tor from usb0:
sudo iptables -t nat -A PREROUTING -i usb0 -p tcp --dport 22 -j REDIRECT --to-ports 22
sudo iptables -t nat -A PREROUTING -i usb0 -p udp --dport 53 -j REDIRECT --to-ports 53
sudo iptables -t nat -A PREROUTING -i usb0 -p tcp --dport 9050 -j REDIRECT --to-ports 9050
sudo iptables -t nat -A PREROUTING -i usb0 -p tcp --syn -j REDIRECT --to-ports 9040
Save the running IPTABLES configuration:
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Enable Network Forwarding:
sudo nano /etc/sysctl.conf
Uncomment the following line to enable packet forwarding:
net.ipv4.ip_forward=1
Update your local DNS settings:
sudo nano /etc/resolv.conf
Set nameservers to 1.1.1.1 or another public DNS provider.
Reboot the Raspberry Pi. After restarting, check your network adapter to see if you have an IP within the DHCP range. Disable the WiFi connection and test if Tor is functioning properly.
Chapter 2: Closing Thoughts
This project showcases an interesting gadget. While it may not be the most secure method for accessing Tor, it’s a solid option for casual use. The key advantage is that all system traffic is routed through Tor, not just the browser. I've successfully used various private messaging applications via Tor, enhancing my privacy. Switching to Tor while maintaining established web sessions generally works without a hitch. Although the Pi is limited to 2.4GHz, it seems to perform better than the Tor Browser for standard web browsing. Always remember to use Tor responsibly.
Thank you for your attention, and feel free to reach out with any questions.
Explore the Raspberry Pi Zero USB Dongle in this YouTube video that details its setup and functionality.
Watch this YouTube video on setting up a Raspberry Pi TOR/VPN Router and learn about enhancing your privacy and security.