How To Access Tor Using Python

Published on May 18, 2024

Darkweb Image

Tor (The Onion Router) often associated with the dark web is fundamentally a tool for anonymizing internet activity. Tor was developed by the U.S. Naval Research Laboratory in the 1990s for secure and anonymous communication. The project became open source in 2004 and has since gained popularity among journalists, activists, and individuals seeking privacy.

Tor has been used for both legitimate purposes and crimes. While it provides privacy for journalists and activists, it also enables cybercriminals, peddlers, and others to offer illicit services with significant protection from law enforcement.

Today I am going to write a python script for accessing onion websites through TOR. I expect you are using windows OS for the project.

Requirements :-

  • Python Installed
  • Tor Expert Bundle. Download according to your system requirements. Mostly x86_64 Alpha

  • Requests library
  • ```python pip install requests ```
  • Next we need socks library for requests. Use the below command for that
  • ```python pip install requests[socks] ```

Now once you have got tor expert bundle downloaded. Extract the bundle. There will be two folders inside Tor & Data. Open the Tor folder and start the tor.exe . Once you have started the Tor.exe will start listening on PORT 9050 as a proxy server. We will use this port to access the tor. If you want you can use Tor browser but that uses PORT 9150 as proxy server. We will jump into python script now.

```python import requests torproxies = { 'http': 'socks5h://127.0.0.1:9050', #socks5 is version of proxy and `h` in socks5 says to use tor for DNS requests also 'https': 'socks5h://127.0.0.1:9050' } ```

Once the socks5 proxy variables are made we will pass these proxies to the get method for request.

```python url = "pzhdfe7jraknpj2qgu5cz2u3i4deuyfwmonvzu5i3nyw4t4bmg7o5pad.onion/" #Use your own url. This url is of tor blogs response = requests.get(url,proxies=torproxies) print(response.text) #Print response text or raw html of the response print(response.status_code) #Prints status code of request ```

I am providing the full code below. You can make change the PORT 9050 to 9150 if you are using tor browser, and choose your url accordingly.

```python import requests # Define the proxy proxies = { 'http': 'socks5h://127.0.0.1:9050', 'https': 'socks5h://127.0.0.1:9050' } # Make a request through the Tor network url = 'http://example.onion' # Replace with the .onion site you want to access response = requests.get(url, proxies=proxies) # Print the response print(response.text) ```

Output:

Output for the code