-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathandroid_callrecord.py
470 lines (446 loc) · 21.1 KB
/
android_callrecord.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
#coding=utf-8
__author__ = 'xiaoyuge'
import os
import PA_runtime
import datetime
from PA_runtime import *
import clr
try:
clr.AddReference('System.Xml.Linq')
clr.AddReference('model_callrecord')
clr.AddReference('bcp_basic')
except:
pass
del clr
import hashlib
import bcp_basic
from System.Xml.Linq import *
from System.Xml.XPath import Extensions as XPathExtensions
from model_callrecord import MC, Records, Generate
import model_callrecord
import time
import shutil
import traceback
VERSION_APP_VALUE = 2
class CallsParse(MC):
def __init__(self, node, extractDeleted, extractSource):
self.node = node
self.extractDeleted = extractDeleted
self.extractSource = extractSource
self.db = None
self.db_cmd = None
self.cache_path = ds.OpenCachePath("通话记录")
md5_db = hashlib.md5()
db_name = self.node.AbsolutePath
md5_db.update(db_name.encode(encoding = 'utf-8'))
self.cachedb = self.cache_path + "\\" + md5_db.hexdigest().upper() + ".db"
self.sourceDB = self.cache_path + '\\CallSourceDB'
def parse(self):
if self.need_parse(self.cachedb, VERSION_APP_VALUE):
self.db_create(self.cachedb)
#通用备份案例
if re.findall('callinfo.db', self.node.AbsolutePath):
self.analyze_call_records_case4()
#华为自带备份案例
elif re.findall('calllog.db', self.node.AbsolutePath):
self.analyze_call_records_case5()
#oppo自带备份案例
elif re.findall('callrecord_backup.xml', self.node.AbsolutePath):
self.analyze_call_records_case6()
#meizu自带备份案例
elif re.findall('calllog.vcl', self.node.AbsolutePath):
self.analyze_call_records_case7()
#vivo自带备份案例
elif re.findall('call$', self.node.AbsolutePath):
self.analyze_call_records_case8()
#小米自带备份案例
#华为全盘通话记录存储在calls.db中
elif self.node.Parent.GetByPath('/calls.db'):
self.analyze_call_records_case1()
#vivo全盘通话记录存储在user_de/0/com.android.providers.contact/callllog.db中
elif self.node.Parent.Parent.GetByPath('/databases/calllog.db'):
self.analyze_call_records_case2()
#三星案例
elif re.findall('logs.db', self.node.AbsolutePath):
self.analyze_call_records_sumsung()
#通用全盘案例
elif re.findall('contacts2.db', self.node.AbsolutePath):
#self.analyze_call_records_case3()
nodes = self.node.FileSystem.Search('contacts2.db$')
for node in nodes:
self.node = node
self.analyze_call_records_case3()
self.db_insert_table_version(model_callrecord.VERSION_KEY_DB, model_callrecord.VERSION_VALUE_DB)
self.db_insert_table_version(model_callrecord.VERSION_KEY_APP, VERSION_APP_VALUE)
self.db_commit()
self.db_close()
try:
if os.path.exists(self.sourceDB):
shutil.rmtree(self.sourceDB)
except:
pass
#bcp entry
temp_dir = ds.OpenCachePath('tmp')
PA_runtime.save_cache_path(bcp_basic.BASIC_RECORD_INFORMATION, self.cachedb, temp_dir)
generate = Generate(self.cachedb)
models = generate.get_models()
return models
def analyze_call_records_sumsung(self):
'''解析三星通话记录'''
try:
node = self.node
db = SQLiteParser.Database.FromNode(node, canceller)
ts = SQLiteParser.TableSignature('logs')
ts.Add("number", SQLiteParser.Signatures.SignatureFactory.GetFieldSignature(SQLiteParser.FieldType.Text, SQLiteParser.FieldConstraints.NotNull))
for rec in db.ReadTableRecords(ts, self.extractDeleted, True):
canceller.ThrowIfCancellationRequested()
try:
message_id = self._db_record_get_string_value(rec, 'messageid')
if message_id is not '':
continue
records = Records()
records.id = self._db_record_get_int_value(rec, '_id')
records.phone_number = self._db_record_get_string_value(rec, 'number')
records.date = self._db_record_get_int_value(rec, 'date')
records.duration = self._db_record_get_int_value(rec, 'duration')
records.type = self._db_record_get_int_value(rec, 'type')
name = self._db_record_get_string_value(rec, 'name')
records.name = name if name is not '' else records.phone_number
records.geocoded_location = self._db_record_get_string_value(rec, 'geocoded_location')
records.country_code = self._db_record_get_string_value(rec, 'countryiso')
records.source = node.AbsolutePath
records.deleted = rec.IsDeleted
self.db_insert_table_call_records(records)
except:
traceback.print_exc()
self.db_commit()
except:
traceback.print_exc()
def analyze_call_records_case1(self):
'''解析callls.db'''
try:
node = self.node.Parent.GetByPath('/calls.db')
db = SQLiteParser.Database.FromNode(node, canceller)
ts = SQLiteParser.TableSignature('calls')
for rec in db.ReadTableRecords(ts, self.extractDeleted, True):
canceller.ThrowIfCancellationRequested()
try:
self.give_values_to_model(node, rec)
except:
traceback.print_exc()
self.db_commit()
except:
traceback.print_exc()
def analyze_call_records_case2(self):
'''解析calllog.db'''
try:
node = self.node.Parent.GetByPath('/calllog.db')
db = SQLiteParser.Database.FromNode(node, canceller)
ts = SQLiteParser.TableSignature('calls')
for rec in db.ReadTableRecords(ts, self.extractDeleted, True):
canceller.ThrowIfCancellationRequested()
try:
if 'logtype' in rec:
if rec['logtype'].Value == 300:
continue
self.give_values_to_model(node, rec)
except:
pass
self.db_commit()
except:
traceback.print_exc()
def analyze_call_records_case3(self):
'''解析contacts2.db'''
try:
node = self.node.Parent.GetByPath('/contacts2.db')
db = SQLiteParser.Database.FromNode(node, canceller)
ts = SQLiteParser.TableSignature('calls')
for rec in db.ReadTableRecords(ts, self.extractDeleted, True):
canceller.ThrowIfCancellationRequested()
try:
self.give_values_to_model(node, rec)
except:
pass
self.db_commit()
except:
traceback.print_exc()
def give_values_to_model(self, node, rec):
try:
records = Records()
records.id = self._db_record_get_int_value(rec, '_id')
records.phone_number = self._db_record_get_string_value(rec, 'number')
records.date = self._db_record_get_int_value(rec, 'date')
records.duration = self._db_record_get_int_value(rec, 'duration')
records.type = self._db_record_get_int_value(rec, 'type')
name = self._db_record_get_string_value(rec, 'name')
records.name = name if name is not '' else records.phone_number
records.geocoded_location = self._db_record_get_string_value(rec, 'geocoded_location')
records.ring_times = self._db_record_get_int_value(rec, 'ring_times')
records.mark_type = self._db_record_get_string_value(rec, 'mark_type')
records.country_code = self._db_record_get_string_value(rec, 'countryiso')
records.mark_content = self._db_record_get_string_value(rec, 'mark_content')
records.source = node.AbsolutePath
records.deleted = rec.IsDeleted
self.db_insert_table_call_records(records)
except:
traceback.print_exc()
def analyze_call_records_case4(self):
try:
node = self.node.Parent.GetByPath('/callinfo.db')
db = SQLiteParser.Database.FromNode(node, canceller)
if db is None:
return
try:
ts = SQLiteParser.TableSignature('Calls')
except:
return
records = Records()
id = 0
for rec in db.ReadTableRecords(ts, self.extractDeleted, True):
try:
id += 1
canceller.ThrowIfCancellationRequested()
records.id = id
records.phone_number = self._db_record_get_string_value(rec, 'number')
records.date = self.transdate(rec['time'].Value) if 'time' in rec else None
records.duration = self.transtime(rec['talkTime'].Value) if 'talkTime' in rec else None
records.type = self._db_record_get_int_value(rec, 'callType')
name = self._db_record_get_string_value(rec, 'name')
records.name = name if name is not '' else records.phone_number
records.source = node.AbsolutePath
self.db_insert_table_call_records(records)
except:
pass
self.db_commit()
except:
pass
def transtime(self, calltime):
minute = re.sub('分.*', '', calltime)
second = re.findall('.*分(.*)秒',calltime)[0]
duration = int(minute)*60 + int(second)
return duration
def transdate(self, calldate):
return time.mktime(time.strptime(calldate,'%Y-%m-%d %H:%M:%S'))
def analyze_call_records_case5(self):
'''华为自带备份'''
try:
node = self.node
db = SQLiteParser.Database.FromNode(node, canceller)
ts = SQLiteParser.TableSignature('calls_tb')
id = 0
for rec in db.ReadTableRecords(ts, self.extractDeleted, True):
canceller.ThrowIfCancellationRequested()
try:
records = Records()
id += 1
records.id = id
records.phone_number = self._db_record_get_string_value(rec, 'number')
records.date = self._db_record_get_int_value(rec, 'date')
records.duration = self._db_record_get_int_value(rec, 'duration')
records.type = self._db_record_get_int_value(rec, 'type')
name = self._db_record_get_string_value(rec, 'name')
records.name = name if name is not '' else records.phone_number
records.geocoded_location = self._db_record_get_string_value(rec, 'geocoded_location')
records.ring_times = self._db_record_get_int_value(rec, 'ring_times')
records.mark_type = self._db_record_get_string_value(rec, 'mark_type')
records.country_code = self._db_record_get_string_value(rec, 'countryiso')
records.mark_content = self._db_record_get_string_value(rec, 'mark_content')
records.source = node.AbsolutePath
records.deleted = rec.IsDeleted
self.db_insert_table_call_records(records)
except:
pass
self.db_commit()
except:
traceback.print_exc()
def analyze_call_records_case6(self):
try:
if self.node and self.node.Data:
xml_data = XElement.Parse(self.node.read())
xml_data = str(xml_data)
print(xml_data)
recs = re.findall('<CALL_RECORDS(.*?)/>', xml_data)
for rec in recs:
try:
d = re.findall('"(.*?)"', rec)
records = Records()
records.id = int(d[0])
records.phone_number = str(d[1])
records.date = int(d[4]) if d[4] is not ' ' else 0
records.duration = int(d[2]) if d[2] is not ' ' else 0
records.type = int(d[3]) if d[3] is not ' ' else 0
name = str(d[5])
records.name = name if name is not ' ' else records.phone_number
records.ring_times = int(d[8]) if d[8] is not ' ' else 0
records.source = self.node.AbsolutePath
self.db_insert_table_call_records(records)
except:
traceback.print_exc()
self.db_commit()
except:
traceback.print_exc()
def analyze_call_records_case7(self):
try:
dir = self.node.PathWithMountPoint
f = open(dir, 'r')
lines = f.readlines()
f.close()
dic = {}
for line in lines:
if re.findall('BEGIN:CALLLOG', line):
dic = {}
elif re.findall('END:LOGBODY', line):
records = Records()
records.phone_number = dic['normalized_number'] if 'normalized_number' in dic else dic['formated_number'] if 'formated_number' in dic else dic['number'] if 'number' in dic else ''
records.date = dic['date'] if 'date' in dic else 0
records.duration = dic['duration'] if 'duration' in dic else 0
records.type = dic['type'] if 'type' in dic else 0
name = dic['name'] if 'name' in dic else ''
records.name = name if name is not '' else records.phone_number
records.geocoded_location = dic['geocoded_location'] if 'geocoded_location' in dic else ''
records.country_code = dic['country_iso'] if 'country_iso' in dic else ''
records.source = self.node.AbsolutePath
self.db_insert_table_call_records(records)
#获取姓名
elif re.findall("X-NAME:", line):
dic['name'] = re.sub('X-NAME:', '', line).replace('\n', '')
#获取通话时间
elif re.findall('X-DATE', line):
dic['date'] = re.sub('X-DATE:', '', line).replace('\n', '')
#获取通话时长
elif re.findall('X-DURATION:', line):
dic['duration'] = re.sub('X-DURATION:', '', line).replace('\n', '')
#获取国家码
elif re.findall('X-COUNTRYISO:', line):
dic['country_iso'] = re.sub('X-COUNTRYISO:', '', line).replace('\n', '')
#通信商
elif re.findall('X-GEOCODED_LOCATION:', line):
dic['geocoded_location'] = re.sub('X-GEOCODED_LOCATION:', '', line).replace('\n', '')
#电话号码
elif re.findall('X-NUMBER:', line):
dic['number'] = re.sub('X-NUMBER:', '', line).replace('\n', '')
#标准号码
elif re.findall('X-NORMALIZED_NUMBER:', line):
dic['normalized_number'] = re.sub('X-NORMALIZED_NUMBER:', '', line).replace('\n', '')
#格式化号码
elif re.findall('X-FORMATTED_NUMBER:', line):
dic['formated_number'] = re.sub('X-FORMATTED_NUMBER:', '', line).replace('\n', '')
#通话类型
elif re.findall('X-TYPE:', line):
dic['type'] = re.sub('X-TYPE:', '', line).replace('\n', '')
self.db_commit()
except:
traceback.print_exc()
def analyze_call_records_case8(self):
try:
if self.node and self.node.Data:
xml_data = XElement.Parse(self.node.read())
xml_data = str(xml_data)
recs = re.findall('<call(.*?)/>', xml_data)
for rec in recs:
try:
d = re.findall('"(.*?)"', rec)
records = Records()
records.phone_number = str(d[0])
records.date = int(d[2]) if d[2] is not ' ' else 0
records.duration = int(d[3]) if d[3] is not ' ' else 0
records.type = int(d[1]) if d[1] is not ' ' else 0
records.source = self.node.AbsolutePath
self.db_insert_table_call_records(records)
except:
traceback.print_exc()
self.db_commit()
except:
traceback.print_exc()
@staticmethod
def _db_record_get_value(record, column, default_value=None):
if column not in record:
return default_value
if not record[column].IsDBNull:
return record[column].Value
return default_value
@staticmethod
def _db_record_get_string_value(record, column, default_value=''):
if column not in record:
return default_value
if not record[column].IsDBNull:
try:
value = str(record[column].Value)
#if record.Deleted != DeletedState.Intact:
# value = filter(lambda x: x in string.printable, value)
return value
except Exception as e:
return default_value
return default_value
@staticmethod
def _db_record_get_int_value(record, column, default_value=0):
if column not in record:
return default_value
if not record[column].IsDBNull:
try:
return int(record[column].Value)
except Exception as e:
return default_value
return default_value
@staticmethod
def _db_record_get_blob_value(record, column, default_value=None):
if column not in record:
return default_value
if not record[column].IsDBNull:
try:
value = record[column].Value
return bytes(value)
except Exception as e:
return default_value
return default_value
@staticmethod
def _get_timestamp(timestamp):
try:
if isinstance(timestamp, (long, float, str)) and len(str(timestamp)) > 10:
timestamp = int(str(timestamp)[:10])
if isinstance(timestamp, (Int64, long, int)) and len(str(timestamp)) == 10:
ts = TimeStamp.FromUnixTime(timestamp, False)
if not ts.IsValidForSmartphone():
ts = TimeStamp.FromUnixTime(0, False)
return ts
except:
return TimeStamp.FromUnixTime(0, False)
def analyze_android_calls(node, extractDeleted, extractSource):
pr = ParserResults()
try:
if len(list(node.Search('/com.sec.android.provider.logsprovider/databases/logs.db$'))) != 0:
progress.Start()
pr.Models.AddRange(CallsParse(node.Search('/com.sec.android.provider.logsprovider/databases/logs.db$')[0], extractDeleted, extractSource).parse())
pr.Build('通话记录')
return pr
elif len(list(node.Search('/com.android.providers.contacts/databases/contacts2.db$'))) != 0:
progress.Start()
pr.Models.AddRange(CallsParse(node.Search('/com.android.providers.contacts/databases/contacts2.db$')[0], extractDeleted, extractSource).parse())
pr.Build('通话记录')
return pr
elif len(list(node.Search('/calls/callinfo.db$'))) != 0:
progress.Start()
pr.Models.AddRange(CallsParse(node.Search('/calls/callinfo.db$')[0], extractDeleted, extractSource).parse())
pr.Build('通话记录')
return pr
elif len(list(node.Search('^/calllog.db$'))) != 0:
progress.Start()
pr.Models.AddRange(CallsParse(node.Search('^/calllog.db$')[0], extractDeleted, extractSource).parse())
pr.Build('通话记录')
return pr
elif len(list(node.Search('/callrecord_backup.xml$'))) != 0:
progress.Start()
pr.Models.AddRange(CallsParse(node.Search('/callrecord_backup.xml$')[0], extractDeleted, extractSource).parse())
pr.Build('通话记录')
return pr
elif len(list(node.Search('/call$'))) != 0:
progress.Start()
pr.Models.AddRange(CallsParse(node.Search('/call$')[0], extractDeleted, extractSource).parse())
pr.Build('通话记录')
return pr
else:
progress.Skip()
except:
progress.Skip()
def execute(node, extractDeleted):
return analyze_android_calls(node, extractDeleted, False)