-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapple_blued.py
419 lines (370 loc) · 17.7 KB
/
apple_blued.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
#coding=utf-8
__author__ = "Xu Tao"
from PA_runtime import *
import PA_runtime
import clr
clr.AddReference('System.Core')
clr.AddReference('System.Xml.Linq')
clr.AddReference('System.Data.SQLite')
try:
clr.AddReference('model_im')
clr.AddReference("bcp_im")
clr.AddReference("model_map")
clr.AddReference("safe_read_sqlite")
except:
pass
del clr
import System
from System.Xml.Linq import *
from System.Data.SQLite import *
import model_im
import model_map
import json
CHAT_TYPE_FRIEND = 1 # 好友聊天
CHAT_TYPE_GROUP = 2 # 群聊天
CHAT_TYPE_SYSTEM = 3 # 系统消息
CHAT_TYPE_OFFICIAL = 4 # 公众号
CHAT_TYPE_SUBSCRIBE = 5 # 订阅号
CHAT_TYPE_SHOP = 6 # 商家
CHAT_TYPE_SECRET = 7 # 私密聊天
MESSAGE_TYPE_SYSTEM = 1
MESSAGE_TYPE_SEND = 2
MESSAGE_TYPE_RECEIVE = 3
MESSAGE_CONTENT_TYPE_TEXT = 1 # 文本
MESSAGE_CONTENT_TYPE_IMAGE = 2 # 图片
MESSAGE_CONTENT_TYPE_VOICE = 3 # 语音
MESSAGE_CONTENT_TYPE_VIDEO = 4 # 视频
MESSAGE_CONTENT_TYPE_EMOJI = 5 # 表情
MESSAGE_CONTENT_TYPE_CONTACT_CARD = 6 # 名片
MESSAGE_CONTENT_TYPE_LOCATION = 7 # 坐标
MESSAGE_CONTENT_TYPE_LINK = 8 # 链接
MESSAGE_CONTENT_TYPE_VOIP = 9 # 网络电话
MESSAGE_CONTENT_TYPE_ATTACHMENT = 10 # 附件
MESSAGE_CONTENT_TYPE_RED_ENVELPOE = 11 # 红包
MESSAGE_CONTENT_TYPE_RECEIPT = 12 # 转账
MESSAGE_CONTENT_TYPE_AA_RECEIPT = 13 # 群收款
MESSAGE_CONTENT_TYPE_CHARTLET = 14
MESSAGE_CONTENT_TYPE_SYSTEM = 99 # 系统
def convert_to_unixtime(timestamp):
try:
if len(str(timestamp)) == 13:
timestamp = int(str(timestamp)[0:10])
elif len(str(timestamp)) != 13 and len(str(timestamp)) != 10:
timestamp = 0
elif len(str(timestamp)) == 10:
timestamp = timestamp
return timestamp
except Exception as e:
pass
class Blued(object):
def __init__(self, node, extractDeleted, extractSource):
self.root = node
self.extractDeleted = extractDeleted
self.extractSource = extractSource
self.blued = model_im.IM()
self.cache = ds.OpenCachePath("Blued")
self.friend_list = {}
self.group_list = {}
def parse(self):
db_path = model_map.md5(self.cache, self.root.AbsolutePath)
self.blued.db_create(db_path)
self.main()
self.blued.db_close()
tmp_dir = ds.OpenCachePath("tmp")
PA_runtime.save_cache_path("05005", db_path, tmp_dir)
im_models = model_im.GenerateModel(db_path).get_models()
# map_models = model_map.Genetate(db_path).get_models()
results = []
if results:
results.extend(im_models)
# results.extend(map_models)
return results
def other_parse(self):
db_path = model_map.md5(self.cache, self.root.AbsolutePath)
self.blued.db_create(db_path)
self.get_account(self.root)
node_lists = self.root.Parent.Children
if len(node_lists) == 0:
return
for node in node_lists:
if node.Name.endswith("DB.sqlite"):
account_id = node.Name.replace("DB.sqlite","")
self.get_groups_friends(node, account_id)
self.get_messages(node, account_id)
self.blued.db_close()
tmp_dir = ds.OpenCachePath("tmp")
PA_runtime.save_cache_path("05005", db_path, tmp_dir)
im_models = model_im.GenerateModel(db_path).get_models()
# map_models = model_map.Genetate(db_path).get_models()
results = []
if results:
results.extend(im_models)
# results.extend(map_models)
return results
def main(self):
account_node = self.root.Parent.Parent.Parent.GetByPath("LoginDB.sqlite")
self.get_account(account_node)
node_lists = self.root.Parent.Parent.Parent.Children
for node in node_lists:
if node.Name.endswith("DB.sqlite"):
account_id = node.Name.replace("DB.sqlite","")
self.get_groups_friends(node, account_id)
self.get_messages(node, account_id)
def get_account(self, node):
db = SQLiteParser.Database.FromNode(node, canceller)
if db is None:
return
if 'loginUserTable' not in db.Tables:
return
tbs = SQLiteParser.TableSignature("loginUserTable")
for rec in db.ReadTableRecords(tbs, False):
try:
if canceller.IsCancellationRequested:
return
account = model_im.Account()
account.source = node.AbsolutePath
if "uid" in rec and (not rec["uid"].IsDBNull):
account.account_id = rec["uid"].Value
if "password" in rec and (not rec["password"].IsDBNull):
account.password = rec["password"].Value
if "loginDataString" in rec and (not rec["loginDataString"].IsDBNull):
data = json.loads(rec["loginDataString"].Value)
if "name" in data:
account.nickname = data["name"]
if "avatar" in data:
account.photo = data["avatar"]
if "birthday" in data:
account.birthday = data["birthday"]
if "age" in data:
account.age = data["age"]
if account.account_id:
self.blued.db_insert_table_account(account)
except Exception as e:
pass
self.blued.db_commit()
def get_groups_friends(self, node, account_id):
db = SQLiteParser.Database.FromNode(node, canceller)
if db is None:
return
if 'sessionTable' not in db.Tables:
return
tbs = SQLiteParser.TableSignature("sessionTable")
for rec in db.ReadTableRecords(tbs, False):
try:
if canceller.IsCancellationRequested:
return
# 群
if "sessionType" in rec and rec["sessionType"].Value == 3:
group = model_im.Chatroom()
group.account_id = account_id
group.source = node.AbsolutePath
group.type = 1
if "sessionAvatar" in rec and (not rec["sessionAvatar"].IsDBNull):
group.photo = rec["sessionAvatar"].Value
if "sessionName" in rec and (not rec["sessionName"].IsDBNull):
group.name = rec["sessionName"].Value
if "sessionId" in rec and (not rec["sessionId"].IsDBNull):
group.chatroom_id = rec["sessionId"].Value
if group.chatroom_id and group.name:
self.group_list[group.chatroom_id] = group.name
if group.account_id and group.chatroom_id:
self.blued.db_insert_table_chatroom(group)
# 好友
elif "sessionType" in rec and rec["sessionType"].Value == 2:
friend = model_im.Friend()
friend.account_id = account_id
friend.source = node.AbsolutePath
friend.type = 1
if "sessionAvatar" in rec and (not rec["sessionAvatar"].IsDBNull):
friend.photo = rec["sessionAvatar"].Value
if "sessionName" in rec and (not rec["sessionName"].IsDBNull):
friend.nickname = rec["sessionName"].Value
if "sessionId" in rec and (not rec["sessionId"].IsDBNull):
friend.friend_id = rec["sessionId"].Value
if friend.friend_id and friend.nickname:
self.friend_list[friend.friend_id] = friend.nickname
if friend.account_id and friend.friend_id:
self.blued.db_insert_table_friend(friend)
except Exception as e:
pass
self.blued.db_commit()
def get_messages(self, node, account_id):
db = SQLiteParser.Database.FromNode(node, canceller)
if db is None:
return
if 'messageTable' not in db.Tables:
return
tbs = SQLiteParser.TableSignature("messageTable")
for rec in db.ReadTableRecords(tbs, self.extractDeleted, True):
try:
if canceller.IsCancellationRequested:
return
messages = model_im.Message()
messages.account_id = account_id
messages.source = node.AbsolutePath
if rec.Deleted == DeletedState.Deleted:
messages.deleted = 1
if "sessionId" in rec and (not rec["sessionId"].IsDBNull):
messages.talker_id = rec["sessionId"].Value
if rec["sessionId"].Value in self.friend_list.keys():
messages.talker_type = 1
messages.talker_name = self.friend_list[rec["sessionId"].Value]
elif rec["sessionId"].Value in self.group_list.keys():
messages.talker_type = 2
messages.talker_name = self.group_list[rec["sessionId"].Value]
if "fromId" in rec and (not rec["fromId"].IsDBNull):
messages.sender_id = rec["fromId"].Value
if rec["fromId"].Value == int(account_id):
messages.is_sender = 1
if "fromUserName" in rec and (not rec["fromUserName"].IsDBNull):
messages.sender_name = rec["fromUserName"].Value
if "sendTime" in rec and (not rec["sendTime"].IsDBNull):
messages.send_time = convert_to_unixtime(rec["sendTime"].Value)
if "messageType" in rec and (not rec["messageType"].IsDBNull):
messages.type = 1
msg_type = rec["messageType"].Value
if msg_type == 1: # text
messages.type = 1
messages.content = rec["messageContent"].Value
elif msg_type == 2: # img
messages.type = 2
if rec["messageContent"].Value.find("||") != -1:
messages.media_path = rec["messageContent"].Value.split("||")[1]
elif rec["messageContent"].Value == "deleted":
messages.content = rec["messageContent"].Value
else:
messages.media_path = rec["messageContent"].Value
elif msg_type == 3: # voice
messages.type = 3
if rec["messageContent"].Value.find("||") != -1:
messages.media_path = rec["messageContent"].Value.split("||")[1]
else:
messages.content = rec["messageContent"].Value
elif msg_type == 4: # location
messages.type = 7
try:
lng,lat,addr = rec["messageContent"].Value.split(",")
location = messages.create_location()
location.latitude = lat
location.longitude = lng
location.address = addr
messages.insert_db(self.blued)
except Exception as e:
pass
elif msg_type == 5: # video
messages.type = 4
if rec["messageContent"].Value.find("||") != -1:
messages.media_path = rec["messageContent"].Value.split("||")[1]
else:
messages.media_path = rec["messageContent"].Value
elif msg_type == 6:
messages.content = rec["messageContent"].Value
elif msg_type == 10:
messages.content = rec["messageContent"].Value
elif msg_type == 11:
messages.type = 99
messages.content = rec["messageContent"].Value
elif msg_type == 12:
messages.type = 99
messages.content = rec["messageContent"].Value
elif msg_type == 13:
messages.type = 99
messages.content = rec["messageContent"].Value
elif msg_type == 14:
messages.type = 99
messages.content = rec["messageContent"].Value
elif msg_type == 24:
messages.type = 2
messages.content = "照片已销毁"
elif msg_type == 41: # 直播
try:
if "msgExtra" in rec and (not rec["msgExtra"].IsDBNull):
data = json.loads(rec["msgExtra"].Value)
except Exception as e:
pass
# elif msg_type == 75: # live
# try:
# if "msgExtra" in rec and (not rec["msgExtra"].IsDBNull):
# data = json.loads(rec["msgExtra"].Value)
# except Exception as e:
# pass
elif msg_type == 55: # 撤回
messages.type = 99
messages.content = rec["fromUserName"].Value + " 撤回了该条消息。"
elif msg_type == 56: # card
try:
messages.type = 6
if "messageContent" in rec and (not rec["messageContent"].IsDBNull):
data = json.loads(rec["messageContent"].Value)
if "avatar" in data:
messages.media_path = data["avatar"]
if "name" in data:
name = data["name"]
except Exception as e:
pass
elif msg_type == 58: # 直播
try:
if "messageContent" in rec and (not rec["messageContent"].IsDBNull):
data = json.loads(rec["messageContent"].Value)
if "gif" in data:
messages.media_path = data["gif"]
except Exception as e:
pass
elif msg_type == 41: # live
try:
if "msgExtra" in rec and (not rec["msgExtra"].IsDBNull):
data = json.loads(rec["msgExtra"].Value)
messages.content = rec["messageContent"].Value
if "avatar" in data:
messages.media_path = data["avatar"]
except Exception as e:
pass
elif msg_type == 67: # feed
try:
if "messageContent" in rec and (not rec["messageContent"].IsDBNull):
data = json.loads(rec["msgExtra"].Value)
messages.content = rec["messageContent"].Value
feed = model_im.Feed()
if "feed_img_url" in data:
feed.image_path = data["feed_img_url"]
if "feed_text" in data:
feed.content = data["feed_text"]
if "feed_time" in data:
feed.send_time = convert_to_unixtime(data["feed_time"])
feed.account_id = account_id
if "fromId" in rec and (not rec["fromId"].IsDBNull):
feed.sender_id = rec["fromId"].Value
self.blued.db_insert_table_feed(feed)
except Exception as e:
pass
elif msg_type == 75: # live
try:
if "messageContent" in rec and (not rec["messageContent"].IsDBNull):
data = json.loads(rec["msgExtra"].Value)
messages.content = rec["messageContent"].Value
if "avatar" in data:
messages.media_path = data["avatar"]
except Exception as e:
pass
except Exception as e:
print(e)
if messages.account_id and messages.talker_id and messages.sender_id:
self.blued.db_insert_table_message(messages)
self.blued.db_commit()
def analyze_blued(node, extract_Deleted, extract_Source):
pr = ParserResults()
if node.Name == "group.blued.share.plist":
results = Blued(node, extract_Deleted, extract_Source).parse()
if results:
pr.Models.AddRange(results)
pr.Build("Blued")
return pr
elif node.Name == "LoginDB.sqlite":
analyze_blued_other(node, extract_Deleted, extract_Source)
def analyze_blued_other(node, extract_Deleted, extract_Source):
pr = ParserResults()
results = Blued(node, extract_Deleted, extract_Source).other_parse()
if results:
pr.Models.AddRange(results)
pr.Build("Blued")
return pr