Tor, short for The Onion Router, is a decentralized network that allows users to browse the internet anonymously. One of its key features is the ability to host “hidden services,” which are web services that can only be accessed through the Tor network. In this tutorial, we will explore how to build a Tor hidden service proxy in Python, which allows you to route HTTP traffic through a Tor hidden service.
Setting Up the Environment
To begin, you will need to set up a few components before building the proxy:
1. **Install Tor**: Tor must be installed and running on your machine to enable the hidden service.
On Ubuntu, you can install Tor by running:
sudo apt update
sudo apt install tor
Install Python Dependencies: We will use requests for making HTTP requests and stem for controlling the Tor process. Install them with pip:
pip install requests stem
Start Tor Service: Ensure that the Tor service is running. You can start it with the following command:
sudo systemctl start tor
Configure the Hidden Service: You need to modify the Tor configuration to set up your hidden service. Open the torrc file:
sudo nano /etc/tor/torrc
Add the following lines to the configuration file:
swift
HiddenServiceDir /var/lib/tor/hidden_service/
HiddenServicePort 80 127.0.0.1:8080
This configuration tells Tor to forward requests on port 80 to a local HTTP server running on port 8080. After saving the changes, restart Tor:
sudo systemctl restart tor
Tor will create a .onion address for your hidden service in the HiddenServiceDir directory.
Building the Tor Proxy in Python
Now that the environment is set up, we can proceed to build the Python-based proxy. This script will listen for HTTP requests, forward them through the Tor network, and return the response.
Setting Up the Proxy Script
Import Required Libraries: Begin by importing the necessary Python libraries.
python
import socks
import socket
from stem import Signal
from stem.control import Controller
import requests
Configure the Tor Proxy: To route traffic through Tor, you need to configure the requests library to use a SOCKS5 proxy. The following code sets up the proxy.
python
def configure_tor_proxy():
socks.set_default_proxy(socks.SOCKS5, “127.0.0.1”, 9050)
socket.socket = socks.socksocket
Create a Function to Renew Tor Circuit: Tor works by using circuits to route traffic. You can request a new circuit to improve anonymity. The following function will send a signal to the Tor network to create a new circuit.
python
def renew_tor_circuit():
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
Making Requests Through Tor: Next, create a function to handle HTTP requests and forward them through the Tor network.
python
def make_request(url):
configure_tor_proxy()
response = requests.get(url)
return response.text
Setting Up the HTTP Server
Now, let’s set up a simple HTTP server that will act as a proxy, accepting requests from clients and passing them through Tor. We’ll use Python’s built-in http.server module.
Create the Proxy Server: Define a class that extends http.server.BaseHTTPRequestHandler to handle HTTP requests.
python
from http.server import BaseHTTPRequestHandler, HTTPServer
class TorProxyHandler(BaseHTTPRequestHandler):
def do_GET(self):
url = ‘http://example.com’ # Replace with your desired target
response_text = make_request(url)
self.send_response(200)
self.send_header(‘Content-type’, ‘text/html’)
self.end_headers()
self.wfile.write(response_text.encode())
Start the Server: Finally, start the HTTP server that will listen for incoming requests on port 8080.
python
def run_server():
server_address = (”, 8080)
httpd = HTTPServer(server_address, TorProxyHandler)
print(“Starting Tor proxy server…”)
httpd.serve_forever()
if __name__ == “__main__”:
run_server()
Testing the Proxy
After starting the server, your proxy should now be running on http://localhost:8080. To test it, you can send requests to the proxy, and it will route them through the Tor network to access the target URL.
Example Test Using curl
Use curl to send a request to your proxy:
curl http://localhost:8080
You should receive the HTML content from the target website, routed through Tor.
Conclusion
This Python script demonstrates how to build a simple Tor hidden service proxy. By routing traffic through Tor, the proxy ensures anonymity for both the client and the server. You can extend this setup to handle other HTTP methods or integrate it with a more sophisticated application. Be sure to follow Tor’s best practices for maintaining privacy and security.
We earn commissions using affiliate links.