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
|
#!/usr/bin/python3
#
# Copyright (C) 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script can get info out of dexfiles using libdexfile.so external API
#
from abc import ABC
from ctypes import *
import os.path
import functools
import zipfile
libdexfile = CDLL(
os.path.expandvars("$ANDROID_HOST_OUT/lib64/libdexfile.so"))
DexFileStr = c_void_p
ExtDexFile = c_void_p
class ExtMethodInfo(Structure):
"""Output format for MethodInfo"""
_fields_ = [("offset", c_int32), ("len", c_int32), ("name", DexFileStr)]
AllMethodsCallback = CFUNCTYPE(c_int, POINTER(ExtMethodInfo), c_void_p)
libdexfile.ExtDexFileOpenFromFd.argtypes = [
c_int, c_size_t, c_char_p,
POINTER(DexFileStr),
POINTER(ExtDexFile)
]
libdexfile.ExtDexFileOpenFromFd.restype = c_int
libdexfile.ExtDexFileOpenFromMemory.argtypes = [
c_void_p,
POINTER(c_size_t), c_char_p,
POINTER(DexFileStr),
POINTER(ExtDexFile)
]
libdexfile.ExtDexFileOpenFromMemory.restype = c_int
libdexfile.ExtDexFileFree.argtypes = [ExtDexFile]
libdexfile.ExtDexFileGetAllMethodInfos.argtypes = [
ExtDexFile, c_int, AllMethodsCallback, c_void_p
]
libdexfile.ExtDexFileGetString.argtypes = [
DexFileStr, POINTER(c_size_t)
]
libdexfile.ExtDexFileGetString.restype = c_char_p
libdexfile.ExtDexFileFreeString.argtypes = [DexFileStr]
class DexClass(object):
"""Accessor for DexClass Data"""
def __init__(self, name):
self.name = name.strip()
self.arrays = name.count("[")
self.base_name = self.name if self.arrays == 0 else self.name[:-(
self.arrays * 2)]
def __repr__(self):
return self.name
@functools.cached_property
def descriptor(self):
"""The name as a descriptor"""
if self.base_name == "int":
return "[" * self.arrays + "I"
elif self.base_name == "short":
return "[" * self.arrays + "S"
elif self.base_name == "long":
return "[" * self.arrays + "J"
elif self.base_name == "char":
return "[" * self.arrays + "C"
elif self.base_name == "boolean":
return "[" * self.arrays + "Z"
elif self.base_name == "byte":
return "[" * self.arrays + "B"
elif self.base_name == "float":
return "[" * self.arrays + "F"
elif self.base_name == "double":
return "[" * self.arrays + "D"
elif self.base_name == "void":
return "[" * self.arrays + "V"
else:
return "[" * self.arrays + "L{};".format("/".join(
self.base_name.split(".")))
class Method(object):
"""Method info wrapper"""
def __init__(self, mi):
self.offset = mi.offset
self.len = mi.len
self.name = libdexfile.ExtDexFileGetString(
mi.name, byref(c_size_t(0))).decode("utf-8")
libdexfile.ExtDexFileFreeString(mi.name)
def __repr__(self):
return "(" + self.name + ")"
@functools.cached_property
def descriptor(self):
"""name as a descriptor"""
ret = DexClass(self.name.split(" ")[0])
non_ret = self.name[len(ret.name) + 1:]
arg_str = non_ret[non_ret.find("(") + 1:-1]
args = [] if arg_str == "" else map(
lambda a: DexClass(a.strip()).descriptor, arg_str.split(","))
class_and_meth = non_ret[0:non_ret.find("(")]
class_only = DexClass(class_and_meth[0:class_and_meth.rfind(".")])
meth = class_and_meth[class_and_meth.rfind(".") + 1:]
return "{cls}->{meth}({args}){ret}".format(
cls=class_only.descriptor,
meth=meth,
args="".join(args),
ret=ret.descriptor)
@functools.cached_property
def name_only(self):
"""name without the return-type or arguments in java format"""
ret = DexClass(self.name.split(" ")[0])
non_ret = self.name[len(ret.name) + 1:]
class_and_meth = non_ret[0:non_ret.find("(")]
return class_and_meth
@functools.cached_property
def klass(self):
"""declaring DexClass."""
ret = DexClass(self.name.split(" ")[0])
non_ret = self.name[len(ret.name) + 1:]
class_and_meth = non_ret[0:non_ret.find("(")]
return DexClass(class_and_meth[0:class_and_meth.rfind(".")])
class BaseDexFile(ABC):
"""DexFile base class"""
def __init__(self):
self.ext_dex_file_ = None
return
@functools.cached_property
def methods(self):
"""Methods in the dex-file"""
meths = []
@AllMethodsCallback
def my_cb(info, _):
"""Callback visitor for method infos"""
meths.append(Method(info[0]))
return 0
libdexfile.ExtDexFileGetAllMethodInfos(self.ext_dex_file_,
c_int(1), my_cb, c_void_p())
return meths
class FdDexFile(BaseDexFile):
"""DexFile using an opened file-descriptor"""
def __init__(self, fd, loc):
super().__init__()
res_fle_ptr = pointer(c_void_p())
err_ptr = pointer(c_void_p())
res = libdexfile.ExtDexFileOpenFromFd(
c_int(fd), 0, create_string_buffer(bytes(loc, "utf-8")), err_ptr,
res_fle_ptr)
if res == 0:
err = libdexfile.ExtDexFileGetString(err_ptr.contents,
byref(c_size_t()))
out = Exception("Failed to open file: {}. Error was: {}".format(loc, err))
libdexfile.ExtDexFileFreeString(err_ptr.contents)
raise out
self.ext_dex_file_ = res_fle_ptr.contents
class FileDexFile(FdDexFile):
"""DexFile using a file"""
def __init__(self, file, loc):
if type(file) == str:
self.file = open(file, "rb")
else:
self.file = file
super().__init__(self.file.fileno(), loc)
class MemDexFile(BaseDexFile):
"""DexFile using memory"""
def __init__(self, dat, loc):
assert type(dat) == bytes
super().__init__()
# Don't want GC to screw us over.
self.mem_ref = (c_byte * len(dat)).from_buffer_copy(dat)
res_fle_ptr = pointer(c_void_p())
err_ptr = pointer(c_void_p())
res = libdexfile.ExtDexFileOpenFromMemory(
self.mem_ref, byref(c_size_t(len(dat))),
create_string_buffer(bytes(loc, "utf-8")), err_ptr, res_fle_ptr)
if res == 0:
err = libdexfile.ExtDexFileGetString(err_ptr.contents,
byref(c_size_t()))
out = Exception("Failed to open file: {}. Error was: {}".format(loc, err))
libdexfile.ExtDexFileFreeString(err_ptr.contents)
raise out
self.ext_dex_file_ = res_fle_ptr.contents
def OpenJar(fle):
"""Opens all classes[0-9]*.dex files in a zip archive"""
res = []
with zipfile.ZipFile(fle) as zf:
for f in zf.namelist():
if f.endswith(".dex") and f.startswith("classes"):
res.append(
MemDexFile(zf.read(f), "classes" if type(fle) != str else fle))
return res
|