MASTG-TECH-0045: 运行时逆向工程
运行时逆向工程可以看作是即时版本的逆向工程,您无需将二进制数据放到主机上。 相反,您将直接从应用程序的内存中分析它。
我们将继续使用 HelloWorld JNI 应用程序,使用 r2frida r2 frida://usb//sg.vantagepoint.helloworldjni
打开一个会话,您可以使用 :i
命令开始显示目标二进制文件信息
[0x00000000]> :i
arch arm
bits 64
os linux
pid 13215
uid 10096
objc false
runtime V8
java true
cylang false
pageSize 4096
pointerSize 8
codeSigningPolicy optional
isDebuggerAttached false
cwd /
dataDir /data/user/0/sg.vantagepoint.helloworldjni
codeCacheDir /data/user/0/sg.vantagepoint.helloworldjni/code_cache
extCacheDir /storage/emulated/0/Android/data/sg.vantagepoint.helloworldjni/cache
obbDir /storage/emulated/0/Android/obb/sg.vantagepoint.helloworldjni
filesDir /data/user/0/sg.vantagepoint.helloworldjni/files
noBackupDir /data/user/0/sg.vantagepoint.helloworldjni/no_backup
codePath /data/app/sg.vantagepoint.helloworldjni-1/base.apk
packageName sg.vantagepoint.helloworldjni
androidId c92f43af46f5578d
cacheDir /data/local/tmp
jniEnv 0x7d30a43c60
使用 :is <lib>
搜索某个模块的所有符号,例如 :is libnative-lib.so
。
[0x00000000]> :is libnative-lib.so
[0x00000000]>
在这种情况下,它们是空的。 或者,您可能更喜欢查看导入/导出。 例如,使用 :ii <lib>
列出导入
[0x00000000]> :ii libnative-lib.so
0x7dbe1159d0 f __cxa_finalize /system/lib64/libc.so
0x7dbe115868 f __cxa_atexit /system/lib64/libc.so
并使用 :iE <lib>
列出导出
[0x00000000]> :iE libnative-lib.so
0x7d1c49954c f Java_sg_vantagepoint_helloworldjni_MainActivity_stringFromJNI
对于大型二进制文件,建议将输出通过管道传输到内部 less 程序,方法是附加
~..
,即:ii libandroid_runtime.so~..
(否则,对于此二进制文件,您将获得近 2500 行输出到您的终端)。
您可能想查看的下一件事是当前加载的 Java 类
[0x00000000]> :ic~sg.vantagepoint.helloworldjni
sg.vantagepoint.helloworldjni.MainActivity
列出类字段
[0x00000000]> :ic sg.vantagepoint.helloworldjni.MainActivity~sg.vantagepoint.helloworldjni
public native java.lang.String sg.vantagepoint.helloworldjni.MainActivity.stringFromJNI()
public sg.vantagepoint.helloworldjni.MainActivity()
请注意,我们已按包名称进行过滤,因为这是 MainActivity
,它包含来自 Android Activity
类的所有方法。
您还可以显示有关类加载器的信息
[0x00000000]> :icL
dalvik.system.PathClassLoader[
DexPathList[
[
directory "."]
,
nativeLibraryDirectories=[
/system/lib64,
/vendor/lib64,
/system/lib64,
/vendor/lib64]
]
]
[email protected][
DexPathList[
[
zip file "/data/app/sg.vantagepoint.helloworldjni-1/base.apk"]
,
nativeLibraryDirectories=[
/data/app/sg.vantagepoint.helloworldjni-1/lib/arm64,
/data/app/sg.vantagepoint.helloworldjni-1/base.apk!/lib/arm64-v8a,
/system/lib64,
/vendor/lib64]
]
]
接下来,假设您对 libnative-lib.so 导出的方法 0x7d1c49954c f Java_sg_vantagepoint_helloworldjni_MainActivity_stringFromJNI
感兴趣。 您可以使用 s 0x7d1c49954c
跳转到该地址,分析该函数 af
并打印其反汇编的 10 行 pd 10
[0x7d1c49954c]> pdf
;-- sym.fun.Java_sg_vantagepoint_helloworldjni_MainActivity_stringFromJNI:
╭ (fcn) fcn.7d1c49954c 18
│ fcn.7d1c49954c (int32_t arg_40f942h);
│ ; arg int32_t arg_40f942h @ x29+0x40f942
│ 0x7d1c49954c 080040f9 ldr x8, [x0]
│ 0x7d1c499550 01000090 adrp x1, 0x7d1c499000
│ 0x7d1c499554 21801591 add x1, x1, 0x560 ; hit0_4
│ 0x7d1c499558 029d42f9 ldr x2, [x8, 0x538] ; [0x538:4]=-1 ; 1336
│ 0x7d1c49955c 4000 invalid
请注意,标记为 ; hit0_4
的行对应于我们之前找到的字符串:0x7d1c499560 hit0_4 Hello from C++
。
要了解更多信息,请参阅 r2frida wiki。