forked from muccc/iridium-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reassembler.py
executable file
·399 lines (367 loc) · 14 KB
/
reassembler.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
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
#!/usr/bin/env python
# vim: set ts=4 sw=4 tw=0 et pm=:
import sys
import fileinput
import getopt
import datetime
import re
import struct
import math
import os
verbose = False
ifile= None
ofile= None
mode= "undef"
options, remainder = getopt.getopt(sys.argv[1:], 'vi:o:m:', [
'verbose',
'input=',
'output=',
'mode=',
])
for opt, arg in options:
if opt in ('-v', '--verbose'):
verbose = True
elif opt in ('-i', '--input'):
ifile=arg
elif opt in ('-o', '--output'):
ofile=arg
elif opt in ('-m', '--mode'):
mode=arg
else:
raise Exception("unknown argument?")
basename=None
if ifile == None:
if not remainder:
basename="stdin"
ifile = "/dev/stdin"
else:
ifile = remainder[0]
if not basename:
basename=re.sub('\.[^.]*$','',ifile)
# basename=os.path.basename(re.sub('\.[^.]*$','',ifile))
if ofile == None:
ofile="%s.%s" % (basename, mode)
outfile=sys.stdout
elif ofile == "" or ofile == "=":
ofile="%s.%s" % (basename, mode)
outfile=open(ofile,"w")
else:
basename=re.sub('\.[^.]*$','',ofile)
outfile=open(ofile,"w")
if verbose:
print "ifile",ifile
print "ofile",ofile
print "basen",basename
def fixtime(n,t):
try:
(crap,ts,fnord)=n.split("-",3)
return (float(ts)+int(t)/1000)
except:
return int(t)/1000
class MyObject(object):
pass
class Reassemble(object):
def __init__(self):
raise Exception("undef")
stat_line=0
stat_filter=0
def run(self,producer):
for line in producer:
res=self.filter(line)
if res != None:
self.stat_filter+=1
zz=self.process(res)
if zz != None:
for mo in zz:
self.consume(mo)
self.end()
def filter(self,line):
self.stat_line+=1
q=MyObject()
q.typ,q.name,q.time,q.frequency,q.confidence,q.level,q.symbols,q.uldl,q.data=line.split(None,8)
q.frequency=int(q.frequency)
q.time=int(q.time)
q.level=float(q.level)
return q
def end(self):
print "Kept %d/%d (%3.1f%%) lines"%(self.stat_filter,self.stat_line,100.0*self.stat_filter/self.stat_line)
class ReassembleIDA(Reassemble):
def __init__(self):
pass
def filter(self,line):
try:
q=super(ReassembleIDA,self).filter(line)
except:
q=MyObject()
q.typ="NOTIDA:"
if q.typ=="IDA:":
qqq=re.compile('.* CRC:OK')
if not qqq.match(q.data):
return
# 0010 0 ctr=000 000 len=02 0:0000 [06.3a] 7456/0000 CRC:OK 0000
# 0000 0 ctr=000 000 len=00 0:0000 [8a.ed.09.b2.e0.a9.e7.0b.06.78.c9.49.0d.9b.60.6f.c0.07.fc.00.00.00.00] --- 0000
p=re.compile('.* cont=(\d) (\d) ctr=(\d+) \d+ len=(\d+) 0:0000 \[([0-9a-f.]*)\]\s+..../.... CRC:OK')
m=p.match(q.data)
if(not m):
print >> sys.stderr, "Couldn't parse IDA: ",q.data
else:
q.ul= (q.uldl=='UL')
q.f1= m.group(1)
q.f2= int(m.group(2))
q.ctr= int(m.group(3),2)
q.length= int(m.group(4))
q.data= m.group(5)
q.cont=(q.f1=='1')
# print "%s %s ctr:%02d %s"%(q.time,q.frequency,q.ctr,q.data)
return q
buf=[]
stat_broken=0
stat_ok=0
stat_fragments=0
stat_dupes=0
otime=0
odata=None
ofreq=0
def process(self,m):
# rudimentary De-Dupe
if (self.otime-1)<=m.time<=(self.otime+1) and self.odata==m.data and (self.ofreq-200)<m.frequency<(self.ofreq+200):
self.stat_dupes+=1
if verbose:
print "dupe: ",m.time,"(",m.cont,m.ctr,")",m.data
return
self.otime=m.time
self.odata=m.data
self.ofreq=m.frequency
ok=False
for (idx,(freq,time,ctr,dat,cont,ul)) in enumerate(self.buf[:]):
if (freq-260)<m.frequency<(freq+260) and time[-1]<=m.time<=(time[-1]+280) and (ctr+1)%8==m.ctr and ul==m.ul:
del self.buf[idx]
dat=dat+"."+m.data
time.append(m.time)
if m.cont:
self.buf.append([m.frequency,time,m.ctr,dat,m.cont,m.ul])
else:
self.stat_ok+=1
if verbose:
print ">assembled: [%s] %s"%(",".join(["%s"%x for x in time+[m.time]]),dat)
data="".join([chr(int(x,16)) for x in dat.split(".")])
return [[data,m.time,ul,m.level]]
self.stat_fragments+=1
ok=True
break
if ok:
pass
elif m.ctr==0 and not m.cont:
if verbose:
print ">single: [%s] %s"%(m.time,m.data)
data="".join([chr(int(x,16)) for x in m.data.split(".")])
return [[data,m.time,m.ul,m.level]]
elif m.ctr==0 and m.cont: # New long packet
self.stat_fragments+=1
if verbose:
print "initial: ",m.time,"(",m.cont,m.ctr,")",m.data
self.buf.append([m.frequency,[m.time],m.ctr,m.data,m.cont,m.ul])
elif m.ctr>0:
self.stat_broken+=1
self.stat_fragments+=1
if verbose:
print "orphan: ",m.time,"(",m.cont,m.ctr,")",m.data
pass
else:
print "unknown: ",m.time,m.cont,m.ctr,m.data
# expire packets
for (idx,(freq,time,ctr,dat,cont,ul)) in enumerate(self.buf[:]):
if time[-1]+1000<=m.time:
self.stat_broken+=1
del self.buf[idx]
if verbose:
print "timeout:",time,"(",cont,ctr,")",dat
data="".join([chr(int(x,16)) for x in dat.split(".")])
#could be put into assembled if long enough to be interesting?
break
def end(self):
super(ReassembleIDA,self).end()
print "%d valid packets assembled from %d fragments (1:%1.2f)."%(self.stat_ok,self.stat_fragments,((float)(self.stat_fragments)/self.stat_ok))
print "%d/%d (%3.1f%%) broken fragments."%(self.stat_broken,self.stat_fragments,(100.0*self.stat_broken/self.stat_fragments))
print "%d dupes removed."%(self.stat_dupes)
def consume(self,q):
(data,time,ul,level)=q
if ul:
ul="UL"
else:
ul="DL"
str=""
for c in data:
if( ord(c)>=32 and ord(c)<127):
str+=c
else:
str+="."
print >>outfile, "%09d %s %s | %s"%(time,ul," ".join("%02x"%ord(x) for x in data),str)
class ReassembleIDALAP(ReassembleIDA):
first=True
outfile=None
def consume(self,q):
if self.first:
pcap_hdr=struct.pack("<LHHlLLL",0xa1b2c3d4,0x2,0x4,0x0,0,0xffff,1)
outfile.write(pcap_hdr)
self.first=False
# Filter non-GSM packets (see IDA-GSM.txt)
(data,time,ul,level)=q
if ord(data[0])&0xf==6 or ord(data[0])&0xf==8 or (ord(data[0])>>8)==7:
return
if len(data)==1:
return
lapdm=data
try:
olvl=10*math.log(level,10)
except:
olvl=0
if olvl>127:
olvl=127
if olvl<-126:
olvl=-126
if ul:
gsm=struct.pack("!BBBBHbBLBBBB",2,4,1*0+2,0,0x4000,olvl,0,0,7,0,0,0)+lapdm
else:
gsm=struct.pack("!BBBBHbBLBBBB",2,4,1*0+2,0,0x0000,olvl,0,0,7,0,0,0)+lapdm
udp=struct.pack("!HHHH",45988,4729,8+len(gsm),0xffff)+gsm
if ul:
ip=struct.pack("!BBHHBBBBHBBBBBBBB",(0x4<<4)+5,0,len(udp)+20,0xdaae,0x40,0x0,0x40,17,0xffff,10,0,0,1,127,0,0,1)+udp
else:
ip=struct.pack("!BBHHBBBBHBBBBBBBB",(0x4<<4)+5,0,len(udp)+20,0xdaae,0x40,0x0,0x40,17,0xffff,127,0,0,1,10,0,0,1)+udp
if ul:
eth=struct.pack("!BBBBBBBBBBBBH",0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x10,0x22,0x33,0x44,0x55,0x66,0x800)+ip
else:
eth=struct.pack("!BBBBBBBBBBBBH",0x10,0x22,0x33,0x44,0x55,0x66,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x800)+ip
pcap=struct.pack("<IIII",time/1000,time%1000,len(eth),len(eth))+eth
outfile.write(pcap)
if verbose:
if ul:
ul="UL"
else:
ul="DL"
print "%09d %.3f %s %s"%(time,level,ul,".".join("%02x"%ord(x) for x in data))
class ReassembleIRA(Reassemble):
def __init__(self):
pass
def filter(self,line):
q=super(ReassembleIRA,self).filter(line)
if q.typ=="IRA:":
p=re.compile('.*sat:(\d+) beam:(\d+) pos=\((.[0-9.]+)/(.[0-9.]+)\) alt=([-0-9]+) .* bc_sb:\d+ (.*)')
m=p.match(q.data)
if(not m):
print >> sys.stderr, "Couldn't parse IRA: ",q.data
else:
q.sat= int(m.group(1))
q.beam= int(m.group(2))
q.posx= m.group(3)
q.posy= m.group(4)
q.alt= int(m.group(5))
p=re.compile('PAGE\(tmsi:([0-9a-f]+) msc_id:([0-9]+)\)')
q.pages=p.findall(m.group(6))
return q
def process(self,q):
for x in q.pages:
return ["%02d %02d %s %s %03d : %s %s"%(q.sat,q.beam,q.posx,q.posy,q.alt,x[0],x[1])]
def consume(self,q):
print >> outfile, q
class ReassembleMSG(Reassemble):
def __init__(self):
pass
def filter(self,line):
q=super(ReassembleMSG,self).filter(line)
if q.typ == "MSG:":
#ric:0098049 fmt:05 seq:43 1010010000 1/1 oNEZCOuxvM3PuiQHujzQYd5n0Q8ra0wfMG2WnnhoxAnunT9xzIBSkXyvNP[3] +11111
p=re.compile('.* ric:(\d+) fmt:(\d+) seq:(\d+) [01]+ (\d)/(\d) csum:([0-9a-f][0-9a-f]) msg:([0-9a-f]+)\.([01]*) ')
m=p.match(q.data)
if(not m):
print >> sys.stderr, "Couldn't parse MSG: ",q.data
else:
q.msg_ric= int(m.group(1))
q.fmt= int(m.group(2))
q.msg_seq= int(m.group(3))
q.msg_ctr= int(m.group(4))
q.msg_ctr_max= int(m.group(5))
q.msg_checksum=int(m.group(6),16)
q.msg_hex= m.group(7)
q.msg_brest= m.group(8)
q.time= fixtime(q.name,q.time)
q.msg_msgdata = ''.join(["{0:08b}".format(int(q.msg_hex[i:i+2], 16)) for i in range(0, len(q.msg_hex), 2)])
q.msg_msgdata+=q.msg_brest
# convert to 7bit thingies
m=re.compile('(\d{7})').findall(q.msg_msgdata)
q.msg_ascii=""
q.msg=[]
for (group) in m:
character = int(group, 2)
q.msg.append(character)
if(character<32 or character==127):
q.msg_ascii+="[%d]"%character
else:
q.msg_ascii+=chr(character)
if len(q.msg_msgdata)%7:
q.msg_rest=q.msg_msgdata[-(len(q.msg_msgdata)%7):]
else:
q.msg_rest=""
return q
buf={}
ricseq={}
wrapmargin=10
def process(self,m):
# msg_seq wraps around after 61, detect it, and fix it.
if m.msg_ric in self.ricseq:
if (m.msg_seq + self.wrapmargin) < self.ricseq[m.msg_ric][1]: # seq wrapped around
self.ricseq[m.msg_ric][0]+=62
if (m.msg_seq + self.wrapmargin - 62) > self.ricseq[m.msg_ric][1]: # "wrapped back" (out-of-order old message)
self.ricseq[m.msg_ric][0]-=62
else:
self.ricseq[m.msg_ric]=[0,0]
self.ricseq[m.msg_ric][1]=m.msg_seq
id="%07d %04d"%(m.msg_ric,(m.msg_seq+self.ricseq[m.msg_ric][0]))
ts=m.time
if id in self.buf:
if self.buf[id].msg_checksum != m.msg_checksum:
print "Whoa! Checksum changed? Message %s (1: @%d checksum %d/2: @%d checksum %d)"%(id,self.buf[id].time,self.buf[id].msg_checksum,m.time,m.msg_checksum)
# "Wrap around" to not miss the changed packet.
self.ricseq[m.msg_ric][0]+=62
id="%07d %04d"%(m.msg_ric,(m.msg_seq+self.ricseq[m.msg_ric][0]))
m.msgs=['[MISSING]']*3
self.buf[id]=m
else:
m.msgs=['[MISSING]']*3
self.buf[id]=m
self.buf[id].msgs[m.msg_ctr]=m.msg_ascii
def messagechecksum(self,msg):
csum=0
for x in msg:
csum=(csum+ord(x))%128
return (~csum)%128
def consume(self,q):
print "consume()"
pass
def end(self): # XXX should be rewritten to consume
for b in sorted(self.buf, key=lambda x: self.buf[x].time):
msg="".join(self.buf[b].msgs[:1+self.buf[b].msg_ctr_max])
msg=re.sub("(\[3\])+$","",msg) # XXX: should be done differently
cmsg=re.sub("\[10\]","\n",msg) # XXX: should be done differently
csum=self.messagechecksum(cmsg)
str="Message %s @%s (len:%d)"%(b,datetime.datetime.fromtimestamp(self.buf[b].time).strftime("%Y-%m-%dT%H:%M:%S"),self.buf[b].msg_ctr_max)
str+= " %3d"%self.buf[b].msg_checksum
str+= (" fail"," OK ")[self.buf[b].msg_checksum == csum]
str+= ": %s"%(msg)
print >> outfile, str
zx=None
if False:
pass
if mode == "ida":
zx=ReassembleIDA()
elif mode == "lap":
if outfile == sys.stdout: # Force file, since it's binary
ofile="%s.%s" % (basename, "pcap")
outfile=open(ofile,"w")
zx=ReassembleIDALAP()
elif mode == "page":
zx=ReassembleIRA()
elif mode == "msg":
zx=ReassembleMSG()
zx.run(fileinput.input(ifile))