forked from ainfosec/FISSURE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fissure_libutils.py
738 lines (622 loc) · 30.9 KB
/
fissure_libutils.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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
#!/usr/bin/env python3
import yaml
import copy
#convenience function to keep from having to re-write the library schema
def getFields(library, protocol, packet_type):
"""
"""
try:
fields = sorted(library['Protocols'][protocol]['Packet Types'][packet_type]['Fields'], key=lambda x: library['Protocols'][
protocol]['Packet Types'][packet_type]['Fields'][x]['Sort Order'])
except KeyError as e:
fields = []
return fields
def newField(fieldname="",defaultvalue="",length=0,sortorder=1, iscrc="False",crcrange=""):
"""
"""
if not fieldname:
fieldname = 'New Field'
if not length:
length = len(defaultvalue)
if iscrc == "True":
iscrc = True
else:
iscrc = False
if crcrange == "":
new_field_subdict = {fieldname: {"Default Value": defaultvalue, "Length": int(length), 'Sort Order': int(sortorder), 'Is CRC':bool(iscrc)}}
else:
new_field_subdict = {fieldname: {"Default Value": defaultvalue, "Length": int(length), 'Sort Order': int(sortorder), 'Is CRC':bool(iscrc), 'CRC Range':str(crcrange)}}
return new_field_subdict
def newPacket(pkttype="", fields=newField()):
"""
"""
if not pkttype:
pkttype = "New Packet"
new_packet_type_dict = {pkttype: {'Fields': fields, 'Dissector':{'Filename': None, 'Port': None}, 'Sort Order':-1}}
return new_packet_type_dict
def getPacketTypes(library, protocol):
"""
"""
try:
packettypes = sorted(library['Protocols'][protocol]['Packet Types'], key=lambda x: library['Protocols'][
protocol]['Packet Types'][x]['Sort Order'])
except KeyError as e:
packettypes = []
return packettypes
def getFieldProperties(library, protocol, packet_type, field):
"""
"""
try:
field_properties = library['Protocols'][protocol]['Packet Types'][packet_type]['Fields'][field]
except KeyError as e:
field_properties = []
return field_properties
def getDefaults(library, protocol, packet_type):
"""
"""
try:
fields = sorted(library['Protocols'][protocol]['Packet Types'][packet_type]['Fields'], key=lambda x: library['Protocols'][
protocol]['Packet Types'][packet_type]['Fields'][x]['Sort Order'])
default_field_data = [library['Protocols'][protocol]['Packet Types'][
packet_type]['Fields'][field]['Default Value'] for field in fields]
except KeyError as e:
fields = []
default_field_data = []
return default_field_data
def newProtocol(protocolname="", attacks={},demodflowgraph='',modtype="", \
pkttypes = newPacket()):
""" Adds an empty protocol to a library.
"""
new_protocol_dict = {protocolname: {'Null Placeholder':None}}
return new_protocol_dict
def getProtocols(library):
"""
"""
try:
protocols = [protocols for protocols in library['Protocols']]
except KeyError as e:
protocols = []
return protocols
def getProtocol(library, protocol_name):
"""
"""
try:
protocol = library['Protocols'][protocol_name]
except KeyError as e:
protocol = []
return protocol
def getDemodulationFlowGraphsModulation(library, protocol=None):
""" Returns the modulation types for a protocol's demodulation flow graphs.
"""
get_modulation = []
if protocol:
try:
get_modulation.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'])
except KeyError as e:
pass
else:
for protocol in getProtocols(library):
try:
get_modulation.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'])
except KeyError as e:
pass
return list(set(get_modulation))
def findValueByKey(d, tag):
for k, v in d.items():
if isinstance(v, dict):
for i in findValueByKey(v, tag):
yield i
elif k == tag:
yield v
def getDemodulationFlowGraphsSnifferType(library, flow_graph):
""" Returns the sniffer types for a protocol's demodulation flow graphs.
"""
get_sniffer_type = ""
for x in findValueByKey(library, flow_graph):
get_sniffer_type = x[0]
break
return get_sniffer_type
def getDemodulationFlowGraphsHardware(library, protocol=None, modulation=None):
""" Returns the hardware types for demodulation flow graphs of a library.
"""
get_hardware = []
if protocol:
try:
if modulation:
library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation]
get_hardware.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation])
else:
for m in library['Protocols'][protocol]['Demodulation Flow Graphs']:
get_hardware.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][m])
except KeyError as e:
pass
else:
for protocol in getProtocols(library):
try:
if modulation:
get_hardware.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation])
else:
for m in library['Protocols'][protocol]['Demodulation Flow Graphs']:
get_hardware.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][m])
except KeyError as e:
pass
return list(set(get_hardware))
def getDemodulationFlowGraphs(library, protocol=None, modulation=None, hardware=None):
"""
"""
demod_flowgraphs = []
if protocol:
try:
if modulation:
if hardware:
try:
demod_flowgraphs.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation][hardware])
except:
pass
else:
for h in library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation]:
demod_flowgraphs.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation][h])
else:
for m in library['Protocols'][protocol]['Demodulation Flow Graphs']:
if hardware:
try:
demod_flowgraphs.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][m][hardware])
except:
pass
else:
for h in library['Protocols'][protocol]['Demodulation Flow Graphs'][m]:
demod_flowgraphs.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][m][h])
except KeyError as e:
#print("Error: No Demodulation Flowgraph Defined ",e, "for protocol", protocol)
pass
else:
for protocol in getProtocols(library):
try:
if modulation:
if hardware:
try:
demod_flowgraphs.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation][hardware])
except:
pass
else:
for h in library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation]:
demod_flowgraphs.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation][h])
else:
for m in library['Protocols'][protocol]['Demodulation Flow Graphs']:
if hardware:
try:
demod_flowgraphs.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][m][hardware])
except:
pass
else:
for h in library['Protocols'][protocol]['Demodulation Flow Graphs'][m]:
demod_flowgraphs.extend(library['Protocols'][protocol]['Demodulation Flow Graphs'][m][h])
except KeyError as e:
#print("Error: No Demodulation Flowgraph Defined ",e, "for protocol", protocol)
pass
return list(set(demod_flowgraphs)) # Only the Unique Values
def newSOI(frequency = 0, modulation = '', bandwidth = 0, continuous = "False", start_frequency = 0, end_frequency = 0, notes = '', subtype = ''):
"""
"""
if continuous == "True":
continuous = True
else:
continuous = False
new_soi_dict = {str(subtype): {'Frequency': float(frequency), \
'Modulation': str(modulation), \
'Bandwidth': float(bandwidth), \
'Continuous': bool(continuous), \
'Start Frequency': float(start_frequency), \
'End Frequency': float(end_frequency), \
'Notes': str(notes)}}
return new_soi_dict
def newStatistics(datarates = [],packetlengths = []):
"""
"""
new_statistics_dict = {'Statistics': {'Data Rates': datarates, \
'Median Packet Lengths': packetlengths}}
return new_statistics_dict
def getAllSOIs(library):
"""
"""
sois = {}
for protocol in getProtocols(library):
try:
sois.update({protocol: library['Protocols'][protocol]['SOI Data']})
except KeyError as e:
pass
#print("Error: No Key ",e, "for protocol", protocol)
return sois
def getSOIs(library, protocol):
""" Returns the SOIs of a protocol.
"""
sois = {}
try:
sois = library['Protocols'][protocol]['SOI Data']
except KeyError as e:
pass
#print("Error: No Key ",e, "for protocol", protocol)
return sois
def getAttacks(library, protocol):
""" Returns the attacks for a protocol.
"""
attacks = {}
updated_attacks = []
try:
attacks = library['Protocols'][protocol]['Attacks']
exclude_list = ["Single-Stage", "Denial of Service", "Jamming", "Spoofing", "Sniffing/Snooping", "Probe Attacks", "Installation of Malware", "Misuse of Resources", "File", "Multi-Stage", "New Multi-Stage", "Fuzzing", "Variables"]
for n in attacks:
if n not in exclude_list:
updated_attacks.append(n)
except KeyError as e:
pass
#print("Error: No Key ",e, "for protocol", protocol)
return updated_attacks
def getStatistics(library, protocol):
""" Returns the statistical categories for a protocol.
"""
statistics = {}
try:
statistics = library['Protocols'][protocol]['Statistics']
except KeyError as e:
pass
#print("Error: No Key ",e, "for protocol", protocol)
return statistics
def getStatisticValues(library, protocol, statistic):
""" Returns the values for a statistic.
"""
values = []
try:
if library['Protocols'][protocol]['Statistics'][statistic] != "None":
values = library['Protocols'][protocol]['Statistics'][statistic]
except KeyError as e:
pass
#print("Error: No Key ",e, "for protocol", protocol)
return values
def getModulations(library, protocol):
"""
"""
modulations = []
try:
modulations = library['Protocols'][protocol]['Modulation Types']
except KeyError as e:
pass
#print("Error: No Key ",e, "for protocol", protocol)
return modulations
def getDissector(library, protocol, packet_type):
""" Returns the name and port of a dissector for a particular packet type.
"""
dissector = library['Protocols'][protocol]['Packet Types'][packet_type]['Dissector']
return dissector
def getNextDissectorPort(library):
""" Returns an unassigned dissector UDP port.
"""
max_dissector_port = -1
for protocol in getProtocols(library):
for packet_type in getPacketTypes(library, protocol):
try:
get_port = int(library['Protocols'][protocol]['Packet Types'][packet_type]['Dissector']['Port'])
if get_port > max_dissector_port:
max_dissector_port = get_port + 1
except:
pass
return max_dissector_port
def addPacketType(library, protocol, packet_type):
"""
"""
try:
library['Protocols'][protocol]['Packet Types'].update(packet_type)
except KeyError as e:
library['Protocols'][protocol].update({'Packet Types':packet_type})
def addDissector(library, protocol, packet_type, dissector_filename, dissector_port):
""" Replaces the dissector filename and port for a packet type. There should only be one dissector per packet type.
"""
try:
dissector_dict = {'Filename':dissector_filename, 'Port':dissector_port}
library['Protocols'][protocol]['Packet Types'][packet_type]['Dissector'].update(dissector_dict)
except KeyError as e:
library['Protocols'][protocol]['Packet Types'][packet_type].update({'Dissector':dissector_dict})
def addProtocol(library, protocol=newProtocol()):
"""
"""
library['Protocols'].update(protocol)
def addModulation(library, protocol, modulation):
""" Adds a new modulation type to a library.
"""
# Check if Template Exists
try:
library['Protocols'][protocol]['Modulation Types'].append(modulation)
except KeyError as e:
library['Protocols'][protocol].update({'Modulation Types':[modulation]})
## Add Initial Template
#empty = ""
#try:
#empty = library['Protocols'][protocol]['Attack Categories']['Single-Stage']
#except KeyError:
## Default Attack Categories
#attack_dict = {'Single-Stage':{modulation:{'Hardware':{'USRP X310':'None'}}}}
#attack_dict['Single-Stage'][modulation]['Hardware'].update({'USRP XB210':'None'})
#attack_dict['Single-Stage'][modulation]['Hardware'].update({'HackRF':'None'})
#attack_dict['Single-Stage'][modulation]['Hardware'].update({'RTL2832U':'None'})
#attack_dict['Single-Stage'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#attack_dict.update({'Multi-Stage':{modulation:{'Hardware':{'USRP X310':'None'}}}})
#attack_dict['Multi-Stage'][modulation]['Hardware'].update({'USRP XB210':'None'})
#attack_dict['Multi-Stage'][modulation]['Hardware'].update({'HackRF':'None'})
#attack_dict['Multi-Stage'][modulation]['Hardware'].update({'RTL2832U':'None'})
#attack_dict['Multi-Stage'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#attack_dict.update({'New Multi-Stage':{modulation:{'Hardware':{'USRP X310':'None'}}}})
#attack_dict['New Multi-Stage'][modulation]['Hardware'].update({'USRP XB210':'None'})
#attack_dict['New Multi-Stage'][modulation]['Hardware'].update({'HackRF':'None'})
#attack_dict['New Multi-Stage'][modulation]['Hardware'].update({'RTL2832U':'None'})
#attack_dict['New Multi-Stage'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#attack_dict.update({'Fuzzing':{modulation:{'Hardware':{'USRP X310':'None'}}}})
#attack_dict['Fuzzing'][modulation]['Hardware'].update({'USRP XB210':'None'})
#attack_dict['Fuzzing'][modulation]['Hardware'].update({'HackRF':'None'})
#attack_dict['Fuzzing'][modulation]['Hardware'].update({'RTL2832U':'None'})
#attack_dict['Fuzzing'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#attack_dict.update({'Variables':{modulation:{'Hardware':{'USRP X310':'None'}}}})
#attack_dict['Variables'][modulation]['Hardware'].update({'USRP XB210':'None'})
#attack_dict['Variables'][modulation]['Hardware'].update({'HackRF':'None'})
#attack_dict['Variables'][modulation]['Hardware'].update({'RTL2832U':'None'})
#attack_dict['Variables'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#library['Protocols'][protocol].update({'Attack Categories':attack_dict})
## Do Other Attempts
#if len(empty) > 0:
#library['Protocols'][protocol]['Attacks']['Single-Stage'].update({modulation:{'Hardware':{'USRP X310':'None'}}})
#library['Protocols'][protocol]['Attacks']['Single-Stage'][modulation]['Hardware'].update({'USRP XB210':'None'})
#library['Protocols'][protocol]['Attacks']['Single-Stage'][modulation]['Hardware'].update({'HackRF':'None'})
#library['Protocols'][protocol]['Attacks']['Single-Stage'][modulation]['Hardware'].update({'RTL2832U':'None'})
#library['Protocols'][protocol]['Attacks']['Single-Stage'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#library['Protocols'][protocol]['Attacks']['Multi-Stage'].update({modulation:{'Hardware':{'USRP X310':'None'}}})
#library['Protocols'][protocol]['Attacks']['Multi-Stage'][modulation]['Hardware'].update({'USRP XB210':'None'})
#library['Protocols'][protocol]['Attacks']['Multi-Stage'][modulation]['Hardware'].update({'HackRF':'None'})
#library['Protocols'][protocol]['Attacks']['Multi-Stage'][modulation]['Hardware'].update({'RTL2832U':'None'})
#library['Protocols'][protocol]['Attacks']['Multi-Stage'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#library['Protocols'][protocol]['Attacks']['New Multi-Stage'].update({modulation:{'Hardware':{'USRP X310':'None'}}})
#library['Protocols'][protocol]['Attacks']['New Multi-Stage'][modulation]['Hardware'].update({'USRP XB210':'None'})
#library['Protocols'][protocol]['Attacks']['New Multi-Stage'][modulation]['Hardware'].update({'HackRF':'None'})
#library['Protocols'][protocol]['Attacks']['New Multi-Stage'][modulation]['Hardware'].update({'RTL2832U':'None'})
#library['Protocols'][protocol]['Attacks']['New Multi-Stage'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#library['Protocols'][protocol]['Attacks']['Fuzzing'].update({modulation:{'Hardware':{'USRP X310':'None'}}})
#library['Protocols'][protocol]['Attacks']['Fuzzing'][modulation]['Hardware'].update({'USRP XB210':'None'})
#library['Protocols'][protocol]['Attacks']['Fuzzing'][modulation]['Hardware'].update({'HackRF':'None'})
#library['Protocols'][protocol]['Attacks']['Fuzzing'][modulation]['Hardware'].update({'RTL2832U':'None'})
#library['Protocols'][protocol]['Attacks']['Fuzzing'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
#library['Protocols'][protocol]['Attacks']['Variables'].update({modulation:{'Hardware':{'USRP X310':'None'}}})
#library['Protocols'][protocol]['Attacks']['Variables'][modulation]['Hardware'].update({'USRP XB210':'None'})
#library['Protocols'][protocol]['Attacks']['Variables'][modulation]['Hardware'].update({'HackRF':'None'})
#library['Protocols'][protocol]['Attacks']['Variables'][modulation]['Hardware'].update({'RTL2832U':'None'})
#library['Protocols'][protocol]['Attacks']['Variables'][modulation]['Hardware'].update({'802.11x Adapter':'None'})
def addAttack(library, protocol, attack):
""" Adds a new attack to a library.
"""
attack_dict = {attack[0]:{attack[1]:{attack[2]:{attack[3]:{attack[4]:attack[5]}}}}}
file_type = attack[6]
tree_parent = attack[7]
# Check if Attack, Modulation, Hardware Exists
attacks_key_exists = []
attack_exists = []
modulation_exists = []
hardware_exists = []
try:
# Check if 'Attacks' Key Exists
library['Protocols'][protocol]['Attacks']
attacks_key_exists = [1]
# Check if Attack Exists
library['Protocols'][protocol]['Attacks'][attack[0]]
attack_exists = [1]
# Check if Modulation Type Exists
library['Protocols'][protocol]['Attacks'][attack[0]][attack[1]]
modulation_exists = [1]
# Check if Hardware Exists
library['Protocols'][protocol]['Attacks'][attack[0]][attack[1]]['Hardware'][attack[3]]
hardware_exists = [1]
except KeyError as e:
pass
# Add Attack and 'Attack' Key if not Already Present
if len(attacks_key_exists) > 0:
if len(attack_exists) > 0:
if len(modulation_exists) > 0:
if len(hardware_exists) > 0:
pass # Should not get here: Error
else:
library['Protocols'][protocol]['Attacks'][attack[0]][attack[1]]['Hardware'].update(attack_dict[attack[0]][attack[1]][attack[2]])
else:
library['Protocols'][protocol]['Attacks'][attack[0]].update(attack_dict[attack[0]])
else:
library['Protocols'][protocol]['Attacks'].update(attack_dict)
else:
library['Protocols'][protocol].update({'Attacks':attack_dict})
# Add Attack to the Attacks Section (Whole Attack List) of the Library
attack_list = copy.deepcopy(library["Attacks"][file_type + " Attacks"])
for attack_item in attack_list:
if tree_parent == attack_item.split(",")[0]:
index_value = library["Attacks"][file_type + " Attacks"].index(attack_item) + 1
level = int(attack_item.split(",")[1]) + 1
new_attack = list(attack_dict.keys())[0] + "," + str(level)
if new_attack not in library["Attacks"][file_type + " Attacks"]:
library["Attacks"][file_type + " Attacks"].insert(index_value,new_attack)
def addSOI(library, protocol, soi):
""" Adds an SOI to a library.
"""
# Renumerate Existing SOIs
existing_sois = getSOIs(library,protocol)
# temp_soi_dict = {}
# for s in existing_sois:
# new_key = "SOI " + str(len(temp_soi_dict) + 1)
# temp_soi_dict[new_key] = existing_sois[s]
# New SOI
existing_sois[list(soi.keys())[0]] = soi.values()[0]
# Add SOI
try:
check_key = library['Protocols'][protocol]['SOI Data']
except KeyError:
check_key = None
if check_key == None:
library['Protocols'][protocol].update({'SOI Data':existing_sois})
else:
library['Protocols'][protocol]['SOI Data'].update(existing_sois)
def removeSOI(library, protocol, soi):
""" Removes an SOI from a library.
"""
# Remove SOI from a Copy
existing_sois = getSOIs(library,protocol)
existing_sois.pop(soi)
# # Renumerate Existing SOIs
# temp_soi_dict = {}
# for s in existing_sois:
# new_key = "SOI " + str(len(temp_soi_dict) + 1)
# temp_soi_dict[new_key] = existing_sois[s]
# Update the Library
library['Protocols'][protocol]['SOI Data'] = existing_sois
if len(existing_sois) == 0:
del library['Protocols'][protocol]['SOI Data']
def removePacketType(library, protocol, packet_type):
""" Removes packet type from a library.
"""
# Update the Library
del library['Protocols'][protocol]['Packet Types'][packet_type]
if len(library['Protocols'][protocol]['Packet Types']) == 0:
del library['Protocols'][protocol]['Packet Types']
def removeDemodulationFlowGraph(library, protocol, modulation_type, hardware, demodulation_fg):
""" Removes demodulation flow graph from a library.
"""
# Update the Library
for n in range(0,len(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation_type][hardware])):
if demodulation_fg == library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation_type][hardware][n]:
del library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation_type][hardware][n]
break
if len(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation_type][hardware]) == 0:
del library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation_type][hardware]
if len(library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation_type]) == 0:
del library['Protocols'][protocol]['Demodulation Flow Graphs'][modulation_type]
if len(library['Protocols'][protocol]['Demodulation Flow Graphs']) == 0:
del library['Protocols'][protocol]['Demodulation Flow Graphs']
def removeModulationType(library, protocol, modulation_type):
""" Removes modulation type from a library.
"""
# Update the Library
for n in range(0,len(library['Protocols'][protocol]['Modulation Types'])):
if library['Protocols'][protocol]['Modulation Types'][n] == modulation_type:
del library['Protocols'][protocol]['Modulation Types'][n]
if len(library['Protocols'][protocol]['Modulation Types']) == 0:
del library['Protocols'][protocol]['Modulation Types']
# Delete Attacks from Library
try:
attack_dict = copy.deepcopy(library['Protocols'][protocol]['Attacks'])
for n in attack_dict:
if modulation_type in attack_dict[n].keys():
del library['Protocols'][protocol]['Attacks'][n][modulation_type]
if len(library['Protocols'][protocol]['Attacks'][n]) == 0:
del library['Protocols'][protocol]['Attacks'][n]
if len(library['Protocols'][protocol]['Attacks']) == 0:
del library['Protocols'][protocol]['Attacks']
except:
# To Avoid ['Attacks'] Key Errors
pass
def addDemodulationFlowGraph(library, protocol, demodulation_type, flow_graph, hardware_type, sniffer_type):
""" Adds to the list of demodulation flow graphs for a protocol/modulation combination.
"""
# Add Modulation Type if not Already Present
try:
check_key = library['Protocols'][protocol]['Demodulation Flow Graphs']
except KeyError:
check_key = None
try:
library['Protocols'][protocol]['Demodulation Flow Graphs'][demodulation_type][hardware_type][flow_graph].append(sniffer_type)
except KeyError:
if check_key == None:
library['Protocols'][protocol].update({'Demodulation Flow Graphs':{demodulation_type:{hardware_type:{flow_graph:[sniffer_type]}}}})
else:
library['Protocols'][protocol]['Demodulation Flow Graphs'][demodulation_type].update({hardware_type:{flow_graph:[sniffer_type]}})
###################################
# These were copied over, check these two functions.
def SOI_AutoSelect( list1, SOI_priorities, SOI_filters ):
""" Sort the SOI_list using specified criteria and choose the best SOI to examine.
"priority" is a list specifying which list elements in the SOI_list will be sorted by.
priority = (2, 0, 1) will produce a list that is sorted by element2, then element0, and then element1
"must_contain" is a list containing elements that narrows the SOI list further by checking for matches after the SOI list is sorted by priority
"""
#print('Unsorted list: {}' .format(list1))
# Sort the list by element priority
descending = False
for x in reversed(range(0,len(SOI_priorities))):
if SOI_filters[x] == "Highest":
list1 = sorted(list1, key=lambda list1: float(list1[SOI_priorities[x]]), reverse=True)
elif SOI_filters[x] == "Lowest":
list1 = sorted(list1, key=lambda list1: float(list1[SOI_priorities[x]]), reverse=False)
elif SOI_filters[x] == "Nearest to":
# Take Absolute Value of Value Differences and then Sort
new_list_matching = []
abs_value_list = []
# Absolute Value
for soi in range(0,len(list1)):
abs_value_list.append(abs(float(SOI_parameters[x]) - float(list1[soi][SOI_priorities[x]])))
# Sort from Absolute Value
sorted_index = sorted(range(len(abs_value_list)),key=lambda x:abs_value_list[x])
for index in range(0,len(sorted_index)):
new_list_matching.append(list1[sorted_index[index]])
list1 = new_list_matching
elif SOI_filters[x] == "Greater than":
# Keep Things that Fit Criteria
new_list_matching = []
for soi in range(0,len(list1)):
if float(list1[soi][SOI_priorities[x]]) > float(SOI_parameters[x]):
new_list_matching.append(list1[soi])
list1 = new_list_matching
elif SOI_filters[x] == "Less than":
# Keep Things that Fit Criteria
new_list_matching = []
for soi in range(0,len(list1)):
if float(list1[soi][SOI_priorities[x]]) < float(SOI_parameters[x]):
new_list_matching.append(list1[soi])
list1 = new_list_matching
elif SOI_filters[x] == "Containing":
# Keep Things that Fit Criteria
new_list_matching = []
for soi in range(0,len(list1)):
if list1[soi][0] in SOI_parameters[x]:
new_list_matching.append(list1[soi])
list1 = new_list_matching
#print('Sorted list: {}' .format(list1))
# Check if the list is empty
if len(list1) > 0:
soi = list1[0]
else:
print("No SOI Matches the Criteria")
soi = []
print('Selected SOI: {}' .format(soi))
return soi
def SOI_LibraryCheck( soi ):
""" Look up the SOI to recommend a best-fit flow graph from the library
"""
# There needs to be some kind of look up to limit the possibilities
# What parameters will be used to search the library? Modulation, Spreading, Frequency, Bandwidth?... How will the search library be managed?
# (Modulation_Type, Modulation_Sub_Parameters, Same_Modulation_Type_Index) : (Flow_Graph_Name, [Default_Variables], [Default_Values], [Potential_Attacks])
# Find Flow Graphs with Same Modulation Type
same_modulation_list_names=[v[0] for k,v in SOI_library.items() if k[0]==soi[0]] # Look Through Each Key in the Search Dictionary
if not same_modulation_list_names:
same_modulation_list_names = [v[0] for v in SOI_library.values()]
return same_modulation_list_names
###################################
if __name__ == '__main__':
pass
# print("Testing Lib Utils with current library...")
# filename1='/home/user/FISSURE/YAML/library.yaml'
# with open(filename1) as library:
# pd_library=yaml.load(library)
# prots=getProtocols(pd_library)
# print(prots)
# sois = getSOIs(pd_library)
# print(sois)
# demods = getDemodulationFlowGraphs(pd_library)
# print(demods)
# for prot in prots:
# print("Protocol :", prot)
# pkts=getPacketTypes(pd_library,prot)
# print("Packets in ", prot, ": ",pkts)
# for pkt in pkts:
# flds = getFields(pd_library, prot, pkt)
# defaults = getDefaults(pd_library, prot, pkt)
# for i in range(len(flds)):
# print("Fields ",i, ": ", flds[i], "default = ", defaults[i])