frida反调试绕过姿势

[复制链接]
查看1285 | 回复1 | 2025-3-3 17:06:31 | 显示全部楼层 |阅读模式

立即注册,突破游客限制,与技术大牛交流切磋,畅享海量资源,让你的技术之路不再孤单!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
前言
当前许多应用通过集成 libmsaoaidsec.so 实现针对 Frida 的反调试机制,其核心逻辑是在应用启动过程中,当加载 libmsaoaidsec.so 动态库时,会通过 pthread_create() 创建独立线程,并在该线程内执行反调试函数,主动扫描 Frida 进程、端口、内存特征等痕迹。若检测到 Frida 存在,则触发进程终止或崩溃。
针对这种机制的绕过思路可以从破坏反调试线程的加载或执行入手,文章会对不同的app进行测试,对于使用libmsaoaidsec.so来进行反调试的app,思路是一样的。
定位
通过hook dlopen()函数根据最后加载的so文件来确定程序是在加载哪个so之后退出的
  1. function hook_dlopen(){
  2.     //Android8.0之后加载so通过android_dlopen_ext函数
  3.     var android_dlopen_ext = Module.findExportByName(null,"android_dlopen_ext");
  4.     console.log("addr_android_dlopen_ext",android_dlopen_ext);
  5.     Interceptor.attach(android_dlopen_ext,{
  6.         onEnter:function(args){
  7.             var pathptr = args[0];
  8.             if(pathptr!=null && pathptr != undefined){
  9.                 var path = ptr(pathptr).readCString();
  10.                 console.log("android_dlopen_ext:",path);
  11.             }
  12.         },
  13.         onLeave:function(retvel){
  14.             console.log("leave!");
  15.         }
  16.     })
  17. }
  18. hook_dlopen()
复制代码
如下,可以看到加载完libmsaoaidsec.so之后程序就退出了。
截图202503031703019270.png
接下来我们找一个时机,就是在加载libmsaoaidsec.so的时候打印它里面创建的线程
  1. function hook_dlopen(){
  2.     var android_dlopen_ext = Module.findExportByName(null,"android_dlopen_ext");
  3.     console.log("addr_android_dlopen_ext",android_dlopen_ext);
  4.     Interceptor.attach(android_dlopen_ext,{
  5.         onEnter:function(args){
  6.             var pathptr = args[0];
  7.             if(pathptr!=null && pathptr != undefined){
  8.                 var path = ptr(pathptr).readCString();
  9.                 if(path.indexOf("libmsaoaidsec.so")!=-1){
  10.                     console.log("android_dlopen_ext:",path);
  11.                     hook_pthread()
  12.                 }
  13.             }
  14.         },
  15.         onLeave:function(retvel){
  16.             
  17.         }
  18.     })
  19. }
  20. function hook_pthread() {
  21.     var pth_create = Module.findExportByName("libc.so", "pthread_create");
  22.     console.log("[pth_create]", pth_create);
  23.     Interceptor.attach(pth_create, {
  24.         onEnter: function (args) {
  25.             var module = Process.findModuleByAddress(args[2]);
  26.             if (module != null) {
  27.                 console.log("address", module.name, args[2].sub(module.base));
  28.             }
  29.         },
  30.         onLeave: function (retval) {}
  31.     });
  32. }
  33. function main(){
  34.     hook_dlopen()
  35. }
  36. main()
