-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_files_from_js.py
232 lines (204 loc) · 7.19 KB
/
get_files_from_js.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
#!/usr/bin/python3
import re
import urllib.request
import urllib.parse
import http.cookiejar
import json
from datetime import datetime, timezone, timedelta
import binascii
import zlib
"""
Extract referenced files, version numbers and timestamps from
the whatsapp javascript.
serviceworker.js
t.default = {
version: "2.2218.8",
hashedResources: [...],
unhashedResources: [...],
l10n: {
locales: {
...
},
styles: {}
},
releaseDate: 1653347266148
}
bootstrap_main, bootstrap_qr
new Worker(\S+ + "...")
{dir:"LTR",children:"2.2232.7"}
appVersion:"2.2222.8"
wa:uploadLogs version: ${"2.2222.8"}
app.XXX.js
VERSION_BASE:"2.2027.10"
... also has ...}[e]||e)+"."+(...
localtes/*.js
fetch("/emoji_suggestions/\S+.json?v=2.2206.4")
lazy_loaded_low_priority_components
{dir:"LTR",children:"2.2232.7"}
early_error_handling.XXX.js
((0,i.parseUASimple)(n,"2.2218.7"))
const p="2.2203.3",
index.HEX.worker.js
appVersion:"2.2206.4"
runtime
t.u = e => (({
NUM: "<path>",
...
} [e] || e) + "." + {
NUM: "<HEXNUM>",
...
} [e] + ".js"),
t.miniCssF = e => ({
NUM: "<path>",
...
} [e] + "." + {
NUM: "<HEXNUM>",
...
} [e] + ".css"),
"""
def get(url):
with urllib.request.urlopen(url) as response:
return response.read().decode('utf-8')
def decode_svc(js):
"""
get info from serviceworker.js file
"""
class SvcInfo:
def __init__(self):
self.version = None
self.releaseDate = None
self.hashedResources = []
self.unhashedResources = []
self.locales = []
self.styles = []
def splitresources(r):
return re.split(r'",\s*"', r[1:-1], flags=re.DOTALL)
def splitlocales(r):
"""
"\S+":"localtes/...",
"""
if not r:
return []
return re.findall(r'"\S*?":\s*"(\S*?)"', r)
info = SvcInfo()
js = re.sub(r'\n\s+', '', js)
if m := re.search(r'''
\{
\s*"?version"?:\s*"(\S*?)",
\s*"?hashedResources"?:\s*\[(.*?)\],
\s*"?unhashedResources"?:\s*\[(.*?)\]
(?:,
\s*"?l10n"?:\s*\{\s*"?locales"?:\s*\{(.*?)\},\s*"?styles"?:\s*\{(.*?)\}\s*\},
\s*"?releaseDate"?:\s*(\d+))?
\s*\}''', js, re.DOTALL|re.VERBOSE):
info.version = m[1]
info.hashedResources = splitresources(m[2])
info.unhashedResources = splitresources(m[3])
info.locales = splitlocales(m[4])
info.styles = splitlocales(m[5])
info.releaseDate = datetime.fromtimestamp(int(m[6])/1000) if m[6] else None
return info
def getbootstrapworkers(jstext):
"""
get workers info from bootstrap_qr/app and bootstrap_main
"""
return re.findall(r'new Worker\(\s*\S+?\s*\+\s*"(\S+?)\"\s*\)', jstext)
def getruntime(jstext):
"""
extract resources from runtime.*.js
"""
for m in re.finditer(r'e=>\(\(?(\{\S+?\})\[e\](?:\|\|e\))?\+"\."\+(\{\S+?\})\[e\]\+"(\.\w+)"\)', jstext):
part1 = eval(m[1])
part2 = eval(m[2])
ext = m[3]
for k in set(part1.keys()) | set(part2.keys()):
yield part1.get(k, str(k)) + "." + part2.get(k, "?") + ext
def getversion(jstext):
if m := re.search(r'appVersion"?:\s*"(\d\.\d\S*?)"', jstext):
return m[1]
if m := re.search(r'"LTR",\s*"?children"?:\s*"(\d\.\d\S*?)"', jstext):
return m[1]
if m := re.search(r'VERSION_BASE"?:\s*"(\d\.\d\S*?)"', jstext):
return m[1]
if m := re.search(r'emoji_suggestions/\S+?\.json?v=(\d\.\d\S*?)"', jstext):
return m[1]
if m := re.search(r'parseUASimple\(\s*\w+,\s*"(\d\.\d\S*?)"', jstext):
return m[1]
if m := re.search(r'\bversion"?:\s*"(\d\.\d\S*?)"', jstext):
return m[1]
if m := re.search(r'\buploadLogs version:\W+"(\d\.\d\S*?)"', jstext):
return m[1]
def decompress(data):
d = zlib.decompressobj(wbits=47)
res = d.decompress(data)
if not d.eof:
print("WARN: incomplete gzipped data")
return res
def is_unsupported_page(txt):
return txt.startswith('<!DOCTYPE html>') and txt.find("URL=/unsupportedbrowser") > 0
def is_archive_wrapper_page(txt):
return txt.startswith('<!DOCTYPE html>') and txt.find("<title>Wayback Machine</title>") > 0 and txt.find('<iframe id="playback" src="https://web.archive.org') > 0
def main():
import argparse
parser = argparse.ArgumentParser(description='get whatsapp source')
parser.add_argument('--debug', action='store_true')
parser.add_argument('FILES', nargs='*')
args = parser.parse_args()
if not args.FILES:
svctext = get("https://web.whatsapp.com/serviceworker.js")
svc = decode_svc(svctext)
print(svc.version, svc.releaseDate)
print(svc.hashedResources)
print(svc.unhashedResources)
print(svc.locales)
print(svc.styles)
else:
for fn in args.FILES:
try:
if fn.endswith('.http'):
continue
if fn.endswith(".css"):
continue
with open(fn, "rb") as fh:
data = fh.read()
if data.startswith(b"\x1f\x8b"):
data = decompress(data)
txt = data.decode('utf-8', errors='ignore')
if is_unsupported_page(txt):
continue
if is_archive_wrapper_page(txt):
continue
ver = getversion(txt)
if fn.find('bootstrap')>=0 or fn.startswith('app') or fn.startswith('main'):
print("--- [%s] %s" % (ver, fn))
files = getbootstrapworkers(txt)
if files:
print("\t" + "\n\t".join(files))
else:
print("??? no files in bootstrap")
elif fn.find('serviceworker')>=0 or fn.find('stuff.js')>=0:
print("--- [%s] %s" % (ver, fn))
svc = decode_svc(txt)
if svc:
print("----->", svc.version, svc.releaseDate)
print("\t" + "\n\t".join(svc.hashedResources))
print("\t" + "\n\t".join(svc.unhashedResources))
print("\t" + "\n\t".join(svc.locales))
print("\t" + "\n\t".join(svc.styles))
else:
print("??? can't decode serviceworker")
elif fn.find('runtime')>=0:
print("--- [%s] %s" % (ver, fn))
files = getruntime(txt)
if files:
print("\t" + "\n\t".join(files))
else:
print("??? no files in runtime")
elif ver:
print("--- [%s] %s" % (ver, fn))
except Exception as e:
print("ERROR", e, fn)
if args.debug:
raise
if __name__ == '__main__':
main()