Dump il2cpp 通常有两种方法,一种是用 Il2CppDumper 加载二进制文件直接dump. 另一种是用Zygisk-Il2CppDumper注入目标进程来dump il2cpp.接下来介绍一种我常用的方法。
原理
工具
Qiling(https://qiling.io/) 一个系统模拟框架。
准备
attach -p pid
snapshot data
[OS64]
load_address = 0x8FFF000000000000
stack_address = 0x8FFF800000000000
stack_size = 0x8000000
mmap_address = 0
加载内存镜像
def load_memory(mu):
idx = 0
for region in memory_info["regions"]:
file = region["file"]
if file.startswith("/dev/kgsl") or region["prot"] == 0 or region["desc"].endswith("stack]"):
continue
if 'aidl' in file or 'hidl' in file or 'vndk' in file or 'android.hardware' in file
or file.endswith("dex") or file.endswith("jar") or file.endswith("apk") or file.endswith("art")
or file.endswith("oat") or 'dalvik' in file or 'dalvik' in region['desc'] or file.startswith('/vendor') or 'hardware' in file:
continue
size = region["end"] - region["begin"]
mu.mem_map(region["begin"], size)
memory_data.seek(region["saved_offset"], 0)
mem = memory_data.read(region["saved_size"])
mu.mem_write(region["begin"], mem)
del mem
print(f"Load {idx}/{len(memory_info['regions'])} {region['begin']:x}-{region['end']:x} {size} {region['file']} {region['desc']}")
idx += 1
ql = Qiling(["dump.elf"],
rootfs='./rootfs',
verbose=QL_VERBOSE.OFF,
profile='./dump.ql',
ostype="Linux",
archtype="ARM64")
load_memory(ql.uc)
END_ADDRESS = 0x55aa55aa55aa55aa
STACK_ADDRESS = 0x8FFF800000000000
STACK_SIZE = 0x8000000
TLS_ADDRESS = STACK_ADDRESS + 0x1000
TCB_ADDRESS = TLS_ADDRESS + 0x1000
BIONIC_TLS_ADDRESS = TCB_ADDRESS + 0x1000
# TLS
tls = struct.pack('<QQQQQQQQQ',
BIONIC_TLS_ADDRESS, # BIONIC_TLS
0, # DTV
TCB_ADDRESS, # THREAD ID
# 0x778ca4d508-16,
0, # APP
0, # OGL
0, # OGL API
0, # STACK GUARD
0, # SANITIZER
0, # ART THREAD TLS
)
ql.uc.mem_write(TLS_ADDRESS, tls)
class pthread_attr_t(ctypes.Structure):
_fields_ = [
("flags", ctypes.c_uint32),
("stack_base", ctypes.c_void_p),
("stack_size", ctypes.c_size_t),
]
class pthread_internal_t(ctypes.Structure):
_fields_ = [
("next", ctypes.c_void_p),
("prev", ctypes.c_void_p),
("tid", ctypes.c_int),
("cache_pid_and_vforked", ctypes.c_uint32),
("attr", pthread_attr_t),
]
thread_attr = pthread_attr_t(0, STACK_ADDRESS, STACK_SIZE)
thread = pthread_internal_t(0, 0, 0, 0, thread_attr)
ql.uc.mem_write(TCB_ADDRESS, bytes(thread))
ql.uc.reg_write(UC_ARM64_REG_TPIDR_EL0, TLS_ADDRESS + 8)
HEAP_ADDRESS = STACK_ADDRESS + 0x8000
def malloc(*args):
global HEAP_ADDRESS
sz = ql.uc.reg_read(UC_ARM64_REG_X0)
ql.uc.reg_write(UC_ARM64_REG_X0, HEAP_ADDRESS)
HEAP_ADDRESS += (sz + 15) & ~15
# print(f"[py] malloc {sz}")
def free(*args):
pass
def calloc(*args):
global HEAP_ADDRESS
n = ql.uc.reg_read(UC_ARM64_REG_X0)
sz = ql.uc.reg_read(UC_ARM64_REG_X1) * n
ql.uc.reg_write(UC_ARM64_REG_X0, HEAP_ADDRESS)
HEAP_ADDRESS += (sz + 15) & ~15
# print(f"[py] calloc {sz}")
def realloc(*args):
global HEAP_ADDRESS
ptr = ql.uc.reg_read(UC_ARM64_REG_X0)
sz = ql.uc.reg_read(UC_ARM64_REG_X1)
# print(f"[py] realloc 0x{ptr:x} {sz}")
if ptr != 0:
if sz == 0:
# free
return
data = ql.uc.mem_read(ptr, sz)
ql.uc.mem_write(HEAP_ADDRESS, bytes(data))
ql.uc.reg_write(UC_ARM64_REG_X0, HEAP_ADDRESS)
HEAP_ADDRESS += (sz + 15) & ~15
MALLOC_ADDR, FREE_ADDR, CALLOC_ADDR, REALLOC_ADDR = libc.get_funcs(memory_info, memory_data, 'libc.so', ['malloc', 'free', 'calloc', 'realloc'])
IL2CPP_BASE_DATA, IL2CPP_BASE_ADDR, IL2CPP_BASE_END = libc.read_so(memory_info, memory_data, "libil2cpp.so")
print(f'malloc 0x{MALLOC_ADDR:x}')
print(f'free 0x{FREE_ADDR:x}')
print(f'calloc 0x{CALLOC_ADDR:x}')
print(f'dlsym 0x{DLSYM_ADDR:x}')
ql.uc.mem_write(MALLOC_ADDR, b'xC0x03x5FxD6') # ret
ql.uc.mem_write(FREE_ADDR, b'xC0x03x5FxD6') # ret
ql.uc.mem_write(CALLOC_ADDR, b'xC0x03x5FxD6') # ret
ql.uc.mem_write(REALLOC_ADDR, b'xC0x03x5FxD6') # ret
ql.uc.hook_add(UC_HOOK_CODE, malloc, None, MALLOC_ADDR, MALLOC_ADDR + 4)
ql.uc.hook_add(UC_HOOK_CODE, free, None, FREE_ADDR, FREE_ADDR + 4)
ql.uc.hook_add(UC_HOOK_CODE, calloc, None, CALLOC_ADDR, CALLOC_ADDR + 4)
ql.uc.hook_add(UC_HOOK_CODE, realloc, None, REALLOC_ADDR, REALLOC_ADDR + 4)
DLSYM_ADDR, = libc.get_funcs(memory_info, memory_data, 'libdl.so', ['dlsym'])
dlsym_code = ql.uc.mem_read(DLSYM_ADDR, 16 * 4)
# mov lr, x2 => mov x2, x2
ql.uc.mem_write(DLSYM_ADDR + dlsym_code.find(bytes.fromhex('E2 03 1E AA')), bytes.fromhex('e2 03 02 aa'))
try:
ql.run(end=END_ADDRESS)
except:
pc = ql.uc.reg_read(UC_ARM64_REG_PC) - 8
print(f"pc 0x{pc:x}")
dis = ql.arch.disassembler
code = ql.uc.mem_read(pc, 4 * 8)
print(code.hex())
for i in list(dis.disasm(code, pc)):
print("0x%x:t%st%s" %(i.address, i.mnemonic, i.op_str))
raise
Payload
#include "il2cpp-tabledefs.h"
#include "il2cpp-class.h"
#define DO_API(r, n, p) r (*n) p
#include "il2cpp-api-functions.h"
#undef DO_API
typedef void* (*dlsym_t)(void *handle, const char *symbol, const void* caller_addr);
void _uprint(int fd, const char* format, ...) {
char buffer[4096];
va_list args;
va_start(args, format);
auto n = vsnprintf(buffer, 4096, format, args);
va_end(args);
write(fd, buffer, n);
}
#define uprint(...) _uprint(2, __VA_ARGS__)
int dump_fd = 2;
uint64_t _il2cpp_base = 0;
dlsym_t _dlsym = 0;
void init_il2cpp_api() {
#define DO_API(r, n, p) {
n = (r (*) p)_dlsym(NULL, #n, (void*)_il2cpp_base);
if(!n) {
uprint("api not found %sn", #n);
}
}
#include "il2cpp-api-functions.h"
#undef DO_API
}
struct Dump {
const Dump& operator <<(const char* str) const {
_uprint(dump_fd, "%s", str);
return *this;
}
const Dump& operator <<(unsigned long val) const {
_uprint(dump_fd, "%lu", val);
return *this;
}
const Dump& operator <<(long val) const {
_uprint(dump_fd, "%ld", val);
return *this;
}
};
const Dump dump{};
。。。
void dump_method(Il2CppClass *klass) {
。。。
}
void dump_property(Il2CppClass *klass) {
。。。
}
void dump_field(Il2CppClass *klass) {
。。。
}
void dump_type(const Il2CppType *type) {
。。。
}
extern "C"
int entry(dlsym_t dlsym, uintptr_t il2cpp_base)
{
_dlsym = dlsym;
_il2cpp_base = il2cpp_base;
uprint("payload dlsym %pn", dlsym);
dump_fd = open("/dump.cs", O_RDWR | O_CREAT, 0777);
。。。
uprint("done...n");
return 0;
}
源代码
后记
任何人不得将本文介绍的技术用于任何违法乱纪的事情,违者后果自负。
看雪ID:vrolife
https://bbs.kanxue.com/user-home-978503.htm
#
原文始发于微信公众号(看雪学苑):用 Qiling/Unicorn 框架来 dump il2cpp
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论