-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathandroid_radio_log.py
461 lines (423 loc) · 21.4 KB
/
android_radio_log.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
#coding=utf-8
__author__ = "Xu Tao"
import clr
try:
clr.AddReference("System")
clr.AddReference("PNFA.Formats.NextStep")
clr.AddReference('PNFA.InfraLib.Exts')
clr.AddReference('bcp_connectdevice')
except:
pass
del clr
import System
from PA.Formats.NextStep import *
from PA.InfraLib.Extensions import PlistHelper
import requests
import json
import re
import datetime
import time
import bcp_connectdevice
import hashlib
from PA_runtime import *
import PA_runtime
'''
herf: http://safe.it168.com/a2016/0913/2916/000002916475.shtml
Radio类型日志是与射频相关的日志,主要包含SIM卡信息,STK信息,无线,通话等信息,手机基站信息就存在于其中
通过提取手机Radio日志,并对其中基站信息进行提取和解析,就可以找到基站地理位置数据,进而可以找到该手机历史地理位置数据
'''
mainfest_path = ds.ProjectState.ProjectDir.FullName + "\\Manifest.pgfd"
HUAWEI_PHONE = "HUAWEI"
VIVO_PHONE = "VIVO"
NEXUS_PHONE = "NEXUS"
ONEPLUS_PHONE = "ONEPLUS"
SAMSUNG_PHONE = "SAMSUNG"
SMARTISAN_PHONE = "SAMARTISAN"
MCC = 1
MNC = 2
LAC = 3
TAC = 4
CI = 5
# 各种安卓机型的基站信息匹配规则
# PATTERN_RULES = {
# HUAWEI_PHONE: ".*CellIdentity.*mMcc=(.*) .*mMnc=(.*) .*mLac=(.*) .*mCid=(.*) .*mArfcn.*",
# VIVO_PHONE: ".*CellIdentity.*mMcc=(.*) .*mMnc=(.*) .*mLac=(.*) .*mCid=(.*) .*mPsc.*",
# NEXUS_PHONE: ".*CellIdentity.*mMcc=(.*) mMnc=(.*) mCi=(.*) mPci.*mTac=(.*) mEarfcn.*CellIdentityLte.*",
# ONEPLUS_PHONE: ".*CellIdentity.*mCi=(.*?) mPci=.*mTac=(.*?) mEarfcn.*mMcc=(.*?) mMnc=(.*?) mAlphaLong.*CellIdentityLte.*CellIdentityLte.*",
# SAMSUNG_PHONE: ".*cellIdentity.*{.mcc = (.*), .mnc = (.*), .ci = (.*), .pci = 0, .tac = (.*), .earfcn.*",
# SMARTISAN_PHONE: ".*cellIdentity.*{.mcc = (.*), .mnc = (.*), .ci = (.*), .pci.*tac = (.*), .earfcn.*"
# }
PATTERN_RULES = {
"(.*) D/RILJ.*CellIdentity.*mMcc=(.*) .*mMnc=(.*) .*mLac=(.*) .*mCid=(.*) .*mArfcn.*": [MCC, MNC, LAC, CI],
"(.*) D/RILJ.*CellIdentity.*mMcc=(.*) .*mMnc=(.*) .*mLac=(.*) .*mCid=(.*) .*mPsc.*": [MCC, MNC, LAC, CI],
"(.*) D/RILJ.*CellIdentity.*mMcc=(.*) mMnc=(.*) mCi=(.*) mPci.*mTac=(.*) mEarfcn.*CellIdentityLte.*": [MCC, MNC, CI, TAC],
"(.*) D/RILJ.*CellIdentity.*mCi=(.*?) mPci=.*mTac=(.*?) mEarfcn.*mMcc=(.*?) mMnc=(.*?) mAlphaLong.*CellIdentityLte.*CellIdentityLte.*": [CI, TAC, MCC, MNC],
"(.*) D/RILJ.*cellIdentity.*{.mcc = (.*), .mnc = (.*), .ci = (.*), .pci = 0, .tac = (.*), .earfcn.*": [MCC, MNC, CI, TAC],
"(.*) D/RILJ.*cellIdentity.*{.mcc = (.*), .mnc = (.*), .ci = (.*), .pci.*tac = (.*), .earfcn.*": [MCC, MNC, CI, TAC]
}
class Radio(object):
def __init__(self, node, extract_Deleted, extract_Source):
self.root = node
self.extract_Deleted = extract_Deleted
self.extract_Source = extract_Source
def parse(self):
IS_REGEX = False
self.create_cache()
models = []
plist = NSHelpers.ReadPlist[NSDictionary](mainfest_path)
if plist is None:
return
device = NextStepExts.SafeGetObject[NSDictionary](plist,"Device")
if device is None:
return
radio_log = NextStepExts.SafeGetString(device,"RadioLog")
for key, value in PATTERN_RULES.items():
if not IS_REGEX:
model_list = self.parse_android_radio(radio_log, key, value)
if model_list:
IS_REGEX = True
models.extend(model_list)
# if "Xiaomi" in phone_name:
# pass
# elif "vivo" in phone_name:
# models.extend(self.parse_vivo_radio(radio_log, PATTERN_RULES[VIVO_PHONE]))
# elif "HUAWEI" in phone_name:
# models.extend(self.parse_huawei_radio(radio_log, PATTERN_RULES[HUAWEI_PHONE]))
# elif "honor" in phone_name:
# pass
# elif "oppo" in phone_name:
# pass
# elif "LGE" in phone_name:
# models.extend(self.parse_nexus_radio(radio_log, PATTERN_RULES[NEXUS_PHONE]))
# elif "oneplus" in phone_name:
# models.extend(self.parse_oneplus_radio(radio_log, PATTERN_RULES[ONEPLUS_PHONE]))
# elif "zte" in phone_name:
# pass
# elif "meizu" in phone_name:
# pass
# elif "samsung" in phone_name:
# models.extend(self.parse_samsung_radio(radio_log, PATTERN_RULES[SAMSUNG_PHONE]))
# elif "smartisan" in phone_name:
# models.extend(self.parse_smartisan_radio(radio_log, PATTERN_RULES[SMARTISAN_PHONE]))
self.close_cache()
return models
def create_cache(self):
'''创建中间数据库'''
self.cd = bcp_connectdevice.ConnectDeviceBcp()
cachepath = ds.OpenCachePath("基站信息")
md5_db = hashlib.md5()
db_name = 'radio_log'
md5_db.update(db_name.encode(encoding = 'utf-8'))
self.db_path = cachepath + '\\' + md5_db.hexdigest().upper() + '.db'
self.cd.db_create(self.db_path)
def insert_cache(self, mcc, mnc, lac, ci, latitude, longitude, timestamp):
'''插入数据'''
base = bcp_connectdevice.BasestationInfo()
base.MCC = mcc
base.MNC = mnc
base.LAC = lac
base.CellID = ci
base.LATITUDE = latitude
base.LONGITUDE = longitude
base.START_TIME = timestamp
self.cd.db_insert_table_basestation_information(base)
def close_cache(self):
'''关闭数据库,导出bcp'''
try:
self.cd.db_commit()
self.cd.db_close()
#bcp entry
temp_dir = ds.OpenCachePath('tmp')
PA_runtime.save_cache_path(bcp_connectdevice.BASESTATION_INFORMATION, self.db_path, temp_dir)
except:
pass
def parse_android_radio(self, radio_log, pattern, rules):
if radio_log is None:
return
models = []
log_list = radio_log.split("\n")
pattern = re.compile(pattern)
for line in log_list:
if (line.find("RIL_REQUEST_GET_CELL_INFO_LIST") != -1 or line.find("DATA_REGISTRATION_STATE") != -1 or line.find("VOICE_REGISTRATION_STATE") != -1) and line.find("error") == -1 and len(line) > 153:
results = re.match(pattern, line)
if results:
if len(results.groups()[1]) != 0 and len(results.groups()[2]) != 0 and len(results.groups()[3]) != 0 and len(results.groups()[4]) != 0:
try:
if int(results.groups()[2]) in [0, 1, 2, 3, 5, 6, 7, 9, 11, 20]:
f_time, a, b, c, d = results.groups()
unix_time = None
if f_time:
unix_time = self.format_time(f_time)
cell_tower = [None, None, None, None]
for i in rules:
if i == 1:
cell_tower[0] = a
elif i == 2:
cell_tower[1] = b
elif i == 3:
cell_tower[2] = c
elif i == 4:
cell_tower[2] = c
elif i == 5:
cell_tower[3] = d
celltower = CellTower()
if unix_time:
celltower.TimeStamp.Value = TimeStamp.FromUnixTime(unix_time)
celltower.MNC.Value = cell_tower[0]
celltower.MCC.Value = cell_tower[1]
celltower.LAC.Value = cell_tower[2]
celltower.CID.Value = cell_tower[3]
models.append(celltower)
longitude,latitude = self._get_lbs_data(cell_tower[0], cell_tower[1], cell_tower[2], cell_tower[3])
if longitude and latitude:
loc = Location()
coord = Coordinate()
coord.Longitude.Value = longitude
coord.Latitude.Value = latitude
loc.Position.Value = coord
models.append(loc)
self.insert_cache(cell_tower[0], cell_tower[1], cell_tower[2], cell_tower[3], latitude, longitude, unix_time)
except Exception as e:
print(e)
return models
# def parse_huawei_radio(self, radio_log, pattern):
# if radio_log is None:
# return
# models = []
# log_list = radio_log.split("\n")
# pattern = re.compile(pattern)
# for line in log_list:
# if line.find("RIL_REQUEST_GET_CELL_INFO_LIST") != -1 and line.find("error") == -1 and len(line) > 153:
# results = re.match(pattern, line)
# if results:
# if len(results.groups()[0]) != 0 and len(results.groups()[1]) != 0 and len(results.groups()[2]) != 0 and len(results.groups()[3]) != 0:
# try:
# if int(results.groups()[1]) in [0,1,2,3,5,6,7,9,11,20]:
# celltower = CellTower()
# mcc,mnc,ci,tac = results.groups()
# celltower.MNC.Value = mnc
# celltower.MCC.Value = mcc
# celltower.LAC.Value = tac
# celltower.CID.Value = ci
# models.append(celltower)
# longitude,latitude = self._get_lbs_data(mcc, mnc, tac, ci)
# if longitude and latitude:
# loc = Location()
# coord = Coordinate()
# coord.Longitude.Value = longitude
# coord.Latitude.Value = latitude
# loc.Position.Value = coord
# models.append(loc)
# self.insert_cache(mcc,mnc,tac,ci,latitude,longitude)
# except Exception as e:
# pass
# return models
# def parse_vivo_radio(self, radio_log, pattern):
# if radio_log is None:
# return
# models = []
# log_list = radio_log.split("\n")
# pattern = re.compile(pattern)
# for line in log_list:
# if line.find("RIL_REQUEST_GET_CELL_INFO_LIST") != -1:
# results = re.match(pattern, line)
# if results:
# if len(results.groups()[0]) != 0 and len(results.groups()[1]) != 0 and len(results.groups()[2]) != 0 and len(results.groups()[3]) != 0:
# try:
# if int(results.groups()[1]) in [0,1,2,3,5,6,7,9,11,20]:
# mcc,mnc,ci,tac = results.groups()
# celltower = CellTower()
# mcc,mnc,ci,tac = results.groups()
# celltower.MNC.Value = mnc
# celltower.MCC.Value = mcc
# celltower.LAC.Value = tac
# celltower.CID.Value = ci
# models.append(celltower)
# longitude,latitude = self._get_lbs_data(mcc, mnc, tac, ci)
# if longitude and latitude:
# loc = Location()
# coord = Coordinate()
# coord.Longitude.Value = longitude
# coord.Latitude.Value = latitude
# loc.Position.Value = coord
# models.append(loc)
# self.insert_cache(mcc,mnc,tac,ci,latitude,longitude)
# except Exception as e:
# pass
# return models
# def parse_nexus_radio(self, radio_log, pattern):
# if radio_log is None:
# return
# models = []
# log_list = radio_log.split("\n")
# pattern = re.compile(pattern)
# for line in log_list:
# if line.find("RIL_REQUEST_GET_CELL_INFO_LIST") != -1:
# results = re.match(pattern, line)
# if results:
# if len(results.groups()[0]) != 0 and len(results.groups()[1]) != 0 and len(results.groups()[2]) != 0 and len(results.groups()[3]) != 0:
# try:
# if int(results.groups()[1]) in [0,1,2,3,5,6,7,9,11,20]:
# mcc,mnc,lac,ci = results.groups()
# celltower = CellTower()
# celltower.MNC.Value = mnc
# celltower.MCC.Value = mcc
# celltower.LAC.Value = lac
# celltower.CID.Value = ci
# models.append(celltower)
# longitude,latitude = self._get_lbs_data(mcc, mnc, lac, ci)
# if longitude and latitude:
# loc = Location()
# coord = Coordinate()
# coord.Longitude.Value = longitude
# coord.Latitude.Value = latitude
# loc.Position.Value = coord
# models.append(loc)
# self.insert_cache(mcc,mnc,lac,ci,latitude,longitude)
# except Exception as e:
# pass
# return models
# def parse_oneplus_radio(self, radio_log, pattern):
# if radio_log is None:
# return
# models = []
# log_list = radio_log.split("\n")
# pattern = re.compile(pattern)
# for line in log_list:
# if line.find("RIL_REQUEST_GET_CELL_INFO_LIST") != -1:
# results = re.match(pattern, line)
# if results:
# if len(results.groups()[0]) != 0 and len(results.groups()[1]) != 0 and len(results.groups()[2]) != 0 and len(results.groups()[3]) != 0:
# try:
# if int(results.groups()[1]) in [0,1,2,3,5,6,7,9,11,20]:
# mci,tac,mcc,mnc = results.groups()
# celltower = CellTower()
# celltower.MNC.Value = mnc
# celltower.MCC.Value = mcc
# celltower.LAC.Value = tac
# celltower.CID.Value = mci
# models.append(celltower)
# longitude,latitude = self._get_lbs_data(mcc, mnc, tac, mci)
# if longitude and latitude:
# loc = Location()
# coord = Coordinate()
# coord.Longitude.Value = longitude
# coord.Latitude.Value = latitude
# loc.Position.Value = coord
# models.append(loc)
# self.insert_cache(mcc,mnc,tac,mci,latitude,longitude)
# except Exception as e:
# pass
# return models
# def parse_samsung_radio(self, radio_log, pattern):
# if radio_log is None:
# return
# models = []
# log_list = radio_log.split("\n")
# pattern = re.compile(pattern)
# for line in log_list:
# if line.find("DATA_REGISTRATION_STATE") != -1:
# results = re.match(pattern, line)
# if results:
# if len(results.groups()[0]) != 0 and len(results.groups()[1]) != 0 and len(results.groups()[2]) != 0 and len(results.groups()[3]) != 0:
# try:
# if int(results.groups()[1]) in [0,1,2,3,5,6,7,9,11,20]:
# mcc,mnc,mci,tac = results.groups()
# celltower = CellTower()
# celltower.MNC.Value = mnc
# celltower.MCC.Value = mcc
# celltower.LAC.Value = tac
# celltower.CID.Value = mci
# models.append(celltower)
# longitude,latitude = self._get_lbs_data(mcc, mnc, tac, mci)
# if longitude and latitude:
# loc = Location()
# coord = Coordinate()
# coord.Longitude.Value = longitude
# coord.Latitude.Value = latitude
# loc.Position.Value = coord
# models.append(loc)
# self.insert_cache(mcc,mnc,tac,mci,latitude,longitude)
# except Exception as e:
# pass
# return models
# def parse_smartisan_radio(self, radio_log, pattern):
# if radio_log is None:
# return
# models = []
# log_list = radio_log.split("\n")
# pattern = re.compile(pattern)
# for line in log_list:
# if line.find("DATA_REGISTRATION_STATE") != -1:
# results = re.match(pattern, line)
# if results:
# if len(results.groups()[0]) != 0 and len(results.groups()[1]) != 0 and len(results.groups()[2]) != 0 and len(results.groups()[3]) != 0:
# try:
# if int(results.groups()[1]) in [0,1,2,3,5,6,7,9,11,20]:
# mcc,mnc,mci,tac = results.groups()
# celltower = CellTower()
# celltower.MNC.Value = mnc
# celltower.MCC.Value = mcc
# celltower.LAC.Value = tac
# celltower.CID.Value = mci
# models.append(celltower)
# longitude,latitude = self._get_lbs_data(mcc, mnc, tac, mci)
# if longitude and latitude:
# loc = Location()
# coord = Coordinate()
# coord.Longitude.Value = longitude
# coord.Latitude.Value = latitude
# loc.Position.Value = coord
# models.append(loc)
# self.insert_cache(mcc,mnc,tac,mci,latitude,longitude)
# except Exception as e:
# pass
# return models
def _get_lbs_data(self, mcc, mnc, lac, ci):
"""[summary]
Arguments:
mcc {[int]} -- [mcc国家代码:中国代码 460]
mnc {[int]} -- [mnc网络类型:0移动,1联通(电信对应sid),十进制]
lac {[int]} -- [lac(电信对应nid),十进制]
ci {[int]} -- [cellid(电信对应bid),十进制]
Return:
longitude {[float]} -- [经度]
latitude {[float]} -- [维度]
"""
payload = {
"mcc":int(mcc),
"mnc":int(mnc),
"lac":int(lac),
"ci":int(ci),
"output":"json"
}
# 所有免费接口禁止从移动设备端直接访问,请使用固定IP的服务器转发请求
# 每5分钟限制查询300次,基站接口每日限制查询1000次
lbs_api = "http://api.cellocation.com:81/cell/"
try:
json_data = requests.get(lbs_api, params=payload, timeout=5)
longitude, latitude = None, None
response = json.loads(json_data.content.decode("utf-8"))
if "lon" in response:
longitude = response["lon"]
if "lat" in response:
latitude = response["lat"]
return float(longitude),float(latitude)
except Exception as e:
return None, None
def format_time(self, v):
try:
year = str(datetime.date.today())[:4]
full_time = year + "-" + v
_format = "%Y-%m-%d %H:%M:%S.%f"
unix_time = time.strptime(full_time, _format)
return time.mktime(unix_time)
except:
return
def analyze_radio(node, extract_Deleted, extract_Source):
pr = ParserResults()
results = Radio(node, extract_Deleted, extract_Source).parse()
if results:
pr.Models.AddRange(results)
pr.Build("安卓Radio日志")
return pr