forked from dev4privacy/gdpr-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdpr_analyzer.py
More file actions
310 lines (247 loc) · 10.5 KB
/
gdpr_analyzer.py
File metadata and controls
310 lines (247 loc) · 10.5 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
#!/usr/bin/env python3.7
# coding: utf-8
import sys
import os
import argparse
import json
from splinter import Browser
from urllib.parse import urlparse
import requests
from requests.exceptions import ConnectionError, HTTPError
import urllib3
import platform
from mozprofile import FirefoxProfile
import glob
import sqlite3
import shutil
from modules.crypto.crypto import TransmissionSecurity
from modules.report.generate_report import generate_report
from modules.web_beacon.web_beacon import find_beacon, json_parser
from modules.cookies.cookies import cookie_evaluate
class Bcolors:
HEADER = '\033[95m'
CYAN = "\033[36m"
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
RESET = '\033[0m'
REVERSE = "\033[;7m"
def banner():
"""
Print the tool's banner
"""
print("""%s
\t ____ ____ ____ ____ _
\t / ___| _ \| _ \| _ \ __ _ _ __ __ _| |_ _ _______ _ __
\t| | _| | | | |_) | |_) | / _` | '_ \ / _` | | | | |_ / _ \ '__|
\t| |_| | |_| | __/| _ < | (_| | | | | (_| | | |_| |/ / __/ |
\t \____|____/|_| |_| \_\ \__,_|_| |_|\__,_|_|\__, /___\___|_|
\t |___/
%s""" % (Bcolors.CYAN, Bcolors.RESET))
def get_content(target):
"""
Get html, css and cookies from the target site
:param target: the target site
:return: content_cookies, content_html
"""
print("{}[-] Retrieving website content {}".format(Bcolors.RESET, Bcolors.RESET))
# create a new profile so as not to mix the user's browsing info with that of the analysis
profile_conf_name = "/tmp/gdpr-analyzer/gdpr-analyzer.default"
FirefoxProfile(profile=profile_conf_name)
# define profile preferences
browser = Browser('firefox', headless=True, profile=profile_conf_name, timeout=1000, wait_time=200,
profile_preferences={"network.cookie.cookieBehavior": 0})
# navigation run
with browser:
browser.visit(target)
# only gives us first party cookies
# content_cookies = browser.cookies.all(verbose=True)
# sad trick shot to access cookies database only work for linux because of path
paterform = platform.system()
if paterform == "Darwin":
profile_repo = glob.glob('/var/folders/sd/*/T/rust_mozprofile*')
else:
profile_repo = glob.glob('/tmp/rust_mozprofile*')
latest_profile_repo = max(profile_repo, key=os.path.getctime)
# copy database because we can not access to the one which is temporary create
db_source = latest_profile_repo + "/cookies.sqlite"
db_destination = "/tmp/gdpr-analyzer/cookies.sqlite"
shutil.copyfile(db_source, db_destination)
content_html = browser.html
# get cookie content from db
con = sqlite3.connect(db_destination)
cur = con.cursor()
cur.execute("SELECT * FROM moz_cookies")
rows = cur.fetchall()
content_cookies = []
for cookie in rows:
content_cookies.append(cookie)
con.close()
print("{}[-] Website content obtained {}".format(Bcolors.GREEN, Bcolors.RESET))
return content_cookies, content_html
def cookie(content_cookies, target):
"""
Starts the cookies process
:param content_cookies: list of cookies
:param target: the target site
:return: result
"""
print("{}[-] Checking cookies {}\n".format(Bcolors.CYAN, Bcolors.RESET))
result = cookie_evaluate(content_cookies, target)
return result
def web_beacon(content_html):
"""
Starts the web beacons process
:param content_html: html content
:return: result
"""
print("{}[-] Checking web beacon{}\n".format(Bcolors.CYAN, Bcolors.RESET))
beacon_score, beacon_info = find_beacon(content_html)
result = json_parser(beacon_score, beacon_info)
return result
def crypto(target):
"""
Starts the transmission security process
:param target: the target site
:return: result
"""
print("{}[-] Checking transmission security {}\n".format(Bcolors.CYAN, Bcolors.RESET))
crypto = TransmissionSecurity(target)
crypto.evaluate()
return crypto.json_parser()
def full(content_cookies, content_html, target):
"""
Starts each process (cookie, web beacon and transmission security)
:param content_cookies: list of cookies
:param content_html: the html content of the target site
:param target: the target site
:return: full_result
"""
result_cookie = None
result_web_beacon = None
result_crypto = None
full_result = []
result_cookie = cookie(content_cookies, target)
result_web_beacon = web_beacon(content_html)
result_crypto = crypto(target)
full_result = json.loads(result_cookie)
full_result.update(json.loads(result_web_beacon))
full_result.update(json.loads(result_crypto))
return full_result
def check_target(target):
"""
Check if the URL is valid and if the target site is online
:param target: the target site
:return: target_parse
"""
print("{}[-] Checking the url{}".format(Bcolors.RESET, Bcolors.RESET))
if not (target.startswith('//') or target.startswith('http://') or target.startswith('https://')):
target_parse = urlparse('//' + target, 'https')
else:
target_parse = urlparse(target, 'https')
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/50.0.2661.102 Safari/537.36'}
r = requests.get(target_parse.geturl(), headers=headers, verify=False)
r.raise_for_status()
except ConnectionError as e:
print("{}[X] Error : Failed to establish a connection, verify that the target exists{}".format(Bcolors.RED,
Bcolors.RESET))
sys.exit(1)
except HTTPError as e:
print("{}[X] Error : {}{}".format(Bcolors.RED, e, Bcolors.RESET))
sys.exit(1)
else:
print("{}[-] url OK{}".format(Bcolors.GREEN, Bcolors.RESET))
return target_parse
def assess_rank(result):
"""
Assess the global rank of the site
:param result: Concatenation of the result of each module
:return: rank
"""
rank = None
if "cookies" in result:
grade = result["cookies"]["grade"]
if rank is None or grade > rank:
rank = grade
if "web_beacons" in result:
grade = result["web_beacons"]["grade"]
if rank is None or grade > rank:
rank = grade
if "security_transmission" in result:
grade = result["security_transmission"]["grade"]
if rank is None or grade > rank:
rank = grade
print("\n{}{}{}WEBSITE GRADE :{} {}\n".format(Bcolors.CYAN, Bcolors.UNDERLINE, Bcolors.BOLD, Bcolors.RESET, rank))
return rank
def start():
"""
Parse arguments and starts the web site analysis
"""
banner()
parser = argparse.ArgumentParser(description='Description')
parser.add_argument('url', help='target URL')
parser.add_argument('yourname', help="report owner's name")
parser.add_argument('-f', '--full', help='get full analysis, test all available options', action='store_true')
parser.add_argument('-c', '--cookie', help='analyse the cookies and generate the score', action='store_true')
parser.add_argument('-w', '--webbeacon', help='check for the presence of web beacons', action='store_true')
parser.add_argument('-t', '--crypto', help='evaluate the transmission security', action='store_true')
parser.add_argument('-r', '--report', help='generate a pdf report', action='store_true')
parser.add_argument('-j', '--json', help='export the result in a json file', action='store_true')
args = parser.parse_args()
name = args.yourname
result = {}
target = check_target(args.url)
if args.webbeacon or args.cookie:
content_cookies, content_html = get_content(target.geturl())
if args.full or (not args.cookie and not args.webbeacon and not args.crypto):
content_cookies, content_html = get_content(target.geturl())
result = full(content_cookies, content_html, target.netloc)
else:
if args.webbeacon:
result_web_beacon = web_beacon(content_html)
result.update(json.loads(result_web_beacon))
if args.cookie:
result_cookie = cookie(content_cookies, target.netloc)
result.update(json.loads(result_cookie))
if args.crypto:
result_crypto = crypto(target.netloc)
result.update(json.loads(result_crypto))
result_info = {}
result_info["target"] = target.netloc
result_info["grade"] = assess_rank(result)
result.update(json.loads(json.dumps(result_info)))
result_target = "reports"
if args.report or args.json:
try:
if not os.path.exists(result_target):
os.mkdir(result_target)
except OSError:
print("{}Error : The folder '{}'(to save result) not exist and failed to create{}".format(Bcolors.RED,
result_target,
Bcolors.RESET))
if args.report:
if result is None:
print("{}[X] Error : No result available{}".format(Bcolors.RED, Bcolors.RESET))
else:
path_report = result_target + "/gdpranalyzer_" + name + "_" + target.netloc + ".pdf"
generate_report(name, json.dumps(result), path_report)
if args.json:
print("{}[-] Generate the JSON{}".format(Bcolors.RESET, Bcolors.RESET))
if result is None:
print("{}[X] Error : No result available{}".format(Bcolors.RED, Bcolors.RESET))
else:
path_json = result_target + "/gdpranalyzer_" + name + "_" + target.netloc + ".json"
with open(path_json, 'w') as outfile:
json.dump(result, outfile)
print("{}[-] JSON generated, it is stored in {}{}".format(Bcolors.GREEN, path_json, Bcolors.RESET))
if __name__ == '__main__':
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
if platform.python_version()[0:3] < '3.7':
print('{}[!] Make sure you have Python 3.7+ installed, quitting.{}'.format(Bcolors.YELLOW, Bcolors.RESET))
sys.exit(1)
start()