-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabel.py
218 lines (174 loc) · 6.22 KB
/
label.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
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
"""#### Import"""
import pandas as pd
import numpy as np
import os
import json
import math
import time
import tldextract
import traceback
from urllib.parse import urlparse
from adblockparser import AdblockRules
"""#### EasyList & EasyPrivacyList
- update the EasyList and EasyPrivacyList file paths
`CheckTrackingReq(rules, url, top_level_url, resource_type)`
"""
# Description: append the filter rules list
# input: filename = file containing easylist and easyprivacylist
# return: Adblock rules object
def getRules(filename):
df = pd.read_excel(filename)
rules = []
for i in df.index:
rules.append(df["url"][i])
Rules = AdblockRules(rules)
return Rules
# Description: setting predefined rules
easylist = getRules("EasyPrivacyList.xlsx")
easyPrivacylist = getRules("easyList.xlsx")
# Description: extract domain from given url
# input: url = url for which domain is needed
# return: domain
def getDomain(url):
ext = tldextract.extract(url)
return ext.domain + "." + ext.suffix
# Description: check if its thirparty request
# input: url = url
# input: top_level_url = top_level_url
# return: returns True if its thirdparty request otherwise false
def isThirdPartyReq(url, top_level_url):
d_url = getDomain(url)
d_top_level_url = getDomain(top_level_url)
if d_url == d_top_level_url:
return False
else:
return True
# Description: check if the request is tracking or non-tracking
# input: rules = Adblock rules object
# input: url = url
# input: top_level_url = top_level_url
# input: resource_type = resource_type
# return: returns True if it has tracking status otherwise false
def CheckTrackingReq(rules, url, top_level_url, resource_type):
return int(
rules.should_block(
url,
{
"resource_type": resource_type,
"domain": getDomain(url),
"third-party": isThirdPartyReq(url, top_level_url),
},
)
)
"""#### Check Ancestor Nodes for Tracking Behavior
`CheckAncestoralNodes(df, row.call_stack)`
`callstack` -> `stack` & `type`='Script' -> `callframes` & `parent`
"""
# Description: Search the tracking status for each unique script url's in the stack
# input: dataset = complete http_req table with easylist and easyprivacylist flags
# input: callstack = call stack object as shown above
# return: it returns 1 if any ancestoral node has tracking status otherwise 0
def CheckAncestoralNodes(dataset, callstack):
# handling non-script type
if callstack["type"] != "script":
return None
# unique scripts in the stack
unique_scripts = []
# recursively insert unique scripts in the stack
rec_stack_checker(callstack["stack"], unique_scripts)
# check the tracking status of the unique scripts
return check_script_url(dataset, unique_scripts)
# Description: Search the tracking status for each unique script url's in the stack
# input: dataset = complete http_req table with easylist and easyprivacylist flags
# input: unique_scripts = unique scripts in the given stack
# return: it returns 1 if any unique script url has tracking status otherwise 0
def check_script_url(dataset, unique_scripts):
for i in range(len(unique_scripts)):
for j in dataset.index:
if dataset["http_req"][j] == unique_scripts[i]:
if (
dataset["easylistflag"][j] == 1
or dataset["easyprivacylistflag"][j] == 1
):
return 1
return 0
# Description: it appends the unique script url's recursively
# input: stack = stack object as shown in the image above
# input: unique_scripts = unique scripts in the given stack
# return: nothing
def rec_stack_checker(stack, unique_scripts):
# append unique script_url's
for item in stack["callFrames"]:
if item["url"] not in unique_scripts:
unique_scripts.append(item["url"])
# if parent object doen't exist return (base-case)
if "parent" not in stack.keys():
return
# else send a recursive call for this
else:
rec_stack_checker(stack["parent"], unique_scripts)
"""### DataFrame to Excel
`df_to_excel(dataset, 'test.xlsx')`
"""
# Description: Converts dataframe to excel file
# input: dataset = dataframe to be converted
# input: filename = name of the csv file 'test.xlsx'
# return: nothing
def df_to_excel(dataset, filename):
writer = pd.ExcelWriter(
filename, engine="xlsxwriter", options={"strings_to_urls": False}
)
dataset.to_excel(writer)
writer.close()
"""#### Intilization
Pass complete dataset and it will add columns for:
- EasyList
- EasyPrivacyList
- AncestorFlag
All of these are boolean(0/1) flags where:
- 0 means non-tracking status
- 1 means tracking status
`df = intilization('/output.json')`
"""
# Description: Handles all initilization process like EasyList, EasyPrivacyList, Ancestor Flags
# input: JSONfile_path = file containg the http request data
# return: returns updated dataframe
def intilization(JSONfile_path, folder):
# # reading file as dataframe
dataset = pd.read_json(JSONfile_path, lines=True)
# adding easylistflag column
dataset["easylistflag"] = dataset.apply(
lambda row: CheckTrackingReq(
easylist, row.http_req, row.frame_url, row.resource_type
),
axis=1,
)
# adding easyprivacylistflag column
dataset["easyprivacylistflag"] = dataset.apply(
lambda row: CheckTrackingReq(
easyPrivacylist, row.http_req, row.frame_url, row.resource_type
),
axis=1,
)
# adding ancestor flag column
dataset["ancestorflag"] = dataset.apply(
lambda row: CheckAncestoralNodes(dataset, row.call_stack), axis=1
)
dataset.to_json(folder + "label_request.json", orient="records")
def main():
fold = os.listdir("server/output")
count = 0
for f in fold:
try:
print("labeled: ", f)
intilization(
"server/output/" + f + "/request.json",
"server/output/" + f + "/",
)
count += 1
with open("logs/label_logs.txt", "w") as log:
log.write(str(count))
log.close()
except:
print("not-labeled: ", f)
main()