-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToT.py
152 lines (130 loc) · 4.76 KB
/
ToT.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
import os
import getpass
import signal
import subprocess
import argparse
import time
from sys import stdout
parser = argparse.ArgumentParser(description='My example explanation')
parser.add_argument(
'-s',
'--port_source',
default=8765,
help='provide an integer (default: 8765)'
)
parser.add_argument(
'-ip',
'--ip_destination',
default="127.0.0.1",
help='provide an ip (default: 127.0.0.1)'
)
parser.add_argument(
'-d',
'--port_destination',
default=22,
help='provide an integer (default: 22)'
)
parser.add_argument(
'-f',
'--force',
default="no",
help='provide yes or no (default: no)'
)
parser.add_argument(
'--clean',
default="no",
help='provide brutal or no (default: no)'
)
parser.add_argument(
'--stats',
default="no",
help='provide yes or no (default: no)'
)
params = parser.parse_args()
if params.clean =="brutal":
eradicate1 = subprocess.Popen("pkill -f netcat -9", shell=True, preexec_fn=os.setsid)
eradicate1.wait()
eradicate2 = subprocess.Popen("pkill -f xxd -9", shell=True, preexec_fn=os.setsid)
eradicate2.wait()
eradicate3 = subprocess.Popen("pkill -f exp -9", shell=True, preexec_fn=os.setsid)
eradicate3.wait()
print "Eradication done. Exiting..."
exit()
checks = subprocess.Popen("ps aux | egrep 'xxd|netcat|exp' | grep -v grep > /dev/null", shell=True, preexec_fn=os.setsid)
checks.wait()
if checks.returncode==0 and params.force =="no":
print "Environment may not be clean: xxd,netcat or expect scripts are running"
print "To try a hard cleaning, use --clean brutal argument. Will kill xxd,netcat and expect running under user ## "+getpass.getuser()+" ##"
print "To bypass checks, add --force yes argument"
exit()
if params.stats =="yes":
checks2 = subprocess.Popen("echo test | pv 2> /dev/null 1> /dev/null", shell=True, stdin=None, stdout=None, stderr=None, preexec_fn=os.setsid)
checks2.wait()
if checks2.returncode!=0:
print "You seem to miss pv command. Install it if you need statistics. Else don't use statistics."
exit()
proc1 = subprocess.Popen("while :; do stdbuf -o0 xxd -p -c 1 < fromN > fromN-HEX; done", shell=True, preexec_fn=os.setsid)
if params.stats == "yes":
proc2 = subprocess.Popen("cat fromN-HEX | pv -f | ./remote-write.exp > /dev/null", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid, universal_newlines=True)
proc3 = subprocess.Popen("./remote-read.exp | pv -f > fromNC-HEX", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid, universal_newlines=True)
else:
proc2 = subprocess.Popen("cat fromN-HEX | ./remote-write.exp > /dev/null", shell=True, preexec_fn=os.setsid)
proc3 = subprocess.Popen("./remote-read.exp > fromNC-HEX", shell=True, preexec_fn=os.setsid)
proc4 = subprocess.Popen("while :; do stdbuf -o0 xxd -p -c 1 -r < fromNC-HEX > toN; done", shell=True, preexec_fn=os.setsid)
proc5 = subprocess.Popen("cat toN | netcat -l -p "+str(params.port_source)+" > fromN", shell=True, preexec_fn=os.setsid)
time.sleep(2)
proc6 = subprocess.Popen("./connect.exp "+str(params.ip_destination)+" "+str(params.port_destination)+" > /dev/null", shell=True, preexec_fn=os.setsid)
print("Press CTRL-C to exit...")
if params.stats == "yes":
print("Traffic in tunnel (in HEX + Overhead)")
while True:
try:
if params.stats == "yes":
stdout.write("\r\x1b[K"+"Output bytes = "+proc2.stderr.readline().split("[")[0]+"["+proc2.stderr.readline().split("[")[1]+"; Input bytes = "+proc3.stderr.readline().split("[")[0]+"["+proc3.stderr.readline().split("[")[1])
stdout.flush()
time.sleep(1)
if proc1.poll() is not None:
print "\r\nTunnel closed. Exiting..."
break
if proc2.poll() is not None:
print "\r\nTunnel closed. Exiting..."
break
if proc3.poll() is not None:
print "\r\nTunnel closed. Exiting..."
break
if proc4.poll() is not None:
print "\r\nTunnel closed. Exiting..."
break
if proc5.poll() is not None:
print "\r\nTunnel closed. Exiting..."
break
if proc6.poll() is not None:
print "\r\nTunnel closed. Exiting..."
break
except KeyboardInterrupt:
print "\r\nUser interrupt. Exiting..."
break
try:
os.killpg(proc1.pid, signal.SIGTERM)
except:
pass
try:
os.killpg(proc2.pid, signal.SIGTERM)
except:
pass
try:
os.killpg(proc3.pid, signal.SIGTERM)
except:
pass
try:
os.killpg(proc4.pid, signal.SIGTERM)
except:
pass
try:
os.killpg(proc5.pid, signal.SIGTERM)
except:
pass
try:
os.killpg(proc6.pid, signal.SIGTERM)
except:
pass