DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java

Trending

  • DZone's Article Submission Guidelines
  • Using Python Libraries in Java
  • The Smart Way to Talk to Your Database: Why Hybrid API + NL2SQL Wins
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Using Selenium WebDriver with Tor C# Code

Using Selenium WebDriver with Tor C# Code

By 
Anton Angelov user avatar
Anton Angelov
·
Mar. 17, 15 · Interview
Likes (0)
Comment
Save
Tweet
Share
19.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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.

As 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/

Integrate Tor and Selenium WebDriver

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:\Users\aangelov\Desktop\Tor Browser\Browser\firefox.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.

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 TorBrowser\Tor\PluggableTransports\fteproxy --managed

## obfsproxy configuration
ClientTransportPlugin obfs2,obfs3,scramblesuit exec TorBrowser\Tor\PluggableTransports\obfsproxy 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 TorBrowser\Tor\PluggableTransports\flashproxy-client --register :0 :9000

## meek configuration
ClientTransportPlugin meek exec TorBrowser\Tor\PluggableTransports\terminateprocess-buffer TorBrowser\Tor\PluggableTransports\meek-client-torbrowser --exit-on-stdin-eof -- TorBrowser\Tor\PluggableTransports\meek-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();
    }
}

You can find the full source code at the following address: https://github.com/angelovstanton/Projects/tree/master/Demos/TelerikAcademy_WebDriver

Tor (network)

Published at DZone with permission of Anton Angelov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!