-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathandriod_app_lists.py
358 lines (328 loc) · 15.4 KB
/
andriod_app_lists.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
# coding:utf-8
import clr
clr.AddReference('System.Core')
clr.AddReference('System.Xml.Linq')
clr.AddReference('System.Data.SQLite')
try:
clr.AddReference('model_applists')
clr.AddReference('bcp_other')
except:
pass
del clr
import PA_runtime
import System
from System.IO import Path
from PA_runtime import *
from System.Data.SQLite import *
from System.Xml.XPath import Extensions as XPathExtensions
from System.Xml.Linq import *
from PA.InfraLib.Services import ServiceGetter, IApplicationService
import zipfile
import bcp_other
import pickle
import model_applists
import re
from collections import defaultdict
import os
appService = ServiceGetter.Get[IApplicationService]()
runPath = appService.RunPath
destDir = Path.Combine(runPath, "bin", "aapt.exe")
def GetString(reader, idx):
return reader.GetString(idx) if not reader.IsDBNull(idx) else ""
class AppLists(object):
def __init__(self, node, extract_Deleted, extract_Source):
self.root = node
self.extract_Deleted = extract_Deleted
self.extract_Source = extract_Source
self.cache = ds.OpenCachePath("应用列表")
self.apps_db = model_applists.Apps()
self.binds_set = set()
def parse(self):
icon_list = []
app_lists = self.root.Children
cache_db = self.cache + "\\appinfo.db"
self.apps_db.db_create(cache_db)
for app in app_lists:
try:
if app.Type == NodeType.Directory:
for _node in app.Children:
if _node.Name.endswith(".apk"):
base_apk_path = _node.PathWithMountPoint
else:
base_apk_path = app.PathWithMountPoint
tmp_path = " dump badging {0}".format(base_apk_path)
file_content = os.popen(
'"{0}"'.format(destDir) + tmp_path).read()
icon = self._get_app_data(file_content, base_apk_path)
if icon:
icon_list.append(icon)
except:
pass
other_icon = self.search_packages_xml()
self.apps_db.db_commit()
self.apps_db.db_close()
results = model_applists.Generate(cache_db).get_models()
results.extend(icon_list)
results.extend(other_icon)
tmp_dir = ds.OpenCachePath("tmp")
PA_runtime.save_cache_path(
bcp_other.BCP_OTHER_APP_INSTALLED, cache_db, tmp_dir)
return results
def search_packages_xml(self):
res = []
results = self.root.FileSystem.Search("packages.xml")
if results:
node = results[0]
res.extend(self.parse_xml(node))
self.apps_db.db_commit()
return res
def parse_xml(self, node):
icon_list = []
data = XElement.Load(node.Data)
if data is None:
return
if str(data.Name) == "packages":
if data.Elements("package"):
for package in data.Elements("package"):
NEED_RUN = True
app_info = model_applists.Info()
dicts = defaultdict(list)
install_time = update_time = None
name = package.Attribute("name").Value if package.Attribute("name") else ""
code_path = package.Attribute("codePath").Value if package.Attribute("codePath") else ""
if package.Attribute("it"):
install_time = self._format_time(package.Attribute("it").Value)
if package.Attribute("ut"):
update_time = self._format_time(package.Attribute("ut").Value)
if name in self.binds_set:
continue
app_info.name = name
app_info.bind_id = name
app_info.installedPath = code_path
if install_time:
app_info.purchaseDate = install_time
if update_time:
app_info.deletedDate = update_time
path = os.path.join(ds.FileSystem.MountPoint, code_path)
if os.path.isdir(path):
_paths = os.listdir(path)
for files in _paths:
if os.path.isfile(os.path.join(path, files)) and files.endswith(".apk"):
base_apk_path = os.path.join(path, files)
tmp_path = " dump badging {0}".format(base_apk_path)
file_content = os.popen(
'"{0}"'.format(destDir) + tmp_path).read()
if install_time and update_time:
time_data = [install_time, update_time]
else:
time_data = []
icon = self._get_app_data(file_content, base_apk_path, time_data)
NEED_RUN = False
if icon:
icon_list.append(icon)
if not NEED_RUN:
continue
perm = package.Element("perms")
if perm is None:
if app_info.bind_id:
self.apps_db.db_insert_table_applists(app_info)
continue
perm_list = perm.Elements("item")
if perm_list:
for item in perm_list:
name = item.Attribute("name").Value if item.Attribute("name") else ""
granted = item.Attribute("granted").Value if item.Attribute("granted") else ""
if granted and name and granted == "true":
dicts["permission"].append(name)
app_info.permission = pickle.dumps(dicts["permission"])
if app_info.bind_id:
self.apps_db.db_insert_table_applists(app_info)
return icon_list
def _get_app_data(self, file_content, base_apk_path, times=[]):
icon_path = None
icon_id = None
icon = KeyValueModel()
dicts = defaultdict(list)
if not file_content:
return
app_info = model_applists.Info()
if len(times) == 2:
app_info.purchaseDate, app_info.deletedDate = times
app_info.sourceFile = base_apk_path
app_info.installedPath = base_apk_path
content_list = file_content.split("\n")
for line in content_list:
if line.find("package") != -1:
reg = re.compile("package:.*name='(.*?)'.*versionName='(.*?)'")
results = re.match(reg, line)
if results:
try:
bind_id, version = results.groups()
app_info.bind_id = bind_id
self.binds_set.add(bind_id)
app_info.version = version
icon.Key.Value = bind_id
except Exception as e:
print(e)
elif line.find("uses-permission") != -1:
reg = re.compile(".*name='(.*?)'")
results = re.match(reg, line)
if results:
try:
name = results.group(1)
dicts["permission"].append(name)
except Exception as e:
print(e)
elif line.find("application-label") != -1:
reg = re.compile("application-label:'(.*?)'")
results = re.match(reg, line)
if results:
try:
name = results.group(1)
app_info.name = name
except Exception as e:
print(e)
elif line.find("application-icon-160:") != -1:
reg = re.compile("application-icon-160:'(.*?)'")
results = re.match(reg, line)
if results:
try:
name = results.group(1)
if os.path.isfile(base_apk_path):
with zipfile.ZipFile(base_apk_path) as apk:
if '{0}'.format(name) in apk.namelist():
export_path = self.cache + "\\" + icon.Key.Value
byte_icon = apk.extract(
'{0}'.format(name), export_path)
icon.Value.Value = export_path + "\\" + name
except Exception as e:
print(e)
elif line.find("application-icon-160:") != -1:
reg = re.compile("application-icon-160:'(.*?)'")
results = re.match(reg, line)
if results:
try:
name = results.group(1)
if os.path.isfile(base_apk_path):
with zipfile.ZipFile(base_apk_path) as apk:
if '{0}'.format(name) in apk.namelist():
export_path = self.cache + "\\" + icon.Key.Value
byte_icon = apk.extract(
'{0}'.format(name), export_path)
icon.Value.Value = export_path + "\\" + name
except Exception as e:
print(e)
elif line.find("application-icon-240:") != -1:
if icon.Value.Value == None:
reg = re.compile("application-icon-240:'(.*?)'")
results = re.match(reg, line)
if results:
try:
name = results.group(1)
if os.path.isfile(base_apk_path):
with zipfile.ZipFile(base_apk_path) as apk:
if '{0}'.format(name) in apk.namelist():
export_path = self.cache + "\\" + icon.Key.Value
byte_icon = apk.extract(
'{0}'.format(name), export_path)
icon.Value.Value = export_path + "\\" + name
except Exception as e:
print(e)
elif line.find("application-icon-320:") != -1:
if icon.Value.Value == None:
reg = re.compile("application-icon-320:'(.*?)'")
results = re.match(reg, line)
if results:
try:
name = results.group(1)
if os.path.isfile(base_apk_path):
with zipfile.ZipFile(base_apk_path) as apk:
if '{0}'.format(name) in apk.namelist():
export_path = self.cache + "\\" + icon.Key.Value
byte_icon = apk.extract(
'{0}'.format(name), export_path)
icon.Value.Value = export_path + "\\" + name
except Exception as e:
print(e)
elif line.find("application-icon-480:") != -1:
if icon.Value.Value == None:
reg = re.compile("application-icon-480:'(.*?)'")
results = re.match(reg, line)
if results:
try:
name = results.group(1)
if os.path.isfile(base_apk_path):
with zipfile.ZipFile(base_apk_path) as apk:
if '{0}'.format(name) in apk.namelist():
export_path = self.cache + "\\" + icon.Key.Value
byte_icon = apk.extract(
'{0}'.format(name), export_path)
icon.Value.Value = export_path + "\\" + name
except Exception as e:
print(e)
if "permission" in dicts:
app_info.permission = pickle.dumps(dicts["permission"])
if app_info.bind_id:
self.apps_db.db_insert_table_applists(app_info)
if icon.Key.Value and icon.Value.Value:
return icon
return
@staticmethod
def _format_time(string_num):
timestamp = str(int(string_num.upper(), 16))
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
def other_parse(self):
path = self.root.PathWithMountPoint
cache_db = self.cache + "\\appinfo.db"
self.apps_db.db_create(cache_db)
try:
db = SQLiteParser.Database.FromNode(self.root, canceller)
if db is None:
return
tb = SQLiteParser.TableSignature("apps")
for rec in db.ReadTableRecords(tb, self.extract_Deleted, True):
if canceller.IsCancellationRequested:
return
app_info = model_applists.Info()
app_info.sourceFile = self.root.AbsolutePath
if "appName" in rec and (not rec["appName"].IsDBNull):
app_info.name = rec["appName"].Value
if "drawable" in rec and (not rec["drawable"].IsDBNull):
app_info.imgUrl = rec["drawable"].Value
if "packageName" in rec and (not rec["packageName"].IsDBNull):
app_info.bind_id = rec["packageName"].Value
if "versionName" in rec and (not rec["versionName"].IsDBNull):
app_info.version = rec["versionName"].Value
if "permissions" in rec and (not rec["permissions"].IsDBNull):
app_info.permission = pickle.dumps(
rec["permissions"].Value)
if rec.Deleted == DeletedState.Deleted:
app_info.deleted = 1
if app_info.name or app_info.bind_id or app_info.version or app_info.permission:
self.apps_db.db_insert_table_applists(app_info)
except Exception as e:
print(e)
self.apps_db.db_commit()
self.apps_db.db_close()
results = model_applists.Generate(cache_db).get_models()
tmp_dir = ds.OpenCachePath("tmp")
PA_runtime.save_cache_path(
bcp_other.BCP_OTHER_APP_INSTALLED, cache_db, tmp_dir)
return results
def analyze_app_lists(node, extract_Deleted, extract_Source):
pr = ParserResults()
if "appinfo" in node.PathWithMountPoint:
results = AppLists(node, extract_Deleted, extract_Source).other_parse()
else:
results = AppLists(node, extract_Deleted, extract_Source).parse()
print(len(results))
if results:
pr.Models.AddRange(results)
pr.Build("应用列表")
return pr