-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfcdecoder.py
More file actions
executable file
·604 lines (503 loc) · 19.3 KB
/
Copy pathnfcdecoder.py
File metadata and controls
executable file
·604 lines (503 loc) · 19.3 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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import os, sys, struct
import yaml
from enum import *
from bitmask import BitMask
from cmdprocessor import *
from apduprocessor import *
ConsoleDebugOutput = False
#ConsoleDebugOutput = True
bBinaryMode = False
class NoValue(Enum):
def __repr__(self):
return '<%s.%s>' % (self.__class__.__name__, self.name)
class CouplingDevice(NoValue):
PICC = 'PICC'
PCD = 'PCD'
UNK = 'Unknown'
#CouplingDevice.PICC.value will return 'PICC'
DecoderState = Enum('DecoderState', '''
INIT
REQAWUPA
ATQA
ANTICOL1
ANTICOL2
ANTICOL3
UID
SELECT1
SELECT2
SELECT3
SAK
RATS
ATS
PROT4
DESELECT
MIFAUTH
MIFTOKENRB
MIFTOKENAB
MIFTOKENBA
MIFENCRYPTED
MIFWRITEA
MIFWRITEB
MIFWRITEBA
MIFREAD
ANY
SAME
''')
def CRC_16A(msg):
poly = 0x8408
crc = 0x6363
for byte in msg:
for _ in range(8):
if (byte ^ crc) & 1:
crc = (crc >> 1) ^ poly
else:
crc >>= 1
byte >>= 1
return [crc & 0xFF, crc >> 8]
class FrameDecoder:
def __init__(self, cmdDB, procClass, decoderState = DecoderState.INIT):
self._state = decoderState
self._frameSrc = CouplingDevice.UNK
self._log = []
self._logDetail = []
self._frameDataL = []
self._lineNum = 0
self._db = cmdDB
self._procClass = procClass
@property
def frameSrc(self):
if(self._frameSrc == CouplingDevice.PCD):
return "RDR"
elif(self._frameSrc == CouplingDevice.PICC):
return "TAG"
else:
return "UNK"
def newFrame(self, frameData, frameSrc, lineN):
self._frameData = frameData
self._lineNum = lineN
self._log.clear()
self._logDetail.clear()
if(frameSrc=="RDR"):
self._frameSrc = CouplingDevice.PCD
elif(frameSrc=="TAG"):
self._frameSrc = CouplingDevice.PICC
else:
self._frameSrc = CouplingDevice.UNK
self._frameDataL = [int(i, 16) for i in self._frameData]
if (ConsoleDebugOutput):
print("Current State ", self._state)
print("======================================== Incoming Frame ", [hex(x) for x in self._frameDataL])
print("Current input line ", self._lineNum)
for self._entry in self._db:
#print("checking entry ", self._entry)
match = False
# only look through list entries from the same source as current frame
if (self._entry['src'] != self._frameSrc.value):
continue
#print("dbg ", self._entry['cmdlen'], self._state, self._entry['state_cur'])
# need to eval if comes from YAML
if (type(self._entry['state_cur']) is str):
self._entry['state_cur'] = eval(self._entry['state_cur'])
if (type(self._entry['state_nxt']) is str):
self._entry['state_nxt'] = eval(self._entry['state_nxt'])
# if value is an empty list then pick the first entry matching our state machine
# script state cannot be DecoderState.ANY for wildcard match as there is nothing else for the search engine to latch on
if (self._entry['value'] == [] and type(self._entry['state_cur']) is list and self._state in self._entry['state_cur']):
match = True
if (self._entry['value'] == [] and type(self._entry['state_cur']) is not list and self._state == self._entry['state_cur']):
match = True
if (match):
if (ConsoleDebugOutput):
print("match entry wildcard ", self._entry['label'])
self.checkCRC()
# allow state machine to retain it's previous state for DecoderState.SAME
if (self._entry['state_nxt'] != DecoderState.SAME):
self._state = self._entry['state_nxt']
if ('log' in self._entry):
self._log.append(self._entry['log'])
if ('data' in self._entry):
for testElem in self._entry['data']:
# for negative offsets point at the data from the right, e.g. -1 points to the last byte
if (testElem['offset'] < 0):
dataOffset = testElem['offset']
else:
dataOffset = self._entry['cmdlen'] + testElem['offset']
try:
self._frameDataL[dataOffset]
# add to log and execute a proc when either happens:
# 1. test bits condition satisfied
# 2. direct value vector matches
# 1 and 2 are mutually exclusive
logNproc = False
# check for specific values given
# if value is a list then check by elements in list
# note in case of negative offset, value list should also be approcached 'from the right'
# e.g. offset -1 and value 90 00 means 00 is at -1 and 90 is at -2
if ('value' in testElem):
if (type(testElem['value']) is list):
if (testElem['offset'] < 0):
offsetStart = 1+dataOffset-len(testElem['value'])
offsetEnd = 1+dataOffset
if (offsetEnd == 0):
if (self._frameDataL[offsetStart:] == testElem['value']):
self._logDetail.append('Value %s at offset %d' % (str(" ".join(["{0:02x}".format(x) for x in testElem['value']])), offsetStart))
logNproc = True
else:
if (self._frameDataL[offsetStart:offsetEnd] == testElem['value']):
self._logDetail.append('Value %s at offset %d' % (str(" ".join(["{0:02x}".format(x) for x in testElem['value']])), offsetStart))
logNproc = True
else:
if (self._frameDataL[dataOffset:dataOffset+len(testElem['value'])] == testElem['value']):
self._logDetail.append('Value %s at offset %d' % (str(" ".join(["{0:02x}".format(x) for x in testElem['value']])), offsetStart))
logNproc = True
else:
if (self._frameDataL[dataOffset] == testElem['value']):
self._logDetail.append('Value 0x%02x at offset %d' % (self._frameDataL[dataOffset], dataOffset))
logNproc = True
else:
# if test bits not supplied then log the byte's value as a whole
if ('test_bits_on' not in testElem and 'test_bits_off' not in testElem):
self._logDetail.append('Value 0x%02x at offset %d' % (self._frameDataL[dataOffset], dataOffset))
if ('test_bits_on' in testElem):
# need to eval if comes from YAML
if (type(testElem['test_bits_on']) is str):
bitsOn = eval(testElem['test_bits_on'])
else:
bitsOn = testElem['test_bits_on']
if ((self._frameDataL[dataOffset] & bitsOn) == bitsOn):
bOn = 1
else:
bOn = 0
else:
bOn = 1
if ('test_bits_off' in testElem):
# need to eval if comes from YAML
if (type(testElem['test_bits_off']) is str):
bitsOff = eval(testElem['test_bits_off'])
else:
bitsOff = testElem['test_bits_off']
bOff = self._frameDataL[dataOffset] & bitsOff
else:
bOff = 0
if (bOn != 0 and bOff == 0):
logNproc = True
if (logNproc):
if ('log' in testElem):
self._logDetail.append(testElem['log'])
if ('proc' in testElem):
procLog = getattr(self._procClass, testElem['proc'])(self._frameDataL, dataOffset)
if (type(procLog) is list and procLog != []):
self._logDetail.extend(procLog)
except IndexError:
# TODO may want to know more about where and when it falls over
pass
break
# if value is NOT an empty list then:
# 1. check that we have a match of minimum length
# 2. check entry's state matches our state machine's state unless DecoderState.ANY is given
# 3. check 'command' value matches that of the input frame
if ('frame_fixedlen' in self._entry):
if (len(self._frameDataL) != self._entry['frame_fixedlen']):
continue
elif (len(self._frameDataL) < (self._entry['cmdlen'] + self._entry['datalen'])):
continue
if (self._frameDataL[0:self._entry['cmdlen']] == self._entry['value']):
if (type(self._entry['state_cur']) is list and (self._state in self._entry['state_cur'] or DecoderState.ANY in self._entry['state_cur'])):
match = True
if (type(self._entry['state_cur']) is not list and (self._state == self._entry['state_cur'] or self._entry['state_cur'] == DecoderState.ANY)):
match = True
if (match):
if (ConsoleDebugOutput):
print("match entry ", self._entry['label'])
self.checkCRC()
# allow state machine to retain it's previous state for DecoderState.SAME
if (self._entry['state_nxt'] != DecoderState.SAME):
self._state = self._entry['state_nxt']
if ('log' in self._entry):
self._log.append(self._entry['log'])
break
else:
self._log.append(',UNABLE TO INTERPRET')
def status(self):
print("State ", self._state)
print("Frame came from ", self._frameSrc.value)
print("Frame data ", [hex(x) for x in self._frameDataL])
print("******************** LOG ********************** Frame info ", self._log)
print("******************** LOG2 ********************* Frame info ", self._logDetail)
def getLog(self):
return self._log
def getLogDetail(self):
return self._logDetail
def checkCRC(self):
if ('crc' in self._entry and self._entry['crc'] != 0):
crc16A = CRC_16A(self._frameDataL[0:-2])
if (self._frameDataL[-2:] != crc16A):
self._log.append('CRC MISMATCH: %s vs %s received' % (str(" ".join([hex(x) for x in crc16A])), str(" ".join([hex(x) for x in self._frameDataL[-2:]]))))
else:
self._log.append('CRC MATCH: (%s)' % str(" ".join([hex(x) for x in crc16A])))
else:
self._log.append('CRC N/A (or Short Frame)')
class FrameTime:
def __init__(self):
self._timeMicroSec = 0
self._timeMicroSecPrev = 0
self._timeDeltaMicroSec = 0
self._timeRelativeMicroSec = 0
def newFrameTime(self, cpuCycles):
# 10ms delay: 1680030 cycles. 1us = 168 CPU cycles
if(self._timeMicroSecPrev==0):
self._timeMicroSec = int(cpuCycles, 16)
self._timeMicroSecPrev = self._timeMicroSec
else:
self._timeMicroSecPrev = self._timeMicroSec
self._timeMicroSec = int(cpuCycles, 16)
self._timeDeltaMicroSec = self._timeMicroSec - self._timeMicroSecPrev
if(self._timeDeltaMicroSec<0):
self._timeDeltaMicroSec += 2**32
self._timeRelativeMicroSec += self._timeDeltaMicroSec
@property
def timeRelativeMicroSec(self):
return int(self._timeRelativeMicroSec/168)
@property
def timeDeltaMicroSec(self):
return int(self._timeDeltaMicroSec/168)
@timeRelativeMicroSec.setter
def timeRelativeMicroSec(self, value):
if value < 0:
raise ValueError("not possible")
print("Setting value")
self._timeRelativeMicroSec = timeRelativeMicroSec
def getBinElement(binFile):
fileSize = binFile.seek(-1, 2) + 1
binFile.seek(0)
if (ConsoleDebugOutput):
print('File size is ', fileSize)
#for fileOffest in range(0, fileSize):
while (binFile.tell() < fileSize):
bSuccess = False
#fileOffest = binFile.tell()
if (ConsoleDebugOutput):
print('New element at ', binFile.tell())
protOptionsB = binFile.read(1)
if (protOptionsB != ''):
protOptions = int.from_bytes(protOptionsB, byteorder='little')
if (protOptions & BitMask.B3):
bStartTimestamp = True
else:
bStartTimestamp = False
if (protOptions & BitMask.B4):
bEndTimestamp = True
else:
bEndTimestamp = False
if (protOptions & BitMask.B5):
bParity = True
else:
bParity = False
protModulB = binFile.read(1)
protModul = int.from_bytes(protModulB, byteorder='little')
if (protModul != ''):
if (protModul == 1):
frameSrc = 'RDR'
elif (protModul == 2):
frameSrc = 'TAG'
else:
frameSrc = 'UNK'
encLen = binFile.read(2)
if (len(encLen) == 2):
frameLen = int.from_bytes(encLen, byteorder='little')
# already read 4 bytes (length is the length of entire frame)
frameLen -= 4
frameData = binFile.read(frameLen)
if (ConsoleDebugOutput):
print('frameLen ', frameLen)
print('frameData ', str(" ".join(["{0:02x}".format(x) for x in list(frameData)])))
if (len(frameData) == frameLen):
dataStartPtr = 0
dataEndPtr = frameLen
if (bStartTimestamp):
startTimestamp = int.from_bytes(frameData[0:4], byteorder='little')
dataStartPtr += 4
else:
startTimestamp = 0
if (bEndTimestamp):
endTimestamp = int.from_bytes(frameData[-4:], byteorder='little')
dataEndPtr -= 4
else:
endTimestamp = 0
bSuccess = True
dataBytes = b''
parityBytes = b''
if (bParity):
dataAndParityList = bytearray(frameData[dataStartPtr:dataEndPtr])
bOdd = False
if ((dataEndPtr-dataStartPtr)%2 == 1):
bOdd = True
dataAndParityList.append(0)
if (ConsoleDebugOutput):
print('odd len at ', binFile.tell())
try:
frameDataIter = struct.iter_unpack('@cc', dataAndParityList)
except struct.error:
print('struct error! File offset ', binFile.tell())
print('frameLen ', frameLen)
print('frameData ', str(" ".join(["{0:02x}".format(x) for x in list(frameData)])))
print('dataStartPtr ', dataStartPtr)
print('dataEndPtr ', dataEndPtr)
for byteAndParity in frameDataIter:
dataBytes += byteAndParity[0]
parityBytes += byteAndParity[1]
parityDataSet = set(parityBytes)
parityTestSet = set([0,1])
diffSet = parityDataSet-parityTestSet
if diffSet:
print('Error! Parity bytes are not 0 or 1')
print('set test ', diffSet)
print('dataBytes ', dataBytes)
print('parityBytes ', parityBytes)
else:
dataBytes = frameData[dataStartPtr:dataEndPtr]
if (bSuccess):
yield bSuccess, "{0:08x}".format(startTimestamp), "{0:08x}".format(endTimestamp), frameSrc, ["{0:02x}".format(x) for x in list(dataBytes)], list(parityBytes)
else:
yield bSuccess,
def logFrame():
pass
if __name__ == '__main__':
if(len(sys.argv) != 2):
#print("Usage: %s <Source HydraNFC TXT File> <Output CSV File>" \
print("Usage: %s <Source HydraNFC TXT (.txt) or BIN (.bin) File>" \
%os.path.basename(sys.argv[0]))
sys.exit(2)
with open('data/db.yml', 'r') as f:
decoderDB = yaml.load(f)
with open('data/db_apdu.yml', 'r') as f:
apduDB = yaml.load(f)
#nameWoExt = os.path.splitext(sys.argv[1])[0]
nameWoExt, extWoName = os.path.splitext(sys.argv[1])
if (extWoName == '.bin'):
bBinaryMode = True
infile = open(sys.argv[1], 'rb')
else:
infile = open(sys.argv[1], 'r')
outfile = open(nameWoExt+'.csv', 'w')
#outfileApdu = open(nameWoExt+'.apdu', 'w')
allSrcDataList = []
lineTokenList = []
lineDecodedTokenList = []
dataFrameList = []
lineNum = 0
frameTimestamp = FrameTime()
procCmdA = ProcessorCmdA()
procApdu = ProcessorApdu()
frameDec = FrameDecoder(decoderDB, procCmdA)
frameDecApdu = FrameDecoder(apduDB, procApdu, DecoderState.PROT4)
headerStr = 'CPU Cycles,Originator,Data Received,Time elapsed in uS,Time Delta in uS,CRC,Data Interpretation,Further Details,APDU,APDU Basic Info,APDU Detailed Info'
outfile.write(headerStr+'\n')
if (bBinaryMode):
for binElement in getBinElement(infile):
lineNum +=1
if (ConsoleDebugOutput):
print('element: ', binElement)
if (binElement[0] == True):
lineTokenList.clear()
lineTokenList.append(binElement[1])
lineTokenList.append(binElement[3])
lineTokenList.append('\"'+" ".join(binElement[4])+'\"')
lineDecodedTokenList.clear()
frameTimestamp.newFrameTime(binElement[1])
lineDecodedTokenList.append(frameTimestamp.timeRelativeMicroSec)
lineDecodedTokenList.append(frameTimestamp.timeDeltaMicroSec)
frameDec.newFrame(binElement[4], binElement[3], lineNum)
if (ConsoleDebugOutput):
frameDec.status()
logMainL = lineTokenList+lineDecodedTokenList+frameDec.getLog()
logMainStr = ''
for logEntry in logMainL:
logMainStr += str(logEntry)+','
logMainStr += '\n'
logDetailL = frameDec.getLogDetail()
logDetailStr = ''
if (logDetailL != []):
for logEntry in logDetailL:
logDetailStr += ',,,,,,,'+str(logEntry)+','+'\n'
outfile.write(logMainStr)
outfile.write(logDetailStr)
# apdu details will follow
if (procCmdA.isNewApdu):
apduData = procCmdA.getNewApdu()
#outfile.write(",,,,,,,,%s\n" % str(" ".join(["{0:02x}".format(x) for x in apduData])))
#if (ConsoleDebugOutput):
# print('New apdu ', str(" ".join(["{0:02x}".format(x) for x in apduData])))
frameDecApdu.newFrame(apduData, frameDec.frameSrc, lineNum)
if (ConsoleDebugOutput):
print('APDU Frame status: ')
frameDecApdu.status()
logMainL = frameDecApdu.getLog()
logMainStr = ''
for logEntry in logMainL:
logMainStr += ',,,,,,,,,'+str(logEntry)+','+'\n'
logDetailL = frameDecApdu.getLogDetail()
logDetailStr = ''
if (logDetailL != []):
for logEntry in logDetailL:
logDetailStr += ',,,,,,,,,,'+str(logEntry)+','+'\n'
outfile.write(logMainStr)
outfile.write(logDetailStr)
else:
print('element parsing error at frame: ', lineNum)
else:
for line in infile:
lineNum +=1
tabCount = line.count('\t')
if(tabCount==2):
lineTokenList=line.split('\t')
lineDecodedTokenList.clear()
frameTimestamp.newFrameTime(lineTokenList[0])
lineDecodedTokenList.append(frameTimestamp.timeRelativeMicroSec)
lineDecodedTokenList.append(frameTimestamp.timeDeltaMicroSec)
dataFrameList = lineTokenList[2].split()
lineTokenList[2] = '\"'+" ".join(dataFrameList)+'\"'
frameDec.newFrame(dataFrameList, lineTokenList[1], lineNum)
if (ConsoleDebugOutput):
frameDec.status()
logMainL = lineTokenList+lineDecodedTokenList+frameDec.getLog()
logMainStr = ''
for logEntry in logMainL:
logMainStr += str(logEntry)+','
logMainStr += '\n'
logDetailL = frameDec.getLogDetail()
logDetailStr = ''
if (logDetailL != []):
for logEntry in logDetailL:
logDetailStr += ',,,,,,,'+str(logEntry)+','+'\n'
outfile.write(logMainStr)
outfile.write(logDetailStr)
# apdu details will follow
if (procCmdA.isNewApdu):
apduData = procCmdA.getNewApdu()
#outfile.write(",,,,,,,,%s\n" % str(" ".join(["{0:02x}".format(x) for x in apduData])))
#if (ConsoleDebugOutput):
# print('New apdu ', str(" ".join(["{0:02x}".format(x) for x in apduData])))
frameDecApdu.newFrame(apduData, frameDec.frameSrc, lineNum)
if (ConsoleDebugOutput):
print('APDU Frame status: ')
frameDecApdu.status()
logMainL = frameDecApdu.getLog()
logMainStr = ''
for logEntry in logMainL:
logMainStr += ',,,,,,,,,'+str(logEntry)+','+'\n'
logDetailL = frameDecApdu.getLogDetail()
logDetailStr = ''
if (logDetailL != []):
for logEntry in logDetailL:
logDetailStr += ',,,,,,,,,,'+str(logEntry)+','+'\n'
outfile.write(logMainStr)
outfile.write(logDetailStr)
else:
print("Malformed line %d ignored" %(lineNum))
#for apduStr in procCmdA.getApdu():
# outfileApdu.write(apduStr)
infile.close()
outfile.close()
#outfileApdu.close()
print("All good, %d lines from input file processed" %(lineNum))