Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Controlling Tor

Tags:

python

tor

I'm attempting to control Tor with Python. I've read a couple of the other questions asked about this subject on stackoverflow but none of them answer this question.

I'm looking for a method to have tor give you a 'new identity', a new IP address, when the command is run. I've googled around and found the TorCtl module as a method for controlling tor, but can't find a way to get a new identity. Here's what I have so far for atleast connecting to tor, but can't get any farther.

from TorCtl import TorCtl

conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123")

Any help on this is appreciated, if there are other modules better then TorCtl that'd be great too! Thank you!

like image 595
Dustin Avatar asked Mar 28 '12 15:03

Dustin


2 Answers

Well, out of luck I managed to find a PHP script that did the exact same thing I wanted, and with the help of that I converted it to work in TorCtl. This is what it looks like for anyone else needing it in the future!

from TorCtl import TorCtl

conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="123")

TorCtl.Connection.send_signal(conn, "NEWNYM")
like image 98
Dustin Avatar answered Oct 15 '22 03:10

Dustin


You can use a similar code in python:

def renewTorIdentity(self, passAuth):
    try:
        s = socket.socket()
        s.connect(('localhost', 9051))
        s.send('AUTHENTICATE "{0}"\r\n'.format(passAuth))
        resp = s.recv(1024)

        if resp.startswith('250'):
            s.send("signal NEWNYM\r\n")
            resp = s.recv(1024)

            if resp.startswith('250'):
                print "Identity renewed"
            else:
                print "response 2:", resp

        else:
            print "response 1:", resp

    except Exception as e:
        print "Can't renew identity: ", e 

You can check this post for a mini-tutorial

like image 45
Ferran Salguero Avatar answered Oct 15 '22 04:10

Ferran Salguero