-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest_parser.py
220 lines (174 loc) · 7.28 KB
/
manifest_parser.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
# -- author: Biniam Fisseha Demissie
import re
import subprocess
import json
import traceback
class Parser():
manifest = ""
package = ""
def __init__(self, apk_path):
result = subprocess.run([f'aapt dump xmltree {apk_path} AndroidManifest.xml'], shell = True, capture_output=True, text=True)
if result.returncode == 0:
self.manifest = result.stdout
self.package = re.findall(r'package="([^"]+)"', self.manifest)[0]
else:
raise Exception(f"Could not dump AndroidManifest.xml for {apk_path}")
def get_components(self):
lines = self.manifest.splitlines()
skip = True
depth = 0
for l in lines:
if "uses-permission" in l:
yield l
__depth = l.find("N: ") + l.find("E: ") + l.find("A: ") + 2
if __depth < depth:
break
if l.strip().startswith("E"):
if "activity" in l \
or "receiver" in l \
or "service" in l \
or "action" in l \
or "data" in l \
or "intent-filter" in l \
or "category" in l:
yield l
if l.strip().startswith("A"):
if "android:name" in l or "package" in l or "scheme" in l or "pathPattern" in l or "host" in l:
yield l
def get_x(self, x, line):
if f"android:{x}" in line:
name = re.findall(r'android:' + x + r'[^"]+"([^"]+)"', line)
return name[0] if len(name) > 0 else ""
def visit_action(self, gen):
line = next(gen)
name = self.get_x("name",line)
try:
line = next(gen)
except StopIteration:
line = None
return line,{"action": name}
def visit_category(self, gen):
line = next(gen)
name = self.get_x("name", line)
try:
line = next(gen)
except StopIteration:
line = None
return line, {"category": name}
def visit_data(self, gen):
host = ""
pattern = []
scheme = ""
try:
while True:
line = next(gen)
if "E: " in line and "E: data" not in line:
break
if "scheme" in line:
scheme = self.get_x("scheme", line)
if "pathPattern" in line:
pattern.append(self.get_x("pathPattern", line))
if "host" in line:
host = self.get_x("host", line)
except StopIteration:
pass
return line, {"data": {"scheme": scheme, "pathPattern": pattern, "host": host}}
def visit_intent_filter(self, component, name, line, gen):
component = dict([("name", name),("type", component), ("intent-filters", [])])
try:
filter = []
while True:
if not line:
return None, component
if "E: action" in line:
line, action = self.visit_action(gen)
filter.append(action)
continue
if "E: category" in line:
line, category = self.visit_category(gen)
filter.append(category)
continue
if "E: data" in line:
line, data = self.visit_data(gen)
filter.append(data)
continue
if len(filter) > 0:
component["intent-filters"].append(filter)
filter = []
if "E: activity" in line or "E: receiver" in line or "E: service" in line or "E: uses-permission" in line:
return line, component
try:
line = next(gen)
except StopIteration:
return None, component
except StopIteration:
traceback.print_exc()
pass
def get_comp_type(self, line):
if "E: activity" in line:
return "Activity"
elif "E: receiver" in line:
return "Receiver"
elif "E: service" in line:
return "Service"
else:
return "Permission"
def visit_component(self, component, gen):
line = next(gen)
name = self.get_x("name", line)
try:
while True:
if "E: activity" in line or "E: receiver" in line or "E: service" in line or "E: uses-permission" in line:
return self.visit_component(self.get_comp_type(line), gen)
line = next(gen)
# if "E: activity" in line or "E: receiver" in line or "E: service" in line:
# continue
if "meta-data" in line:
continue
if "layout" in line:
continue
if "A: " in line:
continue
if "intent-filter" in line:
return self.visit_intent_filter(component, name, line, gen)
else:
component = dict([("name", name),("type", component), ("intent-filters", [])])
return line, component
except StopIteration:
component = dict([("name", name),("type", component), ("intent-filters", [])])
return None, component
def parse(self):
generator = self.get_components()
app = dict([("package", self.package), ("permissions", []), ("components", [])])
permissions = set()
components = []
line = ""
try:
while True:
if "E: activity" in line or "E: receiver" in line or "E: service" in line or "E: uses-permission" in line:
if "E: uses-permission" in line:
line = next(generator)
permissions.add(self.get_x("name", line))
try:
line = next(generator)
except StopIteration:
break
continue
line1, component = self.visit_component(self.get_comp_type(line), generator)
if component is None:
break
components.append(component)
if line1 is None:
break
line = line1
continue
try:
line = next(generator)
except StopIteration:
break
app["components"].append(components)
app["permissions"].append(list(permissions))
return json.dumps(app)
except StopIteration:
traceback.print_exc()
pass