forked from cmand/scamper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc_attach.py
executable file
·123 lines (112 loc) · 3.06 KB
/
sc_attach.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
#!/usr/bin/env python
#
# Program: $Id: sc_attach.py 1537 2015-02-06 21:53:51Z rbeverly $
# Author: Robert Beverly <[email protected]>
# Description: Base class to interact with Scamper over its control socket
#
import sys
from socket import *
from binascii import a2b_uu
from time import sleep
import threading
import select
class Scamper:
def __init__(self, outfile, host='localhost', port=31337, verbose=False):
self.server = (host, port)
self.verbose = verbose
try:
self.fd_out = open(outfile, 'wb')
except Exception, e:
print "Couldn't open warts file:", e
sys.exit(-1)
def __del__(self):
print "Done."
if self.sfd:
self.sfd.close()
if self.fd_out:
self.fd_out.close()
#self.listener.join()
@staticmethod
def readline(sock):
ret = ''
while True:
c = sock.recv(1)
if c == '\n' or c == '':
break
else:
ret +=c
return ret
@staticmethod
def hexdump(buf):
for i in range(len(buf)):
print "%02X" % ord(buf[i]),
print
@staticmethod
def rob_uu_decode(text):
res = ''
lines = text.strip().split('\n')
for line in lines:
decoded_line = a2b_uu(line)
res += decoded_line
return res
def connect(self):
print "Connecting:", self.server
try:
self.sfd = socket(AF_INET, SOCK_STREAM)
self.sfd.connect(self.server)
except Exception, e:
print "Error connecting:", e
sys.exit(-1)
self.listener = threading.Thread(target=self.fetch_scamper_result)
self.listener.start()
# give thread time to startup before doing work
sleep(1)
def attach(self):
self.sfd.send('ATTACH\n')
def fetch_scamper_result(self):
print "Listener."
self.timeouts = 0
while True:
if self.timeouts > 10:
break
fdready = select.select([self.sfd], [], [], 1)
if len(fdready[0]) == 0:
print "timeout."
self.timeouts+=1
continue
line = ''
while True:
line = Scamper.readline(self.sfd)
if line.find('ERR') != -1:
print "** error response:", line
raise Exception('shit, talk to rob')
elif line.find('MORE') != -1:
continue
elif line.find('DATA') != -1:
break
cmds = line.strip().split()
datalen = int(cmds[1])
data = self.sfd.recv(datalen)
decoded_data = Scamper.rob_uu_decode(data)
if self.verbose:
Scamper.hexdump(decoded_data)
self.fd_out.write(decoded_data)
return decoded_data
def execute(self, cmd):
self.sfd.send(cmd + '\n')
def executeFile(self, fname):
for line in open(fname):
dst = line.strip()
#opts = '-P icmp-paris -w 10'
opts = ''
print "Tracing to:", dst
self.execute('trace ' + dst + ' ' + opts)
if __name__ == "__main__":
scamper = Scamper('rob.warts', '10.10.10.1', 31337, verbose=True)
scamper.connect()
scamper.attach()
scamper.execute('trace 10.10.10.221')
#scamper.execute('trace 172.20.186.26')
#scamper.executeFile('targets.dat')
del scamper
sys.exit(0)