The Stem module in Python is a controller library that allows developers to interact with the Tor daemon and manage Tor processes. It provides a comprehensive set of functions and classes for controlling Tor’s behavior, including starting, stopping, and configuring the Tor process. Stem also enables users to change Tor identities, monitor Tor traffic, and interact with hidden services.
Key Features of the Stem Module:
- Controlling Tor Processes: Start, stop, and restart Tor processes using the Controller class.
- Configuring Tor Settings: Modify Tor configuration options, such as setting hidden service directories and configuring SOCKS proxy settings.
- Changing Tor Identities: Renew Tor identities and IP addresses using the NEWNYM signal.
- Monitoring Tor Traffic: Retrieve information about Tor connections, including active relays and bandwidth usage.
- Interacting with Hidden Services: Connect to and interact with hidden services on the Tor network.
Installing Stem:
Stem can be installed using pip, the Python package manager:
pip3 install stemBasic Usage:
- Import Stem: Import the Stem module into your Python script:
from stem import Signal, Controller- Create a Controller: Create an instance of the Controller class to manage the Tor process:
with Controller.from_port(port=9051) as controller:- Authenticate with the Controller: If you have set a password for the Tor controller, authenticate with the Controller object:
controller.authenticate()- Signal to Change Identity: To change your Tor identity and renew your IP address, send the NEWNYM signal to the Controller:
controller.signal(Signal.NEWNYM)- Check Tor Status: Verify if the Tor connection is established and functioning correctly:
if controller.is_running():
print("Tor is running successfully.")
else:
print("Tor is not running or not connected.")
- Perform Tasks: Once connected to the Tor network, you can perform tasks like making anonymous requests using libraries like Requests:
import requests
session = requests.Session()
session.proxies = {'http': 'socks5://localhost:9050'}
response = session.get('https://hidden-service-url.onion')
- Close Controller: Close the Controller object to release resources:
controller.close()Further Exploration:
The Stem module provides a rich set of functionalities for interacting with the Tor network from Python. Explore the Stem documentation and examples to discover how to utilize its advanced features for more complex tasks.
Using Stem Library to Retrieve Tor Network Descriptors
This Python script demonstrates how to use the stem library to download and print information about Tor network descriptors, specifically the consensus document.
1. Importing the Required Module:
from stem.descriptor.remote import DescriptorDownloaderThe script imports the DescriptorDownloader class from the stem.descriptor.remote module.
2. Creating Descriptor Downloader and Retrieving Consensus:
downloader = DescriptorDownloader()
descriptors = downloader.get_consensus().run()An instance of DescriptorDownloader is created. The get_consensus().run() method is then called to retrieve the Tor network consensus. The descriptors variable holds the retrieved descriptors.
3. Printing Descriptor Information:
for descriptor in descriptors:
print('Nickname:', descriptor.nickname)
print('Fingerprint:', descriptor.fingerprint)
print('Address:', descriptor.address)
print('Bandwidth:', descriptor.bandwidth)A loop iterates over the retrieved descriptors, and information such as nickname, fingerprint, address, and bandwidth is printed for each descriptor.
Explanation Summary:
- The script uses the
stemlibrary to download and retrieve Tor network descriptors. - The
DescriptorDownloaderclass is utilized to fetch the consensus document, which contains information about Tor relays. - Descriptor information such as nickname, fingerprint, address, and bandwidth is then printed for each relay in the consensus.
from stem.descriptor.remote import DescriptorDownloader
downloader = DescriptorDownloader()
descriptors = downloader.get_consensus().run()
for descriptor in descriptors:
print('Nickname:',descriptor.nickname)
print('Fingerprint:',descriptor.fingerprint)
print('Address:',descriptor.address)
print('Bandwidth:',descriptor.bandwidth)Note:
Make sure to have the stem library installed (pip install stem) before running this script. Additionally, running this script requires a working internet connection to fetch the consensus from the Tor network.
Using Stem Library to Retrieve Tor Network Status
This Python script demonstrates how to use the stem library to connect to the Tor control port, authenticate, and retrieve information about Tor network router entries.
1. Importing the Required Module:
from stem.control import ControllerThe script imports the Controller class from the stem.control module.
2. Connecting to Tor Control Port and Authenticating:
controller = Controller.from_port(port=9051)
controller.authenticate()An instance of the Controller class is created, connecting to the Tor control port on localhost:9051. The authenticate method is called to authenticate with the Tor control port.
3. Retrieving and Printing Tor Network Status:
entries = controller.get_network_statuses()
for routerEntry in entries:
print('Nickname:', routerEntry.nickname)
print('Fingerprint:', routerEntry.fingerprint)The get_network_statuses method is used to retrieve a list of network statuses, representing Tor routers. A loop iterates over the router entries, and information such as nickname and fingerprint is printed for each router.
from stem.control import Controller
controller = Controller.from_port(port=9051)
controller.authenticate()
entries = controller.get_network_statuses()
for routerEntry in entries:
print('Nickname:',routerEntry.nickname)
print('Fingerprint:',routerEntry.fingerprint)Explanation Summary:
- The script uses the
stemlibrary to connect to the Tor control port and authenticate. - The
get_network_statusesmethod is utilized to fetch information about Tor network router entries. - Information such as nickname and fingerprint is printed for each router in the retrieved network statuses.
Using Stem Library to Retrieve Tor Circuits Information
This Python script demonstrates how to use the stem library to connect to the Tor control port, authenticate, and retrieve information about Tor circuits.
1. Importing the Required Module:
from stem import CircStatus
from stem.control import ControllerThe script imports the CircStatus enumeration and the Controller class from the stem library.
2. Connecting to Tor Control Port and Authenticating:
with Controller.from_port(port=9051) as controller:
controller.authenticate()An instance of the Controller class is created using a context manager to ensure proper connection handling. The script connects to the Tor control port on localhost:9051 and authenticates with the control port.
3. Retrieving and Printing Tor Circuits Information:
for circ in sorted(controller.get_circuits()):
if circ.status != CircStatus.BUILT:
continue
print("Circuit %s (%s)" % (circ.id, circ.purpose))
for i, entry in enumerate(circ.path):
div = '+' if (i == len(circ.path) - 1) else '|'
fingerprint, nickname = entry
desc = controller.get_network_status(fingerprint, None)
address = desc.address if desc else 'unknown'
print(" %s- %s (%s, %s)" % (div, fingerprint, nickname, address))The get_circuits method is used to retrieve information about Tor circuits. A loop iterates over the circuits, and for each circuit, information such as ID, purpose, and the path of relays is printed.
from stem import CircStatus
from stem.control import Controller
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
for circ in sorted(controller.get_circuits()):
if circ.status != CircStatus.BUILT:
continue
print("Circuit %s (%s)" % (circ.id, circ.purpose))
for i, entry in enumerate(circ.path):
div = '+' if (i == len(circ.path) - 1) else '|'
fingerprint, nickname = entry
desc = controller.get_network_status(fingerprint, None)
address = desc.address if desc else 'unknown'
print(" %s- %s (%s, %s)" % (div, fingerprint, nickname, address))Explanation Summary:
- The script uses the
stemlibrary to connect to the Tor control port and authenticate. - The
get_circuitsmethod is utilized to fetch information about Tor circuits. - Information such as circuit ID, purpose, and the path of relays in the circuit is printed for each built circuit.
Using Stem Library to Retrieve Tor Server Descriptors
This Python script demonstrates how to use the stem library to download and print information about Tor server descriptors.
1. Importing the Required Module:
from stem.descriptor.remote import DescriptorDownloaderThe script imports the DescriptorDownloader class from the stem.descriptor.remote module.
2. Creating Descriptor Downloader and Retrieving Server Descriptors:
downloader = DescriptorDownloader()
descriptors = downloader.get_server_descriptors().run()An instance of DescriptorDownloader is created. The get_server_descriptors().run() method is then called to retrieve Tor server descriptors. The descriptors variable holds the retrieved server descriptors.
3. Printing Server Descriptor Information:
for descriptor in descriptors:
print('Descriptor', str(descriptor))
print('Certificate', descriptor.certificate)
print('Onion key', descriptor.onion_key)
print('Signing key', descriptor.signing_key)
print('Signature', descriptor.signature)A loop iterates over the retrieved server descriptors, and information such as the descriptor itself, certificate, onion key, signing key, and signature is printed for each server.
from stem.descriptor.remote import DescriptorDownloader
downloader = DescriptorDownloader()
descriptors = downloader.get_server_descriptors().run()
for descriptor in descriptors:
print('Descriptor', str(descriptor))
print('Certificate', descriptor.certificate)
print('ONion key', descriptor.onion_key)
print('Signing key', descriptor.signing_key)
print('Signature', descriptor.signature)Explanation Summary:
- The script uses the
stemlibrary to download and retrieve Tor server descriptors. - The
DescriptorDownloaderclass is utilized to fetch server descriptors. - Information such as the descriptor, certificate, onion key, signing key, and signature is printed for each server descriptor.
Using Stem Library to Retrieve Hidden Service Descriptors
This Python script demonstrates how to use the stem library to connect to the Tor control port, authenticate, and retrieve information about a specific hidden service.
1. Importing the Required Module:
from stem.control import ControllerThe script imports the Controller class from the stem.control module.
2. Connecting to Tor Control Port and Authenticating:
with Controller.from_port(port=9051) as controller:
controller.authenticate()An instance of the Controller class is created using a context manager to ensure proper connection handling. The script connects to the Tor control port on localhost:9051 and authenticates with the control port.
3. Retrieving and Printing Hidden Service Descriptor Information:
desc = controller.get_hidden_service_descriptor('3g2upl4pq6kufc4m')
print("DuckDuckGo's introduction points are...\n")
for introduction_point in desc.introduction_points():
print(' %s:%s => %s' % (introduction_point.address, introduction_point.port, introduction_point.identifier))The get_hidden_service_descriptor method is used to retrieve information about the hidden service with the specified identifier (‘3g2upl4pq6kufc4m’). The script then prints information about the introduction points of the hidden service.
from stem.control import Controller
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
desc = controller.get_hidden_service_descriptor('3g2upl4pq6kufc4m')
print("DuckDuckGo's introduction points are...\n")
for introduction_point in desc.introduction_points():
print(' %s:%s => %s' % (introduction_point.address, introduction_point.port, introduction_point.identifier))Explanation Summary:
- The script uses the
stemlibrary to connect to the Tor control port and authenticate. - The
get_hidden_service_descriptormethod is utilized to fetch information about a specific hidden service. - Information about the introduction points of the hidden service is printed.
Using Stem Library to Request a New Tor Identity
This Python script demonstrates how to use the stem library to connect to the Tor control port, authenticate, and request a new identity (circuit) to change the Tor exit node.
1. Importing the Required Module:
from stem import Signal
from stem.control import ControllerThe script imports the Signal enumeration and the Controller class from the stem library.
2. Connecting to Tor Control Port and Authenticating:
with Controller.from_port(port=9051) as controller:
controller.authenticate()An instance of the Controller class is created using a context manager to ensure proper connection handling. The script connects to the Tor control port on localhost:9051 and authenticates with the control port.
3. Requesting a New Tor Identity:
print("success!")
controller.signal(Signal.NEWNYM)
print("New Tor Connection processed")The signal method is called with Signal.NEWNYM as an argument, which instructs Tor to establish a new identity (circuit). This action changes the Tor exit node, providing a fresh connection.
from stem import Signal
from stem.control import Controller
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
print("success!")
controller.signal(Signal.NEWNYM)
print("New Tor Connection processed")Explanation Summary:
- The script uses the
stemlibrary to connect to the Tor control port and authenticate. - The
signalmethod is utilized to send aNEWNYMsignal, requesting a new Tor identity. - After successfully processing the signal, the script prints a success message and indicates that a new Tor connection has been established.
Rotating Tor Identity Using Stem Library
This Python script demonstrates how to use the stem library to connect to the Tor control port, authenticate, and periodically request a new Tor identity to rotate the Tor exit node.
1. Importing the Required Modules:
import time
from stem import Signal
from stem.control import Controller
import requestsThe script imports the necessary modules, including time for time-related functions, Signal enumeration, Controller class from stem for Tor control, and the requests library for making HTTP requests through Tor.
2. Defining Functions:
def get_tor_session():
session = requests.session()
session.proxies = {'http': 'socks5h://127.0.0.1:9050', 'https': 'socks5h://127.0.0.1:9050'}
return sessionA function get_tor_session is defined to create a Tor session using the requests library with appropriate proxy settings.
3. Main Script:
def main():
while True:
time.sleep(5) # Wait for 5 seconds
print("Rotating IP")
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM) # Gets a new identity
session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)The main function is defined to run continuously. It sleeps for 5 seconds, then connects to the Tor control port, authenticates, and sends a NEWNYM signal to request a new Tor identity. It prints the IP visible through Tor after the identity rotation.
4. Running the Script:
if __name__ == '__main__':
main()The script is executed when run as a standalone program.
import time
from stem import Signal
from stem.control import Controller
import requests
def get_tor_session():
session = requests.session()
# Tor uses the 9050 port as the default socks port
session.proxies = {'http': 'socks5h://127.0.0.1:9050','https': 'socks5h://127.0.0.1:9050'}
return session
def main():
while True:
time.sleep(5)
print ("Rotating IP")
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM) #gets new identity
# Make a request through the Tor connection
# IP visible through Tor
# Should print an IP different than your public IP
session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)
if __name__ == '__main__':
main()Explanation Summary:
- The script uses the
stemlibrary to connect to the Tor control port and authenticate. - It defines a function to create a Tor session using the
requestslibrary with appropriate proxy settings. - The main loop periodically rotates the Tor identity (exit node) and prints the new IP visible through Tor.
Rotating Tor IP Addresses with Socks Proxy
This Python script demonstrates how to use the socks library in conjunction with the stem library to connect to the Tor control port, authenticate, and rotate Tor IP addresses.
1. Importing the Required Modules:
import time
import socks
import socket
import requests
from stem import Signal
from stem.control import ControllerThe script imports necessary modules, including time for time-related functions, socks for SOCKS proxy support, socket for socket manipulation, requests for making HTTP requests, and Signal, Controller from stem for Tor control.
2. Configuring Socks Proxy:
numberIPAddresses = 5The variable numberIPAddresses is set to determine how many times the script will rotate Tor IP addresses.
3. Connecting to Tor Control Port and Rotating IP Addresses:
with Controller.from_port(port=9051) as controller:
controller.authenticate()
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
for i in range(0, numberIPAddresses):
newIPAddress = requests.get("http://icanhazip.com").text
print("New IP Address: %s" % newIPAddress)
controller.signal(Signal.NEWNYM)
if not controller.is_newnym_available():
print("Waiting time for Tor to change IP: %d seconds" % controller.get_newnym_wait())
time.sleep(controller.get_newnym_wait())The script connects to the Tor control port, authenticates, sets the SOCKS proxy, and then iteratively rotates Tor IP addresses. It prints the new IP address obtained from “http://icanhazip.com” after each rotation.
4. Closing the Controller Connection:
controller.close()The connection to the Tor control port is closed after rotating the desired number of IP addresses.
Explanation Summary:
- The script combines the
socksandstemlibraries to connect to the Tor control port, authenticate, and rotate Tor IP addresses. - It uses the
requestslibrary to fetch the new IP address after each rotation. - The script prints the new IP address and waits for Tor to change the IP if necessary.
Scraping information form the Tor network with python tools
TorBot is a tool that allows you to access and interact with hidden services on the Tor network. It is a command-line tool that provides a variety of features, including:
- Listing hidden services: TorBot can list hidden services by category or by keyword.
- Accessing hidden services: TorBot can open hidden services in your web browser.
- Downloading files from hidden services: TorBot can download files from hidden services.
- Uploading files to hidden services: TorBot can upload files to hidden services.
To use TorBot, you will need to have Tor installed and running on your computer. You will also need to have a copy of the TorBot executable file.
Here are the steps on how to use TorBot:
- Start Tor: If Tor is not already running, start it by executing the following command:
sudo service tor start- Launch TorBot: Launch TorBot by executing the following command:
tor-bot- List hidden services: To list hidden services by category, use the following command:
tor-bot list <category>Where is the category of hidden services you want to list. For example, to list hidden services in the “market” category, you would use the following command:
tor-bot list marketTo list hidden services by keyword, use the following command:
tor-bot search <keyword>Where is the keyword you want to search for. For example, to search for hidden services that contain the word “drugs”, you would use the following command:
tor-bot search drugs- Access hidden services: To open a hidden service in your web browser, use the following command:
tor-bot open <onion address>Where is the onion address of the hidden service you want to open. For example, to open the Hidden Wiki, you would use the following command:
tor-bot open http://wiki.onion- Download files from hidden services: To download a file from a hidden service, use the following command:
tor-bot download <onion address> <filename>Where is the onion address of the hidden service you want to download the file from, and is the name of the file you want to save the downloaded file to. For example, to download the file “index.html” from the Hidden Wiki, you would use the following command:
tor-bot download http://wiki.onion index.html- Upload files to hidden services: To upload a file to a hidden service, use the following command:
tor-bot upload <onion address> <filename>Where is the onion address of the hidden service you want to upload the file to, and is the name of the file you want to upload. For example, to upload the file “index.html” to the Hidden Wiki, you would use the following command:
tor-bot upload http://wiki.onion index.html