-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathandriod_qqv2.py
1002 lines (980 loc) · 40.3 KB
/
andriod_qqv2.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#coding=utf-8
import PA_runtime
import clr
import json
from sqlite3 import *
import hashlib
clr.AddReference('System.Web')
clr.AddReference('System.Core')
clr.AddReference('System.Xml.Linq')
clr.AddReference('System.Data.SQLite')
try:
clr.AddReference('model_qq')
clr.AddReference('QQ_struct')
clr.AddReference('bcp_im')
clr.AddReference('qq_pic_message_pb2')
except:
pass
del clr
from System.Data.SQLite import *
import System
from System.IO import MemoryStream
from System.Text import Encoding
from System.Xml.Linq import *
from System.Linq import Enumerable
from System.Xml.XPath import Extensions as XPathExtensions
from PA_runtime import *
from PA.InfraLib.Utils import PList
from PA.InfraLib.Extensions import PlistHelper
#from System.Collections.Generic import *
from collections import defaultdict
import logging
from qq_pic_message_pb2 import *
from model_qq import *
import uuid
from QQ_struct import tencent_struct
import bcp_im
import json
#import bcp_im
import base64
import threading
def SafeGetString(reader,i):
try:
if not reader.IsDBNull(i):
return reader.GetString(i)
else:
return ""
except:
return ""
def SafeGetInt64(reader,i):
try:
if not reader.IsDBNull(i):
return reader.GetInt64(i)
else:
return 0
except:
return 0
def SafeGetBlob(reader,i):
try:
if not reader.IsDBNull(i):
obj = reader.GetValue(i)
return obj #byte[]
else:
return ""
except:
return ""
def SafeGetValue(reader,i):
try:
if not reader.IsDBNull(i):
obj = reader.GetValue(i)
return obj
else:
return None
except:
return None
def decode_blob(imei_bytes, buffers):
ret = bytearray()
for i in range(len(buffers)):
c = ((buffers[i])^ord(imei_bytes[i % len(imei_bytes)]))
ret.append(c)
try:
return ret
except:
return None
def decode_text(imei_bytes, text):
if text is None:
return ''
ret = ''
for i in range(len(text)):
try:
c = bytearray(text[i].encode('utf8'))
c[len(c)-1] = (c[len(c)-1]) ^ ord(imei_bytes[i % len(imei_bytes)])
ret += c.decode('utf-8',"ignore")
except Exception as e:
pass
return ret
def readVarInt(data):
i = 0
j = 0
l = 0
k = 0
while True:
try:
j, = struct.unpack('B',data[k])
k = k+1
except Exception as e:
pass
l = l | (j & 0x7f) << i
if((j & 0x80) == 0):
return l
i = i + 7
hitdict = { '(?i)com.tencent.mobileqq/.*databases$':['QQ','mobileqq'],
'(?i)com.tencent.qqlite/.*databases$':['QQ轻聊版','qqlite'],
'(?i)com.tencent.mobileqqi/.*databases$':['QQ国际版','mobileqqi'],
'(?i)com.tencent.tim/.*databases$':['QQ TIM','tim'],
'(?i)com.tencent.minihd.qq/.*databases$':['平板QQ','minihd']
}
progress_is_start = False
lock = threading.Lock()
def checkhit(root):
nodes = []
global hitdict
for re in hitdict.keys():
node = root.FileSystem.Search(re)
if(len(list(node)) != 0):
if len(list(node)) > 1 :
i = 1
for d in node:
data = [hitdict[re][0] + "_" + str(i), hitdict[re][1]]
i = i+ 1
nodes.append((d,data))
else:
nodes.append((node[0],hitdict[re]))
return nodes
def startthread(root,extdata,extract_deleted,extract_source):
try:
sourceApp = extdata[0]
resFloder = extdata[1]
Andriod_QQParser(root,sourceApp,resFloder, extract_deleted, extract_source).parse()
except Exception as e:
pass
def analyze_andriod_qq(root, extract_deleted, extract_source):
global progress_is_start
pr = ParserResults()
try:
nodes = checkhit(root)
threads = []
for node in nodes:
try:
arg = (node[0],node[1],extract_deleted,extract_source)
t = threading.Thread(target=startthread,args= arg)
threads.append(t)
except:
pass
for th in threads:
th.start()
for th in threads:
th.join()
for node in nodes:
pr.Add(node[1][1])
except:
pass
if progress_is_start == True:
progress.Finish(True)
return pr
def progress_start():
global lock,progress_is_start
if lock.acquire():
if progress_is_start == False:
progress_is_start = True
progress.Start()
lock.release()
class Andriod_QQParser(object):
def __init__(self, app_root_dir, sourceApp,resFloder,extract_deleted, extract_source):
self.root = app_root_dir.Parent
self.extract_source = extract_source
self.extract_deleted = extract_deleted
self.app_name = 'QQ'
self.friendsGroups = collections.defaultdict()
self.friendsNickname = collections.defaultdict()
self.groupContact = collections.defaultdict()
self.nickname = ''
self.models = []
self.accounts = []
self.contacts = {} # uin to contact
self.accounttables = []
self.friendmsgtables =set()
self.troopmsgtables =set()
self.friendhash = collections.defaultdict(str)
self.accinfo = collections.defaultdict(list)
self.troops = collections.defaultdict(Chatroom)
self.im = IM()
self.cachepath = ds.OpenCachePath("QQ")
self.bcppath = ds.OpenCachePath("tmp")
self.imei = ''
self.imeilen = 15
self.VERSION_APP_VALUE = 10000
self.sourceApp = sourceApp
self.resFolder = resFloder
self.cachedb = collections.defaultdict()
self.accountsProg = collections.defaultdict()
def set_progress(self, acc_id,value):
try:
if self.accountsProg[acc_id] is not None and value != self.accountsProg[acc_id].Value:
self.accountsProg[acc_id].Value = value
print('set_progress() %d' % value)
except Exception as e:
pass
def insertaccount(self,ac):
try:
self.im.db_insert_table_account(ac)
self.im.db_commit()
except Exception as e:
pass
def parse(self):
try:
self.decode_accounts()
except:
pass
if(len(self.accounts) != 0):
progress_start()
else:
progress.Skip()
return
prog = progress['APP', self.sourceApp]
prog.Start()
for acc in self.accounts:
acc_id = acc.account_id
m = hashlib.md5()
accountPath = self.root.AbsolutePath + acc_id
m.update(accountPath.encode('utf-8'))
cachedb = self.cachepath + '/' + m.hexdigest().upper() + ".db"
self.cachedb[acc_id] = cachedb
model = QQ.UserAccount()
model.SourceFile = acc.source
model.Account = acc.account_id
model.NickName = acc.nickname
accountprog = progress['APP', self.sourceApp]['ACCOUNT',acc_id , model]
accountprog.Start()
self.accountsProg[acc_id] = accountprog
if self.im.need_parse(cachedb, self.VERSION_APP_VALUE):
self.getImei()
self.im.db_create(cachedb)
self.insert_account(acc)
try:
if canceller.IsCancellationRequested:
return
self.friendsNickname.clear()
self.friendsGroups.clear()
self.groupContact.clear()
self.accounttables = []
self.troops.clear()
#self.nickname = self.accinfo[acc_id][1]
self.contacts = {}
self.friendhash.clear()
self.friendmsgtables =set()
self.troopmsgtables =set()
self.set_progress(acc_id,1)
self.decode_accounttables(acc_id)
self.set_progress(acc_id,2)
self.decode_friends(acc_id)
self.set_progress(acc_id,3)
self.decode_group_info(acc_id)
self.set_progress(acc_id,6)
self.decode_groupMember_info(acc_id)
self.set_progress(acc_id,10)
self.decode_friend_messages(acc_id)
self.set_progress(acc_id,18)
self.decode_group_messages(acc_id)
self.set_progress(acc_id,25)
self.recover_msg_from_friendtable(acc_id)
self.set_progress(acc_id,28)
self.recover_msg_from_trooptable(acc_id)
self.set_progress(acc_id,30)
self.decode_msg_ftstable(acc_id)
self.set_progress(acc_id,32)
self.recover_msg_ftstable(acc_id)
self.set_progress(acc_id,35)
if canceller.IsCancellationRequested:
return
self.im.db_insert_table_version(VERSION_KEY_DB, VERSION_VALUE_DB)
self.im.db_insert_table_version(VERSION_KEY_APP, self.VERSION_APP_VALUE)
self.im.db_commit()
self.im.db_close()
except Exception as e:
print(e)
for acc in self.accounts:
acc_id = acc.account_id
try:
PA_runtime.save_cache_path(bcp_im.CONTACT_ACCOUNT_TYPE_IM_QQ,self.cachedb[acc_id],self.bcppath)
gen = GenerateModel(self.cachedb[acc_id],self.sourceApp,self.accountsProg[acc_id])
gen.get_models()
self.set_progress(acc_id,100)
self.accountsProg[acc_id].Finish(True)
except:
pass
try:
gen.ar.set_unique_id(self.root.AbsolutePath)
self.get_qq_res(gen.ar)
except Exception as e:
TraceService.Trace(TraceLevel.Error, e)
prog.Finish(True)
return
def get_qq_res(self, ar):
dicts = {}
searchkey = 'tencent/'+ self.resFolder + "$"
resnode = self.root.FileSystem.Search(searchkey)
for node in resnode:
ar.save_res_folder(node, "Other")
else:
return
def decode_accounttables(self,acc_id):
node = self.root.GetByPath('/databases/'+ acc_id + '.db')
if node is None:
return
d = node.PathWithMountPoint
datasource = "Data Source = " + d +";ReadOnly=True"
sql = 'select tbl_name from sqlite_master where type ="table"'
conn = SQLiteConnection(datasource)
conn.Open()
if(conn is None):
return
command = SQLiteCommand(conn)
command.CommandText = sql
reader = command.ExecuteReader()
while reader.Read():
self.accounttables.append(SafeGetString(reader,0))
reader.Close()
command.Dispose()
conn.Close()
for table in self.accounttables:
if table.find('mr_friend_') != -1:
self.friendmsgtables.add(table)
elif table.find('mr_troop_') != -1:
self.troopmsgtables.add(table)
def getImei(self):
path = self.root.GetByPath('/files/imei')
if path is None:
return
try:
d = path.PathWithMountPoint
f = open(d,"rb")
l = f.readlines()
for x in l:
pos = x.find('imei=')
if(pos != -1):
self.imei = x[5:len(x)-1]
self.imeilen = len(self.imei)
except Exception as e:
pass
return
def decode_accounts(self):
try:
dblist = []
pattern = r"^([0-9]+).db$"
path = self.root.GetByPath('/databases/')
if path is None:
return
d = path.PathWithMountPoint
for root, dirs, files in os.walk(d):
for f in files:
if(re.match(pattern,f)):
dblist.append(f)
#account
accounts = []
for db in dblist:
acc_id = db[0:db.find('.db')]
accounts.append(acc_id)
#nick
nickfile = self.root.GetByPath('/files/Properties')
nickfilepath = nickfile.PathWithMountPoint
f = open(nickfilepath,'rb')
nickdata = f.readlines()
f.close()
nickdata = sorted(nickdata)
for acc_id in accounts:
for line in nickdata:
name = 'nickName'+ acc_id+ '='
t = acc_id +'_logintime='
pos = line.find(name)
if(pos != -1):
x = line[pos+len(name):len(line)-1]
nickname = unicode(x).decode('unicode-escape')
#nickname = nickname.encode('utf-8')
self.accinfo[acc_id].append(nickname)
postime = line.find(t)
if(postime != -1):
time = line[postime+len(t):len(line)-1]
time = time[:len(time) -3]
self.accinfo[acc_id].append(time)
for acc in self.accinfo:
ac = Account()
ac.ServiceType = self.sourceApp
ac.account_id = acc
ac.source = nickfile.AbsolutePath
try:
ac.nickname = self.accinfo[acc][1]
except Exception as e:
ac.nickname = acc
self.accounts.append(ac)
except Exception as e:
pass
return
def insert_account(self,account):
try:
self.im.db_insert_table_account(account)
self.im.db_commit()
except:
pass
def decode_friends(self,acc_id):
node = self.root.GetByPath('/databases/'+ acc_id + '.db')
if node is None:
return
d = node.PathWithMountPoint
datasource = "Data Source = " + d +";ReadOnly=True"
conn = SQLiteConnection(datasource)
conn.Open()
if(conn is None):
return
command = SQLiteCommand(conn)
command.CommandText = 'select uin ,remark,name,datetime from friends'
reader = command.ExecuteReader()
while reader.Read():
try:
if canceller.IsCancellationRequested:
return
friend = Friend()
friend.friend_id = decode_text(self.imei,SafeGetString(reader,0))
m = hashlib.md5()
m.update(friend.friend_id)
self.friendhash[friend.friend_id] = m.hexdigest().upper()
friend.type = FRIEND_TYPE_FRIEND
friend.account_id = acc_id
friend.nickname = decode_text(self.imei,SafeGetString(reader,2))
friend.source = node.AbsolutePath
friend.remark = decode_text(self.imei,SafeGetString(reader,1))
except Exception as e:
pass
self.im.db_insert_table_friend(friend)
self.im.db_commit()
reader.Close()
command.Dispose()
conn.Close()
def decode_group_info(self,acc_id):
node = self.root.GetByPath('/databases/'+ acc_id + '.db')
if node is None:
return
d = node.PathWithMountPoint
if 'TroopInfo' in self.accounttables:
sql = 'select troopuin,troopcode,troopowneruin,troopname,fingertroopmemo,troopCreateTime,wmemberNum from TroopInfo'
else:
sql = 'select troopuin,troopcode,troopowneruin,troopname,fingertroopmemo, troopCreatetime,wmemberNum from TroopInfoV2'
datasource = "Data Source = " + d +";ReadOnly=True"
conn = SQLiteConnection(datasource)
conn.Open()
if(conn is None):
return
command = SQLiteCommand(conn)
command.CommandText = sql
reader = command.ExecuteReader()
while reader.Read():
try:
if canceller.IsCancellationRequested:
return
g = Chatroom()
g.source = node.AbsolutePath
g.account_id = acc_id
g.chatroom_id = decode_text(self.imei,SafeGetString(reader,0))
g.owner_id = decode_text(self.imei,SafeGetString(reader,2))
g.name = decode_text(self.imei,SafeGetString(reader,3))
g.notice = decode_text(self.imei,SafeGetString(reader,4))
g.create_time = SafeGetInt64(reader,5)
g.member_count = SafeGetInt64(reader,6)
except Exception as e:
pass
self.troops[g.chatroom_id] = g
self.im.db_insert_table_chatroom(g)
reader.Close()
command.Dispose()
conn.Close()
self.im.db_commit()
def decode_groupMember_info(self,acc_id):
node = self.root.GetByPath('/databases/'+ acc_id + '.db')
if node is None:
return
d = node.PathWithMountPoint
sql ='''
select troopuin, memberuin,friendnick,
autoremark,age,join_time,last_active_time,troopnick
from TroopMemberInfo order by troopuin
'''
datasource = "Data Source = " + d +";ReadOnly=True"
conn = SQLiteConnection(datasource)
conn.Open()
if(conn is None):
return
command = SQLiteCommand(conn)
command.CommandText = sql
reader = command.ExecuteReader()
while reader.Read():
try:
if canceller.IsCancellationRequested:
return
mem = ChatroomMember()
mem.account_id = acc_id
mem.chatroom_id = decode_text(self.imei,SafeGetString(reader,0))
mem.member_id = decode_text(self.imei,SafeGetString(reader,1))
nickname = SafeGetString(reader,2)
if nickname == '':
nickname = SafeGetString(reader,7)
mem.display_name = decode_text(self.imei,nickname)
mem.nick_name = mem.display_name
mem.signature = decode_text(self.imei,SafeGetString(reader,3))
mem.age = SafeGetInt64(reader,4)
mem.joinTime = SafeGetInt64(reader,5)
except Exception as e:
pass
self.im.db_insert_table_chatroom_member(mem)
reader.Close()
command.Dispose()
conn.Close()
self.im.db_commit()
def decode_friend_messages(self,acc_id):
for table in self.friendmsgtables:
self.decode_msg_from_friendtbale(acc_id ,table)
def processmedia(self,msg,path):
try:
picheader = {'/offpic_new/':'https://c2cpicdw.qpic.cn', '/gchatpic_new/': 'http://gchat.qpic.cn'}
sdcard = '/storage/emulated/0/'
searchkey = ''
nodes = list()
if path.find(sdcard) != -1 :
searchkey = path[path.find(sdcard) +len(sdcard):]
nodes = self.root.FileSystem.Search(searchkey+'$')
if len(list(nodes)) == 0:
searchkey = path[path.rfind('/')+1:]
nodes = self.root.FileSystem.Search(searchkey+ '$')
for node in nodes:
msg.media_path = node.AbsolutePath
if msg.media_path.endswith('.mp3') :
msg.type = MESSAGE_CONTENT_TYPE_VOICE
elif msg.media_path.endswith('.amr'):
msg.type = MESSAGE_CONTENT_TYPE_VOICE
elif msg.media_path.endswith('.slk') :
msg.type = MESSAGE_CONTENT_TYPE_VOICE
elif msg.media_path.endswith('.mp4'):
msg.type = MESSAGE_CONTENT_TYPE_VIDEO
elif msg.media_path.endswith('.jpg'):
msg.type = MESSAGE_CONTENT_TYPE_IMAGE
elif msg.media_path.endswith('.gif'):
msg.type = MESSAGE_CONTENT_TYPE_IMAGE
elif msg.media_path.endswith('.jpge'):
msg.type = MESSAGE_CONTENT_TYPE_IMAGE
elif msg.media_path.endswith('.png'):
msg.type = MESSAGE_CONTENT_TYPE_IMAGE
else:
msg.type = MESSAGE_CONTENT_TYPE_ATTACHMENT
return True
else:
for header in picheader.keys():
if(path.find(header) == 0):
msg.media_path = picheader[header] + path
msg.type = MESSAGE_CONTENT_TYPE_IMAGE
return True
except Exception as e:
pass
return False
def decode_msg_from_friendtbale(self,acc_id,table):
node = self.root.GetByPath('/databases/'+ acc_id + '.db')
if node is None:
return
d = node.PathWithMountPoint
datasource = "Data Source = " + d +";ReadOnly=True"
conn = SQLiteConnection(datasource)
conn.Open()
command = SQLiteCommand(conn)
command.CommandText = 'select msgseq,extstr,frienduin,msgdata,senderuin,time,msgtype ,_id from ' + table + ' order by time'
reader = command.ExecuteReader()
hash = table[10:42]
m = hashlib.md5()
talker_id = ''
while reader.Read():
try:
if canceller.IsCancellationRequested:
return
msg = Message()
msg.msg_id = SafeGetInt64(reader,0)
msg.account_id = acc_id
frienduin = decode_text(self.imei, SafeGetString(reader,2))
blobdata = SafeGetBlob(reader,3)
msgdata = decode_blob(self.imei, blobdata)
senderuin = decode_text(self.imei, SafeGetString(reader,4))
msg.timestamp = SafeGetInt64(reader,5)
msgtype = SafeGetInt64(reader,6)
msg.status = msgtype
msg.source = node.AbsolutePath
msg.talker_type = CHAT_TYPE_FRIEND
if talker_id is '':
m.update(frienduin)
digest = m.hexdigest().upper()
if digest == hash:
talker_id = frienduin
else:
for friend in self.friendhash:
if(hash == self.friendhash[friend]):
talker_id = friend
break
if talker_id is '':
msg.talker_id = hash
else:
msg.talker_id = talker_id
if senderuin == acc_id:
msg.sender_id = acc_id
else:
msg.sender_id = msg.talker_id
msg.type = MESSAGE_CONTENT_TYPE_TEXT
self.processQQMsg(msg,msgdata,msgtype)
except Exception as e:
pass
reader.Close()
command.Dispose()
conn.Close()
self.im.db_commit()
def decode_group_messages(self,acc_id):
for table in self.troopmsgtables:
self.decode_msg_from_trooptbale(acc_id ,table)
def decode_tencentmap(self,msg,msgstruct):
try:
msg.type = MESSAGE_CONTENT_TYPE_LOCATION
msg.address = msgstruct['meta']['Location.Search']['address'].decode("utf8","ignore")
msg.latitude = msgstruct['meta']['Location.Search']['lat']
msg.longitude = msgstruct['meta']['Location.Search']['lng']
msg.location_type = LOCATION_TYPE_GOOGLE
except:
pass
def decode_msg_from_trooptbale(self,acc_id ,table):
node = self.root.GetByPath('/databases/'+ acc_id + '.db')
if node is None:
return
d = node.PathWithMountPoint
datasource = "Data Source = " + d +";ReadOnly=True"
conn = SQLiteConnection(datasource)
conn.Open()
command = SQLiteCommand(conn)
command.CommandText = 'select msgseq,frienduin,msgdata,senderuin,time,msgtype from ' + table + ' order by time'
reader = command.ExecuteReader()
hash = table[9:41]
m = hashlib.md5()
talker_id = ''
while reader.Read():
try:
if canceller.IsCancellationRequested:
return
msg = Message()
msg.msg_id = SafeGetInt64(reader,0)
msg.account_id = acc_id
frienduin = decode_text(self.imei, SafeGetString(reader,1))
blobdata = SafeGetBlob(reader,2)
msgdata = decode_blob(self.imei, blobdata)
senderuin = decode_text(self.imei, SafeGetString(reader,3))
msg.timestamp = SafeGetInt64(reader,4)
msgtype = SafeGetInt64(reader,5)
msg.talker_type = CHAT_TYPE_GROUP
if talker_id == '':
m.update(frienduin)
digest = m.hexdigest().upper()
if digest == hash:
talker_id = frienduin
else:
for troopuin in self.troops:
m.update(troopuin)
digest = m.hexdigest().upper()
if(hash == digest):
talker_id = troopuin
break
if talker_id == '':
msg.talker_id = hash
else:
msg.talker_id = talker_id
if senderuin == acc_id:
msg.sender_id = acc_id
else:
msg.sender_id = senderuin
msg.source = node.AbsolutePath
msg.status = msgtype
msg.type = MESSAGE_CONTENT_TYPE_TEXT
self.processQQMsg(msg,msgdata,msgtype)
except Exception as e:
pass
reader.Close()
command.Dispose()
conn.Close()
self.im.db_commit()
def recover_msg_from_friendtable(self,acc_id):
node = self.root.GetByPath('/databases/'+ acc_id + '.db')
if node is None:
return
db = SQLiteParser.Database.FromNode(node,canceller)
if db is None:
return
m = hashlib.md5()
talker_id = ''
for table_name in self.friendmsgtables:
hash = table_name[10:42]
ts = SQLiteParser.TableSignature(table_name)
SQLiteParser.Tools.AddSignatureToTable(ts, 'senderuin',1,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'frienduin',1,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'time',2,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'msgtype', 2,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'msgdata', 3,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'msgseq', 2,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'flag', 2,2)
for rec in db.ReadTableDeletedRecords(ts, False):
try:
if canceller.IsCancellationRequested:
return
msg = Message()
msg.msg_id = str(rec['msgseq'].Value)
msg.account_id = acc_id
frienduin = decode_text(self.imei ,rec['frienduin'].Value)
blobdata =rec['msgdata'].Value
msgdata = decode_blob(self.imei, blobdata)
senderuin = decode_text(self.imei,rec['senderuin'].Value)
msg.timestamp = rec['time'].Value
msgtype = rec['msgtype'].Value
msg.status = msgtype
msg.source = node.AbsolutePath
msg.talker_type = CHAT_TYPE_FRIEND
msg.deleted = 1
if talker_id is '':
m.update(frienduin)
digest = m.hexdigest().upper()
if digest == hash:
talker_id = frienduin
else:
for friend in self.friendhash:
if(hash == self.friendhash[friend]):
talker_id = friend
break
if talker_id is '':
msg.talker_id = hash
else:
msg.talker_id = talker_id
if senderuin == acc_id:
msg.sender_id = acc_id
else:
msg.sender_id = msg.talker_id
msg.type = MESSAGE_CONTENT_TYPE_TEXT
self.processQQMsg(msg,msgdata,msgtype)
except Exception as e:
pass
self.im.db_commit()
def recover_msg_from_trooptable(self,acc_id):
node = self.root.GetByPath('/databases/'+ acc_id + '.db')
if node is None:
return
db = SQLiteParser.Database.FromNode(node,canceller)
if db is None:
return
m = hashlib.md5()
talker_id = ''
for table_name in self.troopmsgtables:
hash = table_name[10:42]
ts = SQLiteParser.TableSignature(table_name)
SQLiteParser.Tools.AddSignatureToTable(ts, 'senderuin',1,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'frienduin',1,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'time',2,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'msgtype', 2,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'msgdata', 3,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'msgseq', 2,2)
for rec in db.ReadTableDeletedRecords(ts, False):
try:
if canceller.IsCancellationRequested:
return
msg = Message()
msg.msg_id = str(rec['msgseq'].Value)
msg.account_id = acc_id
frienduin = decode_text(self.imei ,rec['frienduin'].Value)
blobdata =rec['msgdata'].Value
msgdata = decode_blob(self.imei, blobdata)
senderuin = decode_text(self.imei,rec['senderuin'].Value)
msg.timestamp = rec['time'].Value
msgtype = rec['msgtype'].Value
msg.source = node.AbsolutePath
msg.talker_type = CHAT_TYPE_GROUP
msg.deleted = 1
if talker_id is '':
m.update(frienduin)
digest = m.hexdigest().upper()
if digest == hash:
talker_id = frienduin
else:
for friend in self.friendhash:
if(hash == self.friendhash[friend]):
talker_id = friend
break
if talker_id is '':
msg.talker_id = hash
else:
msg.talker_id = talker_id
if senderuin == acc_id:
msg.sender_id = acc_id
else:
msg.sender_id = msg.talker_id
msg.type = MESSAGE_CONTENT_TYPE_TEXT
self.processQQMsg(msg,msgdata,msgtype)
except Exception as e:
pass
self.im.db_commit()
def processQQMsg(self,msg,msgdata,msgtype):
try:
qqmsgstruct = tencent_struct()
msgstruct = qqmsgstruct.getQQMessage(msgtype,bytes(msgdata))
if(msgtype == -2018 or msgtype == -2050):
msg.content = json.dumps(msgstruct[50])
elif(msgtype == -2011 or msgtype == -2054 or msgtype == -2059):
msg.content = msgstruct['mMsgBrief'].decode('utf-8',"ignore")
msgItems = msgstruct['mStructMsgItemLists']
for l in msgItems:
try:
msg.type = MESSAGE_CONTENT_TYPE_LINK
linkurl = l['b']
if linkurl == '':
continue
msg.url = linkurl
msg.insert_db(self.im)
except Exception as e:
pass
return
elif(msgtype == -5003):
pass
elif(msgtype == -1000):
msg.content = msgstruct.decode("utf8","ignore")
elif(msgtype == -3006):
pass
elif(msgtype == -5040 or msgtype == -5020 or msgtype == -5021 or msgtype == -5022 or msgtype == -5023):
content =msgstruct[5][1]
msg.content = content
pass
elif(msgtype == -1034):
pass
elif(msgtype == -1035):
msg1035 = msg_1035()
msg1035.ParseFromString(str(msgdata))
elif(msgtype == -5008 or msgtype == -2007):
try:
if msgstruct['app'] == 'com.tencent.map':
self.decode_tencentmap(msg,msgstruct)
else:
msg.content = str(msgstruct['meta']).encode('utf-8',"ignore")
except Exception as e:
pass
try:
if(msgdata[0:4] == '\xac\xed\x00\05' and msgdata[4] == 0x74):
lens, = struct.unpack('>H',msgdata[5:7])
msgstruct =json.loads(str(msgdata[7:7+lens]))
if msgstruct['app'] == 'com.tencent.map':
self.decode_tencentmap(msg,msgstruct)
else:
msg.content = str(msgstruct['meta']).encode('utf-8',"ignore")
except:
msg.content = str(msgstruct['meta']).encode('utf-8',"ignore")
pass
elif(msgtype == -2000):
url = str(msgstruct['rawMsgUrl'])
content= str(msgstruct['localPath'])
thumb = str(msgstruct['thumbMsgUrl'])
path = str(content)
if(self.processmedia(msg,path) == False):
self.processmedia(msg,url)
elif(msgtype == -2006):
msg.content = msgstruct.decode("utf8","ignore")
pass
elif(msgtype == -2022):
#mp4
filename = msgstruct[3][1]
path = str(filename)
self.processmedia(msg,path)
elif(msgtype == -2053):
pass
elif(msgtype == -1049):
pass
elif(msgtype == -2025):
pass
elif(msgtype == -5012):
if(msgstruct is None):
msgstruct=json.loads(str(msgdata))
msg.content = msgstruct["msg"].decode('utf-8',"ignore")
pass
elif(msgtype == -2038):
pass
elif(msgtype == -5040):
msg.content = str(msgstruct["content"].decode('utf-8',"ignore"))
pass
elif(msgtype == -2005):
sig = 0x16
if(msgdata[0] == sig):
path = str(msgdata[1:msgdata.find("|")]).decode('utf-8',"ignore")
self.processmedia(msg,path)
elif(msgtype == -2002):
if msgstruct is None:
sdcarad = '/storage/emulated/0/'
pos = msgdata.find(sdcarad)
if( pos != -1):
strlens = msgdata[pos -2]
lenpos = pos -2
if(strlens & 0x80 == 0):
strlens = msgdata[pos -1]
lenpos = pos -1
lens = readVarInt(str(msgdata[lenpos:]))
path = str(msgdata[pos:pos+ lens])
self.processmedia(msg,path)
else:
msg.content = msgdata.decode('utf-8',"ignore")
except Exception as e:
pass
msg.insert_db(self.im)
def decode_msg_ftstable(self,acc_id):
node = self.root.GetByPath('/databases/'+ acc_id + '-IndexQQMsg.db')
if node is None:
return
d = node.PathWithMountPoint
datasource = "Data Source = " + d +";ReadOnly=True"
conn = SQLiteConnection(datasource)
conn.Open()
command = SQLiteCommand(conn)
command.CommandText = 'select c1content,c4ext1,c5ext2,c6ext3 from IndexContent_content order by c4ext1'
reader = command.ExecuteReader()
while reader.Read():
try:
msg = Message()
msg.account_id = acc_id
msg.source = node.AbsolutePath
content = SafeGetString(reader,0)
msg.content = base64.b64decode(content).decode('utf-8')
talker_id = base64.b64decode(SafeGetString(reader,1))
msg.type = MESSAGE_CONTENT_TYPE_TEXT
if(talker_id.find('ZzZ1')!= -1):
msg.talker_id = talker_id.replace('ZzZ1','')
msg.talker_type = CHAT_TYPE_GROUP
else:
msg.talker_id = talker_id.replace('ZzZ0','')
msg.talker_type = CHAT_TYPE_FRIEND
msg.sender_id = base64.b64decode(SafeGetString(reader,2))
msg.deleted = 1
except Exception as e:
pass
self.im.db_insert_table_message(msg)
self.im.db_commit()
def recover_msg_ftstable(self,acc_id):
node = self.root.GetByPath('/databases/'+ acc_id + '-IndexQQMsg.db')
if node is None:
return
table = 'IndexContent_content'
db = SQLiteParser.Database.FromNode(node,canceller)
if db is None:
return
ts = SQLiteParser.TableSignature(table)
SQLiteParser.Tools.AddSignatureToTable(ts, 'c1content',1,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'c4ext1',1,2)
SQLiteParser.Tools.AddSignatureToTable(ts, 'c5ext2',2,2)
for rec in db.ReadTableDeletedRecords(ts, False):
try:
msg = Message()
msg.account_id = acc_id
msg.source = node.AbsolutePath
msg.content = base64.b64decode(rec['c1content'].Value).decode("utf-8")
talker_id = base64.b64decode(rec['c4ext1'].Value)
msg.type = MESSAGE_CONTENT_TYPE_TEXT
if(talker_id.find('ZzZ1')!= -1):
msg.talker_id = talker_id.replace('ZzZ1','')
msg.talker_type = CHAT_TYPE_GROUP
else:
msg.talker_id = talker_id.replace('ZzZ0','')
msg.talker_type = CHAT_TYPE_FRIEND
msg.sender_id = base64.b64decode((rec['c5ext2'].Value))
msg.deleted = 1
except Exception as e:
pass