复制代码
如下看打印结果,有兴趣的小伙伴可以利用IDA反编译libmsaoaidsec.so跳转到这些地址去看这些函数都干了什么事情
截图202503031705256790.png
反调试绕过
现在已经知道了是哪些函数来检测的frida,然后一个很重要的事情就是找到一个时机去把这几个函数都替换掉或者nop掉。
如果去IDA里分析的话,跳转到这几个函数的地址,通过交叉引用查看他们的上一级函数,会发现最终他们都会聚集到init_proc函数里,这是一个初始化函数,在so加载时会自动被调用,而且是so加载过程中最先执行的函数,那么就要找一个比init_proc函数执行更早的一个时机来把检测frida的函数替换掉或者nop掉。
在so文件的链接过程中linker里的call_constructors()函数会触发构造函数,对初始化函数进行注册,然后执行初始化函数,注册的初始化函数会放在.init_array函数列表里,可以在调用call_constructors()函数的时候动态替换so文件里检测frida的函数的地址,使它指向自定义的空函数。
姿势1
替换函数
  1. function hook_dlopen(){
  2.     //Android8.0之后加载so通过android_dlopen_ext函数
  3.     var android_dlopen_ext = Module.findExportByName(null,"android_dlopen_ext");
  4.     console.log("addr_android_dlopen_ext",android_dlopen_ext);
  5.     Interceptor.attach(android_dlopen_ext,{
  6.         onEnter:function(args){
  7.             var pathptr = args[0];
  8.             if(pathptr!=null && pathptr != undefined){
  9.                 var path = ptr(pathptr).readCString();
  10.                 if(path.indexOf("libmsaoaidsec.so")!=-1){
  11.                     console.log("android_dlopen_ext:",path);
  12.                     hook_call_constructors()
  13.                 }
  14.             }
  15.         },
  16.         onLeave:function(retvel){
  17.             //console.log("leave!");
  18.         }
  19.     })
  20. }
  21. function hook_call_constructors() {
  22.     let linker = null;
  23.     if (Process.pointerSize === 4) {
  24.         linker = Process.findModuleByName("linker");
  25.     } else {
  26.         linker = Process.findModuleByName("linker64");
  27.     }
  28.     let call_constructors_addr, get_soname
  29.     let symbols = linker.enumerateSymbols();
  30.     for (let index = 0; index < symbols.length; index++) {
  31.         let symbol = symbols[index];
  32.         if (symbol.name === "__dl__ZN6soinfo17call_constructorsEv") {
  33.             call_constructors_addr = symbol.address;
  34.         } else if (symbol.name === "__dl__ZNK6soinfo10get_sonameEv") {
  35.             get_soname = new NativeFunction(symbol.address, "pointer", ["pointer"]);
  36.         }
  37.     }
  38.     console.log(call_constructors_addr)
  39.     var listener = Interceptor.attach(call_constructors_addr, {
  40.         onEnter: function (args) {
  41.             console.log("hooked call_constructors")
  42.             var module = Process.findModuleByName("libmsaoaidsec.so")
  43.             if (module != null) {
  44.                 Interceptor.replace(module.base.add(0x1c544), new NativeCallback(function () {
  45.                     console.log("0x1c544:替换成功")
  46.                 }, "void", []))
  47.                 Interceptor.replace(module.base.add(0x1b8d4), new NativeCallback(function () {
  48.                     console.log("0x1b8d4:替换成功")
  49.                 }, "void", []))
  50.                 Interceptor.replace(module.base.add(0x26e5c), new NativeCallback(function () {
  51.                     console.log("0x26e5c:替换成功")
  52.                 }, "void", []))
  53.                 listener.detach()
  54.             }
  55.             
  56.         },
  57.     })
  58. }
  59. function main(){
  60.     hook_dlopen()
  61. }
  62. main()
复制代码
如下成功绕过了
截图202503031705563272.png
姿势2
nop函数
  1. function nop_addr(addr) {
  2.     Memory.protect(addr, 4 , 'rwx');
  3.     var w = new Arm64Writer(addr);
  4.     w.putRet();
  5.     w.flush();
  6.     w.dispose();
  7. }
  8. function hook_dlopen(){
  9.     //Android8.0之后加载so通过android_dlopen_ext函数
  10.     var android_dlopen_ext = Module.findExportByName(null,"android_dlopen_ext");
  11.     console.log("addr_android_dlopen_ext",android_dlopen_ext);
  12.     Interceptor.attach(android_dlopen_ext,{
  13.         onEnter:function(args){
  14.             var pathptr = args[0];
  15.             if(pathptr!=null && pathptr != undefined){
  16.                 var path = ptr(pathptr).readCString();
  17.                 if(path.indexOf("libmsaoaidsec.so")!=-1){
  18.                     console.log("android_dlopen_ext:",path);
  19.                     hook_call_constructors()
  20.                 }
  21.             }
  22.         },
  23.         onLeave:function(retvel){
  24.             //console.log("leave!");
  25.         }
  26.     })
  27. }
  28. function hook_call_constructors() {
  29.     let linker = null;
  30.     if (Process.pointerSize === 4) {
  31.         linker = Process.findModuleByName("linker");
  32.     } else {
  33.         linker = Process.findModuleByName("linker64");
  34.     }
  35.     let call_constructors_addr, get_soname
  36.     let symbols = linker.enumerateSymbols();
  37.     for (let index = 0; index < symbols.length; index++) {
  38.         let symbol = symbols[index];
  39.         if (symbol.name === "__dl__ZN6soinfo17call_constructorsEv") {
  40.             call_constructors_addr = symbol.address;
  41.         } else if (symbol.name === "__dl__ZNK6soinfo10get_sonameEv") {
  42.             get_soname = new NativeFunction(symbol.address, "pointer", ["pointer"]);
  43.         }
  44.     }
  45.     console.log(call_constructors_addr)
  46.     var listener = Interceptor.attach(call_constructors_addr, {
  47.         onEnter: function (args) {
  48.             console.log("hooked call_constructors")
  49.             var module = Process.findModuleByName("libmsaoaidsec.so")
  50.             if (module != null) {
  51.                 nop_addr(module.base.add(0x1c544))
  52.                 console.log("0x1c544:替换成功")
  53.                 nop_addr(module.base.add(0x1b8d4))
  54.                 console.log("0x1b8d4:替换成功")
  55.                 nop_addr(module.base.add(0x26e5c))
  56.                 console.log("0x26e5c:替换成功")
  57.                 listener.detach()
  58.             }
  59.             
  60.         },
  61.     })
  62. }
  63. function main(){
  64.     hook_dlopen()
  65. }
  66. main()
复制代码
如下也可以绕过
截图202503031706213098.png


这个人还没有签名
霜落千雪 | 2025-4-15 17:07:14 来自手机 | 显示全部楼层
好奇怪,为什么看不到您的效果图啊
这个人还没有签名
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

图文推荐
大神求
大神求
6 天前
热门排行