dsh-reverse-plugin 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/package.json +3 -3
- package/preset/reverse.patch.yml +5 -5
- package/skills/android/adb-install/SKILL.md +50 -0
- package/skills/android/adb-install/agents/openai.yaml +4 -0
- package/skills/android/adb-install/references/linux.md +35 -0
- package/skills/android/adb-install/references/macos.md +67 -0
- package/skills/android/adb-install/references/sources.md +73 -0
- package/skills/android/adb-install/references/windows.md +73 -0
- package/skills/android/adb-install/scripts/list_downloads.py +105 -0
- package/skills/android/android-hook/SKILL.md +69 -0
- package/skills/android/android-hook/agents/openai.yaml +4 -0
- package/skills/android/android-hook/references/cli-and-frida-trace.md +59 -0
- package/skills/android/android-hook/references/frida-16-to-17.md +39 -0
- package/skills/android/android-hook/references/native-and-cmodule-api.md +85 -0
- package/skills/android/android-hook/references/process-and-memory-api.md +135 -0
- package/skills/android/android-hook/references/script-index.md +86 -0
- package/skills/android/android-hook/scripts/01-crypto/crypto-auto-dump.js +181 -0
- package/skills/android/android-hook/scripts/01-crypto/hook-base64.js +15 -0
- package/skills/android/android-hook/scripts/01-crypto/hook-cipher.js +37 -0
- package/skills/android/android-hook/scripts/02-network-ssl/bypass-conscrypt-pinning.js +26 -0
- package/skills/android/android-hook/scripts/02-network-ssl/bypass-ssl-pinning.js +54 -0
- package/skills/android/android-hook/scripts/02-network-ssl/bypass-vpn-proxy.js +39 -0
- package/skills/android/android-hook/scripts/02-network-ssl/dump-client-cert-p12.js +55 -0
- package/skills/android/android-hook/scripts/02-network-ssl/hook-url-okhttp.js +29 -0
- package/skills/android/android-hook/scripts/02-network-ssl/hook-webview.js +35 -0
- package/skills/android/android-hook/scripts/03-java-trace/activity-control.js +36 -0
- package/skills/android/android-hook/scripts/03-java-trace/art-method-spec.js +110 -0
- package/skills/android/android-hook/scripts/03-java-trace/hook-json.js +37 -0
- package/skills/android/android-hook/scripts/03-java-trace/hook-map.js +39 -0
- package/skills/android/android-hook/scripts/03-java-trace/hook-packed-classloader.js +29 -0
- package/skills/android/android-hook/scripts/03-java-trace/hook-storage.js +66 -0
- package/skills/android/android-hook/scripts/03-java-trace/hook-string.js +57 -0
- package/skills/android/android-hook/scripts/03-java-trace/hook-ui-log.js +19 -0
- package/skills/android/android-hook/scripts/03-java-trace/java-inspect-utils.js +81 -0
- package/skills/android/android-hook/scripts/03-java-trace/register-java-class.js +31 -0
- package/skills/android/android-hook/scripts/04-native-hook/enum-modules-imports.js +37 -0
- package/skills/android/android-hook/scripts/04-native-hook/hook-dlopen.js +60 -0
- package/skills/android/android-hook/scripts/04-native-hook/hook-dlsym.js +25 -0
- package/skills/android/android-hook/scripts/04-native-hook/hook-init-array.js +76 -0
- package/skills/android/android-hook/scripts/04-native-hook/hook-jni-newstringutf.js +38 -0
- package/skills/android/android-hook/scripts/04-native-hook/hook-jni-registernatives.js +43 -0
- package/skills/android/android-hook/scripts/04-native-hook/hook-libc-io-proc.js +48 -0
- package/skills/android/android-hook/scripts/04-native-hook/hook-mem-str.js +87 -0
- package/skills/android/android-hook/scripts/04-native-hook/hook-pthread.js +69 -0
- package/skills/android/android-hook/scripts/04-native-hook/wait-for-module.js +24 -0
- package/skills/android/android-hook/scripts/05-anti-frida/bypass-strstr-anti-frida.js +38 -0
- package/skills/android/android-hook/scripts/05-anti-frida/patch-memfd-frida-str.js +66 -0
- package/skills/android/android-hook/scripts/05-anti-frida/read-proc-maps.js +44 -0
- package/skills/android/android-hook/scripts/05-anti-frida/strip-rwxp.js +43 -0
- package/skills/android/android-hook/scripts/06-unpack-dump/dump-dex-defineclass.js +104 -0
- package/skills/android/android-hook/scripts/06-unpack-dump/load-dex.js +19 -0
- package/skills/android/android-hook/scripts/07-lowlevel-trace/arm64-patch-code.js +40 -0
- package/skills/android/android-hook/scripts/07-lowlevel-trace/got-hook.js +213 -0
- package/skills/android/android-hook/scripts/07-lowlevel-trace/stalker-cmodule-events.js +69 -0
- package/skills/android/android-hook/scripts/07-lowlevel-trace/stalker-trace.js +107 -0
- package/skills/android/android-hook/scripts/07-lowlevel-trace/yahfa-loader.mjs +292 -0
- package/skills/android/android-reverse/SKILL.md +73 -0
- package/skills/android/android-reverse/agents/openai.yaml +4 -0
- package/skills/android/android-reverse/references/developer-options.md +29 -0
- package/skills/android/android-root/SKILL.md +65 -0
- package/skills/android/android-root/agents/openai.yaml +4 -0
- package/skills/android/android-root/references/apatch.md +128 -0
- package/skills/android/android-root/references/kernelsu.md +134 -0
- package/skills/android/android-root/references/post-root-apps.md +31 -0
- package/skills/android/android-root/references/preflight-and-fastboot.md +208 -0
- package/skills/android/frida-install/SKILL.md +54 -0
- package/skills/android/frida-install/agents/openai.yaml +4 -0
- package/skills/android/frida-install/references/install-and-verify.md +126 -0
- package/skills/android/frida-install/references/rusda-releases.md +55 -0
- package/skills/android/reqable-capture/SKILL.md +70 -0
- package/skills/android/reqable-capture/agents/openai.yaml +4 -0
- package/skills/android/reqable-capture/references/capture-troubleshooting.md +56 -0
- package/skills/android/reqable-capture/references/cli-reference.md +110 -0
- package/skills/android/reqable-capture/scripts/reqable_cli.py +849 -0
- package/skills/android/reqable-install/SKILL.md +65 -0
- package/skills/android/reqable-install/agents/openai.yaml +4 -0
- package/skills/android/reqable-install/references/install-and-cert.md +96 -0
- package/skills/android/reqable-install/references/proxy-and-verify.md +63 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purpose 扫描 /proc/self/maps 将 rwxp 可疑可读可写可执行页降权为 r-x
|
|
3
|
+
* @frida >= 17(同时兼容 Frida 16.7+)
|
|
4
|
+
* @usage frida -U -F -l scripts/05-anti-frida/strip-rwxp.js
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
function readMapsNative() {
|
|
8
|
+
const libc = Process.getModuleByName('libc.so');
|
|
9
|
+
const open = new NativeFunction(libc.getExportByName('open'), 'int', ['pointer', 'int']);
|
|
10
|
+
const read = new NativeFunction(libc.getExportByName('read'), 'ssize_t', ['int', 'pointer', 'size_t']);
|
|
11
|
+
const close = new NativeFunction(libc.getExportByName('close'), 'int', ['int']);
|
|
12
|
+
|
|
13
|
+
const fd = open(Memory.allocUtf8String('/proc/self/maps'), 0);
|
|
14
|
+
if (fd < 0) return '';
|
|
15
|
+
const bufSize = 4096;
|
|
16
|
+
const buf = Memory.alloc(bufSize);
|
|
17
|
+
let content = '';
|
|
18
|
+
let n = 0;
|
|
19
|
+
do {
|
|
20
|
+
n = read(fd, buf, bufSize);
|
|
21
|
+
if (n > 0) content += buf.readUtf8String(n);
|
|
22
|
+
} while (n > 0);
|
|
23
|
+
close(fd);
|
|
24
|
+
return content;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function stripRwxp() {
|
|
28
|
+
for (const line of readMapsNative().split('\n')) {
|
|
29
|
+
if (line.includes('rwxp')) {
|
|
30
|
+
const [startHex, endHex] = line.split(' ')[0].split('-');
|
|
31
|
+
const start = ptr('0x' + startHex);
|
|
32
|
+
const end = ptr('0x' + endHex);
|
|
33
|
+
try {
|
|
34
|
+
Memory.protect(start, end.sub(start).toInt32(), 'r-x');
|
|
35
|
+
console.log('[rwxp -> r-x]', line);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
console.log('[skip]', line, e);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
stripRwxp();
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purpose Hook libart.so ClassLinker::DefineClass,在类加载时从内存中 Dump DEX 文件到应用目录
|
|
3
|
+
* @frida >= 17(同时兼容 Frida 16.7+)
|
|
4
|
+
* @usage frida -U -f <package> -l scripts/06-unpack-dump/dump-dex-defineclass.js
|
|
5
|
+
* @customize DefineClass 参数位置与 DexFile 结构体偏移因 Android 版本而异(当前示例适配 Android 9 签名)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
function getSelfProcessName() {
|
|
9
|
+
const libc = Process.getModuleByName('libc.so');
|
|
10
|
+
const open = new NativeFunction(libc.getExportByName('open'), 'int', ['pointer', 'int']);
|
|
11
|
+
const read = new NativeFunction(libc.getExportByName('read'), 'int', ['int', 'pointer', 'int']);
|
|
12
|
+
const close = new NativeFunction(libc.getExportByName('close'), 'int', ['int']);
|
|
13
|
+
|
|
14
|
+
const fd = open(Memory.allocUtf8String('/proc/self/cmdline'), 0);
|
|
15
|
+
if (fd !== -1) {
|
|
16
|
+
const buffer = Memory.alloc(0x1000);
|
|
17
|
+
read(fd, buffer, 0x1000);
|
|
18
|
+
close(fd);
|
|
19
|
+
return buffer.readCString();
|
|
20
|
+
}
|
|
21
|
+
return '-1';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function chmodDir(path) {
|
|
25
|
+
const chmod = new NativeFunction(Process.getModuleByName('libc.so').getExportByName('chmod'), 'int', ['pointer', 'int']);
|
|
26
|
+
chmod(Memory.allocUtf8String(path), 0o755);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function ensureDir(path) {
|
|
30
|
+
const libc = Process.getModuleByName('libc.so');
|
|
31
|
+
const mkdir = new NativeFunction(libc.getExportByName('mkdir'), 'int', ['pointer', 'int']);
|
|
32
|
+
const opendir = new NativeFunction(libc.getExportByName('opendir'), 'pointer', ['pointer']);
|
|
33
|
+
const closedir = new NativeFunction(libc.getExportByName('closedir'), 'int', ['pointer']);
|
|
34
|
+
|
|
35
|
+
const cPath = Memory.allocUtf8String(path);
|
|
36
|
+
const dir = opendir(cPath);
|
|
37
|
+
if (!dir.isNull()) {
|
|
38
|
+
closedir(dir);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
mkdir(cPath, 0o755);
|
|
42
|
+
chmodDir(path);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function dumpDex() {
|
|
46
|
+
const libart = Process.getModuleByName('libart.so');
|
|
47
|
+
let defineClassAddr = null;
|
|
48
|
+
for (const symbol of libart.enumerateSymbols()) {
|
|
49
|
+
const name = symbol.name;
|
|
50
|
+
if (
|
|
51
|
+
name.includes('ClassLinker') &&
|
|
52
|
+
name.includes('DefineClass') &&
|
|
53
|
+
name.includes('Thread') &&
|
|
54
|
+
name.includes('DexFile')
|
|
55
|
+
) {
|
|
56
|
+
console.log('[+] DefineClass:', name, symbol.address);
|
|
57
|
+
defineClassAddr = symbol.address;
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!defineClassAddr) {
|
|
63
|
+
console.log('[!] DefineClass symbol not found');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const dexMaps = {};
|
|
68
|
+
let dexCount = 1;
|
|
69
|
+
|
|
70
|
+
Interceptor.attach(defineClassAddr, {
|
|
71
|
+
onEnter(args) {
|
|
72
|
+
const dexFile = args[5];
|
|
73
|
+
// dexFile + pointerSize = begin_, dexFile + 2 * pointerSize = size_
|
|
74
|
+
const base = dexFile.add(Process.pointerSize).readPointer();
|
|
75
|
+
const size = dexFile.add(Process.pointerSize * 2).readUInt();
|
|
76
|
+
const key = base.toString();
|
|
77
|
+
|
|
78
|
+
if (dexMaps[key] !== undefined) return;
|
|
79
|
+
dexMaps[key] = size;
|
|
80
|
+
|
|
81
|
+
const magic = base.readCString(4);
|
|
82
|
+
if (!magic || !magic.startsWith('dex')) return;
|
|
83
|
+
|
|
84
|
+
const procName = getSelfProcessName();
|
|
85
|
+
if (procName === '-1') return;
|
|
86
|
+
|
|
87
|
+
const dirPath = `/data/data/${procName}/files/dump_dex_${procName}`;
|
|
88
|
+
ensureDir(dirPath);
|
|
89
|
+
const dexPath = `${dirPath}/class${dexCount === 1 ? '' : dexCount}.dex`;
|
|
90
|
+
dexCount++;
|
|
91
|
+
|
|
92
|
+
const file = new File(dexPath, 'wb');
|
|
93
|
+
const dexBuffer = base.readByteArray(size);
|
|
94
|
+
if (dexBuffer) {
|
|
95
|
+
file.write(dexBuffer);
|
|
96
|
+
file.flush();
|
|
97
|
+
console.log('[dump dex]:', dexPath, 'size=' + size);
|
|
98
|
+
}
|
|
99
|
+
file.close();
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
dumpDex();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purpose 使用 DexClassLoader 在目标进程中动态加载外部 DEX 文件
|
|
3
|
+
* @frida >= 17(纯 Java 层)
|
|
4
|
+
* @usage frida -U -F -l scripts/06-unpack-dump/load-dex.js
|
|
5
|
+
* @customize 修改 DEX_PATH 与 OPT_CACHE_DIR 为目标 App 可读写的实际路径
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const DEX_PATH = '/data/data/com.example.app/files/hook.dex';
|
|
9
|
+
const OPT_CACHE_DIR = '/data/data/com.example.app/cache';
|
|
10
|
+
|
|
11
|
+
function loadDex(dexPath, optimizedDirectory, librarySearchPath, parent) {
|
|
12
|
+
const DexClassLoader = Java.use('dalvik.system.DexClassLoader');
|
|
13
|
+
return DexClassLoader.$new(dexPath, optimizedDirectory, librarySearchPath, parent);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
Java.perform(function () {
|
|
17
|
+
const cl = loadDex(DEX_PATH, OPT_CACHE_DIR, null, Java.classFactory.loader);
|
|
18
|
+
console.log('[+] Loaded DexClassLoader:', cl);
|
|
19
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purpose ARM64 指令级 Patch 工具(Arm64Writer 写入 ret/nop 与 Memory.patchCode 写入原始机器码)
|
|
3
|
+
* @frida >= 17(同时兼容 Frida 16.7+)
|
|
4
|
+
* @usage frida -U -F -l scripts/07-lowlevel-trace/arm64-patch-code.js 后在 REPL 调用
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// 方式一:使用 Arm64Writer 将函数入口改为直接 ret
|
|
8
|
+
function patchFuncRet(addr) {
|
|
9
|
+
Memory.protect(addr, 4, 'rwx');
|
|
10
|
+
const writer = new Arm64Writer(addr);
|
|
11
|
+
writer.putRet();
|
|
12
|
+
writer.flush();
|
|
13
|
+
writer.dispose();
|
|
14
|
+
console.log('[+] Patched RET at', addr);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 方式二:使用 Arm64Writer 将单条指令改为 nop
|
|
18
|
+
function patchInsnNop(addr) {
|
|
19
|
+
Memory.protect(addr, 4, 'rwx');
|
|
20
|
+
const writer = new Arm64Writer(addr);
|
|
21
|
+
writer.putNop();
|
|
22
|
+
writer.flush();
|
|
23
|
+
writer.dispose();
|
|
24
|
+
console.log('[+] Patched NOP at', addr);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 方式三:使用 Memory.patchCode(自动处理 W^X 内存权限和指令缓存刷新,推荐)
|
|
28
|
+
function patchRetSafe(addr) {
|
|
29
|
+
Memory.patchCode(addr, 4, (code) => {
|
|
30
|
+
code.writeByteArray(new Uint8Array([0xc0, 0x03, 0x5f, 0xd6])); // ARM64: RET
|
|
31
|
+
});
|
|
32
|
+
console.log('[+] Memory.patchCode RET at', addr);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function patchNopSafe(addr) {
|
|
36
|
+
Memory.patchCode(addr, 4, (code) => {
|
|
37
|
+
code.writeByteArray([0x1f, 0x20, 0x03, 0xd5]); // ARM64: NOP
|
|
38
|
+
});
|
|
39
|
+
console.log('[+] Memory.patchCode NOP at', addr);
|
|
40
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purpose 纯 JS 解析 ELF64 定位目标函数 GOT 表项偏移,并通过改写 GOT 指针实现 GOT Hook
|
|
3
|
+
* @frida >= 17(同时兼容 Frida 16.7+;仅支持 64 位 ELF)
|
|
4
|
+
* @usage frida -U -F -l scripts/07-lowlevel-trace/got-hook.js
|
|
5
|
+
* @customize 调用 findGotEntryInModule('libtarget.so', 'pthread_create') 或 hookGotEntry(...)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
function parseElf64Headers(pbuff) {
|
|
9
|
+
const eIdent = pbuff.slice(0, 16);
|
|
10
|
+
const eiClass = eIdent[4];
|
|
11
|
+
const eiData = eIdent[5];
|
|
12
|
+
const littleEndian = eiData === 1;
|
|
13
|
+
if (eiClass !== 2) {
|
|
14
|
+
throw new Error('仅支持 64 位 ELF 文件');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const dv = new DataView(pbuff.buffer, pbuff.byteOffset, pbuff.byteLength);
|
|
18
|
+
let offset = 16;
|
|
19
|
+
const header = {};
|
|
20
|
+
header.e_type = dv.getUint16(offset, littleEndian); offset += 2;
|
|
21
|
+
header.e_machine = dv.getUint16(offset, littleEndian); offset += 2;
|
|
22
|
+
header.e_version = dv.getUint32(offset, littleEndian); offset += 4;
|
|
23
|
+
header.e_entry = dv.getBigUint64(offset, littleEndian); offset += 8;
|
|
24
|
+
header.e_phoff = dv.getBigUint64(offset, littleEndian); offset += 8;
|
|
25
|
+
header.e_shoff = dv.getBigUint64(offset, littleEndian); offset += 8;
|
|
26
|
+
header.e_flags = dv.getUint32(offset, littleEndian); offset += 4;
|
|
27
|
+
header.e_ehsize = dv.getUint16(offset, littleEndian); offset += 2;
|
|
28
|
+
header.e_phentsize = dv.getUint16(offset, littleEndian); offset += 2;
|
|
29
|
+
header.e_phnum = dv.getUint16(offset, littleEndian); offset += 2;
|
|
30
|
+
header.e_shentsize = dv.getUint16(offset, littleEndian); offset += 2;
|
|
31
|
+
header.e_shnum = dv.getUint16(offset, littleEndian); offset += 2;
|
|
32
|
+
header.e_shstrndx = dv.getUint16(offset, littleEndian); offset += 2;
|
|
33
|
+
|
|
34
|
+
const programHeaders = [];
|
|
35
|
+
const ePhoff = Number(header.e_phoff);
|
|
36
|
+
for (let i = 0; i < header.e_phnum; i++) {
|
|
37
|
+
let phOff = ePhoff + i * header.e_phentsize;
|
|
38
|
+
const phdr = {};
|
|
39
|
+
phdr.p_type = dv.getUint32(phOff, littleEndian); phOff += 4;
|
|
40
|
+
phdr.p_flags = dv.getUint32(phOff, littleEndian); phOff += 4;
|
|
41
|
+
phdr.p_offset = dv.getBigUint64(phOff, littleEndian); phOff += 8;
|
|
42
|
+
phdr.p_vaddr = dv.getBigUint64(phOff, littleEndian); phOff += 8;
|
|
43
|
+
phdr.p_paddr = dv.getBigUint64(phOff, littleEndian); phOff += 8;
|
|
44
|
+
phdr.p_filesz = dv.getBigUint64(phOff, littleEndian); phOff += 8;
|
|
45
|
+
phdr.p_memsz = dv.getBigUint64(phOff, littleEndian); phOff += 8;
|
|
46
|
+
phdr.p_align = dv.getBigUint64(phOff, littleEndian); phOff += 8;
|
|
47
|
+
programHeaders.push(phdr);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { littleEndian, programHeaders };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function vaddrToOffset(programHeaders, vaddr) {
|
|
54
|
+
for (const phdr of programHeaders) {
|
|
55
|
+
if (phdr.p_type === 1) { // PT_LOAD
|
|
56
|
+
const pVaddr = Number(phdr.p_vaddr);
|
|
57
|
+
const pMemsz = Number(phdr.p_memsz);
|
|
58
|
+
if (pVaddr <= vaddr && vaddr < pVaddr + pMemsz) {
|
|
59
|
+
return Number(phdr.p_offset) + (vaddr - pVaddr);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function parseDynamicSection(pbuff, littleEndian, programHeaders) {
|
|
67
|
+
let dynamicOffset = null;
|
|
68
|
+
let dynamicSize = null;
|
|
69
|
+
for (const phdr of programHeaders) {
|
|
70
|
+
if (phdr.p_type === 2) { // PT_DYNAMIC
|
|
71
|
+
dynamicOffset = Number(phdr.p_offset);
|
|
72
|
+
dynamicSize = Number(phdr.p_filesz);
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (dynamicOffset === null) {
|
|
77
|
+
throw new Error('未找到 PT_DYNAMIC 段');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const dv = new DataView(pbuff.buffer, pbuff.byteOffset, pbuff.byteLength);
|
|
81
|
+
const entries = [];
|
|
82
|
+
for (let i = 0; i < dynamicSize; i += 16) {
|
|
83
|
+
const d_tag = dv.getBigUint64(dynamicOffset + i, littleEndian);
|
|
84
|
+
const d_val = dv.getBigUint64(dynamicOffset + i + 8, littleEndian);
|
|
85
|
+
entries.push({ d_tag, d_val });
|
|
86
|
+
if (d_tag === 0n) break; // DT_NULL
|
|
87
|
+
}
|
|
88
|
+
return entries;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function extractDynamicInfo(entries) {
|
|
92
|
+
const info = {
|
|
93
|
+
dynsymAddr: null,
|
|
94
|
+
dynstrAddr: null,
|
|
95
|
+
relaPltAddr: null,
|
|
96
|
+
relaPltSize: null,
|
|
97
|
+
dynsymEntsize: null,
|
|
98
|
+
};
|
|
99
|
+
for (const entry of entries) {
|
|
100
|
+
switch (Number(entry.d_tag)) {
|
|
101
|
+
case 6: info.dynsymAddr = entry.d_val; break; // DT_SYMTAB
|
|
102
|
+
case 5: info.dynstrAddr = entry.d_val; break; // DT_STRTAB
|
|
103
|
+
case 23: info.relaPltAddr = entry.d_val; break; // DT_JMPREL
|
|
104
|
+
case 2: info.relaPltSize = entry.d_val; break; // DT_PLTRELSZ
|
|
105
|
+
case 11: info.dynsymEntsize = entry.d_val; break; // DT_SYMENT
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return info;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function parseDynsym(pbuff, littleEndian, programHeaders, dynsymAddr, dynsymEntsize) {
|
|
112
|
+
const dynsymOffset = vaddrToOffset(programHeaders, Number(dynsymAddr));
|
|
113
|
+
if (dynsymOffset === null) throw new Error('无法找到 dynsym 的文件偏移');
|
|
114
|
+
|
|
115
|
+
const data = pbuff.slice(dynsymOffset, Math.min(dynsymOffset + 0x1000, pbuff.byteLength));
|
|
116
|
+
const dv = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
117
|
+
const symSize = Number(dynsymEntsize) || 24;
|
|
118
|
+
|
|
119
|
+
const entries = [];
|
|
120
|
+
for (let i = 0; i + symSize <= data.byteLength; i += symSize) {
|
|
121
|
+
let off = i;
|
|
122
|
+
const st_name = dv.getUint32(off, littleEndian); off += 4;
|
|
123
|
+
const st_info = dv.getUint8(off); off += 1;
|
|
124
|
+
const st_other = dv.getUint8(off); off += 1;
|
|
125
|
+
const st_shndx = dv.getUint16(off, littleEndian); off += 2;
|
|
126
|
+
const st_value = dv.getBigUint64(off, littleEndian); off += 8;
|
|
127
|
+
const st_size = dv.getBigUint64(off, littleEndian); off += 8;
|
|
128
|
+
entries.push({ st_name, st_info, st_other, st_shndx, st_value, st_size });
|
|
129
|
+
}
|
|
130
|
+
return entries;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function parseDynstr(pbuff, programHeaders, dynstrAddr) {
|
|
134
|
+
const dynstrOffset = vaddrToOffset(programHeaders, Number(dynstrAddr));
|
|
135
|
+
if (dynstrOffset === null) throw new Error('无法找到 dynstr 的文件偏移');
|
|
136
|
+
return pbuff.slice(dynstrOffset, Math.min(dynstrOffset + 0x1000, pbuff.byteLength));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function parseRelaPlt(pbuff, littleEndian, programHeaders, relaPltAddr, relaPltSize) {
|
|
140
|
+
const relaOffset = vaddrToOffset(programHeaders, Number(relaPltAddr));
|
|
141
|
+
if (relaOffset === null) throw new Error('无法找到 .rela.plt 的文件偏移');
|
|
142
|
+
|
|
143
|
+
const relaSize = 24;
|
|
144
|
+
const count = Number(relaPltSize) / relaSize;
|
|
145
|
+
const dv = new DataView(pbuff.buffer, pbuff.byteOffset, pbuff.byteLength);
|
|
146
|
+
const entries = [];
|
|
147
|
+
|
|
148
|
+
for (let i = 0; i < count; i++) {
|
|
149
|
+
const off = relaOffset + i * relaSize;
|
|
150
|
+
if (off + relaSize > pbuff.byteLength) break;
|
|
151
|
+
entries.push({
|
|
152
|
+
r_offset: dv.getBigUint64(off, littleEndian),
|
|
153
|
+
r_info: dv.getBigUint64(off + 8, littleEndian),
|
|
154
|
+
r_addend: dv.getBigUint64(off + 16, littleEndian),
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
return entries;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function findGotEntry(pbuff, functionName) {
|
|
161
|
+
const { littleEndian, programHeaders } = parseElf64Headers(pbuff);
|
|
162
|
+
const dynEntries = parseDynamicSection(pbuff, littleEndian, programHeaders);
|
|
163
|
+
const { dynsymAddr, dynstrAddr, relaPltAddr, relaPltSize, dynsymEntsize } = extractDynamicInfo(dynEntries);
|
|
164
|
+
|
|
165
|
+
const dynsymEntries = parseDynsym(pbuff, littleEndian, programHeaders, dynsymAddr, dynsymEntsize);
|
|
166
|
+
const dynstrData = parseDynstr(pbuff, programHeaders, dynstrAddr);
|
|
167
|
+
const relaPltEntries = parseRelaPlt(pbuff, littleEndian, programHeaders, relaPltAddr, relaPltSize);
|
|
168
|
+
|
|
169
|
+
let symbolIndex = null;
|
|
170
|
+
for (let i = 0; i < dynsymEntries.length; i++) {
|
|
171
|
+
let name = '';
|
|
172
|
+
for (let j = dynsymEntries[i].st_name; j < dynstrData.length; j++) {
|
|
173
|
+
if (dynstrData[j] === 0) break;
|
|
174
|
+
name += String.fromCharCode(dynstrData[j]);
|
|
175
|
+
}
|
|
176
|
+
if (name === functionName) {
|
|
177
|
+
symbolIndex = i;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (symbolIndex === null) return null;
|
|
182
|
+
|
|
183
|
+
for (const rela of relaPltEntries) {
|
|
184
|
+
if (Number(rela.r_info >> 32n) === symbolIndex) {
|
|
185
|
+
return Number(rela.r_offset);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function findGotEntryInModule(moduleName, funcName) {
|
|
192
|
+
const mod = Process.getModuleByName(moduleName);
|
|
193
|
+
const data = mod.base.readByteArray(mod.size);
|
|
194
|
+
const gotOffset = findGotEntry(new Uint8Array(data), funcName);
|
|
195
|
+
console.log(`[GOT] ${moduleName} -> ${funcName} offset =`, gotOffset !== null ? '0x' + gotOffset.toString(16) : null);
|
|
196
|
+
return gotOffset;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// 写入新的函数指针到指定 GOT 表项地址(注意:replacementCallback 必须保存在全局变量中防止被 GC 回收)
|
|
200
|
+
function patchGotPointer(moduleName, gotOffset, replacementCallback) {
|
|
201
|
+
const mod = Process.getModuleByName(moduleName);
|
|
202
|
+
const gotAddress = mod.base.add(gotOffset);
|
|
203
|
+
const origPtr = gotAddress.readPointer();
|
|
204
|
+
console.log(`[GOT Hook] ${moduleName}+0x${gotOffset.toString(16)} orig=${origPtr} -> new=${replacementCallback}`);
|
|
205
|
+
|
|
206
|
+
Memory.protect(gotAddress, Process.pointerSize, 'rw-');
|
|
207
|
+
gotAddress.writePointer(replacementCallback);
|
|
208
|
+
Memory.protect(gotAddress, Process.pointerSize, 'r--');
|
|
209
|
+
return origPtr;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// 示例:定位 libc.so 中的 GOT 项
|
|
213
|
+
findGotEntryInModule('libc.so', 'pthread_create');
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purpose Stalker 结合 CModule onEvent 事件回调、非目标模块排除(Stalker.exclude)与 putCallout 寄存器监控示例
|
|
3
|
+
* @frida >= 17(同时兼容 Frida 16.7+;CModule 字符串已按 Frida 17 严格 TinyCC 双反斜杠转义)
|
|
4
|
+
* @usage frida -U -F -l scripts/07-lowlevel-trace/stalker-cmodule-events.js
|
|
5
|
+
* @customize 修改 KEEP_LIBS 为需要保留跟踪的库名列表(其余模块会被 Stalker.exclude 排除以提速)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const KEEP_LIBS = ['libtarget.so'];
|
|
9
|
+
|
|
10
|
+
function excludeLibsExcept(keepList) {
|
|
11
|
+
for (const m of new ModuleMap().values()) {
|
|
12
|
+
if (keepList.includes(m.name)) continue;
|
|
13
|
+
Stalker.exclude({ base: m.base, size: m.size });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function startCModuleEventTrace() {
|
|
18
|
+
excludeLibsExcept(KEEP_LIBS);
|
|
19
|
+
|
|
20
|
+
const cm = new CModule(`
|
|
21
|
+
#include <gum/gumstalker.h>
|
|
22
|
+
#include <stdio.h>
|
|
23
|
+
|
|
24
|
+
void onEvent(const GumEvent *event, GumCpuContext *cpu_context, gpointer user_data) {
|
|
25
|
+
printf("type %d\\n", event->type);
|
|
26
|
+
}
|
|
27
|
+
`);
|
|
28
|
+
|
|
29
|
+
// 如需改用 JS onReceive 解析事件,可将下方 onEvent 替换为 onReceive
|
|
30
|
+
const onReceive = (blob) => {
|
|
31
|
+
const events = Stalker.parse(blob, { annotate: true, stringify: true });
|
|
32
|
+
console.log('onReceive', events.length, events);
|
|
33
|
+
};
|
|
34
|
+
void onReceive;
|
|
35
|
+
|
|
36
|
+
// 注意:Stalker.follow 第一个参数必须是 threadId
|
|
37
|
+
Stalker.follow(Process.getCurrentThreadId(), {
|
|
38
|
+
events: {
|
|
39
|
+
call: true,
|
|
40
|
+
ret: true,
|
|
41
|
+
exec: false,
|
|
42
|
+
block: true,
|
|
43
|
+
compile: false,
|
|
44
|
+
},
|
|
45
|
+
onEvent: cm.onEvent,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 示例:在 transform 中配合 iterator.putCallout 读取/修改寄存器
|
|
50
|
+
function buildCalloutTransform(base, size) {
|
|
51
|
+
return function (iterator) {
|
|
52
|
+
let instruction;
|
|
53
|
+
while ((instruction = iterator.next()) !== null) {
|
|
54
|
+
const addr = instruction.address;
|
|
55
|
+
const inModule = addr.compare(base) >= 0 && addr.compare(base.add(size)) < 0;
|
|
56
|
+
if (inModule) {
|
|
57
|
+
const insnStr = instruction.toString();
|
|
58
|
+
const offset = addr.sub(base);
|
|
59
|
+
iterator.putCallout((context) => {
|
|
60
|
+
console.log(`\t${addr} (+${offset}): ${insnStr} | x0=${context.x0} x8=${context.x8}`);
|
|
61
|
+
// 如需改写寄存器:context.x8 = ptr(0x12345678);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
iterator.keep();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
setTimeout(startCModuleEventTrace, 100);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @purpose Stalker 指令级基本块追踪(含缓冲日志队列、模块范围过滤、以及空转测试卡死模板)
|
|
3
|
+
* @frida >= 17(同时兼容 Frida 16.7+)
|
|
4
|
+
* @usage frida -U -F -l scripts/07-lowlevel-trace/stalker-trace.js
|
|
5
|
+
* @customize 修改 TARGET_SO 与 TARGET_OFFSET 为待追踪的目标函数偏移
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const TARGET_SO = 'libDexHelper.so';
|
|
9
|
+
const TARGET_OFFSET = 0x4b614;
|
|
10
|
+
|
|
11
|
+
class BufferedLog {
|
|
12
|
+
constructor(flushMs = 30) {
|
|
13
|
+
this.queue = [];
|
|
14
|
+
this.timer = setInterval(() => this.flushPrint(), flushMs);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
push(line) {
|
|
18
|
+
this.queue.push(line);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
flushPrint() {
|
|
22
|
+
if (this.queue.length !== 0) {
|
|
23
|
+
console.log(this.queue.join('\n'));
|
|
24
|
+
this.queue = [];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
stop() {
|
|
29
|
+
clearInterval(this.timer);
|
|
30
|
+
this.flushPrint();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 1. 常规 Stalker 追踪:只打印落在目标模块范围内的基本块汇编指令
|
|
35
|
+
function traceFunctionWithStalker(soName, funcOffset) {
|
|
36
|
+
const mod = Process.getModuleByName(soName);
|
|
37
|
+
const base = mod.base;
|
|
38
|
+
const size = mod.size;
|
|
39
|
+
const logBuffer = new BufferedLog(30);
|
|
40
|
+
|
|
41
|
+
Interceptor.attach(base.add(funcOffset), {
|
|
42
|
+
onEnter() {
|
|
43
|
+
this.tid = Process.getCurrentThreadId();
|
|
44
|
+
Stalker.follow(this.tid, {
|
|
45
|
+
transform(iterator) {
|
|
46
|
+
let curOffset = 0;
|
|
47
|
+
while (true) {
|
|
48
|
+
const instruction = iterator.next();
|
|
49
|
+
if (instruction === null) break;
|
|
50
|
+
|
|
51
|
+
const address = instruction.address;
|
|
52
|
+
const isModuleCode =
|
|
53
|
+
address.compare(base) >= 0 && address.compare(base.add(size)) < 0;
|
|
54
|
+
|
|
55
|
+
if (isModuleCode) {
|
|
56
|
+
const instructionStr = instruction.toString();
|
|
57
|
+
if (curOffset === 0) {
|
|
58
|
+
const offset = address.sub(base);
|
|
59
|
+
logBuffer.push(
|
|
60
|
+
`[transform] start: ${address} name:${soName} offset:${offset} base:${base}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
logBuffer.push(`\t${address} <+${curOffset}>: ${instructionStr}`);
|
|
64
|
+
curOffset += instruction.size;
|
|
65
|
+
}
|
|
66
|
+
iterator.keep();
|
|
67
|
+
}
|
|
68
|
+
if (curOffset > 0) {
|
|
69
|
+
logBuffer.push('');
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
onLeave() {
|
|
75
|
+
console.log('[Stalker] 结束追踪');
|
|
76
|
+
Stalker.unfollow(this.tid);
|
|
77
|
+
Stalker.garbageCollect();
|
|
78
|
+
logBuffer.flushPrint();
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 2. 极简空转模板:排查开启 Stalker 本身是否触发反调试或卡死
|
|
84
|
+
function testStalkerNoop(soName, funcOffset) {
|
|
85
|
+
const mod = Process.getModuleByName(soName);
|
|
86
|
+
Interceptor.attach(mod.base.add(funcOffset), {
|
|
87
|
+
onEnter() {
|
|
88
|
+
this.tid = Process.getCurrentThreadId();
|
|
89
|
+
Stalker.follow(this.tid, {
|
|
90
|
+
transform(iterator) {
|
|
91
|
+
while (iterator.next() !== null) {
|
|
92
|
+
iterator.keep();
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
onLeave() {
|
|
98
|
+
console.log('[Stalker noop] 结束');
|
|
99
|
+
Stalker.unfollow(this.tid);
|
|
100
|
+
Stalker.garbageCollect();
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 按需启用
|
|
106
|
+
traceFunctionWithStalker(TARGET_SO, TARGET_OFFSET);
|
|
107
|
+
// testStalkerNoop(TARGET_SO, TARGET_OFFSET);
|