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
| #include <string.h> #include <cstdint> #include <dlfcn.h> #include <link.h> #include <elf.h> #include <iostream> #include <unordered_map> #include <map> #include <vector> #include <unistd.h>
#include "hook_function.h" #include "android/log.h" #include "Dobby/dobby.h" #include "dexFile.h" #include "CodeItem.h" #include "bytehook.h" #include "sys/mman.h"
#if defined(__LP64__) #define LIB_DIR "lib64" #else #define LIB_DIR "lib" #endif
#define DLOGE(...) __android_log_print(ANDROID_LOG_ERROR, "YvYShell", __VA_ARGS__) #define DLOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "YvYShell", __VA_ARGS__)
extern std::unordered_map<int, std::unordered_map<uint32_t, CodeItem*>> dexMap; typedef void* (*mmap_func_t)(void*, size_t, int, int, int, off_t);
bool hook_defineClass(); void hook_execve(); void hook_mmap();
static uint32_t gnu_hash(const char *s) { uint32_t h = 5381; for (unsigned char c = static_cast<unsigned char>(*s); c != 0; c = static_cast<unsigned char>(*++s)) { h = (h << 5) + h + c; } return h; }
struct SymbolResolveContext { const char *soname; const char *symbol; void *address; bool matchedLibrary; };
static int resolve_symbol_from_loaded_so(struct dl_phdr_info *info, size_t size, void *data) { auto *ctx = reinterpret_cast<SymbolResolveContext *>(data); if (info == nullptr || info->dlpi_name == nullptr || strstr(info->dlpi_name, ctx->soname) == nullptr) { return 0; } ctx->matchedLibrary = true;
const ElfW(Phdr) *dynamic_phdr = nullptr; for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) { if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) { dynamic_phdr = &info->dlpi_phdr[i]; break; } } if (dynamic_phdr == nullptr) { DLOGE("PT_DYNAMIC not found. soname=%s", info->dlpi_name); return 0; }
const ElfW(Dyn) *dyn = reinterpret_cast<const ElfW(Dyn) *>(info->dlpi_addr + dynamic_phdr->p_vaddr); const ElfW(Sym) *symtab = nullptr; const char *strtab = nullptr; const uint32_t *hash = nullptr; const uint32_t *gnuHash = nullptr; size_t syment = sizeof(ElfW(Sym));
for (const ElfW(Dyn) *entry = dyn; entry->d_tag != DT_NULL; ++entry) { switch (entry->d_tag) { case DT_SYMTAB: symtab = reinterpret_cast<const ElfW(Sym) *>(info->dlpi_addr + entry->d_un.d_ptr); break; case DT_STRTAB: strtab = reinterpret_cast<const char *>(info->dlpi_addr + entry->d_un.d_ptr); break; case DT_HASH: hash = reinterpret_cast<const uint32_t *>(info->dlpi_addr + entry->d_un.d_ptr); break; case DT_GNU_HASH: gnuHash = reinterpret_cast<const uint32_t *>(info->dlpi_addr + entry->d_un.d_ptr); break; case DT_SYMENT: syment = entry->d_un.d_val; break; default: break; } }
if (symtab == nullptr || strtab == nullptr || syment != sizeof(ElfW(Sym))) { return 0; }
if (hash != nullptr) { uint32_t nchain = hash[1]; for (uint32_t i = 0; i < nchain; ++i) { const ElfW(Sym) *sym = reinterpret_cast<const ElfW(Sym) *>(reinterpret_cast<const char *>(symtab) + i * syment); const char *name = strtab + sym->st_name; if (strcmp(name, ctx->symbol) == 0) { ctx->address = reinterpret_cast<void *>(info->dlpi_addr + sym->st_value); return 1; } } }
if (gnuHash != nullptr) { uint32_t nbuckets = gnuHash[0]; uint32_t symoffset = gnuHash[1]; uint32_t bloom_size = gnuHash[2]; const ElfW(Addr) *bloom = reinterpret_cast<const ElfW(Addr) *>(gnuHash + 4); const uint32_t *buckets = reinterpret_cast<const uint32_t *>(bloom + bloom_size); const uint32_t *chain = buckets + nbuckets; uint32_t hashValue = gnu_hash(ctx->symbol); uint32_t bucket = buckets[hashValue % nbuckets]; if (bucket >= symoffset) { for (uint32_t i = bucket;; ++i) { uint32_t chainValue = chain[i - symoffset]; if ((chainValue | 1U) == (hashValue | 1U)) { const ElfW(Sym) *sym = reinterpret_cast<const ElfW(Sym) *>(reinterpret_cast<const char *>(symtab) + i * syment); const char *name = strtab + sym->st_name; if (strcmp(name, ctx->symbol) == 0) { ctx->address = reinterpret_cast<void *>(info->dlpi_addr + sym->st_value); return 1; } } if ((chainValue & 1U) != 0) { break; } } } }
return 0; }
void* fakeDefineClass(void* thiz, void* self, const char* descriptor, size_t hash, void* class_loader, const void* dex_file, const void* dex_class_def);
void patchClass(const char *descriptor, const void *dex_file, const void *dex_class_def); void patchMethod(uint8_t *begin, const char *location, uint64_t dexSize, int dexIndex, uint32_t method_idx, uint32_t code_off);
void hook_function() { hook_execve(); hook_mmap(); bool hook_res = hook_defineClass(); if (!hook_res) { DLOGE("hook defineClass failed"); return; } DLOGD("hook defineClass success"); }
void* fake_mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) { BYTEHOOK_STACK_SCOPE(); int new_prot = prot | PROT_WRITE; return BYTEHOOK_CALL_PREV(fake_mmap, addr, size, new_prot, flags, fd, offset); }
void hook_mmap() { bytehook_stub_t stub = bytehook_hook_single("/apex/com.android.art/" LIB_DIR "/libart.so", "libc.so", "mmap", (void *)fake_mmap, nullptr, nullptr); if (stub != nullptr) { DLOGD("hook mmap success"); } else { DLOGE("hook mmap failed"); } }
int fake_execve(const char *pathname, char *const argv[], char *const envp[]) { BYTEHOOK_STACK_SCOPE(); if (pathname != nullptr && strstr(pathname, "dex2oat") != nullptr) { DLOGD("blocked dex2oat: %s", pathname); return -1; } return BYTEHOOK_CALL_PREV(fake_execve, pathname, argv, envp); }
void hook_execve() { bytehook_stub_t stub = bytehook_hook_single("/apex/com.android.art/" LIB_DIR "/libart.so", "libc.so", "execve", (void *)fake_execve, nullptr, nullptr); if (stub != nullptr) { DLOGD("hook execve success"); } else { DLOGE("hook execve failed"); } }
bool hook_defineClass() { const char *sym = "_ZN3art11ClassLinker11DefineClassEPNS_6ThreadEPKcmNS_6HandleINS_6mirror11ClassLoaderEEERKNS_7DexFileERKNS_3dex8ClassDefE";
void *defineClassAddress = dlsym(RTLD_DEFAULT, sym); if (defineClassAddress != nullptr) { DLOGD("defineClassAddress 获取成功"); } else { DLOGD("defineClassAddress 获取失败"); } if (defineClassAddress == nullptr) { SymbolResolveContext ctx = {"libart.so", sym, nullptr, false}; dl_iterate_phdr(resolve_symbol_from_loaded_so, &ctx); defineClassAddress = ctx.address; if (!ctx.matchedLibrary) { DLOGE("dl_iterate_phdr did not match any loaded libart.so"); } } if (defineClassAddress == nullptr) { DLOGE("resolve DefineClass failed. symbol=%s", sym); return false; }
int hookRes = DobbyHook(defineClassAddress, (void *)fakeDefineClass, (void **)&origDefineClass); if (hookRes != 0) { DLOGE("DobbyHook DefineClass failed: %d", hookRes); return false; } return true; }
void* fakeDefineClass(void* thiz, void* self, const char* descriptor, size_t hash, void* class_loader, const void* dex_file, const void* dex_class_def) { if (origDefineClass != nullptr) { patchClass(descriptor, dex_file, dex_class_def); void* result = origDefineClass(thiz, self, descriptor, hash, class_loader, dex_file, dex_class_def); return result; } return nullptr; }
void patchClass(const char *descriptor, const void *dex_file, const void *dex_class_def) { if (dex_file != nullptr) { auto* dexFile = (dex::dexFile*)dex_file; std::string location = dexFile->location; if (location.find("source.zip") == std::string::npos) { return; } uint8_t* begin = (uint8_t*)dexFile->begin; uint64_t dexSize = dexFile->header->file_size; auto* class_def = (dex::classDef*)dex_class_def; int dexIndex = 0; std::string locStr(location); size_t pos = locStr.find("classes"); if (pos != std::string::npos) { size_t dotPos = locStr.find(".dex", pos); if (dotPos != std::string::npos && dotPos > pos + 7) { std::string numStr = locStr.substr(pos + 7, dotPos - (pos + 7)); dexIndex = std::stoi(numStr) - 1; } } if (class_def->class_data_off != 0) { size_t num = 0; auto* class_data = (uint8_t*)((uint8_t*)begin + class_def->class_data_off); uint64_t static_fields_size = 0; num += DexFile::readUleb128((class_data + num), &static_fields_size); uint64_t instance_fields_size = 0; num += DexFile::readUleb128((class_data + num), &instance_fields_size); uint64_t direct_methods_size = 0; num += DexFile::readUleb128((class_data + num), &direct_methods_size); uint64_t virtual_methods_size = 0; num += DexFile::readUleb128((class_data + num), &virtual_methods_size); num += DexFile::getFieldSize((class_data + num), static_fields_size); num += DexFile::getFieldSize((class_data + num), instance_fields_size);
auto* directMethods = new dex::Method[direct_methods_size]; num += DexFile::readMethod((class_data + num), directMethods, direct_methods_size); auto* virtualMethods = new dex::Method[virtual_methods_size]; num += DexFile::readMethod((class_data + num), virtualMethods, virtual_methods_size);
for (uint64_t i = 0; i < direct_methods_size; i++) { auto method = directMethods[i]; patchMethod(begin, location.c_str(), dexSize, dexIndex, method.method_idx_diff, method.code_off); } for (uint64_t i = 0; i < virtual_methods_size; i++) { auto method = virtualMethods[i]; patchMethod(begin, location.c_str(), dexSize, dexIndex, method.method_idx_diff, method.code_off); } delete[] virtualMethods; delete[] directMethods; } } }
void patchMethod(uint8_t *begin, const char *location, uint64_t dexSize, int dexIndex, uint32_t method_idx, uint32_t code_off) { if (code_off == 0) { return; }
if (dexMap.find(dexIndex) == dexMap.end()) { return; }
auto &methodMap = dexMap[dexIndex]; auto it = methodMap.find(method_idx); if (it == methodMap.end()) { return; }
CodeItem* item = it->second; if (item == nullptr) { return; }
uint8_t* realCodeItemAddr = begin + code_off; uint8_t* realInsnsAddr = realCodeItemAddr + 16; size_t pageSize = static_cast<size_t>(sysconf(_SC_PAGESIZE)); uintptr_t pageStart = reinterpret_cast<uintptr_t>(realInsnsAddr) & ~(pageSize - 1); uintptr_t pageEnd = (reinterpret_cast<uintptr_t>(realInsnsAddr) + item->getInsnsSize() + pageSize - 1) & ~(pageSize - 1); size_t pageLen = pageEnd - pageStart; if (mprotect(reinterpret_cast<void *>(pageStart), pageLen, PROT_READ | PROT_WRITE) != 0) { DLOGE("patchMethod mprotect rw failed. dexIndex=%d methodId=%u addr=%p len=%zu", dexIndex, method_idx, realInsnsAddr, pageLen); return; } memcpy(realInsnsAddr, item->getInsns(), item->getInsnsSize()); if (mprotect(reinterpret_cast<void *>(pageStart), pageLen, PROT_READ) != 0) { DLOGE("patchMethod mprotect ro failed. dexIndex=%d methodId=%u addr=%p len=%zu", dexIndex, method_idx, realInsnsAddr, pageLen); } }
|