frida java層和c層打印堆棧 (修復優化)


frida 打印堆棧 java層和c層

大家好,我是王鐵頭 一個乙方安全公司搬磚的菜雞 今天分享的是frida 堆棧打印
持續更新移動安全,iot安全,編譯原理相關原創視頻文章

視頻演示:https://space.bilibili.com/430241559

1.java層堆棧打印

java層堆棧打印的例子網上資料比較全,但是在實際應用的時候,因為frida版本的區別,會發現一些不便
所以這里我針對網上的兩個例子做了一些改良。

網上的兩個例子如下

1.1 java堆棧打印 例子1

function showStacks1() 
{
    send(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()));
}  

在新版frida 1405版本環境下做測試
效果圖如下

1.2 java堆棧打印 例子2

  function printStack(name) 
  {
    Java.perform(function () 
    {
        var Exception = Java.use("java.lang.Exception");
        var ins = Exception.$new("Exception");
        var straces = ins.getStackTrace();
        if (straces != undefined && straces != null) 
        {
            var strace = straces.toString();
            var replaceStr = strace.replace(/,/g, "\\n");
            console.log("=============================" + name + " Stack strat=======================");
            console.log(replaceStr);
            console.log("=============================" + name + " Stack end=======================\r\n");
            Exception.$dispose();
        }
    });
}

在新版frida 1405版本環境下做測試
效果圖如下

1.3 java堆棧打印 例子3 改良版

在用了網上兩個在新版frida不能換行的例子后 我這里做了改良 代碼如下

 function showStacks3(str_tag) 
 {
    var Exception=  Java.use("java.lang.Exception");
    var ins = Exception.$new("Exception");
    var straces = ins.getStackTrace();

    if (undefined == straces || null  == straces) 
    {
        return;
    }

    console.log("=============================" + str_tag + " Stack strat=======================");
    console.log("");

    for (var i = 0; i < straces.length; i++)
    {
        var str = "   " + straces[i].toString();
        console.log(str);
    }

    console.log("");
    console.log("=============================" + str_tag + " Stack end=======================\r\n");
    Exception.$dispose();
);

在新版frida 1289 1405版本環境下做測試
效果圖如下

2.c層堆棧打印

c層堆棧打印比較簡單 網上的代碼用起來效果還可以

2.1網上打印c層堆棧的代碼

Interceptor.attach(f, {
  onEnter: function (args) {
    console.log('RegisterNatives called from:\n' +
        Thread.backtrace(this.context, Backtracer.ACCURATE)
        .map(DebugSymbol.fromAddress).join('\n') + '\n');
  }
});

2.2真正有用的一行

上面寫了那么多 其實真正起作用的是這一行代碼

Thread.backtrace(this.context, Backtracer.ACCURATE)
        .map(DebugSymbol.fromAddress).join('\n') + '\n');

2.3 封裝后的代碼、

//context 這里傳入this.context
//str_arg 這里傳入堆棧顯示時展示的標簽
function print_c_stack(context, str_tag)
{
    console.log('');
    console.log("=============================" + str_tag + " Stack strat=======================");       
    console.log(Thread.backtrace(context, Backtracer.ACCURATE).map(DebugSymbol.fromAddress).join('\n'));
    console.log("=============================" + str_tag + " Stack end  =======================");
}

2.4 例子代碼

Java.perform(function () 
{
    console.log("hook c start");

    var str_name_so = "libc.so";
    var str_name_func = "fgets";

    //********************************hook native*********************************//
    //hook export function
    var func_ptr = Module.findExportByName(str_name_so , str_name_func);
    if (null == func_ptr)
    {
        console.log(str_name_func + " point is null");
        return;
    }

    Interceptor.attach(func_ptr, 
    {
        onEnter: function(args) 
        {
            print_c_stack(this.context, str_name_func);
        },
        onLeave:function(retval)
        {
                  
        }
    });   
});


function print_c_stack(context, str_tag)
{
    console.log('');
    console.log("=============================" + str_tag + " Stack strat=======================");       
    console.log(Thread.backtrace(context, Backtracer.ACCURATE).map(DebugSymbol.fromAddress).join('\n'));
    console.log("=============================" + str_tag + " Stack end  =======================");
  
}

效果如下

視頻演示:https://space.bilibili.com/430241559

相關代碼資料 關注公眾號 [移動安全王鐵頭] 回復關鍵字 frida 下載:

持續更新移動安全,iot安全,編譯原理相關原創視頻文章


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM