-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreport.py
165 lines (140 loc) · 4.42 KB
/
report.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
#!/usr/bin/python
"""
GitHub repo : https://github.com/benyaminsalimi/secure-headers
"""
import getopt
import requests
import sys
import json
import datetime
import os
def report(url):
"""
function to generate a report for each url
:param url:
:return: report list object
"""
# TODO: add simple url validity
try:
report = requests.get(url)
except:
print("url is wrong or unreachable")
return False
if report.status_code != 200:
print('status_code: ', report.status_code)
return False
# making report list
output = {}
try:
output['X-Xss-Protection'] = report.headers['X-Xss-Protection']
except:
output['X-Xss-Protection'] = None
try:
output['X-Content-Type-Options'] = report.headers['X-Content-Type-Options']
except:
output['X-Content-Type-Options'] = None
try:
output['Content-Security-Policy'] = report.headers['Content-Security-Policy']
except:
output['Content-Security-Policy'] = None
try:
output['Set-Cookie'] = report.headers['Set-Cookie']
except:
output['Set-Cookie'] = None
try:
output['Strict-Transport-Security'] = report.headers['Strict-Transport-Security']
except:
output['Strict-Transport-Security'] = None
try:
output['Feature-Policy'] = report.headers['Feature-Policy']
except:
output['Feature-Policy'] = None
try:
output['X-Frame-Options'] = report.headers['X-Frame-Options']
except:
output['X-Frame-Options'] = None
try:
output['Referrer-Policy'] = report.headers['Referrer-Policy']
except:
output['Referrer-Policy'] = None
try:
output['Clear-Site-Data'] = report.headers['Clear-Site-Data']
except:
output['Clear-Site-Data'] = None
site = {}
site[url] = output
return site
def json_report(url, filename):
"""
function to write a url report to filename.json file
:param url:
:param filename:
:return:
"""
this_sit_report = report(url)
if this_sit_report is not False:
with open(filename, 'a') as outfile:
json.dump(this_sit_report, outfile)
outfile.write(',')
print('Success: ' + url)
else:
print('Fail: ' + url)
def main(argv):
url = ''
filename = ''
url_list = ''
help_text = "python report.py -u <url> -o <Output Filename> -l <Target List Filename> \n\n example:\n" \
" python report.py -u https://facebook.com -o FBreport \n python report.py -l input.text -o report"
try:
opts, args = getopt.getopt(argv, "hu:o:l:", ["url=", "output=", "list="])
except getopt.GetoptError:
print(help_text)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(help_text)
sys.exit()
elif opt in ("-u", '--url'):
url = arg
elif opt in ("-o", '--output'):
filename = arg
elif opt in ("-l", '--list'):
url_list = arg
# TODO: add CVS export
filename = filename + "_" + str(datetime.datetime.now().date()) + '.json'
if url_list != '':
with open(filename, 'a') as outfile:
outfile.write('[')
outfile.close()
urls = []
with open(url_list, "r") as url_file:
urls = url_file.readlines()
url_file.close()
urls = map(lambda s: s.strip(), urls)
for url in urls:
if url == '':
continue
json_report(url, filename)
with open(filename, 'rb+') as outfile:
# remove ',' at the end of file
outfile.seek(-1, os.SEEK_END)
outfile.truncate()
outfile.write(']')
outfile.close()
else:
json_report(url, filename)
with open(filename, 'rb+') as outfile:
# remove ',' at the end of file
outfile.seek(-1, os.SEEK_END)
outfile.truncate()
print("Reporting is Done \nYour output file : " + filename)
sys.exit(2)
"""
#TODO: add report only arg
info = 'more information about this header https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/'
for head in this_sit_report:
print(head + " : " + this_sit_report[head] if this_sit_report[head] is not None else head + " : " + info + head)
"""
if __name__ == "__main__":
print(__doc__)
main(sys.argv[1:])