/
sparring.py
executable file
·304 lines (261 loc) · 9.74 KB
/
sparring.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python
# Copyright (c) Siemens AG, 2013
#
# This file is part of sparring. sparring is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either version 2
# of the License, or(at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import sys, os, nfqueue, dpkt, user, signal
from socket import AF_INET, AF_INET6, inet_ntoa, inet_aton, gethostbyname_ex, gethostname, socket
from time import sleep
import getopt
import socket
import logging
import transports.tcp as tcp
import transports.udp as udp
#from dpkt import ip
# import patched dpkt ip class
from utils.ip_patched import ip
#from pudb import set_trace; set_trace()
# local imports below!
MODES = ['TRANSPARENT', 'HALF', 'FULL']
def handle_term(arg1, arg2):
pass
class Sparring(object):
def __init__(self, mode, myip, port = 5001, queueno = 0, jfile = None):
self.count = 0
self.nodata_count = 0
self.applications = []
self.tcp = None
self.udp = None
log = logging
self.tcp = tcp.Tcp(mode, self.applications, myip)
self.udp = udp.Udp(mode, self.applications, myip)
self.app_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),'applications')
self.load_applications(self.app_dir, mode)
if __name__ == '__main__':
modules = "loaded modules:"
for application in self.applications:
modules = modules + ' ' + str(application.protocols())
log.info(modules)
self.setup(mode, myip, port, queueno)
def cb(self, payload):
self.count += 1
data = payload.get_data()
# spawn IP packet
try:
pkt = ip.IP(data)
except Exception, e:
log.warning("unsupported Layer 3 protocol or broken IP packet dropped: %s" % e)
payload.set_verdict(nfqueue.NF_DROP)
return
if pkt.p == dpkt.ip.IP_PROTO_TCP:
ret = self.tcp.handle(pkt)
payload.set_verdict(ret[1])
self.nodata_count += ret[0]
elif pkt.p == dpkt.ip.IP_PROTO_UDP:
ret = self.udp.handle(pkt)
payload.set_verdict(ret[1])
self.nodata_count += ret[0]
elif pkt.p == dpkt.ip.IP_PROTO_ICMP:
frame = pkt.data
if frame.type == dpkt.icmp.ICMP_ECHO:
log.info("ICMP ECHO %s" % inet_ntoa(pkt.dst))
return
elif frame.type == dpkt.icmp.ICMP_ECHOREPLY:
log.info("ICMP REPLY from %s" % inet_ntoa(pkt.src))
return
else:
return
else:
log.warning("unsupported protocol %s recieved (ignored)" % pkt.p)
return
def print_connections(self):
# TODO noch offene Verbindungen (i.e. len(data[1]) != 0) mit FIN,ACK
# 'abschliessen'? -> NICHT im transparenten Modus
for id, conn in self.tcp.connections.items() + self.udp.connections.items():
#print " inheap lang: %d incoming: %d" % (len(conn.inheap),len(conn.incoming))
#print " last ACKed : %d max seq: %d " % (conn.inseq, conn.inseq_max)
log.info("%s:%d" % (inet_ntoa(conn.remote[0]), conn.remote[1]))
#if conn.inseq < conn.inseq_max:
# print " WARNING un-ACK-ed data in buffer"
def nfq_setup(self, queueno):
q = nfqueue.queue()
q.set_callback(self.cb)
try:
q.fast_open(queueno, AF_INET)
except RuntimeError, e:
log.error("cannot bind to nf_queue %d: %s. Already in use or not root?" % (queueno, e))
return False
q.set_queue_maxlen(5000)
# won't work as try_run() still requires CAP_? ...
#user.os.setuid(100)
try:
q.try_run()
except KeyboardInterrupt:
pass
q.unbind(AF_INET)
q.close()
return True
def print_stats(self):
if jfile:
self.print_json()
else:
self.print_text()
def print_json(self):
import json
jdata = {}
for application in self.applications:
if [method for method in dir(application) if callable(getattr(application, method))].count('get_stats') == 1:
# OR: write an encoder that will handle tuples and (ip, port) pairs
application.stats.json()
jdata[application.protocols()[0]] = application.stats.cats
try:
f = open(jfile, 'w')
json.dump({'Network_sparring': jdata}, f, indent=2)
f.close()
except Exception, e:
log.error("could not dump JSON data to jfile: %s" % e)
def print_text(self):
for application in self.applications:
if [method for method in dir(application) if callable(getattr(application, method))].count('get_stats') == 1:
print("\n STATISTICS for protocol %s" % application.protocols()[0])
print(application.stats)
else:
log.warning("no stats available for protocol %s" % application.protocols()[0])
def identify_generic(self):
sys.path.append(self.app_dir)
try:
mod = __import__('generic')
mod_instance = mod.init(mode)
self.applications.append(mod_instance)
except Exception, e:
log.error("import of generic module failed: %s" % e)
return
#print "davor:"
#for connection in tcp.connections.values() + udp.connections.values():
# try:
# print "(%s,%d)->(%s,%d) %s" % (inet_ntoa(connection.remote[0]),
# connection.remote[1], inet_ntoa(connection.local[0]),
# connection.local[1], connection.module.stats)
# except:
# print "one with NoneType"
for connection in self.tcp.connections.values() + self.udp.connections.values():
if not connection.module:
connection.module = mod_instance
connection.handle()
#print "danach:"
#for connection in tcp.connections.values() + udp.connections.values():
# print "%s %s" % (inet_ntoa(connection.remote[0]), connection.module)
def load_applications(self, app_dir, mode):
sys.path.append(app_dir)
mod_list = os.listdir(app_dir)
blacklist = ['application', 'generic', '__init__']
for module in mod_list:
try:
module_name, module_ext = os.path.splitext(module)
# better? check for existance and callable() of init()
if module_ext == '.py' and not module_name in blacklist:
mod = __import__(module_name)
self.applications.append(mod.init(mode))
del mod
except ImportError, e:
log.error("import of module %s failed: %s" % (module, e))
def setup(self, mode, ip, port, queueno = 0):
if mode == MODES[0]:
if not self.nfq_setup(queueno):
return
else:
self.create_listener(ip, port)
self.shutdown()
def create_listener(self, ip, port):
import asyncore
servers = {}
server1 = self.tcp.newserver(port, self.tcp, servers)
server2 = self.udp.newserver(port, self.udp, servers)
try:
asyncore.loop(1, True, servers)
except KeyboardInterrupt, e:
pass
except asyncore.ExitNow, e:
pass
def shutdown(self):
log.info("%d packets handled (%d without data)" % (self.count, self.nodata_count))
if log.getLogger().level <= log.DEBUG:
print "Connection table"
print_connections()
# gather generic stats about unknown connections
self.identify_generic()
self.print_stats()
def usage():
print "usage: sparring.py -a ipaddress [-n queuenum] [mode] [-p portnum]"
print "-a ipaddress: specify own IPv4 address"
print "-n queuenuno: use given nfqueue-number instead of the default (0)"
print " modes:"
print " -t run in TRANSPARENT mode (default)"
print " -h run in HALF mode"
print " -f run in FULL mode"
print "-j filename : convert results to json and write to filename"
print "-p portno : half/full mode: listen locally on port portno (default: 5000)"
print "-v verbose : emit info messages during simulation"
if __name__ == '__main__':
myip = None
modeopt = 0
port = 5000
queueno = 0
verbose = False
jfile = None
try:
opts, args = getopt.getopt(sys.argv[1:], "thfa:n:p:j:v")
for opt in opts:
if opt[0] == "-a":
myip = inet_aton(opt[1])
if opt[0] == "-f":
modeopt = 2
elif opt[0] == "-h":
modeopt = 1
elif opt[0] == "-n":
queueno = int(opt[1])
elif opt[0] == "-j":
jfile = opt[1]
elif opt[0] == "-p":
port = opt[1]
port = int(port)
elif opt[0] == "-v":
verbose = True
else:
modeopt = 0
except: #getopt.GetoptError, ValueError:
usage()
sys.exit(2)
if not myip:
usage()
sys.exit(2)
signal.signal(signal.SIGTERM, handle_term)
mode = MODES[modeopt]
log = logging
log.getLogger().setLevel(logging.INFO if verbose else logging.WARNING)
log.info(""" _
_________ ____ ___________(_)___ ____ _
/ ___/ __ \/ __ `/ ___/ ___/ / __ \/ __ `/
(__ ) /_/ / /_/ / / / / / / / / / /_/ /
/____/ .___/\__,_/_/ /_/ /_/_/ /_/\__, /
/_/ /____/
""")
log.info("mode: %s" % mode)
# TODO funktioniert nicht immer
# eigentlich eine Liste (inkl. Broadcastadresse)
#myip = inet_aton('172.16.0.7') #inet_aton(gethostbyname_ex(gethostname())[2][0])
#myip = inet_aton('192.168.1.127') #inet_aton(gethostbyname_ex(gethostname())[2][0])
log.info("own IP address: %s" % inet_ntoa(myip))
s = Sparring(mode, myip, port, queueno, jfile)