-
Notifications
You must be signed in to change notification settings - Fork 39
/
prober.py
executable file
·505 lines (469 loc) · 14.7 KB
/
prober.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
#!/usr/bin/env python
from __future__ import division
import sys
import logging
import os.path
from optparse import OptionParser
from operator import itemgetter
# Ensure we can see the pytls/tls subdir for the pytls submodule
sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'pytls'))
from tls import *
from probes import *
import probe_db
__version__ = '0.0.4'
__author__ = 'Richard J. Moore'
__email__ = 'rich@kde.org'
# List all the probes we have
probes = [
NormalHandshake(),
NormalHandshakePFS(),
NormalHandshake11(),
NormalHandshake11PFS(),
NormalHandshake12(),
NormalHandshake12PFS(),
NormalHandshake12PFSw13(),
ChangeCipherSpec(),
ChangeCipherSpec12(),
ChangeCipherSpec12PFS(),
OnlyECCipherSuites(),
HighTLSVersion(),
HighTLSVersion12(),
HighTLSVersion12PFS(),
VeryHighTLSVersion(),
VeryHighTLSVersion12(),
VeryHighTLSVersion12PFS(),
ZeroTLSVersion(),
ZeroTLSVersion12(),
ZeroTLSVersion12PFS(),
HighHelloVersion(),
HighHelloVersionNew(),
HighHelloVersionPFS(),
VeryHighHelloVersion(),
VeryHighHelloVersionNew(),
VeryHighHelloVersionPFS(),
ZeroHelloVersion(),
BadContentType(),
RecordLengthOverflow(),
RecordLengthUnderflow(),
Heartbeat(),
Heartbeat12(),
Heartbeat12PFS(),
Heartbleed(),
Heartbleed12(),
Heartbleed12PFS(),
BadHandshakeMessage(),
InvalidSessionID(),
InvalidSessionID12(),
InvalidSessionID12PFS(),
InvalidCiphersLength(),
InvalidCiphersLength12(),
InvalidCiphersLength12PFS(),
InvalidExtLength(),
InvalidExtLength12(),
InvalidExtLength12PFS(),
EmptyCompression(),
EmptyCompression12(),
EmptyCompression12PFS(),
CompressOnly(),
CompressOnly12(),
CompressOnly12PFS(),
ExtensionsUnderflow(),
ExtensionsUnderflow12(),
ExtensionsUnderflow12PFS(),
DoubleClientHello(),
DoubleClientHello12(),
DoubleClientHello12PFS(),
EmptyRecord(),
EmptyRecord12(),
EmptyRecord12PFS(),
TwoInvalidPackets(),
SplitHelloRecords(),
SplitHelloRecords12(),
SplitHelloRecords12PFS(),
SplitHelloPackets(),
SplitHelloPackets12(),
SplitHelloPackets12PFS(),
NoCiphers(),
NoCiphers12(),
EmptyChangeCipherSpec(),
EmptyChangeCipherSpec12(),
EmptyChangeCipherSpec12PFS(),
HelloRequest(),
HelloRequest12(),
HelloRequest12PFS(),
SNIWrongName(),
SNIWrongName12(),
SNIWrongName12PFS(),
SNILongName(),
SNILongName12(),
SNILongName12PFS(),
SNIEmptyName(),
SNIEmptyName12(),
SNIEmptyName12PFS(),
SNIOneWrong(),
SNIOneWrong12(),
SNIOneWrong12PFS(),
SNIWithDifferentType(),
SNIWithDifferentType12(),
SNIWithDifferentType12PFS(),
SNIDifferentTypeRev(),
SNIDifferentTypeRev12(),
SNIDifferentTypeRev12PFS(),
SNIOverflow(),
SNIOverflow12(),
SNIOverflow12PFS(),
SNIUnderflow(),
SNIUnderflow12(),
SNIUnderflow12PFS(),
SecureRenegoOverflow(),
SecureRenegoOverflow12(),
SecureRenegoOverflow12PFS(),
SecureRenegoUnderflow(),
SecureRenegoUnderflow12(),
SecureRenegoUnderflow12PFS(),
SecureRenegoNonEmpty(),
SecureRenegoNonEmpty12(),
SecureRenegoNonEmpty12PFS(),
SecureRenegoNull(),
SecureRenegoNull12(),
SecureRenegoNull12PFS(),
MaxFragmentNull(),
MaxFragmentNull12(),
MaxFragmentNull12PFS(),
MaxFragmentInvalid(),
MaxFragmentInvalid12(),
MaxFragmentInvalid12PFS(),
ClientCertURLsNotNull(),
ClientCertURLsNotNull12(),
ClientCertURLsNotNull12PFS(),
TrustedCANull(),
TrustedCANull12(),
TrustedCANull12PFS(),
TrustedCAOverflow(),
TrustedCAOverflow12(),
TrustedCAOverflow12PFS(),
TrustedCAUnderflow(),
TrustedCAUnderflow12(),
TrustedCAUnderflow12PFS(),
TruncatedHMACNotNull(),
TruncatedHMACNotNull12(),
TruncatedHMACNotNull12PFS(),
OCSPNull(),
OCSPNull12(),
OCSPNull12PFS(),
OCSPOverflow(),
OCSPOverflow12(),
OCSPOverflow12PFS(),
OCSPUnderflow(),
OCSPUnderflow12(),
OCSPUnderflow12PFS(),
DoubleExtension(),
DoubleExtension12(),
DoubleExtension12PFS(),
UserMappingNull(),
UserMappingNull12(),
UserMappingNull12PFS(),
UserMappingOverflow(),
UserMappingOverflow12(),
UserMappingOverflow12PFS(),
ClientAuthzNull(),
ClientAuthzNull12(),
ClientAuthzNull12PFS(),
ClientAuthzOverflow(),
ClientAuthzOverflow12(),
ClientAuthzOverflow12PFS(),
ServerAuthzNull(),
ServerAuthzNull12(),
ServerAuthzNull12PFS(),
ServerAuthzOverflow(),
ServerAuthzOverflow12(),
ServerAuthzOverflow12PFS(),
CertTypeNull(),
CertTypeNull12(),
CertTypeNull12PFS(),
CertTypeOverflow(),
CertTypeOverflow12(),
CertTypeOverflow12PFS(),
SupportedGroupsNull(),
SupportedGroupsNull12(),
SupportedGroupsNull12PFS(),
SupportedGroupsOddLen(),
SupportedGroupsOddLen12(),
SupportedGroupsOddLen12PFS(),
SupportedGroupsOverflow(),
SupportedGroupsOverflow12(),
SupportedGroupsOverflow12PFS(),
ECPointFormatsNull(),
ECPointFormatsNull12(),
ECPointFormatsNull12PFS(),
ECPointFormatsOverflow(),
ECPointFormatsOverflow12(),
ECPointFormatsOverflow12PFS(),
ECPointFormatsCompOnly(),
ECPointFormatsCompOnly12(),
ECPointFormatsCompOnly12PFS(),
SRPNull(),
SRPNull12(),
SRPNull12PFS(),
SRPOverflow(),
SRPOverflow12(),
SRPOverflow12PFS(),
SigAlgsNull(),
SigAlgsNull12(),
SigAlgsNull12PFS(),
SigAlgsOddLen(),
SigAlgsOddLen12(),
SigAlgsOddLen12PFS(),
SigAlgsOverflow(),
SigAlgsOverflow12(),
SigAlgsOverflow12PFS(),
UseSrtpNull(),
UseSrtpNull12(),
UseSrtpNull12PFS(),
UseSrtpOddLen(),
UseSrtpOddLen12(),
UseSrtpOddLen12PFS(),
UseSrtpOverflow(),
UseSrtpOverflow12(),
UseSrtpOverflow12PFS(),
HeartbeatNull(),
HeartbeatNull12(),
HeartbeatNull12PFS(),
HeartbeatInvalid(),
HeartbeatInvalid12(),
HeartbeatInvalid12PFS(),
ALPNNull(),
ALPNNull12(),
ALPNNull12PFS(),
ALPNUnknown(),
ALPNUnknown12(),
ALPNUnknown12PFS(),
ALPNOverflow(),
ALPNOverflow12(),
ALPNOverflow12PFS(),
OCSPv2Null(),
OCSPv2Null12(),
OCSPv2Null12PFS(),
OCSPv2Overflow(),
OCSPv2Overflow12(),
OCSPv2Overflow12PFS(),
SignedCertTSNotNull(),
SignedCertTSNotNull12(),
SignedCertTSNotNull12PFS(),
ClientCertTypeNull(),
ClientCertTypeNull12(),
ClientCertTypeNull12PFS(),
ClientCertTypeOverflow(),
ClientCertTypeOverflow12(),
ClientCertTypeOverflow12PFS(),
ServerCertTypeNull(),
ServerCertTypeNull12(),
ServerCertTypeNull12PFS(),
ServerCertTypeOverflow(),
ServerCertTypeOverflow12(),
ServerCertTypeOverflow12PFS(),
PaddingNull(),
PaddingNull12(),
PaddingNull12PFS(),
Padding300Byte(),
Padding300Byte12(),
Padding300Byte12PFS(),
Padding600Byte(),
Padding600Byte12(),
Padding600Byte12PFS(),
Padding16384Byte(),
Padding16384Byte12(),
Padding16384Byte12PFS(),
Padding16385Byte(),
Padding16385Byte12(),
Padding16385Byte12PFS(),
Padding16387Byte(),
Padding16387Byte12(),
Padding16387Byte12PFS(),
Padding16389Byte(),
Padding16389Byte12(),
Padding16389Byte12PFS(),
Padding17520Byte(),
Padding17520Byte12(),
Padding17520Byte12PFS(),
EtMNotNull(),
EtMNotNull12(),
EtMNotNull12PFS(),
EMSNotNull(),
EMSNotNull12(),
EMSNotNull12PFS(),
CachedInfoNull(),
CachedInfoNull12(),
CachedInfoNull12PFS(),
CachedInfoOverflow(),
CachedInfoOverflow12(),
CachedInfoOverflow12PFS(),
SessionTicketNull(),
SessionTicketNull12(),
SessionTicketNull12PFS(),
SessionTicketOverflow(),
SessionTicketOverflow12(),
SessionTicketOverflow12PFS(),
NPNNotNull(),
NPNNotNull12(),
NPNNotNull12PFS(),
TACKNotNull(),
TACKNotNull12(),
TACKNotNull12PFS()
]
def run_one_probe(ipaddress, port, starttls, specified_probe):
for probe in probes:
if specified_probe != type(probe).__name__:
continue
logging.info('Probing... %s', type(probe).__name__)
return {type(probe).__name__: probe.probe(ipaddress, port, starttls)}
def probe(ipaddress, port, starttls):
results = {}
for probe in probes:
logging.info('Probing... %s', type(probe).__name__)
result = probe.probe(ipaddress, port, starttls)
results[type(probe).__name__] = result
return results
def list_probes():
for probe in probes:
if type(probe).__doc__ is None:
print type(probe).__name__
else:
print '%s: %s' % (type(probe).__name__, type(probe).__doc__)
def probe_strength(db, raw_scores):
# return how diverse are the expected results of probes in the
# provided database (which probe is most likely to provide
# unique fingerprint)
if raw_scores:
max_score = max(raw_scores.items(), key=itemgetter(1))
else:
max_score = (None, 0)
# fingerprints that have the same, best, score
tied_fingerprints = set(name for name, val in raw_scores.items()
if val == max_score[1])
probe_matches = {}
probe_presence = {}
for fingerprint in db:
for probe_name, probe_result in fingerprint.probes.items():
probe_matches.setdefault(probe_name, set()).add(probe_result)
if probe_name not in probe_presence:
probe_presence[probe_name] = 0
if fingerprint.metadata['Description'] in tied_fingerprints:
probe_presence[probe_name] += 2
else:
probe_presence[probe_name] += 1
max_len = max(len(val) for val in probe_matches.values())
max_hits = max(probe_presence.values())
return dict((name, len(val) / max_len * probe_presence.get(name, 0) / max_hits)
for name, val in probe_matches.items())
def quick_probe(ipaddress, port, starttls, db):
# try to as quickly as possible reach 10 matching probes
results = {}
raw_scores = {}
best_score = 0
filtered_db = list(db)
while True:
# get list of probes that are most likely to provide unique
# response from server
scored_probes = probe_strength(filtered_db, raw_scores)
probes_iter = (name for name, _ in
sorted(scored_probes.items(), key=itemgetter(1),
reverse=True)
if name not in results)
# run probes against target until we get at least one match
for best_probe in probes_iter:
results.update(run_one_probe(ipaddress, port, starttls, best_probe))
raw_scores = probe_db.find_raw_matches(db, results)
if raw_scores:
break
else:
break
# repeat until the best match has more than 10 matching probes
best_score = max(raw_scores.items(), key=itemgetter(1))
if best_score[1] >= 10:
break
# in next iteration look for best probes given the already matching
# libraries (to try breaking ties)
filtered_db = [fp for fp in db
if fp.description() in raw_scores]
return results
def main():
options = OptionParser(usage='%prog server [options]',
description='A tool to fingerprint SSL/TLS servers')
options.add_option('-p', '--port',
type='int', default=443,
help='TCP port to test (default: 443)')
options.add_option('-m', '--matches', dest='matches',
type='int', default=0,
help=('Only display the first N matching scores'
'(default: 0 which displays them all)') )
options.add_option('-d', '--debug', action='store_true', dest='debug',
default=False,
help='Print debugging messages')
options.add_option( '-s', '--starttls', dest='starttls',
type='choice', action='store', default='auto',
choices=['auto','smtp','ftp','pop3','imap','none'],
help=('Enable a starttls mode. '
'The available modes are: auto, smtp, ftp, pop3, imap, none') )
options.add_option('-t', '--probe', dest='probe',
type='string',
help='Run the specified probe')
options.add_option('-a', '--add', dest='add', type='string',
help='Add the specified fingerprint to the database')
options.add_option('-l', '--list', dest='list', action='store_true',
help='List the fingerprints of the target')
options.add_option('--list-probes', dest='list_probes', action='store_true',
help='List the available probes')
options.add_option('-n', '--thorough', dest='thorough',
action='store_true',
help="Run all probes against target, don't perform a "
"quick scan")
options.add_option('-v', '--version', dest='version', action='store_true',
help='Display the version information')
opts, args = options.parse_args()
if opts.version:
print 'TLS Prober version %s, %s <%s>' % (__version__, __author__, __email__)
return
if opts.list_probes:
list_probes()
return
if len(args) < 1:
options.print_help()
return
if opts.debug:
logging.basicConfig(level=logging.DEBUG)
# Probe the server
if opts.probe:
results = run_one_probe(args[0], opts.port, opts.starttls, opts.probe)
elif opts.add or opts.thorough:
results = probe(args[0], opts.port, opts.starttls)
else:
print 'Running quick scan, results may be unreliable...'
db = probe_db.read_database()
results = quick_probe(args[0], opts.port, opts.starttls, db)
# Add a fingerprint to the db
if opts.add:
filename = probe_db.add_fingerprint(opts.add, results)
print 'Added %s to the database' % opts.add
print 'The fingerprint is located at:', filename
print 'Please submit your new fingerprint for inclusion in the next release!'
return
# Print the results of the probe
if opts.list:
for key, val in sorted(results.items()):
print '%24s:\t%s' % (key, val)
return
# Print the matches
matches = probe_db.find_matches(results)
count = 0
prev_score = None
for server, score in matches:
if opts.matches:
if score != prev_score:
prev_score = score
count += 1
if count > opts.matches:
break
print("{0:>65}: {1:6.2f}%".format(server, score*100))
if __name__ == '__main__':
main()