Challenge: Extracting Security Headers From Website
Compito
Swipe to start coding
This task aims to create a function that extracts specific security headers from a given website. By analyzing the extracted headers, you can assess the web application's security posture. This includes understanding how the application is configured to handle security-related aspects.
Your task is to:
- Get a response from the corresponding website using the
.get()
function of therequests
library. - Extract all headers from the response using the
.headers
attribute of theresponse
object. - Return information about corresponding headers from the function using variable names (
csp_header
andhsts_header
).
Note
The objective of this assignment is to gain familiarity with the process of extracting headers. The analysis of header content falls outside the scope of this course.
Soluzione
99
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
import requests
def get_security_headers(url):
try:
# Send an HTTP GET request to the URL
response = requests.get(url)
# Get the headers from the response
headers = response.headers
# Extract Content-Security-Policy (CSP) header
csp_header = headers.get('Content-Security-Policy', 'CSP header not found')
# Extract Strict-Transport-Security (HSTS) header
hsts_header = headers.get('Strict-Transport-Security', 'HSTS header not found')
return {
'CSP': csp_header, 'HSTS': hsts_header
}
except requests.exceptions.RequestException as e:
return f'Error: {e}'
# Example usage:
url_to_check = 'https://www.kaggle.com'
security_headers = get_security_headers(url_to_check)
# Print the extracted headers
print(f'CSP Header: {security_headers["CSP"]}')
print(f'HSTS Header: {security_headers["HSTS"]}')
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 2. Capitolo 8
single
99
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
import requests
def get_security_headers(url):
try:
# Send an HTTP GET request to the URL
response = requests.___(url)
# Get the headers from the response
headers = response.___
# Extract Content-Security-Policy (CSP) header
csp_header = headers.get('Content-Security-Policy', 'CSP header not found')
# Extract Strict-Transport-Security (HSTS) header
hsts_header = headers.get('Strict-Transport-Security', 'HSTS header not found')
return {
'CSP': ___, 'HSTS': ___
}
except requests.exceptions.RequestException as e:
return f'Error: {e}'
# Example usage:
url_to_check = 'https://www.kaggle.com'
security_headers = get_security_headers(url_to_check)
# Print the extracted headers
print(f'CSP Header: {security_headers["CSP"]}')
print(f'HSTS Header: {security_headers["HSTS"]}')
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione