-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
333 lines (278 loc) · 9.75 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import argparse
import hashlib
import os
import re
import sys
from adb_android import adb_android
from pysqlcipher import dbapi2 as sqlite
from xml.etree import ElementTree
class HostException(Exception):
pass
class DeviceException(Exception):
pass
# Thanks to Fabric for this!
# https://github.com/fabric/fabric
def _wrap_with(code):
def inner(text):
return "\033[1;%sm%s\033[0m" % (code, text)
return inner
red = _wrap_with('31')
green = _wrap_with('32')
yellow = _wrap_with('33')
blue = _wrap_with('34')
magenta = _wrap_with('35')
cyan = _wrap_with('36')
white = _wrap_with('37')
def _shell_command(command):
host_status_code, output = adb_android.shell('%s; echo $?' % command)
if host_status_code != 0:
raise HostException
output = output.splitlines()
device_status_code = int(output[-1])
if device_status_code != 0:
raise DeviceException
return output[:-1]
def _su_shell_command(command):
return _shell_command('su -c "%s"' % command)
wechat_user_directories_regex = re.compile('^[0-9A-Fa-f]{32}$')
def _find_candidate_dbs():
output = _su_shell_command('ls /data/data/com.tencent.mm/MicroMsg/')
candidates = filter(lambda x: wechat_user_directories_regex.match(x) != None, output)
found_dbs = []
for candidate in candidates:
full_db_path = '/data/data/com.tencent.mm/MicroMsg/%s/EnMicroMsg.db' % candidate
try:
_su_shell_command('ls %s' % full_db_path)
except DeviceException:
continue
found_dbs.append(full_db_path)
return found_dbs
def find_dbs(args):
found_dbs = _find_candidate_dbs()
if len(found_dbs) == 0:
print red('=' * 80)
print red('Could not find any candidate databases.')
sys.exit(1)
print green('=' * 80)
print green('Found the following database(s):')
print
for db in found_dbs:
print cyan(' %s' % db)
print
if len(found_dbs) > 1:
print white('Run `%s pull --database-path <PATH_TO_DB> <OUTPUT_FILE>` to import one.' % sys.argv[0])
else:
print white('Run `%s pull <OUTPUT_FILE>` to import it.' % sys.argv[0])
def _find_nonroot_writable_dir():
dirs_to_try = ['/sdcard']
for candidate_dir in dirs_to_try:
try:
_shell_command('touch %s/foo && rm %s/foo' % (candidate_dir, candidate_dir))
except DeviceException:
continue
else:
return candidate_dir
raise Exception
def pull_db(args):
db_to_pull = args.database_path
if not db_to_pull:
found_dbs = _find_candidate_dbs()
if len(found_dbs) == 0:
print red('=' * 80)
print red('Could not find any candidate databases.')
sys.exit(1)
if len(found_dbs) > 1:
print red('=' * 80)
print red('More than one candidate database found!')
print
print white('Run `%s find` to see the options, and then come back with a specific path for me.' % sys.argv[0])
sys.exit(1)
db_to_pull = found_dbs[0]
# Find a writable location
temp_dir = _find_nonroot_writable_dir()
try:
# Copy the database there first
print yellow('Copying the DB to a user directory...')
_su_shell_command('cp %s %s/EnMicroMsg.db' % (db_to_pull, temp_dir))
print yellow('Pulling the DB to your computer...')
status_code, output = adb_android.pull('%s/EnMicroMsg.db' % temp_dir, args.output_file)
except:
print
print red('=' * 80)
print red('An error occurred.')
sys.exit(1)
else:
print
print green('=' * 80)
print green('Success!')
finally:
# Make sure to clean up!
print yellow('Cleaning up...')
_shell_command('rm -f %s/EnMicroMsg.db' % temp_dir)
def get_default_uin(args):
shared_prefs_xml = ''.join(_su_shell_command('cat /data/data/com.tencent.mm/shared_prefs/system_config_prefs.xml'))
xml_tree = ElementTree.fromstring(shared_prefs_xml)
candidates = filter(lambda x: x.attrib['name'] == 'default_uin', xml_tree.getchildren())
if len(candidates) != 1:
print red('=' * 80)
print red('Could not parse system_config_prefs.xml.')
sys.exit(1)
default_uin = candidates[0].attrib['value']
print green('=' * 80)
print green('Found the following UIN:')
print
print cyan(' %s' % default_uin)
def _delete_file_if_exists(file_path):
try:
os.remove(file_path)
except OSError:
pass
def _generate_key(imei, uin):
key = hashlib.md5(str(imei) + str(uin)).hexdigest()[0:7]
return key
def decrypt(args):
print
print yellow('Generating key...')
key = _generate_key(args.imei, args.uin)
print
print green('=' * 80)
print green('The key is:')
print
print cyan(' %s' % key)
print
print yellow('Decrypting, hang on...')
_delete_file_if_exists(args.output_file)
conn = sqlite.connect(args.input_file)
c = conn.cursor()
try:
c.execute('PRAGMA cipher_default_use_hmac = OFF;')
c.execute('PRAGMA key = \'%s\';' % key)
c.execute('ATTACH DATABASE \'%s\' AS wechatdecrypted KEY \'\';' % args.output_file)
c.execute('SELECT sqlcipher_export(\'wechatdecrypted\');')
c.execute('DETACH DATABASE wechatdecrypted;')
except:
print
print red('=' * 80)
print red('An error occurred.')
sys.exit(1)
else:
print
print green('=' * 80)
print green('Success!')
finally:
c.close()
def encrypt(args):
print
print yellow('Generating key...')
key = _generate_key(args.imei, args.uin)
print
print green('=' * 80)
print green('The key is:')
print
print cyan(' %s' % key)
print
print yellow('Encrypting, hang on...')
_delete_file_if_exists(args.output_file)
conn = sqlite.connect(args.input_file)
c = conn.cursor()
try:
c.execute('PRAGMA cipher_default_use_hmac = OFF;')
c.execute('ATTACH DATABASE \'%s\' AS encrypted KEY \'%s\';' % (args.output_file, key))
c.execute('PRAGMA cipher_use_hmac = OFF;')
c.execute('SELECT sqlcipher_export(\'encrypted\');')
c.execute('DETACH DATABASE encrypted;')
except:
print
print red('=' * 80)
print red('An error occurred.')
sys.exit(1)
else:
print
print green('=' * 80)
print green('Success!')
finally:
c.close()
def push_db(args):
# Find a writable location
temp_dir = _find_nonroot_writable_dir()
try:
# Copy the DB to the phone first
print yellow('Pushing the DB to the device...')
status_code, output = adb_android.push(args.input_file, '%s/EnMicroMsg.db' % temp_dir)
print yellow('Copying the DB to its final home...')
_su_shell_command('cp %s/EnMicroMsg.db %s' % (temp_dir, args.database_path))
except:
print
print red('=' * 80)
print red('An error occurred.')
sys.exit(1)
else:
print
print green('=' * 80)
print green('Success!')
finally:
# Make sure to clean up!
print yellow('Cleaning up...')
_shell_command('rm -f %s/EnMicroMsg.db' % temp_dir)
def parse_args():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
find_args = subparsers.add_parser('find')
find_args.set_defaults(fn=find_dbs)
pull_args = subparsers.add_parser('pull')
pull_args.set_defaults(fn=pull_db)
pull_args.add_argument('output_file',
metavar='OUTPUT_FILE',
type=str)
pull_args.add_argument('--database-path',
type=str,
help='Absolute path to the database to pull from the phone.'
'If not specified, and only one database is found, that'
'database will be auto-selected.')
get_uin_args = subparsers.add_parser('get_uin')
get_uin_args.set_defaults(fn=get_default_uin)
decrypt_args = subparsers.add_parser('decrypt')
decrypt_args.set_defaults(fn=decrypt)
decrypt_args.add_argument('uin',
metavar='UIN',
type=str)
decrypt_args.add_argument('imei',
metavar='IMEI',
type=str)
decrypt_args.add_argument('input_file',
metavar='INPUT_FILE',
type=str)
decrypt_args.add_argument('output_file',
metavar='OUTPUT_FILE',
type=str)
encrypt_args = subparsers.add_parser('encrypt')
encrypt_args.set_defaults(fn=encrypt)
encrypt_args.add_argument('uin',
metavar='UIN',
type=str)
encrypt_args.add_argument('imei',
metavar='IMEI',
type=str)
encrypt_args.add_argument('input_file',
metavar='INPUT_FILE',
type=str)
encrypt_args.add_argument('output_file',
metavar='OUTPUT_FILE',
type=str)
push_args = subparsers.add_parser('push')
push_args.set_defaults(fn=push_db)
push_args.add_argument('input_file',
metavar='INPUT_FILE',
type=str)
push_args.add_argument('database_path',
metavar='DATABASE_PATH',
type=str)
return parser.parse_args()
def main():
args = parse_args()
print
print yellow('Waiting for device...')
adb_android.wait_for_device()
args.fn(args)
if __name__ == '__main__':
main()