forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_processor.py
More file actions
402 lines (333 loc) · 14.1 KB
/
Copy pathweb_processor.py
File metadata and controls
402 lines (333 loc) · 14.1 KB
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
import os
import json
import datetime
from urllib.parse import urlparse
current_directory = os.path.dirname(os.path.realpath(__file__))
# Opening JSON file from:
# https://raw.githubusercontent.com/CybercentreCanada/ITSP.40.062/main/transport-layer-security/tls-guidance.json
guidance_file = open(f"{current_directory}/tls-guidance.json")
guidance = json.load(guidance_file)
def process_tls_results(tls_results):
neutral_tags = []
positive_tags = []
negative_tags = []
accepted_cipher_suites = {}
accepted_elliptic_curves = []
ssl_status = "info"
certificate_status = "info"
protocol_status = "info"
cipher_status = "info"
curve_status = "info"
if tls_results.get("error"):
processed_tags = {
"neutral_tags": neutral_tags,
"positive_tags": positive_tags,
"negative_tags": negative_tags,
"accepted_cipher_suites": accepted_cipher_suites,
"accepted_elliptic_curves": accepted_elliptic_curves,
"ssl_status": ssl_status,
"protocol_status": protocol_status,
"cipher_status": cipher_status,
"curve_status": curve_status,
"certificate_status": certificate_status,
}
return processed_tags
for protocol in tls_results["accepted_cipher_suites"].keys():
accepted_cipher_suites[protocol] = []
for cipher_suite in tls_results["accepted_cipher_suites"][protocol]:
if "RC4" in cipher_suite:
negative_tags.append("ssl3")
if "3DES" in cipher_suite:
negative_tags.append("ssl4")
if cipher_suite in (
guidance["ciphers"]["1.2"]["recommended"]
+ guidance["ciphers"]["1.3"]["recommended"]
):
strength = "strong"
elif cipher_suite in (
guidance["ciphers"]["1.2"]["sufficient"]
+ guidance["ciphers"]["1.3"]["sufficient"]
):
strength = "acceptable"
else:
strength = "weak"
negative_tags.append("ssl6")
accepted_cipher_suites[protocol].append({"name": cipher_suite, "strength": strength})
weak_curve = False
for curve in tls_results["accepted_elliptic_curves"]:
if curve.lower() in guidance["curves"]["recommended"]:
strength = "strong"
elif curve.lower() in guidance["curves"]["sufficient"]:
strength = "acceptable"
else:
strength = "weak"
weak_curve = True
negative_tags.append("ssl17")
accepted_elliptic_curves.append({"name": curve, "strength": strength})
try:
signature_algorithm = tls_results["certificate_chain_info"]["certificate_chain"][0][
"signature_hash_algorithm"]
except ValueError:
signature_algorithm = None
except TypeError:
signature_algorithm = None
try:
if tls_results["certificate_chain_info"]["certificate_chain"][0]["expired_cert"]:
negative_tags.append("ssl10")
except TypeError:
pass
try:
if tls_results["certificate_chain_info"]["certificate_chain"][0]["self_signed_cert"]:
negative_tags.append("ssl11")
except TypeError:
pass
try:
if tls_results["certificate_chain_info"]["certificate_chain"][0]["cert_revoked"]:
negative_tags.append("ssl12")
except TypeError:
pass
try:
if tls_results["certificate_chain_info"]["certificate_chain"][0]["cert_revoked_status"] is None:
neutral_tags.append("ssl13")
except TypeError:
pass
try:
if tls_results["certificate_chain_info"]["bad_hostname"]:
negative_tags.append("ssl15")
except TypeError:
pass
try:
if len(
[res for res in tls_results["certificate_chain_info"]["path_validation_results"] if res is False]) > 0:
negative_tags.append("ssl16")
except TypeError:
pass
if signature_algorithm is not None:
for algorithm in (
guidance["signature_algorithms"]["recommended"]
+ guidance["signature_algorithms"]["sufficient"]
):
if signature_algorithm.lower() in algorithm:
# positive_tags.append("ssl5")
break
if tls_results["is_vulnerable_to_heartbleed"]:
negative_tags.append("ssl7")
if tls_results["is_vulnerable_to_ccs_injection"]:
negative_tags.append("ssl8")
# remove duplicate tags
positive_tags = list(set(positive_tags))
neutral_tags = list(set(neutral_tags))
negative_tags = list(set(negative_tags))
# protocol status
unaccepted_tls_protocols = [
"ssl_2_0_cipher_suites",
"ssl_3_0_cipher_suites",
"tls_1_0_cipher_suites",
"tls_1_1_cipher_suites"
]
# ssl status
if len(negative_tags) > 0 or "ssl5" not in positive_tags:
ssl_status = "fail"
else:
ssl_status = "pass"
# certificate status
if any(tag in negative_tags for tag in ["ssl5", "ssl10", "ssl11", "ssl12", "ssl15", "ssl16"]):
certificate_status = "fail"
else:
certificate_status = "pass"
positive_tags.append("ssl21")
# get protocol status
protocol_status = "pass"
for suite_list in unaccepted_tls_protocols:
if len(accepted_cipher_suites[suite_list]) > 0:
protocol_status = "fail"
negative_tags.append("ssl18")
if protocol_status == "pass":
positive_tags.append("ssl18")
# get cipher status
if "ssl6" in negative_tags:
cipher_status = "fail"
else:
cipher_status = "pass"
positive_tags.append("ssl19")
# get curve status
if weak_curve:
curve_status = "fail"
else:
curve_status = "pass"
positive_tags.append("ssl20")
processed_tags = {
"neutral_tags": neutral_tags,
"positive_tags": positive_tags,
"negative_tags": negative_tags,
"accepted_cipher_suites": accepted_cipher_suites,
"accepted_elliptic_curves": accepted_elliptic_curves,
"ssl_status": ssl_status,
"protocol_status": protocol_status,
"cipher_status": cipher_status,
"curve_status": curve_status,
"certificate_status": certificate_status
}
return processed_tags
def process_connection_results(connection_results):
positive_tags = []
neutral_tags = []
negative_tags = []
http_connections = connection_results["http_chain_result"]["connections"]
https_connections = connection_results["https_chain_result"]["connections"]
http_live = not http_connections[0]["error"]
https_live = not https_connections[0]["error"]
hsts_status = None
https_status = None
http_immediately_upgrades = None
http_eventually_upgrades = None
https_immediately_downgrades = None
https_eventually_downgrades = None
hsts_parsed = None
hsts = None
# set hsts status defaults
if http_live or https_live:
# default "fail" if either endpoint alive
hsts_status = "fail"
elif not http_live and not https_live:
# status should be "info" if neither endpoint live
hsts_status = "info"
def check_https_downgrades(connections):
for connection in connections:
if connection["scheme"] == "http":
return True
return False
# check HTTP properties
if http_live:
http_immediately_upgrades = None
http_eventually_upgrades = None
try:
# find index of first https upgrade
first_https_index = list(conn["scheme"] == "https" for conn in http_connections).index(True)
# check if HTTP connection is immediately upgraded (redirected) to HTTPS
if first_https_index == 1:
http_immediately_upgrades = True
# check if HTTP connection eventually is upgraded (redirected) to HTTPS
if first_https_index >= 1:
http_eventually_upgrades = True
except IndexError:
pass
except ValueError:
pass
# check redirect is for same hostname (to ensure HSTS is applied)
redirect_url = http_connections[0]["connection"]["redirect_to"]
if redirect_url is not None:
parsed_url = urlparse(redirect_url)
redirect_url = f'{parsed_url.scheme}://{parsed_url.hostname}'
if redirect_url != f'https://{connection_results["domain"]}':
negative_tags.append("https14")
else:
negative_tags.append("https14")
# check HTTPS properties
if https_live:
# check if https chain immediately downgrades connection
https_immediately_downgrades = check_https_downgrades(https_connections[:1])
# check if https chain eventually downgrades connection
https_eventually_downgrades = check_https_downgrades(https_connections)
# check HSTS header
try:
for header, value in https_connections[0]["connection"]["headers"].items():
if header.lower() == "strict-transport-security":
hsts = value
break
except KeyError:
pass
if hsts:
max_age = None
include_subdomains = False
preload = False
directives = [directive.strip() for directive in hsts.split(";") if len(directive) > 0]
for directive in directives:
match directive:
case d if directive.startswith("max-age="):
max_age = int(d.split("=")[1])
case _ if directive == "includeSubDomains":
include_subdomains = True
case _ if directive.startswith("preload"):
preload = True
hsts_parsed = {
"max_age": max_age,
"include_subdomains": include_subdomains,
"preload": preload
}
if hsts and isinstance(max_age, int) and max_age > 0 and "https14" not in negative_tags:
hsts_status = "pass"
else:
hsts_status = "fail"
http_down_or_redirect = not http_live or http_immediately_upgrades
# process tags
if https_eventually_downgrades or https_immediately_downgrades:
negative_tags.append("https3")
if http_live and not https_live:
negative_tags.append("https6")
if https_live and http_live and not (http_immediately_upgrades or http_eventually_upgrades):
negative_tags.append("https7")
if https_live and http_live and not http_immediately_upgrades and http_eventually_upgrades:
negative_tags.append("https8")
if https_live and not hsts:
negative_tags.append("https9")
try:
if hsts_parsed["preload"]:
pass
except TypeError:
pass
if not http_live and not https_live:
neutral_tags.append("https13")
# calculate status
fail_tags = ["https3", "https6", "https7", "https8"]
if "https13" in neutral_tags:
# no live endpoints, give info status
https_status = "info"
elif any(tag in negative_tags for tag in fail_tags):
https_status = "fail"
else:
https_status = "pass"
positive_tags.append("https15")
if hsts_status == "pass":
positive_tags.append("https16")
# merge results
processed_connection_results = {
"neutral_tags": neutral_tags,
"positive_tags": positive_tags,
"negative_tags": negative_tags,
"hsts_status": hsts_status,
"https_status": https_status,
"http_live": http_live,
"https_live": https_live,
"http_immediately_upgrades": http_immediately_upgrades,
"http_eventually_upgrades": http_eventually_upgrades,
"https_immediately_downgrades": https_immediately_downgrades,
"https_eventually_downgrades": https_eventually_downgrades,
"hsts_parsed": hsts_parsed
} | connection_results
return processed_connection_results
def process_results(results):
processed_tls_results = process_tls_results(results["tls_result"])
tls_result = {
"domain": results["tls_result"]["request_domain"],
"ip_address": results["tls_result"]["request_ip_address"],
"server_location": results["tls_result"]["server_location"],
"certificate_chain_info": results["tls_result"]["certificate_chain_info"],
"supports_ecdh_key_exchange": results["tls_result"].get("supports_ecdh_key_exchange", None),
"heartbleed_vulnerable": results["tls_result"].get("is_vulnerable_to_heartbleed", None),
"ccs_injection_vulnerable": results["tls_result"].get("is_vulnerable_to_ccs_injection", None),
"robot_vulnerable": results["tls_result"].get("is_vulnerable_to_robot", None),
"accepted_cipher_suites": processed_tls_results["accepted_cipher_suites"],
"accepted_elliptic_curves": processed_tls_results["accepted_elliptic_curves"],
"positive_tags": processed_tls_results["positive_tags"],
"neutral_tags": processed_tls_results["neutral_tags"],
"negative_tags": processed_tls_results["negative_tags"],
"ssl_status": processed_tls_results["ssl_status"],
"certificate_status": processed_tls_results["certificate_status"],
"protocol_status": processed_tls_results["protocol_status"],
"cipher_status": processed_tls_results["cipher_status"],
"curve_status": processed_tls_results["curve_status"]
}
processed_connection_results = process_connection_results(results["chain_result"])
timestamp = results.get("timestamp")
return {"tls_result": tls_result, "connection_results": processed_connection_results, "timestamp": timestamp}