Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # |
| 3 | # Copyright (C) 2021 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 18 | # This script can get info out of dexfiles using libdexfile.so external API |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 19 | # |
| 20 | |
| 21 | from abc import ABC |
| 22 | from ctypes import * |
| 23 | import os.path |
| 24 | import functools |
| 25 | import zipfile |
| 26 | |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 27 | libdexfile = CDLL( |
Alex Light | e6a0a20 | 2021-03-30 17:26:31 -0700 | [diff] [blame] | 28 | os.path.expandvars("$ANDROID_HOST_OUT/lib64/libdexfiled.so")) |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 29 | |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 30 | ExtDexFile = c_void_p |
| 31 | |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 32 | class ExtMethodInfo(Structure): |
| 33 | """Output format for MethodInfo""" |
Alex Light | e6a0a20 | 2021-03-30 17:26:31 -0700 | [diff] [blame] | 34 | _fields_ = [("sizeof_struct", c_size_t), |
| 35 | ("addr", c_int32), |
| 36 | ("size", c_int32), |
| 37 | ("name", POINTER(c_char)), |
| 38 | ("name_size", c_size_t)] |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 39 | |
Alex Light | e6a0a20 | 2021-03-30 17:26:31 -0700 | [diff] [blame] | 40 | AllMethodsCallback = CFUNCTYPE(c_int, c_void_p, POINTER(ExtMethodInfo)) |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 41 | libdexfile.ExtDexFileOpenFromMemory.argtypes = [ |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 42 | c_void_p, |
Alex Light | e6a0a20 | 2021-03-30 17:26:31 -0700 | [diff] [blame] | 43 | POINTER(c_size_t), |
| 44 | c_char_p, |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 45 | POINTER(ExtDexFile) |
| 46 | ] |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 47 | libdexfile.ExtDexFileOpenFromMemory.restype = c_int |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 48 | libdexfile.ExtDexFileGetAllMethodInfos.argtypes = [ |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 49 | ExtDexFile, c_int, AllMethodsCallback, c_void_p |
| 50 | ] |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 51 | |
| 52 | class DexClass(object): |
| 53 | """Accessor for DexClass Data""" |
| 54 | |
| 55 | def __init__(self, name): |
| 56 | self.name = name.strip() |
| 57 | self.arrays = name.count("[") |
| 58 | self.base_name = self.name if self.arrays == 0 else self.name[:-( |
| 59 | self.arrays * 2)] |
| 60 | |
| 61 | def __repr__(self): |
| 62 | return self.name |
| 63 | |
| 64 | @functools.cached_property |
| 65 | def descriptor(self): |
| 66 | """The name as a descriptor""" |
| 67 | if self.base_name == "int": |
| 68 | return "[" * self.arrays + "I" |
| 69 | elif self.base_name == "short": |
| 70 | return "[" * self.arrays + "S" |
| 71 | elif self.base_name == "long": |
| 72 | return "[" * self.arrays + "J" |
| 73 | elif self.base_name == "char": |
| 74 | return "[" * self.arrays + "C" |
| 75 | elif self.base_name == "boolean": |
| 76 | return "[" * self.arrays + "Z" |
| 77 | elif self.base_name == "byte": |
| 78 | return "[" * self.arrays + "B" |
| 79 | elif self.base_name == "float": |
| 80 | return "[" * self.arrays + "F" |
| 81 | elif self.base_name == "double": |
| 82 | return "[" * self.arrays + "D" |
| 83 | elif self.base_name == "void": |
| 84 | return "[" * self.arrays + "V" |
| 85 | else: |
| 86 | return "[" * self.arrays + "L{};".format("/".join( |
| 87 | self.base_name.split("."))) |
| 88 | |
| 89 | |
| 90 | class Method(object): |
| 91 | """Method info wrapper""" |
| 92 | |
| 93 | def __init__(self, mi): |
Alex Light | e6a0a20 | 2021-03-30 17:26:31 -0700 | [diff] [blame] | 94 | self.offset = mi.addr |
| 95 | self.len = mi.size |
| 96 | self.name = string_at(mi.name, mi.name_size).decode("utf-8") |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 97 | |
| 98 | def __repr__(self): |
| 99 | return "(" + self.name + ")" |
| 100 | |
| 101 | @functools.cached_property |
| 102 | def descriptor(self): |
| 103 | """name as a descriptor""" |
| 104 | ret = DexClass(self.name.split(" ")[0]) |
| 105 | non_ret = self.name[len(ret.name) + 1:] |
| 106 | arg_str = non_ret[non_ret.find("(") + 1:-1] |
| 107 | args = [] if arg_str == "" else map( |
| 108 | lambda a: DexClass(a.strip()).descriptor, arg_str.split(",")) |
| 109 | class_and_meth = non_ret[0:non_ret.find("(")] |
| 110 | class_only = DexClass(class_and_meth[0:class_and_meth.rfind(".")]) |
| 111 | meth = class_and_meth[class_and_meth.rfind(".") + 1:] |
| 112 | return "{cls}->{meth}({args}){ret}".format( |
| 113 | cls=class_only.descriptor, |
| 114 | meth=meth, |
| 115 | args="".join(args), |
| 116 | ret=ret.descriptor) |
| 117 | |
| 118 | @functools.cached_property |
| 119 | def name_only(self): |
| 120 | """name without the return-type or arguments in java format""" |
| 121 | ret = DexClass(self.name.split(" ")[0]) |
| 122 | non_ret = self.name[len(ret.name) + 1:] |
| 123 | class_and_meth = non_ret[0:non_ret.find("(")] |
| 124 | return class_and_meth |
| 125 | |
| 126 | @functools.cached_property |
| 127 | def klass(self): |
| 128 | """declaring DexClass.""" |
| 129 | ret = DexClass(self.name.split(" ")[0]) |
| 130 | non_ret = self.name[len(ret.name) + 1:] |
| 131 | class_and_meth = non_ret[0:non_ret.find("(")] |
| 132 | return DexClass(class_and_meth[0:class_and_meth.rfind(".")]) |
| 133 | |
| 134 | |
| 135 | class BaseDexFile(ABC): |
| 136 | """DexFile base class""" |
| 137 | |
| 138 | def __init__(self): |
| 139 | self.ext_dex_file_ = None |
| 140 | return |
| 141 | |
| 142 | @functools.cached_property |
| 143 | def methods(self): |
| 144 | """Methods in the dex-file""" |
| 145 | meths = [] |
| 146 | |
| 147 | @AllMethodsCallback |
Alex Light | e6a0a20 | 2021-03-30 17:26:31 -0700 | [diff] [blame] | 148 | def my_cb(_, info): |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 149 | """Callback visitor for method infos""" |
| 150 | meths.append(Method(info[0])) |
| 151 | return 0 |
| 152 | |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 153 | libdexfile.ExtDexFileGetAllMethodInfos(self.ext_dex_file_, |
| 154 | c_int(1), my_cb, c_void_p()) |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 155 | return meths |
| 156 | |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 157 | class MemDexFile(BaseDexFile): |
| 158 | """DexFile using memory""" |
| 159 | |
| 160 | def __init__(self, dat, loc): |
| 161 | assert type(dat) == bytes |
| 162 | super().__init__() |
| 163 | # Don't want GC to screw us over. |
| 164 | self.mem_ref = (c_byte * len(dat)).from_buffer_copy(dat) |
| 165 | res_fle_ptr = pointer(c_void_p()) |
Martin Stjernholm | d3e9ff3 | 2021-03-16 00:44:25 +0000 | [diff] [blame] | 166 | res = libdexfile.ExtDexFileOpenFromMemory( |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 167 | self.mem_ref, byref(c_size_t(len(dat))), |
Alex Light | e6a0a20 | 2021-03-30 17:26:31 -0700 | [diff] [blame] | 168 | create_string_buffer(bytes(loc, "utf-8")), res_fle_ptr) |
| 169 | if res != 0: |
| 170 | raise Exception("Failed to open file: {}. Error {}.".format(loc, res)) |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 171 | self.ext_dex_file_ = res_fle_ptr.contents |
| 172 | |
Alex Light | e6a0a20 | 2021-03-30 17:26:31 -0700 | [diff] [blame] | 173 | class FileDexFile(MemDexFile): |
| 174 | """DexFile using a file""" |
| 175 | |
| 176 | def __init__(self, file, loc): |
| 177 | if type(file) == str: |
| 178 | self.file = open(file, "rb") |
| 179 | self.loc = file |
| 180 | else: |
| 181 | self.file = file |
| 182 | self.loc = "file_obj" |
| 183 | super().__init__(self.file.read(), self.loc) |
Alex Light | 4fa4835 | 2020-12-29 13:45:04 -0800 | [diff] [blame] | 184 | |
| 185 | def OpenJar(fle): |
| 186 | """Opens all classes[0-9]*.dex files in a zip archive""" |
| 187 | res = [] |
| 188 | with zipfile.ZipFile(fle) as zf: |
| 189 | for f in zf.namelist(): |
| 190 | if f.endswith(".dex") and f.startswith("classes"): |
| 191 | res.append( |
| 192 | MemDexFile(zf.read(f), "classes" if type(fle) != str else fle)) |
| 193 | return res |