-
Notifications
You must be signed in to change notification settings - Fork 75
/
extract_jni.py
executable file
·339 lines (292 loc) · 10.2 KB
/
extract_jni.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
#!/usr/bin/env python3
import json
import multiprocessing
from datetime import datetime
from collections import Counter, namedtuple
from typing import Iterator, List
# from androguard.core.analysis.analysis import Analysis
from androguard.core.dex import ClassDefItem, EncodedMethod
from androguard.decompiler.util import TYPE_DESCRIPTOR
from androguard.core import dex as dx
from rich.console import Console
from rich.progress import (
BarColumn,
Progress,
TextColumn,
TimeRemainingColumn,
TimeElapsedColumn,
)
from io import BytesIO
from elftools.elf.elffile import ELFFile
from elftools.elf.sections import SymbolTableSection
DexFile = namedtuple("DexFile", ["name", "data"])
SoFile = namedtuple("SoFile", ["name", "data"])
JNI_COMMON = {
"JNI_OnLoad": ["jint", "JavaVM * vm, void * reserved"],
"JNI_OnUnload": ["void", "JavaVM * vm, void * reserved"],
}
__COMMON__ = [
{"mangle": "JNI_OnLoad", "ret": "jint", "args": ["JavaVM * vm", "void * reserved"]},
{
"mangle": "JNI_OnUnload",
"ret": "void",
"args": ["JavaVM * vm", "void * reserved"],
},
]
def get_type(atype):
"""
Retrieve the java type of a descriptor (e.g : I -> jint)
"""
res = TYPE_DESCRIPTOR.get(atype)
if res:
if res == "void":
return res
else:
return "j" + res
if atype[0] == "L":
if atype == "Ljava/lang/String;":
res = "jstring"
else:
res = "jobject"
elif atype[0] == "[":
if len(atype) == 2 and atype[1] in "ZBSCIJFD":
res = TYPE_DESCRIPTOR.get(atype[1])
else:
res = "object"
res = f"j{res}Array"
else:
print('Unknown descriptor: "%s".', atype)
res = "void"
return res
def mangle_unicode(input_str):
out = ""
for s in input_str:
i = ord(s)
if 0 <= i < 128:
out += s
else:
out += f"_{i:04x}"
return out
def escape(name: str):
name = name.replace("_", "_1")
name = name.replace(";", "_2")
name = name.replace("[", "_3")
name = mangle_unicode(name)
name = name.replace("/", "_")
return name
class JNIMethod:
def __init__(self, jclass, name, descriptor, static=False, overload=False):
self.jclass = jclass # fullname: e.g com.evilpan.Foo
self.name = name # method name
method_args, ret = descriptor[1:].rsplit(")", 1)
self.args = str(method_args).split() # list of smali type, space splited
self.ret = str(ret) # smali type
self.descriptor = f"({''.join(self.args)}){self.ret}"
self.static = static
self.overload = overload
@classmethod
def from_method(cls, em: EncodedMethod) -> "JNIMethod":
flags = em.get_access_flags_string().split()
if "native" not in flags:
return None
# Can be calculated this in the outside loop, but it doesn't really matters...
jclass = str(em.get_class_name()[1:-1].replace("/", "."))
name = str(em.name)
descriptor = str(em.get_descriptor())
return cls(jclass, name, descriptor, static="static" in flags)
@property
def native_name(self):
"""
return crosponding native C symbol name
https://docs.oracle.com/en/java/javase/16/docs/specs/jni/design.html
"""
name = escape(self.jclass + "." + self.name)
name = "Java_" + name.replace(".", "_")
if self.overload:
sig = "".join(self.args)
sig = escape(sig)
name = name + "__" + sig
return name
@property
def native_args(self):
# NOTE: ghidra pointer and type require space inside
native_args_list = [("JNIEnv *", "env")]
if self.static:
native_args_list.append(("jclass", "clazz"))
else:
native_args_list.append(("jobject", "thiz"))
return native_args_list + [
(get_type(arg), f"a{i + 1}") for i, arg in enumerate(self.args)
]
@property
def native_args_list(self) -> List[str]:
return [f"{t} {n}" for t, n in self.native_args]
@property
def native_ret(self):
return get_type(self.ret)
@property
def as_dict(self):
return {self.native_name: [self.native_ret, ", ".join(self.native_args_list)]}
@property
def as_json(self):
return {
"mangle": self.native_name,
"ret": self.native_ret,
"args": self.native_args_list,
"name": self.name,
"sig": self.descriptor,
}
def __repr__(self):
return f"{'static ' if self.static else ''}{self.name}({' '.join(self.args)}){self.ret}"
def __str__(self):
return f"JNIEXPORT {self.native_ret} JNICALL {self.native_name} ({', '.join(self.native_args_list)})"
def parse_class_def(cdef: ClassDefItem) -> List[JNIMethod]:
jms = []
names = []
for em in cdef.get_methods():
jm = JNIMethod.from_method(em)
if jm is None:
continue
jms.append(jm)
names.append(jm.name)
n = Counter(names)
for jm in jms:
if n.get(jm.name) > 1:
jm.overload = True
return jms
def extract_dex_files(apkfile) -> Iterator[DexFile]:
from zipfile import ZipFile
z = ZipFile(apkfile)
for info in z.infolist():
if info.filename.endswith(".dex"):
yield DexFile(info.filename, z.read(info))
def get_exported_functions(filedata):
out = {}
elffile = ELFFile(BytesIO(filedata))
symbol_tables = [
(idx, s)
for idx, s in enumerate(elffile.iter_sections())
if isinstance(s, SymbolTableSection)
]
# base = elffile.header.e_entry
for section_index, section in symbol_tables:
for nsym, symbol in enumerate(section.iter_symbols()):
if (
symbol.entry.st_info.type == "STT_FUNC"
and symbol.entry.st_shndx != "SHN_UNDEF"
):
out[symbol.name] = symbol["st_value"]
return out
def extract_so_files(apkfile: str) -> Iterator[SoFile]:
from zipfile import ZipFile
z = ZipFile(apkfile)
for info in z.infolist():
if info.filename.endswith(".so") and info.filename.startswith("lib/arm64-v8a"):
yield SoFile(info.filename, z.read(info))
def parse_so_sync(sofile: SoFile):
try:
funcs = get_exported_functions(sofile.data)
except Exception as e:
console = Console()
console.log(f"skip library {sofile.name}: {e}")
funcs = {}
return {k: v for k, v in funcs.items() if k.startswith("Java_") or k in JNI_COMMON}
def parse_dex_proc(dex: DexFile):
supress_andro_log()
dexInfo = {}
count = 0
try:
# Use dex.DEX to parse the DEX file
d = dx.DEX(dex.data)
except Exception as e:
return dex, count, e
for cdef in d.get_classes():
count += 1
methods = parse_class_def(cdef)
if not methods:
continue
className = methods[0].jclass
if className not in dexInfo:
dexInfo[className] = []
for m in methods:
dexInfo[className].append(m.as_json)
return dex, count, dexInfo
def parse_apk(apkfile, workers, fn_match=None, outfile=None):
console = Console()
console.log(f"Parsing {apkfile} with {workers} workers ...")
dexes = list(extract_dex_files(apkfile))
console.log(f"Found {len(dexes)} DEX files.")
total = sum(map(lambda d: len(d.data), dexes))
progress = Progress(
TextColumn("[progress.description]{task.description}"),
BarColumn(
complete_style="bar.complete",
finished_style="bar.finished",
pulse_style="bar.pulse",
),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeRemainingColumn(),
TimeElapsedColumn(),
console=console,
)
dexInfo = {"__COMMON__": __COMMON__}
num_classes = 0
t0 = datetime.now()
with progress:
task = progress.add_task("Analyzing Dex...", total=total)
with multiprocessing.Pool(workers) as pool:
result = pool.imap(parse_dex_proc, dexes)
for dex, count, res in result:
if count == 0:
console.log(
f"Parse {dex.name} ({len(dex.data)} bytes) [bold red]failed: {res}"
)
continue
console.log(
f"Parse {dex.name} ({len(dex.data)} bytes), found {count} classes."
)
num_classes += count
progress.update(task, advance=len(dex.data))
for className, methodData in res.items():
if fn_match and not fn_match(className):
continue
dexInfo.update({className: methodData})
console.log(f"Analyzed {num_classes} classes, cost: {datetime.now() - t0}")
# Parse the so information synchronously, since it's fast.
soInfo = {}
soFiles = list(extract_so_files(apkfile))
console.log(f"Found {len(soFiles)} so files.")
for soFile in soFiles:
possible_symbols = parse_so_sync(soFile)
if possible_symbols:
soInfo[soFile.name] = possible_symbols
console.log(f"Found {len(possible_symbols)} JNI symbols in {soFile.name}.")
output = {"dexInfo": dexInfo, "soInfo": soInfo}
if not outfile:
console.print_json(data=output)
else:
with open(outfile, "w", encoding="utf-8") as f:
json.dump(output, f, indent=2, ensure_ascii=False)
def supress_andro_log():
import sys
from loguru import logger
logger.remove()
logger.add(sys.stderr, level="WARNING")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("apk", help="/path/to/apk")
parser.add_argument(
"-j",
dest="workers",
type=int,
default=multiprocessing.cpu_count(),
help="parse apk with multiple workers(processes)",
)
parser.add_argument(
"-o", dest="outfile", help="save JNI methods as formatted json file"
)
args = parser.parse_args()
parse_apk(args.apk, args.workers, outfile=args.outfile)