-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyze.py
executable file
·484 lines (403 loc) · 14.9 KB
/
analyze.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
import json
import sys
import csv
import ast
import json
import config
import os
totalBanners = 0
equivalentDevTypes = {
"nas": ["nvs"],
"scada gateway": ["gateway"],
"DSL modem": ["modem"],
"cable modem": ["modem"],
"network": ["router", "gateway", "access point", "switch", "switches"],
"infrastructure router": ["router"],
"DVR": ["video recorder", "video encoder"],
"scada router": ["router"],
"soho router": ["router"],
"wireless modem": ["modem"],
"DSL/cable modem": ["modem"],
"laser printer": ["printer"],
"Security Camera": ["camera", "ipcam", "netcam", "cam"],
"Router/Gateway": ["router", "gateway"]
}
def listToString(s):
'''
This function lists to strings
:param s: A list
:return: A string
'''
str1 = ""
for ele in s:
str1 += ele
return str1
def getRulesStats(rules, ruleNumToBannerMap):
'''
This function returns useful statistics for rules
:param rules: A dictionary of rules - {rule#: rule}
:return: A dictionary containing useful stats
'''
device = 0
vendor = 0
product = 0
device_vendor = 0
device_product = 0
vendor_product = 0
device_vendor_product = 0
devices = set()
vendors = set()
products = set()
deviceToBannerMap = dict()
vendorToBannerMap = dict()
productToBannerMap = dict()
for ruleNum, rule in rules.items():
banner = ruleNumToBannerMap[ruleNum]
if "deviceType" in rule and "vendor" in rule and "product" in rule:
device_vendor_product += 1
devices.add(rule['deviceType'])
vendors.add(rule['vendor'])
products.add(rule['product'])
deviceToBannerMap.setdefault(rule['deviceType'], set()).add(banner)
vendorToBannerMap.setdefault(rule['vendor'], set()).add(banner)
productToBannerMap.setdefault(rule['product'], set()).add(banner)
elif "deviceType" in rule and "vendor" in rule:
device_vendor += 1
devices.add(rule['deviceType'])
vendors.add(rule['vendor'])
deviceToBannerMap.setdefault(rule['deviceType'], set()).add(banner)
vendorToBannerMap.setdefault(rule['vendor'], set()).add(banner)
elif "deviceType" in rule and "product" in rule:
device_product += 1
devices.add(rule['deviceType'])
products.add(rule['product'])
deviceToBannerMap.setdefault(rule['deviceType'], set()).add(banner)
productToBannerMap.setdefault(rule['product'], set()).add(banner)
elif "vendor" in rule and "product" in rule:
vendor_product += 1
vendors.add(rule['vendor'])
products.add(rule['product'])
vendorToBannerMap.setdefault(rule['vendor'], set()).add(banner)
productToBannerMap.setdefault(rule['product'], set()).add(banner)
elif "deviceType" in rule:
device += 1
devices.add(rule["deviceType"])
deviceToBannerMap.setdefault(rule['deviceType'], set()).add(banner)
elif "vendor" in rule:
vendor += 1
vendors.add(rule['vendor'])
vendorToBannerMap.setdefault(rule['vendor'], set()).add(banner)
elif "product" in rule:
product += 1
products.add(rule['product'])
productToBannerMap.setdefault(rule['product'], set()).add(banner)
for dev, banners in deviceToBannerMap.items():
deviceToBannerMap[dev] = len(banners)
deviceToBannerMap = {
k: v for k,
v in sorted(
deviceToBannerMap.items(),
key=lambda item: item[1],
reverse=True)}
for ven, banners in vendorToBannerMap.items():
vendorToBannerMap[ven] = len(banners)
vendorToBannerMap = {
k: v for k,
v in sorted(
vendorToBannerMap.items(),
key=lambda item: item[1],
reverse=True)}
for prod, banners in productToBannerMap.items():
productToBannerMap[prod] = len(banners)
productToBannerMap = {
k: v for k,
v in sorted(
productToBannerMap.items(),
key=lambda item: item[1],
reverse=True)}
stats = {
"<devices>": device,
"<vendors>": vendor,
"<products>": product,
"<devices, vendor>": device_vendor,
"<devices, product>": device_product,
"<vendors, products>": vendor_product,
"<device, vendor, product>": device_vendor_product,
"<devices list>": deviceToBannerMap,
"<vendors list>": vendorToBannerMap,
"<products list>": productToBannerMap}
return stats
def createBannerToLabelsMap(inputDataset):
'''
This function creates a dictionary that maps banners to their labels for ground truth
:return: A dictionary that maps banner to their labels.
'''
global totalBanners
# only consider those banners which generated a non-empty query
queryLog = json.load(open(config.QUERY_LOG_FILE, encoding="utf-8"))
for bannerID, query in queryLog.items():
if query:
totalBanners += 1
bannerToLabelsMap = dict()
for bannerID, item in inputDataset.items():
deviceTypes = []
vendors = []
products = []
banner = item["banner"]
for _, field in item.items():
# All labels (vendor, product, deviceTypes) must be list type
for element in field:
if "label" in element:
label = element["label"]
else:
continue
if "deviceTypes" in label:
if label["deviceTypes"]:
deviceTypes.extend(label["deviceTypes"])
if "vendor" in label:
if label["vendor"]:
vendors.extend(label["vendor"])
if "product" in label:
if label["product"]:
products.extend(label["product"])
bannerToLabelsMap[banner] = {
"deviceTypes": deviceTypes,
"vendors": vendors,
"products": products,
"bannerID": bannerID}
return bannerToLabelsMap
def checkExists(bannerText, deviceType=None, vendor=None, product=None):
'''
This function checks if device labels exist in groundtruth
:param bannerText: A string containing banner text
:param deviceType: A string containing device type to check
:param vendor: A string containing vendor name type to check
:param product: A string containing product name type to check
:return: Returns True/False if device labels exists in groundtruth
'''
global bannerToLabelsMap
banner = bannerToLabelsMap[bannerText]
devicesTypesGT = banner["deviceTypes"]
vendorsGT = banner["vendors"]
productsGT = banner["products"]
if deviceType and devicesTypesGT:
totalDeviceTypesGT = devicesTypesGT
deviceType = deviceType.lower().strip()
for dev in devicesTypesGT:
if dev in equivalentDevTypes:
totalDeviceTypesGT.extend(equivalentDevTypes[dev])
for dev in totalDeviceTypesGT:
dev = dev.lower().strip()
if dev != "" and (deviceType in dev or dev in deviceType):
return True
return False
if vendor and vendorsGT:
for v in vendorsGT:
vendor = vendor.lower().strip()
v = v.lower().strip()
if v != "" and vendor in v or v in vendor:
return True
return False
if product and productsGT:
for p in productsGT:
product = product.lower().strip()
p = p.lower().strip()
if p != "" and (product in p or p in product):
return True
return False
return True
def prettyWrite(rulesStats):
'''
This function writes a given dictionary in a pretty format in "analysis.txt" file
:param ruleStats: A dictionary
:return: None. Dictionary contents are written in "analysis.txt" file in pretty format.
'''
for title, stat in rulesStats.items():
analysisFile.write(str(title) + ": " + str(stat) + "\n")
def writeToAnalysis(
totalRules,
truePositive,
falsePositive,
bannersInRules,
totalBanners,
allRules,
correctRules,
ruleNumToBannerMap,
filtered):
'''
This function writes to "analysis.txt" file
:param truePositive: A dictionary of rules - {rule#: rule}
:param truePositive: An int containing # of true positives in rules
:param falsePositive: An int containing # of false positives in rules
:param bannersInRules: A set containing all banners in rules
:param totalBanners: An int containing total number of banners in dataset
:param allRules: A dictionary containing all rules - {rule#: rule}
:param correctRuls: A dictionary containing only the correct rules - {rule#: rule}
:param filtered: A bool indicating if this function is called for rules with per banner filtering
:return: A dictionary containing useful stats
'''
if filtered:
string = """
=======================================
With highest confidence per banner filtering:
========================================
\n\n"""
else:
string = """
=======================================
Without highest confidence per banner filtering:
========================================
\n\n"""
analysisFile.write(string)
precision = str(
round((truePositive / (truePositive + falsePositive)) * 100, 2)) + "%"
coverage = str(
round(
((len(bannersInRules)) / totalBanners) * 100,
2)) + "%"
analysis = {
"Total Rules": totalRules,
"True Positive": truePositive,
"False Positive": falsePositive,
"Total Banners": totalBanners,
"Banners with Rules": len(bannersInRules),
"Precision": precision,
"Coverage": coverage}
prettyWrite(analysis)
analysisFile.write("\n\n\nRules Stats:\n")
rulesStats = getRulesStats(allRules, ruleNumToBannerMap)
prettyWrite(rulesStats)
analysisFile.write("\n\n\nCorrect Rules Stats:\n")
rulesStats = getRulesStats(correctRules, ruleNumToBannerMap)
prettyWrite(rulesStats)
analysisFile.write("\n" * 4)
def createLabeledFilteredRules(bannerToRuleLinesMap, ruleNumToBannerMap):
# Create "rules.filtered.labeled.csv" file
rulesLabeledFilteredFile = open(
os.path.join(
config.OUT_PATH,
"rules.filtered.labeled.csv"),
"w",
encoding="utf-8")
totalRules = 0
truePositive = 0
falsePositive = 0
bannersInRules = set()
correctRules = dict()
allRules = dict()
for banner, rulesLines in bannerToRuleLinesMap.items():
bannersInRules.add(bannerToLabelsMap[banner]["bannerID"])
for rulesLine in rulesLines:
for l in csv.reader(
[rulesLine],
quotechar='"',
delimiter=',',
quoting=csv.QUOTE_ALL,
skipinitialspace=True):
csvLine = l
ruleNum = rulesLine.split(",")[0]
rule = ast.literal_eval(csvLine[1])
allRules[ruleNum] = rule
totalRules += 1
rulesLabeledFilteredFile.write(rulesLine)
if not ruleNumToFlagMap[ruleNum]:
falsePositive += 1
else:
truePositive += 1
correctRules[ruleNum] = rule
writeToAnalysis(
totalRules,
truePositive,
falsePositive,
bannersInRules,
totalBanners,
allRules,
correctRules,
ruleNumToBannerMap,
filtered=True)
if __name__ == "__main__":
inputDataset = json.load(open(config.BANNERS_FILE, encoding="utf8"))
rulesFile = open(config.RULES_FILE, encoding="utf8")
rulesLabeledFile = open(
os.path.join(
config.OUT_PATH,
"rules.labeled.csv"),
"w",
encoding="utf-8")
rulesLabeledFile.write(",Items,Support,Confidence\n")
analysisFile = open(os.path.join(config.OUT_PATH, "analysis.txt"), "w")
next(rulesFile)
rulesLines = rulesFile.readlines()
bannerToLabelsMap = createBannerToLabelsMap(inputDataset)
ruleNumToBannerMap = dict()
totalRules = 0
bannersInRules = set()
ruleNumToFlagMap = dict()
correctRules = dict()
allRules = dict()
# bannerToRuleLinesMap keeps rules with highest confidence against banner
bannerToRuleLinesMap = dict()
for l in csv.reader(
rulesLines,
quotechar='"',
delimiter=',',
quoting=csv.QUOTE_ALL,
skipinitialspace=True):
totalRules += 1
rule = ast.literal_eval(l[1])
ruleNum = l[0]
ruleLine = rulesLines[int(ruleNum)]
banner = rule["banner"]
bannersInRules.add(bannerToLabelsMap[banner]["bannerID"])
confidence = l[3]
ruleNumToBannerMap[ruleNum] = banner
if "deviceType" in rule:
deviceType = rule["deviceType"]
if not checkExists(banner, deviceType=deviceType):
ruleNumToFlagMap[ruleNum] = False
if "vendor" in rule:
vendor = rule["vendor"]
if not checkExists(banner, vendor=vendor):
ruleNumToFlagMap[ruleNum] = False
if "product" in rule:
product = rule["product"]
if not checkExists(banner, product=product):
ruleNumToFlagMap[ruleNum] = False
if ruleNum not in ruleNumToFlagMap:
ruleNumToFlagMap[ruleNum] = True
correctRules[ruleNum] = rule
allRules[ruleNum] = rule
ruleLabel = ruleNumToFlagMap[str(ruleNum)]
labeledRuleLine = ruleLine.rstrip() + "," + str(ruleLabel) + "\n"
rulesLabeledFile.write(labeledRuleLine)
if banner not in bannerToRuleLinesMap:
bannerToRuleLinesMap.setdefault(banner, []).append(labeledRuleLine)
for rLine in bannerToRuleLinesMap[banner]:
if rLine == labeledRuleLine:
break
currentConfidence = rLine.split(",")[-2]
if float(confidence) > float(currentConfidence):
bannerToRuleLinesMap[banner] = [labeledRuleLine]
break
if float(currentConfidence) == float(confidence):
bannerToRuleLinesMap.setdefault(
banner, []).append(labeledRuleLine)
break
truePositive = 0
falsePositive = 0
for ruleNum, flag in ruleNumToFlagMap.items():
if flag:
truePositive += 1
else:
falsePositive += 1
writeToAnalysis(
totalRules,
truePositive,
falsePositive,
bannersInRules,
totalBanners,
allRules,
correctRules,
ruleNumToBannerMap,
filtered=False)
createLabeledFilteredRules(bannerToRuleLinesMap, ruleNumToBannerMap)