2
votes

I have written a simple python script that fetches my ip.

import urllib
import socks
import socket

#set the proxy and port
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9150)

#initialize the socket
socket.socket = socks.socksocket

#store the URL that we want
url = 'https://check.torproject.org/'

#open the URL and store it into 'response'
response = urllib.urlopen(url)

#parse the response
html = response.read()

#print to console
print html

Nothing too complex, however the problem starts when analyzing the response from check.torbrowser. The site will always give me an address that is different from my currently running Tor browser that is on the same page. However, the html response will say that I am being routed through the Tor network but it doesnt look to be coming from the 'standard' tor browser. The latter part I understand, though I did not include it in the code above, I was playing with User-Agent strings and other headers, so I will chalk it up to that being the primary cause. What I do not understand is where in the h-e-double hockey sticks did the IP come from that was served as a response from the py script?

My next question, which builds on top of all this, is how do I connect my python script to the tor network correctly? After a little googling, I found that tor will block traffic for everything other than the socks protocol and that an alternative is to use privoxy in conjunction with tor. My initial thought is to do some kind of routing that would result in the layering of software. In my mind, it would look like:

Python -> Privoxy -> Tor -> Destination

My end goal in all of this is to grab a .onion based address and save/read it. However, I have put that to the side after all of these problems started occurring. A little info to help get better answers: I am using a Windows machine, though I have a Linux one if there is some functionality that may be present there that would help this process, and I am using Python 2.7 though, again, this can be easily changed.

I would like to ask that the steps to make all this happen be laid out - or at least some links/direction, I am by no means afraid to read a few good blogs/tutorials about the subject. However, I feel like this is really a couple of seperate questions, and would require quiet a lengthy answer so I would be more than happy to just know that I am on the right path before I rip more of my hair out :)

2

2 Answers

3
votes

Your code is correct, however your assumption that Tor will always give you the same IP address is not. Thanks to circuit isolation, a privacy feature of Tor that ensures isolation between the connections you open, you're routing the request through a different exit node than the Tor Browser will.

Reliably emulating the Tor Browser behavior is hard and I would recommend against it. Your method for connecting to the Tor network looks correct.

Tor will allow you to use any protocol you want, but yes you need to connect through the SOCKS protocol. That's fine though: almost all network protocols (http included) play nicely with SOCKS.

0
votes

With torpy library you can renew circuits as you wish.

>>> from torpy.http.requests import TorRequests
>>> 
>>> def show_ip(resp):
...     for line in resp.text.splitlines():
...         if 'Your IP address appears to be' in line:
...             print(line) 
... 
>>> with TorRequests() as tor_requests:
...     print("build circuit")
...     with tor_requests.get_session() as sess:
...         show_ip(sess.get("https://check.torproject.org/"))
...         show_ip(sess.get("https://check.torproject.org/"))
...     print("renew circuit")
...     with tor_requests.get_session() as sess:
...         show_ip(sess.get("https://check.torproject.org/"))
...         show_ip(sess.get("https://check.torproject.org/"))
... 
build circuit
  <p>Your IP address appears to be:  <strong>178.17.171.102</strong></p>
  <p>Your IP address appears to be:  <strong>178.17.171.102</strong></p>
renew circuit
  <p>Your IP address appears to be:  <strong>49.50.66.209</strong></p>
  <p>Your IP address appears to be:  <strong>49.50.66.209</strong></p>