WebDriver Selenium Tor Integration C# Code

WebDriver
29 Shares
Using Selenium WebDriver Tor

From a really long time, I wanted to write automation using the Tor Web Browser. My preferred automation framework is Selenium WebDriver. However, I found out that there isn’t a built-in integration between the two. I wasted almost half a day to discover how to combine them. Finally, I did it! You can find my discoveries below about the Selenium Tor integration.

What is Tor?

Tor is free software and an open network that helps you defend against traffic analysis, a form of network surveillance that threatens personal freedom and privacy, confidential business activities and relationships, and state security.

It was originally developed with the U.S. Navy in mind, for the primary purpose of protecting government communications. Today, it is used every day for a wide variety of purposes by normal people, the military, journalists, law enforcement officers, activists, and many others.

Tor Browser Initial ViewAs you can see Tor is a modified Firefox browser. You have all features of Firefox plus additional protection.

Download it from here: https://www.torproject.org/

Selenium Tor Integration WebDriver C# Code

In order to accomplish Selenium Tor integration, you first need to setup Firefox to use the Tor proxy. You cannot start the Tor browser using IWebDriver interface because there isn’t an implementation for it. Once started the proxy generated by Tor can be accessed via “127.0.0.1 9051“. So the first step in the plan is to start a new instance of Тоr. Next, we start a new FirefoxDriver configured with a new profile with the proxy settings generated by Tor.

public IWebDriver Driver { get; set; }
public Process TorProcess { get; set; }
public WebDriverWait Wait { get; set; }
[TestInitialize]
public void SetupTest()
{
String torBinaryPath = @”C:UsersaangelovDesktopTor BrowserBrowserfirefox.exe”;
this.TorProcess = new Process();
this.TorProcess.StartInfo.FileName = torBinaryPath;
this.TorProcess.StartInfo.Arguments = -n;
this.TorProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
this.TorProcess.Start();
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference(network.proxy.type, 1);
profile.SetPreference(network.proxy.socks, 127.0.0.1);
profile.SetPreference(network.proxy.socks_port, 9150);
this.Driver = new FirefoxDriver(profile);
this.Wait = new WebDriverWait(this.Driver, TimeSpan.FromSeconds(60));
}
[TestCleanup]
public void TeardownTest()
{
this.Driver.Quit();
this.TorProcess.Kill();
}

By default, Tor is installed on your desktop. We start a new process of the browser which we kill in the test cleanup. The SetPreference method configures the Firefox profile to use the Tor’s proxy settings. You can validate the setup via the test below, just change the IP address with yours. The test will open http://whatismyipaddress.com/ and will verify that your FirefoxDriver is running with different IP.

[TestMethod]
public void Open_Tor_Browser()
{
this.Driver.Navigate().GoToUrl(@”http://whatismyipaddress.com/”);
var expression = By.XPath(//*[@id=’section_left’]/div[2]);
this.Wait.Until(x => x.FindElement(expression));
var element = this.Driver.FindElement(expression);
Assert.AreNotEqual<string>(84.40.65.000, element.Text);
}

Change Tor Identity via Code

As you know this special browser allows you to change your identity (IP address) via the magic Tor button.
Tor button
However, it’s possible to do it via telnet or C# code. Use the following guide for telnet: https://stem.torproject.org/faq.html

% telnet localhost 9051
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
AUTHENTICATE
250 OK
SIGNAL NEWNYM
250 OK

In the following folder “C:\Users\{your_user}\Desktop\Tor Browser\Browser\TorBrowser\Data\Tor“, you are going to find torrc-defaults file (config file). If you want use password authentication, you need to generate a password hash. You can do it via the tor.exe –hash-password “your_password” command.
Then open the torrc-defaults file and add the following line:

hashedControlPassword 16:751C69A9B10D7F4260B04E0D07D7EBCB760EDCEBADD40CDAF40F1FB095

The string after hashedControlPassword is the hash generated for your password.
Your file should be similar to this:

# If non-zero, try to write to disk less frequently than we would otherwise.
AvoidDiskWrites 1
# Where to send logging messages. Format is minSeverity[-maxSeverity]
# (stderr|stdout|syslog|file FILENAME).
Log notice stdout
# Bind to this address to listen to connections from SOCKS-speaking
# applications.
SocksPort 9150
ControlPort 9151
hashedControlPassword 16:751C69A9B10D7F4260B04E0D07D7EBCB760EDCEBADD40CDAF40F1FB095
CookieAuthentication 0
## fteproxy configuration
ClientTransportPlugin fte exec TorBrowserTorPluggableTransportsfteproxy –managed
## obfsproxy configuration
ClientTransportPlugin obfs2,obfs3,scramblesuit exec TorBrowserTorPluggableTransportsobfsproxy managed
## flash proxy configuration
#
# Change the second number here (9000) to the number of a port that can
# receive connections from the Internet (the port for which you
# configured port forwarding).
ClientTransportPlugin flashproxy exec TorBrowserTorPluggableTransportsflashproxy-client –register :0 :9000
## meek configuration
ClientTransportPlugin meek exec TorBrowserTorPluggableTransportsterminateprocess-buffer TorBrowserTorPluggableTransportsmeek-client-torbrowser –exit-on-stdin-eof — TorBrowserTorPluggableTransportsmeek-client

Now you can use the following C# code to refresh the proxy settings.

public void RefreshTorIdentity()
{
Socket server = null;
try
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(127.0.0.1), 9151);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Connect(ip);
server.Send(Encoding.ASCII.GetBytes(AUTHENTICATE johnsmith + Environment.NewLine));
byte[] data = new byte[1024];
int receivedDataLength = server.Receive(data);
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
server.Send(Encoding.ASCII.GetBytes(SIGNAL NEWNYM + Environment.NewLine));
data = new byte[1024];
receivedDataLength = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
if (!stringData.Contains(250))
{
Console.WriteLine(Unable to signal new user to server.);
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
finally
{
server.Close();
}
}
  • denis sorn

    Probably just a small typo, but your sentence “As you can see Tor is a modified Firefox browser.” should read “… Tor Browser is modified Firefox…”. Tor self has nothing to do with FF. It can be used with other browsers and tools.

  • Diomedes Domínguez

    Any examples of Driver.Mouse.MouseDown?

    • You can use the following code:

      Actions builder = new Actions(driver);
      builder.MoveToElement(yourElement).Build().Perform();

  • Diomedes Domínguez

    Any reason for this error?

  • Aron

    Hi, I really enjoy your tutorials. There is anyway to completely change TOR identity without closing and opening the browser? Im using the webdriver with phantomJS.