forked from Pica4x6/SecurePreferencesFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
181 lines (165 loc) · 6.71 KB
/
utils.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
import json, ast, hmac, hashlib
from collections import OrderedDict
def change_spf(json_file, values, sid, seed):
# Reads the file
json_data = open(json_file, encoding="utf-8")
data = json.load(json_data, object_pairs_hook=OrderedDict)
json_data.close()
modifyDict(data, values.split('%'))
temp = OrderedDict(sorted(data.items()))
data = temp
HMAChelper(data['protection']['macs'], data, "", values.split('%'), sid, seed)
# Calculates and sets the super_mac
super_msg = sid + json.dumps(data['protection']['macs']).replace(" ", "")
hash_obj = hmac.new(seed, super_msg.encode("utf-8"), hashlib.sha256)
print('super_mac (BEFORE): {}'.format(data['protection']['super_mac']))
print('super_mac (AFTER): {}'.format(hash_obj.hexdigest().upper()))
def removeEmpty(d):
if type(d) == type(OrderedDict()):
t = OrderedDict(d)
for x, y in t.items():
if type(y) == (type(OrderedDict())):
if len(y) == 0:
del d[x]
else:
removeEmpty(y)
if len(y) == 0:
del d[x]
elif(type(y) == type({})):
if(len(y) == 0):
del d[x]
else:
removeEmpty(y)
if len(y) == 0:
del d[x]
elif (type(y) == type([])):
if (len(y) == 0):
del d[x]
else:
removeEmpty(y)
if len(y) == 0:
del d[x]
else:
if (not y) and (y not in [False, 0]):
del d[x]
elif type(d) == type([]):
for x, y in enumerate(d):
if type(y) == type(OrderedDict()):
if len(y) == 0:
del d[x]
else:
removeEmpty(y)
if len(y) == 0:
del d[x]
elif (type(y) == type({})):
if (len(y) == 0):
del d[x]
else:
removeEmpty(y)
if len(y) == 0:
del d[x]
elif (type(y) == type([])):
if (len(y) == 0):
del d[x]
else:
removeEmpty(y)
if len(y) == 0:
del d[x]
else:
if (not y) and (y not in [False, 0]):
del d[x]
def calculateHMAC(value_as_string, path, sid, seed):
if ((type(value_as_string) == type({})) or (type(value_as_string) == type(OrderedDict()))):
removeEmpty(value_as_string)
message = sid + path + json.dumps(value_as_string, separators=(',', ':'), ensure_ascii=False).replace('<', '\\u003C').replace(
'\\u2122', '™')
hash_obj = hmac.new(seed, message.encode("utf-8"), hashlib.sha256)
return hash_obj.hexdigest().upper()
def HMAChelper(macs, value, path, arg, sid, seed, extension=False):
if isinstance(value,OrderedDict):
if(arg[0] in value):
if arg[0] in macs:
path += arg[0] + "."
macs[arg[0]] = HMAChelper(macs[arg[0]], value[arg[0]] ,path, arg[1:], sid, seed)
else:
if not extension:
macs = HMAChelper(macs, value[arg[0]], path, arg[1:], sid, seed, extension=value)
else:
macs = HMAChelper(macs, value[arg[0]], path, arg[1:], sid, seed, extension)
return macs
elif(arg[0] in macs):
del macs[arg[0]]
return macs
else:
if len(arg)>2:
HMAChelper(macs, value[arg[0]] ,path, arg[1:], sid, seed)
elif len(arg)==2:
if arg[0] in value:
path += arg[0] + "."
print('Before: {}'.format(macs))
macs = calculateHMAC(value[arg[0]], path[:-1], sid, seed)
else:
print('Before: {}'.format(macs))
if extension:
value = extension
macs = calculateHMAC(value, path[:-1], sid, seed)
else:
# print('Before: {}'.format(macs))
macs = calculateHMAC(value,path[:-1], sid, seed)
# print('After : {}'.format(macs))
return macs
else:
print('Before: {}'.format(macs))
if extension:
value = extension
macs = calculateHMAC(value, path[:-1], sid, seed)
print('After : {}'.format(macs))
return macs
def modifyDict(t, arg):
if arg[0] in ['pinned_tabs']:
if arg[0] in t:
t[arg[0]].append({arg[1]:arg[2]})
else:
t[arg[0]] = {arg[1]:arg[2]}
elif(len(arg) > 2):
if arg[0] in t:
modifyDict(t[arg[0]], arg[1:])
else:
t[arg[0]] = {arg[1]: {}}
modifyDict(t[arg[0]], arg[1:])
if "delete" not in arg:
t[arg[0]] = OrderedDict(sorted(t[arg[0]].items()))
elif(len(arg) == 2):
if(arg[1].lower() == "delete"):
if isinstance(t, list):
if arg[0] in t:
index = t.index(arg[0])
del t[index]
else:
if arg[0] in ['web_accessible_resources','permissions','api','matches','ids']:
if arg[0] in t:
t[arg[0]].append(arg[1])
else:
t[arg[0]] = [arg[1]]
elif arg[0] in ['content_scripts']:
if arg[0] not in t.keys():
t[arg[0]] = []
if arg[1].split('.')[-1] in ['js']:
if t[arg[0]]:
if 'js' in t[arg[0]][0].keys():
t[arg[0]][0]['js'].append(arg[1])
if 'run_at' not in t[arg[0]][0].keys():
t[arg[0]][0]['run_at'] = "document_end"
else:
t[arg[0]].append({'js':[arg[1]],'all_frames':True,'matches':['*://*//*']})
else:
if arg[0] in t:
if isinstance(t[arg[0]],str):
t[arg[0]] = arg[1]
else:
t[arg[0]] = ast.literal_eval(arg[1])
else:
try:
t[arg[0]] = ast.literal_eval(arg[1])
except:
t[arg[0]] = arg[1]