-
Notifications
You must be signed in to change notification settings - Fork 94
/
sip_packet.py
executable file
·163 lines (145 loc) · 5.76 KB
/
sip_packet.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
"""
#####################################################################################################
################################ Importing Packages ################################
#####################################################################################################
"""
import random, string, os, socket, re
from time import sleep
from scapy.all import *
"""
#####################################################################################################
################################ SIP Packet Creator Code ################################
#####################################################################################################
"""
class sip_packet:
"""
[[server_ip]]
[[server_port]]
[[client_ip]]
[[client_port]]
[[from_user]]
[[to_user]]
[[user_agent]]
[[sp_user]]
[[expire_duration]]
[[call_id]]
[[branch_value]]
[[tag_value]]
"""
def __init__(self, method, server_ip, server_port,
client_ip, from_user = "", to_user = "",
user_agent = "", sp_user = "", protocol = "tcp", expire_duration=3600, wait=False):
self.method = method
self.protocol = protocol
self.server_ip = server_ip
self.server_port = server_port
self.client_ip = client_ip
self.from_user = from_user
self.to_user = to_user
self.user_agent = user_agent
self.sp_user = sp_user
self.expire_duration = expire_duration
self.wait = wait
TRYING = '100'
RINGING = '180'
OKEY = '200'
BADREQUEST = '400'
AUTHREQ = '401'
INVALIDPASS = '403'
NOTFOUND = '404'
NOTALLOWED = '405'
PROXYAUTHREQ = '407'
UNAVAILABLE = '480'
INEXISTENTTRANSACTION = '481'
SERVICEUN = '503'
DECLINED = '603'
method_location = os.path.join(os.getcwd(), "method")
client_port = random.randint(10000,65535)
@staticmethod
def get_rand_call_id():
prefix = ''.join(random.sample(string.digits + string.ascii_lowercase, 27))
return "{0}{1}".format(str(prefix), str(random.randrange(10000, 99999)))
@staticmethod
def get_rand_branch():
prefix = ''.join(random.sample(string.digits, 10))
return "z9hG4bK-{0}".format(str(prefix))
@staticmethod
def get_rand_tag():
prefix = random.randint(100000,999999)
return "{0}".format(str(prefix))
def fill_packet_data(self, text):
var_dict = {'[[server_ip]]':str(self.server_ip),
'[[server_port]]': str(self.server_port),
'[[client_ip]]': str(self.client_ip),
'[[client_port]]': str(self.client_port),
'[[from_user]]': str(self.from_user),
'[[to_user]]': str(self.to_user),
'[[user_agent]]': str(self.user_agent),
'[[sp_user]]': str(self.sp_user),
'[[expire_duration]]': str(self.expire_duration),
'[[call_id]]': str(self.get_rand_call_id()),
'[[branch_value]]': str(self.get_rand_branch()),
'[[tag_value]]': str(self.get_rand_tag())}
for key, value in list(var_dict.items()):
text = text.replace(key, value)
return text.encode('utf-8')
def generate_packet(self):
try:
f = open(os.path.join(self.method_location, "{0}.message".format(self.method)), "r")
packet_data = f.read()
packet_data = self.fill_packet_data(packet_data)
if self.protocol == "socket":
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(5)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.connect((str(self.server_ip), int(self.server_port)))
s.sendall(packet_data)
if self.wait:
buff,srcaddr = s.recvfrom(8192)
s.close()
status = self.getResponse(buff.decode('utf-8'))
return {"status": True, "response": status}
else:
s.close()
return {"status": True}
elif self.protocol == "scapy":
pkt = IP(src=self.client_ip, dst=self.server_ip) / UDP(sport=int(self.client_port), dport=int(self.server_port)) / packet_data
send(pkt, iface=conf.iface)
return {"status": True}
except Exception as e:
# print(e)
return {"status": False}
def getResponse(self, resp):
import re
nl = '\r\n\r\n'
headers_nl = '\r*\n(?![\t\x20])'
if nl in resp:
header,body = resp.split(nl,1)
else:
header = resp
body = ''
headers = re.split(headers_nl, header)
if len(headers) > 1:
response = dict()
first_line = headers[0].split(' ',2)
if len(first_line) == 3:
version,code,description = first_line
else:
print('Could not parse the first header line: {0}'.format(first_line))
return response
try:
response['code'] = int(code)
except ValueError:
return response
response['headers'] = dict()
for headerline in headers[1:]:
nl = ':'
if nl in headerline:
tmpname,tmpval = headerline.split(nl,1)
name = tmpname.lower().strip()
val = [x.strip() for x in tmpval.split(',')]
else:
name,val = headerline.lower(),None
response['headers'][name] = val
response['body'] = body
return response