Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open tor browser with selenium

Is it possible to make selenium use the TOR browser? Does anyone have any code they could copy-paste?

like image 970
user1907403 Avatar asked Mar 09 '13 21:03

user1907403


People also ask

Can you use Selenium with Tor?

Firefox. Selenium runs Tor using the Firefox package, so make sure that both Tor and Firefox browsers are installed as stand-alone applications.

Does Selenium open a browser?

Once you have a Selenium library installed, and your desired browser driver, you can start and stop a session with a browser.

How do I open the browser window in Selenium?

Open a New Tabget('https://selenium.dev'); // A new tab is opened and switches to it await driver. switchTo(). newWindow('tab'); // Loads Sauce Labs open source website in the newly opened window await driver. get('https://opensource.saucelabs.com/');


2 Answers

Don't use the TBB, just set the correct proxy settings in whatever browser you're using. In FF for example, like this:

#set some privacy settings ff_prof.set_preference( "places.history.enabled", False ) ff_prof.set_preference( "privacy.clearOnShutdown.offlineApps", True ) ff_prof.set_preference( "privacy.clearOnShutdown.passwords", True ) ff_prof.set_preference( "privacy.clearOnShutdown.siteSettings", True ) ff_prof.set_preference( "privacy.sanitize.sanitizeOnShutdown", True ) ff_prof.set_preference( "signon.rememberSignons", False ) ff_prof.set_preference( "network.cookie.lifetimePolicy", 2 ) ff_prof.set_preference( "network.dns.disablePrefetch", True ) ff_prof.set_preference( "network.http.sendRefererHeader", 0 )  #set socks proxy ff_prof.set_preference( "network.proxy.type", 1 ) ff_prof.set_preference( "network.proxy.socks_version", 5 ) ff_prof.set_preference( "network.proxy.socks", '127.0.0.1' ) ff_prof.set_preference( "network.proxy.socks_port", 9050 ) ff_prof.set_preference( "network.proxy.socks_remote_dns", True )  #if you're really hardcore about your security #js can be used to reveal your true i.p. ff_prof.set_preference( "javascript.enabled", False )  #get a huge speed increase by not downloading images ff_prof.set_preference( "permissions.default.image", 2 )  ## # programmatically start tor (in windows environment) ## tor_path = "C:\\this\\is\\the\\location\\of\\" #tor.exe torrc_path = "C:\\you\\need\\to\\create\\this\\file\\torrc"  DETACHED_PROCESS = 0x00000008 #calling as a detached_process means the program will not die with your python program - you will need to manually kill it ## # somebody please let me know if there's a way to make this a child process that automatically dies (in windows) ## tor_process = subprocess.Popen( '"' + tor_path+'tor.exe" --nt-service "-f" "' + torrc_path + '"', creationflags=DETACHED_PROCESS )  #attach to tor controller ## imports ## # import stem.socket # import stem.connection # import stem.Signal ## tor_controller = stem.socket.ControlPort( port=9051 )  control_password = 'password' #in your torrc, you need to store the hashed version of 'password' which you can get with: subprocess.call( '"' + tor_path+'tor.exe" --hash-password %s' %control_password )  stem.connection.authenticate( tor_controller, password=control_password )  #check that everything is good with your tor_process by checking bootstrap status tor_controller.send( 'GETINFO status/bootstrap-phase' ) response = worker.tor_controller.recv() response = response.content()  #I will leave handling of response status to you 
like image 194
user2426679 Avatar answered Sep 16 '22 21:09

user2426679


Yes, it is possible to make selenium use the TOR browser.

I was able to do so on both Ubuntu and Mac OS X.

Two things have to happen:

  1. Set the binary path to the firefox binary that Tor uses. On a Mac this path would typically be /Applications/TorBrowser.app/Contents/MacOS/firefox. On my Ubuntu machine it is /usr/bin/tor-browser/Browser/firefox.

  2. The Tor browser uses a SOCKS host at 127.0.0.1:9150 either through Vidalia or Tor installation. Launch Tor once from the Finder and leave it open so that Vidalia will be running. The instances launched with selenium will use the SOCKS host that Vidalia starts, too.

Here is the code to accomplish those two things. I run this on Mac OS X Yosemite:

import os from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium import webdriver   # path to the firefox binary inside the Tor package binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox' if os.path.exists(binary) is False:     raise ValueError("The binary path to Tor firefox does not exist.") firefox_binary = FirefoxBinary(binary)   browser = None def get_browser(binary=None):     global browser       # only one instance of a browser opens, remove global for multiple instances     if not browser:          browser = webdriver.Firefox(firefox_binary=binary)     return browser  if __name__ == "__main__":     browser = get_browser(binary=firefox_binary)     urls = (         ('tor browser check', 'https://check.torproject.org/'),         ('ip checker', 'http://icanhazip.com')     )     for url_name, url in urls:         print "getting", url_name, "at", url         browser.get(url) 

On an Ubuntu system I was able to run the Tor browser via selenium. This machine has tor running at port 9051 and privoxy http proxy that uses tor at port 8118. In order for the Tor browser to pass the tor check page I had to set the http proxy to privoxy.

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium import webdriver browser = None  proxy_address = "127.0.0.1:8118" proxy = Proxy({     'proxyType': ProxyType.MANUAL,     'httpProxy': proxy_address, })  tor = '/usr/bin/tor-browser/Browser/firefox' firefox_binary = FirefoxBinary(tor)  urls = (     ('tor_browser_check', 'https://check.torproject.org/'),     ('icanhazip', 'http://icanhazip.com'), ) keys, _ = zip(*urls) urls_map = dict(urls)  def get_browser(binary=None, proxy=None):     global browser     if not browser:         browser = webdriver.Firefox(firefox_binary=binary, proxy=proxy)     return browser  if __name__ == "__main__":     browser = get_browser(binary=firefox_binary, proxy=proxy)     for resource in keys:         browser.get(urls_map.get(resource)) 
like image 21
dmmfll Avatar answered Sep 20 '22 21:09

dmmfll