forked from openwpm/OpenWPM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunder_over_blocking.py
More file actions
230 lines (171 loc) · 7.22 KB
/
under_over_blocking.py
File metadata and controls
230 lines (171 loc) · 7.22 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
import json
from datetime import datetime
import sqlite3
import csv
import sys
csv.field_size_limit(sys.maxsize)
#from pandas import read_sql_query
# from .domain import get_ps_plus_1
def get_set_of_script_hosts_from_call_stack(call_stack):
"""Return the urls of the scripts involved in the call stack."""
script_urls = set()
if not call_stack:
return ""
stack_frames = call_stack.strip().split("\n")
for stack_frame in stack_frames:
script_url = stack_frame.rsplit(":", 2)[0].\
split("@")[-1].split(" line")[0]
script_urls.add(get_host_from_url(script_url))
return ", ".join(script_urls)
def get_host_from_url(url):
return strip_scheme_www_and_query(url).split("/", 1)[0]
def strip_scheme_www_and_query(url):
"""Remove the scheme and query section of a URL."""
if url:
return url.split("//")[-1].split("?")[0].lstrip("www.")
else:
return ""
def get_initiator_from_call_stack(call_stack):
"""Return the bottom element of the call stack."""
if call_stack and type(call_stack) == str:
return call_stack.strip().split("\n")[-1]
else:
return ""
def get_initiator_from_req_call_stack(req_call_stack):
"""Return the bottom element of a request call stack.
Request call stacks have an extra field (async_cause) at the end.
"""
return get_initiator_from_call_stack(req_call_stack).split(";")[0]
def get_func_and_script_url_from_initiator(initiator):
"""Remove line number and column number from the initiator."""
if initiator:
return initiator.rsplit(":", 2)[0].split(" line")[0]
else:
return ""
def get_script_url_from_initiator(initiator):
"""Remove the scheme and query section of a URL."""
if initiator:
return initiator.rsplit(":", 2)[0].split("@")[-1].split(" line")[0]
else:
return ""
def get_set_of_script_urls_from_call_stack(call_stack):
"""Return the urls of the scripts involved in the call stack as a
string."""
if not call_stack:
return ""
return ", ".join(get_script_urls_from_call_stack_as_set(call_stack))
def get_script_urls_from_call_stack_as_set(call_stack):
"""Return the urls of the scripts involved in the call stack as a set."""
script_urls = set()
if not call_stack:
return script_urls
stack_frames = call_stack.strip().split("\n")
for stack_frame in stack_frames:
script_url = stack_frame.rsplit(":", 2)[0].\
split("@")[-1].split(" line")[0]
script_urls.add(script_url)
return script_urls
def add_col_bare_script_url(js_df):
"""Add a col for script URL without scheme, www and query."""
js_df['bare_script_url'] =\
js_df['script_url'].map(strip_scheme_www_and_query)
def add_col_set_of_script_urls_from_call_stack(js_df):
js_df['stack_scripts'] =\
js_df['call_stack'].map(get_set_of_script_urls_from_call_stack)
def add_col_unix_timestamp(df):
df['unix_time_stamp'] = df['time_stamp'].map(datetime_from_iso)
def datetime_from_iso(iso_date):
"""Convert from ISO."""
iso_date = iso_date.rstrip("Z")
# due to a bug we stored timestamps
# without a leading zero, which can
# be interpreted differently
# .21Z should be .021Z
# dateutils parse .21Z as .210Z
# add the missing leading zero
if iso_date[-3] == ".":
rest, ms = iso_date.split(".")
iso_date = rest + ".0" + ms
elif iso_date[-2] == ".":
rest, ms = iso_date.split(".")
iso_date = rest + ".00" + ms
try:
# print(iso_date)
return datetime.strptime(iso_date, "%Y-%m-%dT%H:%M:%S.%f")
except ValueError:
# print "ISO", iso_date,
# datetime.strptime(iso_date.rstrip("+0000"), "%Y-%m-%dT%H:%M:%S")
return datetime.strptime(iso_date.rstrip("+0000"), "%Y-%m-%dT%H:%M:%S")
def get_cookie(headers):
# not tested
for item in headers:
if item[0] == "Cookie":
return item[1]
return ""
def get_set_cookie(header):
# not tested
for item in json.loads(header):
if item[0] == "Set-Cookie":
return item[1]
return ""
def get_responses_from_visits(con, visit_ids):
visit_ids_str = "(%s)" % ",".join(str(x) for x in visit_ids)
# qry = """SELECT r.id, r.crawl_id, r.visit_id, r.url,
# sv.site_url, sv.first_party, sv.site_rank,
# r.method, r.referrer, r.headers, r.response_status, r.location,
# r.time_stamp FROM http_responses as r
# LEFT JOIN site_visits as sv
# ON r.visit_id = sv.visit_id
# WHERE r.visit_id in %s;""" % visit_ids_str
# qry = """SELECT r.id, r.incognito, r.browser_id, r.visit_id,
# r.extension_session_uuid, r.event_ordinal, r.window_id,
# r.tab_id, r.frame_id, r.url, r.method, r.response_status,
# r.response_status_text, r.is_cached, r.headers, r.request_id,
# r.location, r.time_stamp, r.content_hash FROM http_responses as r
# LEFT JOIN site_visits as sv
# ON r.visit_id = sv.visit_id
# WHERE r.visit_id in %s;""" % visit_ids_str
qry = """SELECT r.id, r.visit_id, r.url FROM http_responses as r
LEFT JOIN site_visits as sv
ON r.visit_id = sv.visit_id
WHERE r.visit_id in %s;""" % visit_ids_str
return read_sql_query(qry, con)
def get_requests_from_visits(con, visit_ids):
visit_ids_str = "(%s)" % ",".join(str(x) for x in visit_ids)
qry = """SELECT r.id, r.crawl_id, r.visit_id, r.url, r.top_level_url,
sv.site_url, sv.first_party, sv.site_rank,
r.method, r.referrer, r.headers, r.loading_href, r.req_call_stack,
r.content_policy_type, r.post_body, r.time_stamp
FROM http_requests as r
LEFT JOIN site_visits as sv
ON r.visit_id = sv.visit_id
WHERE r.visit_id in %s;""" % visit_ids_str
return read_sql_query(qry, con)
def get_set_of_script_ps1s_from_call_stack(script_urls):
if len(script_urls):
return ", ".join(
set((get_ps_plus_1(x) or "") for x in script_urls.split(", ")))
else:
return ""
def add_col_set_of_script_ps1s_from_call_stack(js_df):
js_df['stack_script_ps1s'] =\
js_df['stack_scripts'].map(get_set_of_script_ps1s_from_call_stack)
def get_list_of_unique_site_visit_ids(con):
qry = """ SELECT sv.visit_id from site_visits AS sv"""
return read_sql_query(qry, con)
if __name__ == '__main__':
http_response_list = []
http_response_hosts = []
with open('/Users/payalkulkarni/Documents/GitHub/OpenWPM-But-Better/responses_ghostery_stateless.csv', 'r') as csvfile:
vanilla_responses = csv.reader(csvfile)
next(vanilla_responses)
for resp in vanilla_responses:
if(resp[1] != "visit_id"):
http_response_list.append(resp[2])
for url in http_response_list:
hostname = get_host_from_url(url)
http_response_hosts.append(hostname)
print(http_response_hosts)
with open('ghostery_stateless_responses_hosts.csv', 'w') as myfile:
wr = csv.writer(myfile, delimiter=",")
wr.writerows([c.strip() for c in r.strip(', ').split(',')] for r in http_response_hosts)