-
Notifications
You must be signed in to change notification settings - Fork 0
/
torController.py
64 lines (55 loc) · 1.83 KB
/
torController.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'''
torController.py
Author: broglea
A very simple and basic torController that uses telnet to the localhost to authenticate
and get new identities
PREREQUISITE TO USING THIS:
1) You must edit your /etc/tor/torrc file to turn on ControlPort (default is 9051)
2) You must edit your /etc/tor/torrc with your HashedControlPassword you want to use,
this can be obtained via the command 'tor --hash-password passwordGoesHere'
usage:
from torController import torController
tor = torController(9051, "passwordGoesHere")
tor.connect()
tor.newIdentity()
'''
import telnetlib
import time
class torController(object):
def __init__(self, PORT, PASS):
self.port = PORT
self.password = PASS
self.tn = telnetlib.Telnet("127.0.0.1", self.port)
def connect(self):
self.tn.write("authenticate " + '"' + self.password + '"\n')
time.sleep(.5)
status = self.tn.read_very_eager()
if(status=="250 OK\r\n"):
print("Authenticated to Tor Controller")
else:
raise Exception("Could not authenticate to Tor Controller: " + status)
def newIdentity(self, country=None):
if country is "any":
self.tn.write("SIGNAL RELOAD\r\n")
time.sleep(.5)
self.read()
elif country is not None:
self.tn.write("SETCONF ExitNode={" + country + "}\r\n")
time.sleep(.5)
self.read()
self.tn.write("SETCONF StrictNodes=1\r\n")
time.sleep(.5)
self.read()
self.tn.write("SIGNAL NEWNYM\r\n")
time.sleep(.5)
self.read("newIdentity")
def read(self, toCheckFor=None):
status = self.tn.read_very_eager()
if toCheckFor is None:
if(status!="250 OK\r\n"):
raise Exception("Error while getting a New Identity: " + status)
elif toCheckFor is "newIdentity":
if(status=="250 OK\r\n"):
print("Successfully got a New Identity")
else:
raise Exception("Error while getting a New Identity: " + status)