-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssl_scan_sock.py
286 lines (241 loc) · 8.88 KB
/
ssl_scan_sock.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
# This file is part of the Perspectives Notary Server
#
# Copyright (C) 2011 Dan Wendlandt
# Copyright (C) 2011 Ondrej Mikle, CZ.NIC Labs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import socket
import struct
import time
import binascii
import traceback
import sys
import errno
import notary_common
import config
import db
# This is a lightweight version of SSL scanning that does not invoke openssl
# at all. Instead, it executes the initial steps of the SSL handshake directly
# using a TCP socket and parses the data itself
SLEEP_LEN_SEC = 0.2
class SSLException(RuntimeError):
pass
class SSLFormatException(SSLException):
pass
class SSLScanTimeoutException(SSLException):
pass
class SSLAlertException(SSLException):
def __init__(self,value):
self.value = value
def read_data(s,data_len, timeout_sec):
buf_str = ""
start_time = time.time()
while(True):
try:
buf_str += s.recv(data_len - len(buf_str))
if len(buf_str) == data_len:
break
except socket.error, e:
if not is_nonblocking_exception(e):
raise e
if time.time() - start_time > timeout_sec:
raise SSLScanTimeoutException("timeout in read_data")
time.sleep(SLEEP_LEN_SEC)
return buf_str
def send_data(s, data, timeout_sec):
start_time = time.time()
while(True):
try:
s.send(data)
break
except socket.error, e:
if is_nonblocking_exception(e):
if time.time() - start_time > timeout_sec:
raise SSLScanTimeoutException("timeout in send_data")
time.sleep(SLEEP_LEN_SEC)
else:
raise e
def is_nonblocking_exception(e):
try:
return e.args[0] == errno.EAGAIN or \
e.args[0] == errno.EINPROGRESS or \
e.args[0] == errno.EALREADY
except:
return False
def do_connect(s, host, port, timeout_sec):
start_time = time.time()
while(True):
try:
s.connect((host, port))
break
except socket.error, e:
if e.args[0] == errno.EISCONN:
break
if is_nonblocking_exception(e):
if time.time() - start_time > timeout_sec:
raise SSLScanTimeoutException("timeout in do_connect")
time.sleep(SLEEP_LEN_SEC)
else:
raise e
def read_record(sock,timeout_sec):
"""Reads one record from SSL/TLS record layer.
@param sock: socket to read from
@param timeout_sec: read timeout
@returns: tuple(byte record_content_type, str inner_data_of_record)
"""
rec_start = read_data(sock,5,timeout_sec)
if len(rec_start) != 5:
raise SSLFormatException("Error: unable to read start of record")
(rec_type, ssl_version, tls_version, rec_length) = struct.unpack('!BBBH',rec_start)
rest_of_rec = read_data(sock,rec_length,timeout_sec)
if len(rest_of_rec) != rec_length:
raise SSLFormatException("Error: unable to read full record")
return (rec_type, rest_of_rec)
def get_all_handshake_protocols(rec_data):
"""Extract handshake protocols from inside of SSL/TLS handshake (22)
message.
@param rec_data: data from tcp stream
@returns: list of tuples (byte handshake_type, str handshake_proto_data)
"""
protos = []
while len(rec_data) > 0:
#unpack handshake type and 24-bit length of inner handshake proto data
t, b1,b2,b3 = struct.unpack('!BBBB',rec_data[0:4])
l = (b1 << 16) | (b2 << 8) | b3
protos.append((t, rec_data[4: 4 + l]))
rec_data = rec_data[4 + l:]
return protos
# rfc 2246 says the server cert if the first one
def get_server_cert_from_protocol(proto_data):
"""Extract site certificate and return observed certificate.
@param proto_data: inner data from hanshake type 11 (certificates) protocol
(after the "length" field)
@return: notary_common.Observation object with cert and fingerprints
"""
certs = []
proto_data = proto_data[3:] # get rid of 3-bytes describing length of all certs
while proto_data:
(b1,b2,b3) = struct.unpack("!BBB",proto_data[0:3]) #length of cert
cert_len = (b1 << 16) | (b2 << 8) | b3
cert = proto_data[3: 3 + cert_len]
if cert_len != len(cert):
raise SSLFormatException("Can't read full handshake protocol")
certs.append(cert)
proto_data = proto_data[3 + cert_len:]
return notary_common.Observation(certs)
def attempt_observation_for_service(service_id, timeout_sec):
"""Run observation for service
@param service_id: requested service as notary_common.ObservedServer
@param timeout_sec: scanning timeout
@return: notary_common.Observation with cert and fingerprints of service
@raise SSLScanTimeoutException: on timeout
"""
host, port = service_id.host, service_id.port
# if we want to try SNI, do such a scan but if that
# scan fails with an SSL alert, retry with a non SNI request
if config.Config.use_sni and host[-1:].isalpha():
try:
return run_scan(host, port, timeout_sec, True)
except SSLAlertException:
pass
return run_scan(host, port, timeout_sec, False)
def run_scan(dns, port, timeout_sec, sni_query):
"""Run SSL/TLS scan on given host.
@param dns: hostname
@param port: port
@param timeout_sec: timeout in seconds
@param sni_query: True iff Server Name Indication extension should be used,
use only if hostname is specified via FQDN and not just IP
@return: observation as instance of notary_common.Observation
@raise SSLScanTimeoutException: on timeout
"""
try:
if sni_query:
# only do SNI query for DNS names, per RFC
client_hello_hex = get_sni_client_hello(dns)
else:
client_hello_hex = get_standard_client_hello()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
do_connect(sock, dns, int(port),timeout_sec)
client_hello = binascii.a2b_hex(client_hello_hex)
send_data(sock, client_hello,timeout_sec)
fp = None
start_time = time.time()
while not fp:
t,rec_data = read_record(sock,timeout_sec)
if t == 22: # handshake message
all_hs_protos = get_all_handshake_protocols(rec_data)
for p in all_hs_protos:
if p[0] == 11:
# server certificate message
fp = get_server_cert_from_protocol(p[1])
break
elif t == 21: # alert message
raise SSLAlertException(rec_data)
if not fp:
time.sleep(SLEEP_LEN_SEC)
if time.time() - start_time > timeout_sec:
break
try:
sock.shutdown(socket.SHUT_RDWR)
except:
pass
if not fp:
raise SSLScanTimeoutException("timeout waiting for data")
sock.close()
return fp
# make sure we always close the socket, but still propogate the exception
except Exception, e:
try:
sock.close()
except:
pass
raise e
def get_standard_client_hello():
"""SSLv2 encoded client hello"""
return "8077010301004e0000002000003900003800003500001600001300000a0700c000003300003200002f0300800000050000040100800000150000120000090600400000140000110000080000060400800000030200800000ff9c82ce1e4bc89df2c726b7cebe211ef80a611945d140834eede5674b597be487"
def get_twobyte_hexstr(intval):
"""Return packed value of intval as hex string of two bytes, network order."""
return binascii.b2a_hex(struct.pack("!H", intval))
def get_threebyte_hexstr(intval):
"""Return packed value of intval as hex string of three bytes, network order."""
return binascii.b2a_hex(struct.pack("!I", intval)[1:])
def get_hostname_extension(hostname):
"""SNI extension for TLSv1 client hello"""
hex_hostname = binascii.b2a_hex(hostname)
hn_len = len(hostname)
return "0000" + get_twobyte_hexstr(hn_len + 5) + get_twobyte_hexstr(hn_len + 3) + \
"00" + get_twobyte_hexstr(hn_len) + hex_hostname
def get_sni_client_hello(hostname):
"""TLSv1 client hello with SNI extension"""
hn_extension = get_hostname_extension(hostname)
all_extensions = hn_extension
the_rest = "03014d786109055e4736b93b63c371507f824c2d0f05a25b2d54b6b52a1e43c2a52c00002800390038003500160013000a00330032002f000500040015001200090014001100080006000300ff020100" + get_twobyte_hexstr(len(all_extensions)/2) + all_extensions
proto_len = (len(the_rest) / 2)
rec_len = proto_len + 4
return "160301" + get_twobyte_hexstr(rec_len) + "01" + get_threebyte_hexstr(proto_len) + the_rest
if __name__ == "__main__":
if len(sys.argv) != 3:
print >> sys.stderr, "ERROR: usage: <service-id> <config_file>"
exit(1)
config.config_initialize(sys.argv[2])
db.db_initialize(config.Config)
service_id = notary_common.ObservedServer(sys.argv[1])
try:
fp = attempt_observation_for_service(service_id, 10)
print "Successful scan complete: '%s' has key '%s' " % (service_id,fp)
notary_common.report_observation(service_id, fp)
except:
print "Error scanning for %s" % service_id
traceback.print_exc(file=sys.stdout)