forked from SusmithKrishnan/torghost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
torghost.py
executable file
·259 lines (213 loc) · 7.2 KB
/
torghost.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import getopt
from requests import get
import subprocess
import time
import signal
from stem import Signal
from stem.control import Controller
from packaging import version
VERSION = "3.1.1"
IP_API = "https://api.ipify.org/?format=json"
LATEST_RELEASE_API = "https://api.github.com/repos/SusmithKrishnan/torghost/releases/latest"
class bcolors:
BLUE = '\033[94m'
GREEN = '\033[92m'
RED = '\033[31m'
YELLOW = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
BGRED = '\033[41m'
WHITE = '\033[37m'
def t():
current_time = time.localtime()
ctime = time.strftime('%H:%M:%S', current_time)
return '[' + ctime + ']'
def sigint_handler(signum, frame):
print("User interrupt ! shutting down")
stop_torghost()
def logo():
print(bcolors.RED + bcolors.BOLD)
print("""
_____ ____ _ _
|_ _|__ _ __ / ___| |__ ___ ___| |_
| |/ _ \| '__| | _| '_ \ / _ \/ __| __|
| | (_) | | | |_| | | | | (_) \__ \ |_
|_|\___/|_| \____|_| |_|\___/|___/\__|
{V} - github.com/SusmithKrishnan/torghost
""".format(V=VERSION))
print(bcolors.ENDC)
def usage():
logo()
print("""
Torghost usage:
-s --start Start Torghost
-r --switch Request new tor exit node
-x --stop Stop Torghost
-h --help print(this help and exit)
-u --update check for update
""")
sys.exit()
def ip():
while True:
try:
jsonRes = get(IP_API).json()
ipTxt = jsonRes["ip"]
except:
continue
break
return ipTxt
def check_root():
if os.geteuid() != 0:
print("You must be root; Say the magic word 'sudo'")
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
TorrcCfgString = \
"""
VirtualAddrNetwork 10.0.0.0/10
AutomapHostsOnResolve 1
TransPort 9040
DNSPort 5353
ControlPort 9051
RunAsDaemon 1
"""
resolvString = 'nameserver 127.0.0.1'
Torrc = '/etc/tor/torghostrc'
resolv = '/etc/resolv.conf'
def start_torghost():
print(t() + ' Always check for updates using -u option')
os.system('sudo cp /etc/resolv.conf /etc/resolv.conf.bak')
if os.path.exists(Torrc) and TorrcCfgString in open(Torrc).read():
print(t() + ' Torrc file already configured')
else:
with open(Torrc, 'w') as myfile:
print(t() + ' Writing torcc file ')
myfile.write(TorrcCfgString)
print(bcolors.GREEN + '[done]' + bcolors.ENDC)
if resolvString in open(resolv).read():
print(t() + ' DNS resolv.conf file already configured')
else:
with open(resolv, 'w') as myfile:
print(t() + ' Configuring DNS resolv.conf file.. '),
myfile.write(resolvString)
print(bcolors.GREEN + '[done]' + bcolors.ENDC)
print(t() + ' Stopping tor service '),
os.system('sudo systemctl stop tor')
os.system('sudo fuser -k 9051/tcp > /dev/null 2>&1')
print(bcolors.GREEN + '[done]' + bcolors.ENDC)
print(t() + ' Starting new tor daemon '),
os.system('sudo -u debian-tor tor -f /etc/tor/torghostrc > /dev/null'
)
print(bcolors.GREEN + '[done]' + bcolors.ENDC)
print(t() + ' setting up iptables rules'),
iptables_rules = \
"""
NON_TOR="192.168.1.0/24 192.168.0.0/24"
TOR_UID=%s
TRANS_PORT="9040"
iptables -F
iptables -t nat -F
iptables -t nat -A OUTPUT -m owner --uid-owner $TOR_UID -j RETURN
iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 5353
for NET in $NON_TOR 127.0.0.0/9 127.128.0.0/10; do
iptables -t nat -A OUTPUT -d $NET -j RETURN
done
iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $TRANS_PORT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
for NET in $NON_TOR 127.0.0.0/8; do
iptables -A OUTPUT -d $NET -j ACCEPT
done
iptables -A OUTPUT -m owner --uid-owner $TOR_UID -j ACCEPT
iptables -A OUTPUT -j REJECT
""" \
% subprocess.getoutput('id -ur debian-tor')
os.system(iptables_rules)
print(bcolors.GREEN + '[done]' + bcolors.ENDC)
print(t() + ' Fetching current IP...')
print(t() + ' CURRENT IP : ' + bcolors.GREEN + ip() + bcolors.ENDC)
def stop_torghost():
print(bcolors.RED + t() + 'STOPPING torghost' + bcolors.ENDC)
print(t() + ' Flushing iptables, resetting to default'),
os.system('mv /etc/resolv.conf.bak /etc/resolv.conf')
IpFlush = \
"""
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t nat -F
iptables -t mangle -F
iptables -F
iptables -X
"""
os.system(IpFlush)
os.system('sudo fuser -k 9051/tcp > /dev/null 2>&1')
print(bcolors.GREEN + '[done]' + bcolors.ENDC)
print(t() + ' Restarting Network manager'),
os.system('service network-manager restart')
print(bcolors.GREEN + '[done]' + bcolors.ENDC)
print(t() + ' Fetching current IP...')
time.sleep(3)
print(t() + ' CURRENT IP : ' + bcolors.GREEN + ip() + bcolors.ENDC)
def switch_tor():
print(t() + ' Please wait...')
time.sleep(7)
print(t() + ' Requesting new circuit...'),
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
print(bcolors.GREEN + '[done]' + bcolors.ENDC)
print(t() + ' Fetching current IP...')
print(t() + ' CURRENT IP : ' + bcolors.GREEN + ip() + bcolors.ENDC)
def check_update():
print(t() + ' Checking for update...')
jsonRes = get(LATEST_RELEASE_API).json()
newversion = jsonRes["tag_name"][1:]
print(newversion)
if version.parse(newversion) > version.parse(VERSION):
print(t() + bcolors.GREEN + ' New update available!' + bcolors.ENDC)
print(t() + ' Your current TorGhost version : ' + bcolors.GREEN + VERSION + bcolors.ENDC)
print(t() + ' Latest TorGhost version available : ' + bcolors.GREEN + newversion + bcolors.ENDC)
yes = {'yes', 'y', 'ye', ''}
no = {'no', 'n'}
choice = input(
bcolors.BOLD + "Would you like to download latest version and build from Git repo? [Y/n]" + bcolors.ENDC).lower()
if choice in yes:
os.system(
'cd /tmp && git clone https://github.com/SusmithKrishnan/torghost')
os.system('cd /tmp/torghost && sudo ./build.sh')
elif choice in no:
print(t() + " Update aborted by user")
else:
print("Please respond with 'yes' or 'no'")
else:
print(t() + " Torghost is up to date!")
def main():
check_root()
if len(sys.argv) <= 1:
check_update()
usage()
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'srxhu', [
'start', 'stop', 'switch', 'help', 'update'])
except (getopt.GetoptError):
usage()
sys.exit(2)
for (o, a) in opts:
if o in ('-h', '--help'):
usage()
elif o in ('-s', '--start'):
start_torghost()
elif o in ('-x', '--stop'):
stop_torghost()
elif o in ('-r', '--switch'):
switch_tor()
elif o in ('-u', '--update'):
check_update()
else:
usage()
if __name__ == '__main__':
main()