-
Notifications
You must be signed in to change notification settings - Fork 3
/
hfspoof_discovery.py
174 lines (150 loc) · 6.68 KB
/
hfspoof_discovery.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
#!/usr/bin/env python3
# vim: set expandtab tabstop=4 shiftwidth=4:
# Copyright 2019 Christopher J. Kucera
# <http://apocalyptech.com/contact.php>
#
# Borderlands 3 Hotfix Injector is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# Borderlands 3 Hotfix Injector is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Borderlands 3 Hotfix Injector. If not, see
# <https://www.gnu.org/licenses/>.
import os
import gzip
import json
from mitmproxy import http
class SpoofHotfix:
def __init__(self):
# Various bits of data
self.mtimes = {}
self.mod_data = {}
self.to_load = []
self.next_prefix = 0
self.modlist_pathname = 'injectdata/modlist.txt'
self.base_hotfixes_file = '/home/pez/git/b2patching/bl3hotfixes/point_in_time/hotfixes_2020_06_11_-_16_10_03_-_guardian_takedown.json'
with open(self.base_hotfixes_file) as df:
# Do a little dance here to un-prettify the JSON, since I save it in a more
# human-friendly format on disk.
base_hotfixes = json.load(df)
self.base_hotfixes = json.dumps(base_hotfixes, separators=(',', ':'))
def load_modlist(self):
cur_mtime = os.path.getmtime(self.modlist_pathname)
if self.modlist_pathname in self.mtimes and self.mtimes[self.modlist_pathname] == cur_mtime:
return
print('Updating mod list...')
self.mtimes[self.modlist_pathname] = cur_mtime
self.to_load = []
with open(self.modlist_pathname) as df:
for line in df:
line = line.strip()
if line == '':
continue
if line[0] == '#':
continue
if line[0] == '/':
mod_path = '{}.txt'.format(line)
else:
mod_path = 'injectdata/{}.txt'.format(line)
if os.path.exists(mod_path):
self.to_load.append(mod_path)
else:
print('WARNING: {} not found'.format(mod_path))
print('Set {} mod(s) to load'.format(len(self.to_load)))
def process_mod(self, pathname):
# Make sure the mod file exists, and fail gracefully rather than allowing
# an exception
if not os.path.exists(pathname):
if pathname in self.mtimes:
del self.mtimes[pathname]
print('WARNING: {} not found'.format(pathname))
return []
# Now continue on
hf_counter = 0
cur_mtime = os.path.getmtime(pathname)
if pathname in self.mtimes and self.mtimes[pathname] == cur_mtime:
return self.mod_data[pathname]
print('Processing {}'.format(pathname))
self.mtimes[pathname] = cur_mtime
statements = []
# We're generating our own prefixes now. Just start at 0 and keep adding 1,
# encoding as hex. (I'd like to actually encode in base 36 or whatever, but
# I don't care enough to implement that.)
prefix = '{:X}'.format(self.next_prefix)
self.next_prefix += 1
# Read the file
with open(pathname) as df:
for line in df:
line = line.strip()
if line == '':
continue
if line[0] == '#':
continue
# Process the hotfix
hf_counter += 1
try:
(hftype, hf) = line.split(',', 1)
except ValueError as e:
print('ERROR: Line could not be processed as hotfix, aborting this mod: {}'.format(line))
return []
statements.append('{{"key":"{}-Apoc{}-{}","value":"{}"}}'.format(
hftype,
prefix,
hf_counter,
hf.replace('"', '\\"'),
))
self.mod_data[pathname] = statements
return statements
def request(self, flow):
if flow.request.path.startswith('/v2/client/'):
# Initial "authentication" request
if flow.request.path.endswith('/pc/oak/authentication'):
with open('spoof_data/authentication_response.json') as df:
flow.response = http.HTTPResponse.make(
200,
df.read().encode('utf8'),
{'Content-Type': 'application/json; charset=utf-8'},
)
# Now the "verification" request (this is what sends the hotfixes, among other service info)
elif flow.request.path.endswith('/pc/oak/verification'):
with open('spoof_data/verification_response.json') as df:
cur_data = df.read().strip()
# Make sure that the data we read is primed to accept a new Micropatch stanza at
# the end
assert(cur_data.endswith(']}'))
self.load_modlist()
statements = ['']
for pathname in self.to_load:
statements.extend(self.process_mod(pathname))
if len(statements) > 1:
to_inject = ','.join(statements)
else:
to_inject = ''
cur_data = '{},{}{}{}{}'.format(
cur_data[:-2],
self.base_hotfixes[:-2],
to_inject,
self.base_hotfixes[-2:],
cur_data[-2:],
)
flow.response = http.HTTPResponse.make(
200,
# Looks like mitmproxy will automatically do the compression for us, I guess?
# If I manually gzip, it ends up double-gzipped.
cur_data.encode('utf8'),
#gzip.compress(cur_data.encode('utf8')),
{
'Content-Encoding': 'gzip',
'Content-Type': 'application/json; charset=utf-8',
},
)
addons = [
SpoofHotfix()
]