forked from WestpointLtd/tls_prober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobe_db.py
105 lines (79 loc) · 2.48 KB
/
probe_db.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
#!/usr/bin/python
import sys
import glob
import string
import os.path
fingerprint_dir = 'fingerprints'
class Fingerprint(object):
def __init__(self, metadata, probes):
self.metadata = metadata
self.probes = probes
def description(self):
return self.metadata['Description']
def read_fingerprint(filename):
f = open(filename)
in_body = False
probes = {}
metadata = {}
for line in f:
line = line.strip()
if line.startswith('#'):
continue
if in_body:
key, value = line.split(':',1)
probes[key] = value.strip()
elif line == '':
in_body = True
else:
key, value = line.split(':',1)
metadata[key] = value.strip()
f.close()
return Fingerprint(metadata, probes)
def read_database():
database = []
for f in glob.glob(os.path.join(fingerprint_dir, '*.fp')):
fingerprint = read_fingerprint(f)
database += [fingerprint]
return database
def find_matches(probes):
scores = {}
database = read_database()
for f in database:
for key in probes.keys():
if f.probes.has_key(key) and f.probes[key] == probes[key]:
scores[f.description()] = scores.get(f.description(), 0)+1
# Remove entries that don't match at all
for desc in scores.keys():
if scores[desc] == 0:
del scores[desc]
# Convert the matches to a list
results = []
matches = sorted(scores, key=scores.__getitem__, reverse=True)
for match in matches:
results += [ [match, scores[match]] ]
return results
def add_fingerprint(description, probes):
# Create filename
filename = description.translate(None, string.punctuation)
filename = filename.strip()
filename = filename.replace(' ', '-')
filename = filename.lower()
filename += '.fp'
f = open(os.path.join(fingerprint_dir, filename), 'w')
f.write('Description: %s\n' % description)
f.write('\n')
for probe in probes.keys():
f.write('%s: %s\n' % (probe, probes[probe]))
f.close()
return os.path.join(fingerprint_dir, filename)
if __name__ == '__main__':
database = read_database()
for fingerprint in database:
print 'Description:'
print fingerprint.description()
print 'Metadata:'
print fingerprint.metadata
print 'Probes:'
print fingerprint.probes
matches = find_matches(database[0].probes)
print matches