3

I'm trying to run Tor through python. My goal is to be able to switch exits or otherwise alter my IP from time-to-time at a time of my choosing. I've followed several tutorials and encountered several different errors.

This code prints my IP address

import requests
r = requests.get('http://icanhazip.com/')
r.content
# returns my regular IP

I've downloaded and 'installed' the Tor browser (although it seems to run from an .exe). When it is running, I can use the following code to return my Tor IP, which seems to change every 10 minutes or so, so everything seems to be working so far

import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9150)
socket.socket = socks.socksocket

r = requests.get('http://icanhazip.com/')
print r.content
# Returns a different IP

Now, I've found instructions here on how to request a new identity programmatically, but this is where things begin to go wrong. I run the following code

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9151) as controller:
    controller.authenticate()
    controller.signal(Signal.NEWNYM)

and I get the error "SOCKS5Error: 0x01: General SOCKS server failure" at the line that creates the controller. I thought this might be a configuration problem - the instructions say that there should be a config file called 'torrc' that contains the port numbers, apart from other things.

Eventually, in the directory C:\Users\..myname..\Desktop\Tor Browser\Browser\TorBrowser\Data\Tor I found three files, a 'torrc', a 'torrc-defaults', and a 'torrc.orig.1'. My 'torrc-defaults' looks similar to the 'torrc' shown in the documentation, my 'torrc.orig.1' is empty, and my 'torrc' has two lines of comments that look as follows with some more settings afterwards but no port settings

# This file was generated by Tor; if you edit it, comments will not be preserved
# The old torrc file was renamed to torrc.orig.1 or similar, and Tor will ignore it
...

I've tried to make sure that the two ports listed in the 'torrc-defaults' match the ports in the socks statement at the beginning and the controller statment further on. When these are 9150 and 9151 respectively I get the general SOCKS server failure listed above.

When I try and run the Controller with the wrong port (in this case, 9051 which was recommended in other posts but for me leads to Tor browser failing to load when I adjust the 'torrc-defaults') then I instead get the error message "[Errno 10061] No connection could be made because the target machine actively refused it"

Finally, when I run the Controller with Tor browser running in the background but without first running the SOCKS statement, I instead get a lot of warning text and finally an error, as shown in part below:

...
...
250 __OwningControllerProcess
DEBUG:stem:GETCONF __owningcontrollerprocess (runtime: 0.0010)
TRACE:stem:Sent to tor:
SIGNAL NEWNYM
TRACE:stem:Received from tor:
250 OK
TRACE:stem:Received from tor:
650 SIGNAL NEWNYM
TRACE:stem:Sent to tor:
QUIT
TRACE:stem:Received from tor:
250 closing connection
INFO:stem:Error while receiving a control message (SocketClosed): empty socket content

At first I thought this looked quite promising - I put in a quick check line like this both before and after the new user request

print requests.get('http://icanhazip.com/').content

It prints the IP, but unfortunately it's the same both before and afterwards.

I'm pretty lost here - any support would be appreciated!

Thanks.

StackG
  • 2,730
  • 5
  • 29
  • 45

2 Answers2

2

Get your hashed password :

tor --hash-password my_password

Modify the configuration file :

sudo gedit /etc/tor/torrc

Add these lines

ControlPort 9051 
HashedControlPassword 16:E600ADC1B52C80BB6022A0E999A7734571A451EB6AE50FED489B72E3DF
CookieAuthentication 1

Note :- The HashedControlPassword is generated from first command

Refer this link

I haven't tried it on my own because I am using mac, but I hope this helps with configuration

Manjush
  • 166
  • 1
  • 8
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11284370) – Jasper Feb 16 '16 at 10:54
  • Well, I hope this is relevant answer to the question. The question is about configuration. So, I added only those parts to the answer – Manjush Feb 16 '16 at 11:51
1

You can use torify to achieve your goal. for example run the python interpreter as this command "sudo torify python" and every connection will be routed through TOR.

Reference on torify: https://trac.torproject.org/projects/tor/wiki/doc/TorifyHOWTO

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
  • Thanks for your response, very interesting. I use an iPython notebook server as my VDE, how can I integrate your solution into that? – StackG Apr 22 '15 at 12:24