Skip to content

Instantly share code, notes, and snippets.

@jvloo
Forked from woswos/tor-browser-selenium-wire.py
Created August 16, 2021 18:48
Show Gist options
  • Save jvloo/af1f22f339e06bbdcded33a38f8d3a38 to your computer and use it in GitHub Desktop.
Save jvloo/af1f22f339e06bbdcded33a38f8d3a38 to your computer and use it in GitHub Desktop.
This script creates a proxy server between the Tor Browser and Tor to capture requests/responses, using the seleniumwire library. You can access and modify the HTTP headers that are being sent/received, including the onion services. Note: You need to have Tor installed and running on the localhost while running this script.
import os
from seleniumwire import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
# Uncomment these if you need additional information for debugging
#import logging
#logging.basicConfig(level=logging.DEBUG)
# The location of the Tor Browser bundle
# Update this to match the location on your computer
tbb_dir = "/home/woswos/tor-browser_en-US/"
# Disable Tor Launcher to prevent it connecting the Tor Browser to Tor directly
os.environ['TOR_SKIP_LAUNCH'] = '1'
os.environ['TOR_TRANSPROXY'] = '1'
# Set the Tor Browser binary and profile
tb_binary = os.path.join(tbb_dir, 'Browser/firefox')
tb_profile = os.path.join(tbb_dir, 'Browser/TorBrowser/Data/Browser/profile.default')
binary = FirefoxBinary(os.path.join(tbb_dir, 'Browser/firefox'))
profile = FirefoxProfile(tb_profile)
# We need to disable HTTP Strict Transport Security (HSTS) in order to have
# seleniumwire between the browser and Tor. Otherwise, we will not be able
# to capture the requests and responses using seleniumwire.
profile.set_preference("security.cert_pinning.enforcement_level", 0)
profile.set_preference("network.stricttransportsecurity.preloadlist", False)
# Tell Tor Button it is OK to use seleniumwire
profile.set_preference("extensions.torbutton.local_tor_check", False)
profile.set_preference("extensions.torbutton.use_nontor_proxy", True)
# Required if you need JavaScript at all, otherwise JS stays disabled regardless
# of the Tor Browser's security slider value
profile.set_preference("browser.startup.homepage_override.mstone", "68.8.0");
# Configure seleniumwire to upstream traffic to Tor running on port 9050
# You might want to increase/decrease the timeout if you are trying
# to a load page that requires a lot of requests. It is in seconds.
options = {
'proxy': {
'http': 'socks5h://127.0.0.1:9050',
'https': 'socks5h://127.0.0.1:9050',
'connection_timeout': 10
}
}
driver = webdriver.Firefox(firefox_profile=profile,
firefox_binary=binary,
seleniumwire_options=options)
driver.get("https://check.torproject.org/")
for request in driver.requests:
if request.response:
print(
request.path,
request.response.status_code,
request.response.headers['Content-Type']
)
diver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment