forked from tobiw/voip-hpc
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathextras.py
More file actions
560 lines (467 loc) · 17.4 KB
/
extras.py
File metadata and controls
560 lines (467 loc) · 17.4 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import datetime
import logging
import os
import pprint
import re
import sqlite3
import struct
import time
logger = logging.getLogger('sip')
logger.setLevel(logging.DEBUG)
DEFAULT_SDP = """
v=0
o=- 1304279835 1 IN {addrtype} {unicast_address}
s=SIP Session
c=IN {addrtype} {unicast_address}
t=0 0
[audio_port]
m=audio {audio_port} RTP/AVP 111 0 8 9 101 120
a=sendrecv
a=rtpmap:111 Speex/16000/1
a=fmtp:111 sr=16000,mode=any
a=rtpmap:0 PCMU/8000/1
a=rtpmap:8 PCMA/8000/1
a=rtpmap:9 G722/8000/1
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16,32,36
a=rtpmap:120 NSE/8000
a=fmtp:120 192-193
[/audio_port]
[video_port]
m=video {video_port} RTP/AVP 34 96 97
c=IN {addrtype} {unicast_address}
a=rtpmap:34 H263/90000
a=fmtp:34 QCIF=2
a=rtpmap:96 H263-1998/90000
a=fmtp:96 QCIF=2
a=rtpmap:97 H263-N800/90000
[/video_port]
"""
def int2bytes(x):
"""
Convert integer to bytes
"""
return x.to_bytes((x.bit_length() // 8) + 1, byteorder='little')
class ErrorWithResponse(Exception):
def __init__(self, msg, response_code, status_message):
self._msg = msg
self._response_code = response_code
self._status_message = status_message
def create_response(self):
return self._msg.create_response(self._response_code, self._status_message)
class SipConfig(object):
"""
This class helps to access the config values.
"""
def __init__(self, config):
"""
:param config: The config dict from dionaea
:type config: dict
"""
self.root_path = os.getcwd()
self.users = config.get("users", "sipaccounts.sqlite")
self._conn = sqlite3.connect(self.users)
self._cur = self._conn.cursor()
if not self._table_exists("users"):
logger.info("creating table users")
self._cur.execute("""CREATE TABLE IF NOT EXISTS users (username STRING, password STRING, personality STRING,
pickup_delay_min INTEGER, pickup_delay_max INTEGER, action STRING, sdp STRING)""")
# example without password
self._cur.execute("""INSERT INTO users (username, password, personality, pickup_delay_min, pickup_delay_max,
action, sdp) VALUES ('^[0-9]{1,12}$', '', 'default', 5, 10, 'default', 'default')""")
# example with password
self._cur.execute("""INSERT INTO users (username, password, personality, pickup_delay_min, pickup_delay_max,
action, sdp) VALUES ('^pw[0-9]{1,12}$', 'password', 'default', 5, 10, 'default',
'default')""")
if not self._table_exists("sdp"):
self._cur.execute("CREATE TABLE IF NOT EXISTS sdp (name STRING, sdp STRING)")
self._cur.execute("INSERT INTO sdp (name, sdp) VALUES ('default', ?)", (DEFAULT_SDP,))
# set default values
self.personalities = {
"default": {
"domain": "localhost",
"name": "",
"personality": "generic",
"serve": [],
"default_sdp": "default",
"handle": ["REGISTER", "OPTIONS", "INVITE", "CANCEL", "BYE", "ACK"]
}
}
for pers_name, personality in config.get("personalities", {}).items():
if not pers_name in self.personalities:
self.personalities[pers_name] = {}
for n in ["domain", "name", "personality", "serve", "default_sdp", "handle"]:
v = personality.get(n, self.personalities["default"][n])
if type(v) != type(self.personalities["default"][n]):
v = self.personalities["default"][n]
# convert values
if n == "handle":
# convert all values to uppercase
v = [t.upper() for t in v]
self.personalities[pers_name][n] = v
self.actions = config.get("actions", {})
# ToDo: read values from config
# set default values
self.timers = {
"idle": {
"timeout": 30
}
}
self._rtp = config.get("rtp", {})
rtp_enable = self._rtp.get("enable", "no")
if rtp_enable.lower() == "yes":
self._rtp["enable"] = True
else:
self._rtp["enable"] = False
self.actions = config.get("actions", {})
def _table_exists(self, name):
"""
Check if table exists
"""
ret = self._cur.execute("SELECT name FROM sqlite_master WHERE type='table' and name=?", (name,))
if not ret.fetchone():
return False
return True
def get_action(self, name):
# ToDo:
#return (func, params)
pass
def get_handlers_by_personality(self, personality):
if not personality in self.personalities:
personality = "default"
return self.personalities[personality]["handle"]
def get_timer(self, name):
if not name in self.timers:
return False
timer = self.timers[name]
return Timer(
timeout = timer["timeout"]
)
def get_user_by_username(self, personality, username):
conn = sqlite3.connect(self.users)
def regexp(expr, value):
if type(expr) != str:
expr = str(expr)
regex = re.compile(expr)
return regex.match(value) is not None
sqlite3.enable_callback_tracebacks(True)
conn.create_function("regexp", 2, regexp)
if not username:
username = b""
username = username.decode("utf-8")
cur = conn.cursor()
cur.execute("SELECT username, password, pickup_delay_min, pickup_delay_max, action, sdp FROM users WHERE personality = ? AND ? REGEXP username", (personality, username))
row = cur.fetchone()
if not row:
return None
password = row[1]
if type(password) == int:
password = str(password)
sdp = row[5]
if sdp == '' or not sdp:
sdp = self.personalities[personality].default_sdp
return User(
username=username,
username_regex=row[0],
password=password,
pickup_delay_min=row[2],
pickup_delay_max=row[3],
action=row[4],
sdp=row[5]
)
def get_personality_by_address(self, address):
for pers_name, personality in self.personalities.items():
for serve in personality["serve"]:
if serve == address:
return pers_name
return "default"
def get_rtp(self, msg_stack=[]):
pcap_conf = self._rtp.get("pcap", {})
return RTP(
path=pcap_conf.get("path", "var/dionaea/rtp/{personality}/%Y-%m-%d/"),
filename=pcap_conf.get("filename", "%H:%M:%S_{remote_host}_{remote_port}_in.pcap"),
enable=self._rtp.get("enable", False),
mode=self._rtp.get("mode", ["pcap"])
)
def get_pcap(self):
pcap_conf = self._rtp.get("pcap", {})
return PCAP(
path=pcap_conf.get("path", "var/dionaea/rtp/{personality}/%Y-%m-%d/"),
filename=pcap_conf.get("filename", "%H:%M:%S_{remote_host}_{remote_port}_in.pcap"),
)
def get_sdp_by_name(self, name, media_ports, **params):
"""
Fetch the SDP content from the database and add missing values.
"""
logger.debug("Loading sdp with: params = {}, media_ports {}".format(pprint.pformat(params), pprint.pformat(media_ports)))
ret = self._cur.execute("SELECT sdp FROM sdp WHERE name='?'")
data = ret.fetchone()
if not data:
# try to fetch the default sdp from the db
ret = self._cur.execute("SELECT sdp FROM sdp WHERE name='default'")
data = ret.fetchone()
if not data:
data = (DEFAULT_SDP,)
sdp = data[0]
for n, v in media_ports.items():
if not v:
sdp = re.sub("\[" + n + "\].*\[\/" + n + "\]", "", sdp, 0, re.DOTALL)
else:
params[n] = v
sdp = sdp.format(**params)
return bytes(sdp, "utf-8")
def get_sdp_media_port_names(self, name):
"""
Find all media ports.
"""
ret = self._cur.execute("SELECT sdp FROM sdp WHERE name='?'")
data = ret.fetchone()
if not data:
# try to fetch the default sdp from the db
ret = self._cur.execute("SELECT sdp FROM sdp WHERE name='default'")
data = ret.fetchone()
if not data:
data = (DEFAULT_SDP,)
media_ports = re.findall("{(audio_port[0-9]*|video_port[0-9]*)}", data[0])
return media_ports
def is_handled_by_personality(self, handler_name, personality = "default"):
"""
Check if dionaea handles the given SIP-Method
"""
if personality in self.personalities:
personality = "default"
if handler_name.upper() in self.personalities[personality]["handle"]:
return True
return False
class PCAP(object):
def __init__(self, path, filename):
self.path = path
self.filename = filename
self._fp = None
def __del__(self):
if self._fp:
self._fp.close()
def _carry_arround_add(self, a, b):
c = a + b
return (c & 0xffff) + (c >> 16)
def _ip_checksum(self, data):
s = 0
for i in range(0, len(data), 2):
word = data[i] + (data[i + 1] << 8)
s = self._carry_arround_add(s, word)
return s & 0xffff
def close(self):
if self._fp:
self._fp.close()
self._fp = None
def open(self, msg_stack, **params):
path = self.path.format(**params)
today = datetime.datetime.now()
path = today.strftime(path)
#'%H:%M:%S_{remote_host}_{remote_port}_in.rtp'
filename = today.strftime(self.filename)
filename = filename.format(**params)
# ToDo: error check
try:
if not os.path.exists(path):
os.makedirs(path)
except:
logger.info("Can't create RTP-Dump dir: {}".format(path))
try:
self._fp = open(os.path.join(path, filename), "wb")
except:
logger.warning("Can't create RTP-Dump file: {}".format(os.path.join(path, filename)))
if not self._fp:
return False
# write pcap global header
self._fp.write(b"\xd4\xc3\xb2\xa1")
# version 2.4
self._fp.write(b"\x02\x00\x04\x00")
# GMT to local correction
self._fp.write(b"\x00\x00\x00\x00")
# accuracy of timestamps
self._fp.write(b"\x00\x00\x00\x00")
# max length of captured packets, in octets
self._fp.write(b"\xff\xff\x00\x00")
# data link type (1 = Ethernet) http://www.tcpdump.org/linktypes.html
self._fp.write(b"\x01\x00\x00\x00")
for msg in msg_stack:
t = msg[1].time
ts = int(t)
tm = int((t - ts) * 1000000)
src_port = 5060
dst_port = 5060
if msg[0] == "in":
src_ether = b"\x00\x00\x00\x00\x00\x02"
src_host = b"\x0A\x00\x00\x02" # 10.0.0.2
dst_ether = b"\x00\x00\x00\x00\x00\x01"
dst_host = b"\x0A\x00\x00\x01" # 10.0.0.1
else:
src_ether = b"\x00\x00\x00\x00\x00\x01"
src_host = b"\x0A\x00\x00\x01" # 10.0.0.1
dst_ether = b"\x00\x00\x00\x00\x00\x02"
dst_host = b"\x0A\x00\x00\x02" # 10.0.0.2
self.write(ts=ts, tm=tm, src_ether=src_ether, src_host=src_host, src_port=src_port, dst_ether=dst_ether,
dst_host=dst_host, dst_port=dst_port, data=msg[1].dumps())
def write(self, ts=None, tm=None, src_ether=b"\x00\x00\x00\x00\x00\x02", src_host=b"\x0A\x00\x00\x02",
src_port=5060, dst_ether=b"\x00\x00\x00\x00\x00\x01", dst_host=b"\x0A\x00\x00\x01", dst_port=5060,
data=b""):
if not self._fp:
return
if not ts or not tm:
t = time.time()
ts = int(t)
tm = int((t - ts) * 1000000)
src_ether = b"\x00\x00\x00\x00\x00\x02"
src_host = b"\x0A\x00\x00\x02" # 10.0.0.2
dst_ether = b"\x00\x00\x00\x00\x00\x01"
dst_host = b"\x0A\x00\x00\x01" # 10.0.0.1
# udp header
udp = struct.pack(">H", src_port) # port src
udp += struct.pack(">H", dst_port) # port dst
udp += struct.pack(">H", len(data) + 8) # length
udp += struct.pack(">H", 0) # checksum
udp += data
# IPv4 header
ip = b"\x45" # version + header length 20bytes
ip += b"\x00"
ip += struct.pack(">H", len(udp) + 20) # pkt length
ip += b"\x00\x00" # identification
ip += b"\x40\x00" # flags(do not fragment) + fragment offset(0)
ip += b"\x40\x11" # ttl(64) + protocol(udp)
ip += b"\x00\x00" # header checksum
ip += dst_host # b"\x0A\x00\x00\x01" # ip src
ip += src_host # b"\x0A\x00\x00\x02" # ip dst
# add checksum to ip header
ip = ip[:10] + struct.pack("<H", self._ip_checksum(ip)) + ip[12:]
# ethernet header
ether = src_ether # MAC src
ether += dst_ether # MAC dst
ether += b"\x08\x00" # pkt type IPv4
pkt = ether + ip + udp
# pcap packet header
pcap = struct.pack("i", ts) # time seconds
pcap += struct.pack("i", tm) # microseconds
pcap += struct.pack("i", len(pkt)) # length captured
pcap += struct.pack("i", len(pkt)) # real length
pcap += pkt
self._fp.write(pcap)
class Timer(object):
def __init__(self, **kwargs):
self.timeout = kwargs.get("timeout", 30)
class User(object):
def __init__(self, **kwargs):
self.realm = kwargs.get("realm", "test")
self.username = kwargs.get("username", "")
self.username_regex = kwargs.get("username_regex", "")
self.password = kwargs.get("password", "")
self.pickup_delay_min = kwargs.get("pickup_delay_min", 5)
self.pickup_delay_max = kwargs.get("pickup_delay_max", 10)
self.action = kwargs.get("action", "default")
self.sdp = kwargs.get("sdp", "default")
def msg_to_icd(msg,d=None):
def via_to_dict(v, d=None):
if d is None:
d = {}
for i in ['protocol',
'address',
'port',
'_params'
]:
d[i] = v.__dict__[i]
return d
def uri_to_dict(u, d=None):
if d is None:
d = {}
for i in ['scheme',
'user',
'password',
'host',
'port',
'params',
'headers']:
d[i] = u.__dict__[i]
return d
def addr_to_dict(a, d=None):
if d is None:
d = {}
for k, v in {'display_name': None,
'uri': uri_to_dict,
#'must_quote':None,
'params': None
}.items():
if v is None:
d[k] = a.__dict__[k]
else:
d[k] = v(a.__dict__[k])
return d
def connectiondata_to_dict(c,d=None):
if d is None:
d = {}
for i in ['nettype',
'addrtype',
'connection_address',
'ttl',
'number_of_addresses']:
d[i] = c.__dict__[i]
return d
def origin_to_dict(o,d=None):
if d is None:
d = {}
for i in ['username',
'sess_id',
'sess_version',
'nettype',
'addrtype',
'unicast_address']:
d[i] = o.__dict__[i]
return d
def media_to_dict(m, d=None):
if d is None:
d = {}
for i in ['media',
'port',
'number_of_ports',
'proto',
'fmt',
# 'attributes'
]:
d[i] = m.__dict__[i]
return d
def sdp_to_dict(b, d=None):
if d is None:
d = {}
if b is None:
return None
d['c'] = connectiondata_to_dict(b[b'c'])
d['o'] = origin_to_dict(b[b'o'])
d['m'] = [media_to_dict(i) for i in b[b'm']]
return d
def allow_to_list(a):
if a is None:
return []
allow = []
for value in a:
for val in value._value.decode('ascii').split(','):
allow.append(val.strip())
return allow
if d is None:
d = {}
d.set('method', msg.method)
d.set('call_id', msg.headers.get('call-id').value)
d.set('addr', addr_to_dict(msg.uri))
d.set('via', [via_to_dict(i._value) for i in msg.headers.get('via')])
d.set('to', addr_to_dict(msg.headers.get('to')._value))
d.set('contact', addr_to_dict(msg.headers.get('to')._value))
d.set('from', [addr_to_dict(f._value) for f in msg.headers.get('from')])
d.set('sdp', sdp_to_dict(msg.sdp))
if msg.headers.get('allow') is not None:
d.set('allow', allow_to_list(msg.headers.get('allow')))
else:
d.set('allow', [])
if msg.headers.get('user-agent') is not None:
d.set('user_agent', msg.headers.get('user-agent')._value)
else:
d.set('user_agent', None)
print(d)
return d