forked from WestpointLtd/tls_prober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probes.py
592 lines (436 loc) · 20.1 KB
/
probes.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
#!/usr/bin/python
import socket
import select
import errno
import logging
import socks
import os
from prober_utils import *
settings = {
# Note that changing these will invalidate many of the fingerprints
'default_hello_version': TLSRecord.TLS1_0,
'default_record_version': TLSRecord.TLS1_0,
'socket_timeout': 5
}
class Probe(object):
#
# Reusable standard elements
#
def connect(self, ipaddress, port, starttls_mode):
# Check if we're using socks
if os.environ.has_key('socks_proxy'):
socks_host, socks_port = os.environ['socks_proxy'].split(':')
s = socks.socksocket()
s.setproxy(socks.PROXY_TYPE_SOCKS5, socks_host, int(socks_port))
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(settings['socket_timeout'])
s.connect((ipaddress, port))
# Do starttls if relevant
starttls(s, port, starttls_mode)
return s.makefile('rw', 0)
def test(self, sock):
pass
def process_response(self, sock):
response = ''
got_done = False
while True:
# Check if there is anything following the server done
if got_done:
# If no data then we're done (the server hasn't sent anything further)
# we allow 500ms to give the followup time to arrive
if not select.select([sock.fileno(),],[],[],0.5)[0]:
break
try:
record = read_tls_record(sock)
response += '*(%x)' % record.version() # TODO: Not sure that recording the record layer version is worth it?
except socket.timeout, e:
response += 'error:timeout'
break
except socket.error, e:
response += 'error:%s|' % errno.errorcode[e.errno]
break
except IOError, e:
response += 'error:%s|' % str(e)
break
if record.content_type() == TLSRecord.Handshake:
# A single handshake record can contain multiple handshake messages
processed_bytes = 0
while processed_bytes < record.message_length():
message = HandshakeMessage.from_bytes(record.message()[processed_bytes:])
if message.message_type() == message.ServerHello:
response += 'handshake:%s(%x)|' % (message.message_types[message.message_type()], message.server_version())
else:
response += 'handshake:%s|' % (message.message_types[message.message_type()])
if message.message_type() == HandshakeMessage.ServerHelloDone:
got_done = True
processed_bytes += message.message_length() + 4
if got_done:
continue
elif record.content_type() == TLSRecord.Alert:
alert = AlertMessage.from_bytes(record.message())
if alert.alert_level() == AlertMessage.Fatal:
response += 'alert:%s:fatal|' % alert.alert_types[alert.alert_type()]
break
else:
response += 'alert:%s:warning|' % alert.alert_types[alert.alert_type()]
else:
if record.content_types.has_key(record.content_type()):
response += 'record:%s|' % record.content_types[record.content_type()]
else:
response += 'record:type(%x)|' % record.content_type()
if got_done:
break
return response
def probe(self, ipaddress, port, starttls):
sock = self.connect(ipaddress, port, starttls)
try:
result = self.test(sock)
except socket.timeout, e:
result = 'writeerror:timeout'
return result
except socket.error, e:
result = 'writeerror:%s|' % errno.errorcode[e.errno]
return result
if result:
return result
return self.process_response(sock)
class NormalHandshake(Probe):
'''A normal handshake'''
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(make_hello())
class DoubleClientHello(Probe):
'''Two client hellos'''
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(make_hello())
logging.debug('Sending Client Hello...')
sock.write(make_hello())
class ChangeCipherSpec(Probe):
'''Send a hello then change cipher spec'''
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(make_hello())
logging.debug('Sending ChangeCipherSpec...')
sock.write(make_ccs())
class EmptyChangeCipherSpec(Probe):
'''Send a hello then an empty change cipher spec'''
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(make_hello())
logging.debug('Sending Empty ChangeCipherSpec...')
record = TLSRecord.create(content_type=TLSRecord.ChangeCipherSpec,
version=TLSRecord.TLS1_0,
message='')
sock.write(record.bytes)
class BadHandshakeMessage(Probe):
'''An invalid handshake message'''
def make_bad_handshake(self):
content = 'Something'
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=TLSRecord.TLS1_0,
message=content)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(make_hello())
logging.debug('Sending bad handshake message...')
sock.write(self.make_bad_handshake())
class OnlyECCipherSuites(Probe):
'''Try connecting with ECC cipher suites only'''
def make_ec_hello(self):
hello = ClientHelloMessage.create(TLSRecord.TLS1_0,
'01234567890123456789012345678901',
[TLS_ECDH_RSA_WITH_RC4_128_SHA,
TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256])
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=TLSRecord.TLS1_0,
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_ec_hello())
class Heartbeat(Probe):
'''Try to send a heartbeat message'''
def make_hb_hello(self):
hb_extension = HeartbeatExtension.create()
hello = ClientHelloMessage.create(TLSRecord.TLS1_0,
'01234567890123456789012345678901',
DEFAULT_CIPHERS,
extensions = [ hb_extension ])
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=TLSRecord.TLS1_0,
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def make_heartbeat(self):
heartbeat = HeartbeatMessage.create(HeartbeatMessage.HeartbeatRequest,
'XXXX')
record = TLSRecord.create(content_type=TLSRecord.Heartbeat,
version=TLSRecord.TLS1_0,
message=heartbeat.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_hb_hello())
logging.debug('Sending Heartbeat...')
sock.write(self.make_heartbeat())
class Heartbleed(Probe):
'''Try to send a heartbleed attack'''
def make_hb_hello(self):
hb_extension = HeartbeatExtension.create()
hello = ClientHelloMessage.create(TLSRecord.TLS1_0,
'01234567890123456789012345678901',
DEFAULT_CIPHERS,
extensions = [ hb_extension ])
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=TLSRecord.TLS1_0,
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def make_heartbleed(self):
heartbeat = HeartbeatMessage.create(HeartbeatMessage.HeartbeatRequest,
'XXXX', 0x4000)
record = TLSRecord.create(content_type=TLSRecord.Heartbeat,
version=TLSRecord.TLS1_0,
message=heartbeat.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_hb_hello())
logging.debug('Sending Heartbleed...')
sock.write(self.make_heartbleed())
class HighTLSVersion(Probe):
'''Set a high TLS version in the record'''
def make_high_tls_hello(self):
hello = ClientHelloMessage.create(settings['default_hello_version'],
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=0x400,
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_high_tls_hello())
class VeryHighTLSVersion(Probe):
'''Set a very high TLS version in the record'''
def make_very_high_tls_hello(self):
hello = ClientHelloMessage.create(settings['default_hello_version'],
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=0xffff,
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_very_high_tls_hello())
class ZeroTLSVersion(Probe):
'''Set a zero version in the record'''
def make_zero_tls_hello(self):
hello = ClientHelloMessage.create(settings['default_hello_version'],
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=0x000,
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_zero_tls_hello())
class HighHelloVersion(Probe):
'''Set a high version in the hello'''
def make_high_tls_hello(self):
hello = ClientHelloMessage.create(0x400,
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=settings['default_record_version'],
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_high_tls_hello())
class VeryHighHelloVersion(Probe):
'''Set a very high version in the hello'''
def make_high_tls_hello(self):
hello = ClientHelloMessage.create(0xffff,
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=settings['default_record_version'],
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_high_tls_hello())
class ZeroHelloVersion(Probe):
'''Set a zero version in the hello'''
def make_zero_tls_hello(self):
hello = ClientHelloMessage.create(0x000,
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=settings['default_record_version'],
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_zero_tls_hello())
class BadContentType(Probe):
'''Use an invalid content type in the record'''
def make_bad_content_type(self):
hello = ClientHelloMessage.create(settings['default_hello_version'],
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=17,
version=settings['default_record_version'],
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_bad_content_type())
class RecordLengthOverflow(Probe):
'''Make the record length exceed the stated one'''
def make_record_length_overflow(self):
hello = ClientHelloMessage.create(settings['default_hello_version'],
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=settings['default_record_version'],
message=hello.bytes,
length=0x0001)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_record_length_overflow())
class RecordLengthUnderflow(Probe):
'''Make the record shorter than the specified length'''
def make_record_length_underflow(self):
hello = ClientHelloMessage.create(settings['default_hello_version'],
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=settings['default_record_version'],
message=hello.bytes,
length=0xffff)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
try:
sock.write(self.make_record_length_underflow())
except socket.timeout, e:
result = 'writeerror:timeout'
return result
except socket.error, e:
result = 'writeerror:%s|' % errno.errorcode[e.errno]
return result
class EmptyRecord(Probe):
'''Send an empty record then the hello'''
def make_empty_record(self):
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=settings['default_record_version'],
message='')
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending empty record...')
sock.write(self.make_empty_record())
sock.write(make_hello())
class SplitHelloRecords(Probe):
'''Split the hello over two records'''
def make_split_hello(self):
hello = ClientHelloMessage.create(settings['default_hello_version'],
'01234567890123456789012345678901',
DEFAULT_CIPHERS)
first = hello.bytes[:10]
second = hello.bytes[10:]
record_one = TLSRecord.create(content_type=TLSRecord.Handshake,
version=settings['default_record_version'],
message=first)
record_two = TLSRecord.create(content_type=TLSRecord.Handshake,
version=0x301,
message=second)
#hexdump(record.bytes)
return record_one, record_two
def test(self, sock):
logging.debug('Sending split hello...')
part_one, part_two = self.make_split_hello()
sock.write(part_one)
try:
sock.write(part_two)
except socket.timeout, e:
result = 'writeerror:timeout'
return result
except socket.error, e:
result = 'writeerror:%s|' % errno.errorcode[e.errno]
return result
class SplitHelloPackets(Probe):
'''Split the hello over two packets'''
def test(self, sock):
logging.debug('Sending Client Hello part one...')
record = make_hello()
sock.write(record[:10])
sock.flush()
logging.debug('Sending Client Hello part two...')
sock.write(record[10:])
class NoCiphers(Probe):
'''Send an empty cipher list'''
def make_no_ciphers_hello(self):
hello = ClientHelloMessage.create(settings['default_hello_version'],
'01234567890123456789012345678901',
[])
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=settings['default_record_version'],
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending No ciphers Hello...')
sock.write(self.make_no_ciphers_hello())
class SNIWrongName(Probe):
'''Send a server name indication for a non-matching name'''
def make_sni_hello(self, name):
sni_extension = ServerNameExtension.create(name)
hello = ClientHelloMessage.create(TLSRecord.TLS1_0,
'01234567890123456789012345678901',
DEFAULT_CIPHERS,
extensions = [ sni_extension ])
record = TLSRecord.create(content_type=TLSRecord.Handshake,
version=TLSRecord.TLS1_0,
message=hello.bytes)
#hexdump(record.bytes)
return record.bytes
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_sni_hello('thisisnotyourname'))
class SNILongName(SNIWrongName):
'''Send a server name indication with a long name'''
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_sni_hello('x'*500))
class SNIEmptyName(SNIWrongName):
'''Send a server name indication with an empty name'''
def test(self, sock):
logging.debug('Sending Client Hello...')
sock.write(self.make_sni_hello(''))