-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_security.py
More file actions
600 lines (505 loc) · 24.9 KB
/
http_security.py
File metadata and controls
600 lines (505 loc) · 24.9 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
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
593
594
595
596
597
598
599
600
import asyncio
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import socket
from logger import get_logger
from utils import ensure_url_has_protocol, make_http_request
logger = get_logger('http_security')
class HttpSecurityChecker:
"""Class for checking HTTP security headers and configurations."""
def __init__(self):
"""Initialize the HTTP security checker."""
# Security headers to check
self.security_headers = {
'Strict-Transport-Security': {
'description': 'HTTP Strict Transport Security (HSTS)',
'recommendation': 'Add HSTS header with a long max-age value'
},
'Content-Security-Policy': {
'description': 'Content Security Policy (CSP)',
'recommendation': 'Implement a strict CSP to prevent XSS attacks'
},
'X-Content-Type-Options': {
'description': 'X-Content-Type-Options',
'recommendation': 'Set X-Content-Type-Options to nosniff'
},
'X-Frame-Options': {
'description': 'X-Frame-Options',
'recommendation': 'Set X-Frame-Options to DENY or SAMEORIGIN'
},
'X-XSS-Protection': {
'description': 'X-XSS-Protection',
'recommendation': 'Set X-XSS-Protection to 1; mode=block'
},
'Referrer-Policy': {
'description': 'Referrer Policy',
'recommendation': 'Set a restrictive Referrer-Policy'
},
'Permissions-Policy': {
'description': 'Permissions Policy',
'recommendation': 'Implement a restrictive Permissions-Policy'
}
}
async def check_http_headers(self, url):
"""
Check for the presence of specific HTTP security headers.
Args:
url (str): The target URL.
Returns:
dict: A dictionary containing the status of headers and their vulnerability status.
"""
url = ensure_url_has_protocol(url)
logger.info(f"Checking HTTP security headers for {url}")
try:
response = await make_http_request(url)
headers = response.headers
results = {
'headers_present': {},
'headers_missing': {},
'recommendations': []
}
# Check each security header
for header, info in self.security_headers.items():
if header in headers:
results['headers_present'][header] = {
'value': headers[header],
'description': info['description']
}
else:
results['headers_missing'][header] = {
'description': info['description'],
'recommendation': info['recommendation']
}
results['recommendations'].append(info['recommendation'])
# Determine overall security status
if len(results['headers_missing']) > 3:
results['security_status'] = 'Vulnerable'
results['severity'] = 'high' if 'Content-Security-Policy' in results['headers_missing'] else 'medium'
elif len(results['headers_missing']) > 0:
results['security_status'] = 'Potentially Vulnerable'
results['severity'] = 'medium'
else:
results['security_status'] = 'Secure'
results['severity'] = 'low'
logger.info(f"HTTP header check completed for {url}: {results['security_status']}")
return results
except Exception as e:
logger.error(f"Error checking HTTP headers for {url}: {e}")
return {
'error': str(e),
'security_status': 'Error',
'severity': 'unknown'
}
async def check_client_access_policies(self, url):
"""
Check for client access policies (crossdomain.xml and clientaccesspolicy.xml).
Args:
url (str): The target URL.
Returns:
dict: Results of the client access policy check.
"""
url = ensure_url_has_protocol(url)
base_url = f"{urlparse(url).scheme}://{urlparse(url).netloc}"
logger.info(f"Checking client access policies for {base_url}")
results = {
'crossdomain_xml': {'exists': False, 'content': None, 'issues': []},
'clientaccesspolicy_xml': {'exists': False, 'content': None, 'issues': []},
'recommendations': []
}
try:
# Check crossdomain.xml
crossdomain_url = f"{base_url}/crossdomain.xml"
try:
response = await make_http_request(crossdomain_url)
if response.status_code == 200:
content = response.text
results['crossdomain_xml']['exists'] = True
results['crossdomain_xml']['content'] = content
# Check for overly permissive policies
if '<allow-access-from domain="*"' in content:
results['crossdomain_xml']['issues'].append('Allows access from any domain')
results['recommendations'].append('Restrict crossdomain.xml to only allow specific domains')
except Exception as e:
logger.debug(f"Error checking crossdomain.xml: {e}")
# Check clientaccesspolicy.xml
clientaccess_url = f"{base_url}/clientaccesspolicy.xml"
try:
response = await make_http_request(clientaccess_url)
if response.status_code == 200:
content = response.text
results['clientaccesspolicy_xml']['exists'] = True
results['clientaccesspolicy_xml']['content'] = content
# Check for overly permissive policies
if '<domain uri="*"' in content:
results['clientaccesspolicy_xml']['issues'].append('Allows access from any domain')
results['recommendations'].append('Restrict clientaccesspolicy.xml to only allow specific domains')
except Exception as e:
logger.debug(f"Error checking clientaccesspolicy.xml: {e}")
# Determine overall security status
if (results['crossdomain_xml']['exists'] and results['crossdomain_xml']['issues']) or \
(results['clientaccesspolicy_xml']['exists'] and results['clientaccesspolicy_xml']['issues']):
results['security_status'] = 'Vulnerable'
results['severity'] = 'medium'
elif results['crossdomain_xml']['exists'] or results['clientaccesspolicy_xml']['exists']:
results['security_status'] = 'Potentially Vulnerable'
results['severity'] = 'low'
else:
results['security_status'] = 'Secure'
results['severity'] = 'info'
logger.info(f"Client access policy check completed for {base_url}: {results['security_status']}")
return results
except Exception as e:
logger.error(f"Error checking client access policies for {base_url}: {e}")
return {
'error': str(e),
'security_status': 'Error',
'severity': 'unknown'
}
async def check_security_txt(self, url):
"""
Check for security.txt file.
Args:
url (str): The target URL.
Returns:
dict: Results of the security.txt check.
"""
url = ensure_url_has_protocol(url)
base_url = f"{urlparse(url).scheme}://{urlparse(url).netloc}"
logger.info(f"Checking security.txt for {base_url}")
results = {
'exists': False,
'content': None,
'recommendations': []
}
try:
# Check /.well-known/security.txt (preferred location)
wellknown_url = f"{base_url}/.well-known/security.txt"
try:
response = await make_http_request(wellknown_url)
if response.status_code == 200:
results['exists'] = True
results['location'] = wellknown_url
results['content'] = response.text
except Exception:
pass
# If not found in .well-known, check at root
if not results['exists']:
root_url = f"{base_url}/security.txt"
try:
response = await make_http_request(root_url)
if response.status_code == 200:
results['exists'] = True
results['location'] = root_url
results['content'] = response.text
except Exception:
pass
# Add recommendations if security.txt doesn't exist
if not results['exists']:
results['recommendations'].append('Create a security.txt file in the /.well-known/ directory')
results['recommendations'].append('Include contact information for security researchers')
results['security_status'] = 'Missing'
results['severity'] = 'low'
else:
results['security_status'] = 'Present'
results['severity'] = 'info'
logger.info(f"Security.txt check completed for {base_url}: {results['security_status']}")
return results
except Exception as e:
logger.error(f"Error checking security.txt for {base_url}: {e}")
return {
'error': str(e),
'security_status': 'Error',
'severity': 'unknown'
}
async def check_robots_txt(self, url):
"""
Check for robots.txt file and analyze its content.
Args:
url (str): The target URL.
Returns:
dict: Results of the robots.txt check.
"""
url = ensure_url_has_protocol(url)
base_url = f"{urlparse(url).scheme}://{urlparse(url).netloc}"
robots_url = f"{base_url}/robots.txt"
logger.info(f"Checking robots.txt for {base_url}")
results = {
'exists': False,
'content': None,
'disallowed_paths': [],
'sensitive_paths': [],
'recommendations': []
}
sensitive_paths = [
'/admin', '/login', '/wp-admin', '/administrator', '/phpmyadmin',
'/config', '/backup', '/db', '/database', '/api', '/private',
'/secret', '/secure', '/test', '/dev', '/staging', '/beta'
]
try:
response = await make_http_request(robots_url)
if response.status_code == 200:
content = response.text
results['exists'] = True
results['content'] = content
# Parse disallowed paths
for line in content.split('\n'):
if line.lower().startswith('disallow:'):
path = line.split(':', 1)[1].strip()
if path:
results['disallowed_paths'].append(path)
# Check if disallowed path contains sensitive information
for sensitive_path in sensitive_paths:
if sensitive_path in path.lower():
results['sensitive_paths'].append({
'path': path,
'reason': f'Contains sensitive pattern: {sensitive_path}'
})
# Add recommendations based on findings
if results['sensitive_paths']:
results['recommendations'].append('Review robots.txt for sensitive paths that could reveal system information')
results['security_status'] = 'Potentially Vulnerable'
results['severity'] = 'medium'
else:
results['security_status'] = 'Secure'
results['severity'] = 'info'
else:
results['security_status'] = 'Missing'
results['severity'] = 'info'
results['recommendations'].append('Consider adding a robots.txt file to control search engine crawling')
logger.info(f"Robots.txt check completed for {base_url}: {results['security_status']}")
return results
except Exception as e:
logger.error(f"Error checking robots.txt for {base_url}: {e}")
return {
'error': str(e),
'security_status': 'Error',
'severity': 'unknown'
}
async def check_and_store_csp(self, url):
"""
Check if CSP header is present and return the status.
Args:
url (str): The target URL.
Returns:
dict: CSP check results.
"""
url = ensure_url_has_protocol(url)
logger.info(f"Checking CSP for {url}")
results = {
'exists': False,
'value': None,
'directives': {},
'issues': [],
'recommendations': []
}
try:
response = await make_http_request(url)
headers = response.headers
# Check for CSP header (both standard and legacy)
csp_header = None
if 'Content-Security-Policy' in headers:
csp_header = headers['Content-Security-Policy']
results['header_name'] = 'Content-Security-Policy'
elif 'X-Content-Security-Policy' in headers:
csp_header = headers['X-Content-Security-Policy']
results['header_name'] = 'X-Content-Security-Policy'
results['issues'].append('Using deprecated X-Content-Security-Policy header')
results['recommendations'].append('Use the standard Content-Security-Policy header instead')
if csp_header:
results['exists'] = True
results['value'] = csp_header
# Parse CSP directives
directives = csp_header.split(';')
for directive in directives:
directive = directive.strip()
if not directive:
continue
parts = directive.split(' ', 1)
if len(parts) == 1:
# Directive without values
results['directives'][parts[0]] = []
else:
# Directive with values
directive_name = parts[0]
directive_values = parts[1].strip().split(' ')
results['directives'][directive_name] = directive_values
# Check for unsafe CSP configurations
if 'default-src' not in results['directives'] and 'script-src' not in results['directives']:
results['issues'].append('Missing default-src or script-src directive')
results['recommendations'].append('Add default-src or script-src directive to restrict script execution')
for directive, values in results['directives'].items():
if "'unsafe-inline'" in values:
results['issues'].append(f"'{directive}' allows unsafe inline scripts/styles")
results['recommendations'].append(f"Remove 'unsafe-inline' from {directive} directive")
if "'unsafe-eval'" in values:
results['issues'].append(f"'{directive}' allows unsafe eval()")
results['recommendations'].append(f"Remove 'unsafe-eval' from {directive} directive")
if '*' in values:
results['issues'].append(f"'{directive}' uses wildcard (*) source")
results['recommendations'].append(f"Replace wildcard in {directive} with specific domains")
# Determine security status based on issues
if results['issues']:
results['security_status'] = 'Potentially Vulnerable'
results['severity'] = 'medium'
else:
results['security_status'] = 'Secure'
results['severity'] = 'info'
else:
results['security_status'] = 'Missing'
results['severity'] = 'high'
results['recommendations'].append('Implement a Content Security Policy to prevent XSS attacks')
logger.info(f"CSP check completed for {url}: {results['security_status']}")
return results
except Exception as e:
logger.error(f"Error checking CSP for {url}: {e}")
return {
'error': str(e),
'security_status': 'Error',
'severity': 'unknown'
}
async def check_clickjacking_vulnerability(self, url):
"""
Check if a URL is vulnerable to clickjacking.
Args:
url (str): The target URL.
Returns:
dict: Clickjacking vulnerability check results.
"""
url = ensure_url_has_protocol(url)
logger.info(f"Checking clickjacking vulnerability for {url}")
results = {
'vulnerable': False,
'protection_headers': {},
'recommendations': []
}
try:
response = await make_http_request(url)
headers = response.headers
# Check for X-Frame-Options header
if 'X-Frame-Options' in headers:
value = headers['X-Frame-Options'].upper()
results['protection_headers']['X-Frame-Options'] = value
if value not in ['DENY', 'SAMEORIGIN']:
results['vulnerable'] = True
results['recommendations'].append('Set X-Frame-Options to DENY or SAMEORIGIN')
else:
results['vulnerable'] = True
results['recommendations'].append('Add X-Frame-Options header with DENY or SAMEORIGIN value')
# Check for CSP frame-ancestors directive
if 'Content-Security-Policy' in headers:
csp = headers['Content-Security-Policy']
results['protection_headers']['Content-Security-Policy'] = csp
if 'frame-ancestors' in csp:
# Extract frame-ancestors directive
for directive in csp.split(';'):
directive = directive.strip()
if directive.startswith('frame-ancestors'):
values = directive.split(' ', 1)[1].strip() if ' ' in directive else ''
if values in ['none', "'none'"]:
results['vulnerable'] = False
elif values in ["'self'"] or not values:
# 'self' is equivalent to SAMEORIGIN
results['vulnerable'] = False
elif '*' in values:
results['vulnerable'] = True
results['recommendations'].append('Remove wildcard (*) from frame-ancestors directive')
else:
# No frame-ancestors directive in CSP
if 'X-Frame-Options' not in headers:
results['recommendations'].append('Add frame-ancestors directive to CSP')
# Set final vulnerability status and severity
if results['vulnerable']:
results['security_status'] = 'Vulnerable'
results['severity'] = 'medium'
# Create a PoC recommendation
results['recommendations'].append('Implement proper frame protection to prevent clickjacking attacks')
else:
results['security_status'] = 'Secure'
results['severity'] = 'info'
logger.info(f"Clickjacking check completed for {url}: {results['security_status']}")
return results
except Exception as e:
logger.error(f"Error checking clickjacking vulnerability for {url}: {e}")
return {
'error': str(e),
'security_status': 'Error',
'severity': 'unknown'
}
async def get_cookie_details(self, url):
"""
Get cookie details and security attributes.
Args:
url (str): The target URL.
Returns:
dict: Cookie details and security analysis.
"""
url = ensure_url_has_protocol(url)
logger.info(f"Checking cookies for {url}")
results = {
'cookies': [],
'secure_cookies': 0,
'insecure_cookies': 0,
'httponly_cookies': 0,
'samesite_cookies': 0,
'recommendations': []
}
try:
response = requests.get(url, allow_redirects=True)
cookies = response.cookies
for cookie in cookies:
cookie_info = {
'name': cookie.name,
'domain': cookie.domain,
'path': cookie.path,
'secure': cookie.secure,
'httponly': cookie.has_nonstandard_attr('HttpOnly'),
'samesite': None,
'expires': cookie.expires
}
# Check for SameSite attribute
for attr in cookie._rest.keys():
if attr.lower() == 'samesite':
cookie_info['samesite'] = cookie._rest[attr]
# Count secure vs insecure cookies
if cookie_info['secure']:
results['secure_cookies'] += 1
else:
results['insecure_cookies'] += 1
results['recommendations'].append(f"Set 'Secure' flag for cookie: {cookie.name}")
# Count HttpOnly cookies
if cookie_info['httponly']:
results['httponly_cookies'] += 1
else:
results['recommendations'].append(f"Set 'HttpOnly' flag for cookie: {cookie.name}")
# Count SameSite cookies
if cookie_info['samesite']:
results['samesite_cookies'] += 1
else:
results['recommendations'].append(f"Set 'SameSite' attribute for cookie: {cookie.name}")
results['cookies'].append(cookie_info)
# Set security status based on findings
if results['cookies']:
if results['insecure_cookies'] > 0:
results['security_status'] = 'Vulnerable'
results['severity'] = 'medium'
elif results['httponly_cookies'] < len(results['cookies']) or results['samesite_cookies'] < len(results['cookies']):
results['security_status'] = 'Potentially Vulnerable'
results['severity'] = 'low'
else:
results['security_status'] = 'Secure'
results['severity'] = 'info'
else:
results['security_status'] = 'No Cookies'
results['severity'] = 'info'
logger.info(f"Cookie check completed for {url}: {results['security_status']}")
return results
except Exception as e:
logger.error(f"Error checking cookies for {url}: {e}")
return {
'error': str(e),
'security_status': 'Error',
'severity': 'unknown'
}
# Create a global instance
http_security = HttpSecurityChecker()