Frida腳本教程

2020-09-05 無情劍客Burning

準備工作

Frida

在這裡對Frida進行了簡單的介紹。

設備

設備: android 10 ROOT

PC: Ubuntu18.04

Python切換

有些時候,需要使用Python的2.x版本,而有些時候又需要使用python的3.x版本,這個時候就需要能夠靈活設置python版本 使用下面的命令設置Python2是默認:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100

使用下面的命令設置Python3是默認

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

使用 sudo update-alternatives--config python命令切換到制定的版本

由於Frida建議使用Python3.x,因此這裡切換到3的版本。在二進位漏洞中,exploit使用的基本都是Python2.7。

Frida腳本

FRIDA腳本就是利用FRIDA動態插樁框架,使用FRIDA導出的API和方法,對內存空間裡的對象方法進行監視、修改或者替換的一段代碼。FRIDA的API是使用JavaScript實現的,所以我們可以充分利用JS的匿名函數的優勢、以及大量的hook和回調函數的API。

Hello world

Hello world經典的入門程序,frida版本的來一個。

setTimeout(function (){ Java.perform(function (){ console.log(&34;); });});

使用的API

setTimeout(func, delay[, ...parameters]): call func after delay milliseconds, optionally passing it one or more parameters. Returns an id that can be passed to clearTimeout to cancel it.

clearTimeout(id): cancel id returned by call to setTimeout.

Java.perform(fn): ensure that the current thread is attached to the VM and call fn. (This isn’t necessary in callbacks from Java.) Will defer calling fn if the app’s class loader is not available yet. Use Java.performNow() if access to the app’s classes is not needed.

console.log(line), console.warn(line), console.error(line): write line to the console of your Frida-based application.

JavaScript匿名函數

匿名函數:就是沒有函數名的函數。這怎麼可能,一個函數居然沒有函數名,但JavaScript就是支持這種語法的,這和Java等編譯型語言又很大的區別。匿名函數最大的用途是創建閉包。閉包是JavaScript的精髓,後續會專門講解。

<script> var fun = function (){ alert(&34;) } fun();</script>

運行程序

通過前面的分析,可知這個程序最終調用 console.log(&34;);

在手機端開啟frida-server.

然後在PC端運行下面的命令:

frida -U -l hello.js android.process.media

運行後的結果如下

枚舉所有的類

setTimeout(function (){ Java.perform(function (){ console.log(&34;); Java.enumerateLoadedClasses({ onMatch: function(_className){ console.log(&39;&34;&34;); }, onComplete: function(){ console.log(&34;); } }); }); });

其中: Java.enumerateLoadedClasses(callbacks): enumerate classes loaded right now, where callbacks is an object specifying:

  • onMatch: function (name, handle): called for each loaded class with name that may be passed to use() to get a JavaScript wrapper. You may also Java.cast() the handle to java.lang.Class.
  • onComplete: function (): called when all classes have been enumerated.

運行js代碼,同上,結果如下

枚舉藍牙類和實例

setTimeout(function (){ Java.perform(function (){ console.log(&34;); Java.enumerateLoadedClasses({ onMatch: function(_className){ if(_className.split(&34;)[1] == bluetooth){ console.log(&39;&34;&34;); } }, onComplete: function(){ console.log(&34;); } }); }); });

運行後,可以看出有關bluetooth相關的類和實例都被枚舉出來了。

選定&34;,獲取peer device的實例信息。

Java.choose(&34;,{ onMatch: function (instance){ console.log(&34;+&34;+&39;&34;&34;); //這裡是類型轉化的用法 //console.log(Java.cast(instance,Java.use(&34;) ).getName()); console.log(instance.getName()); // bluetoothDeviceInfo(instance); }, onComplete: function() { console.log(&34;);} });

其中: Java.choose(className, callbacks): enumerate live instances of the className class by scanning the Java heap, where callbacks is an object specifying:

  • onMatch: function (instance): called with each live instance found with a ready-to-use instance just as if you would have called Java.cast() with a raw handle to this particular instance. This function may return the string stop to cancel the enumeration early.
  • onComplete: function (): called when all instances have been enumerated

運行腳本,列印出peer device的名字和地址。

frida -U -l hello.js com.android.bluetooth

枚舉所有方法

function enumMethods(targetClass) { var hook = Java.use(targetClass); var ownMethods = hook.class.getDeclaredMethods(); hook.$dispose; return ownMethods; } var a = enumMethods(&34;) a.forEach(function(s) { console.log(s); });

其中: Java.use(className): dynamically get a JavaScript wrapper for className that you can instantiate objects from by calling $new() on it to invoke a constructor. Call $dispose() on an instance to clean it up explicitly (or wait for the JavaScript object to get garbage-collected, or script to get unloaded). Static and non-static methods are available, and you can even replace a method implementation and throw an exception from it:

Java.perform(function () { var Activity = Java.use(&39;); var Exception = Java.use(&39;); Activity.onResume.implementation = function () { throw Exception.$new(&39;); };});

使用下面的命令運行腳本,枚舉android.bluetooth.BluetoothDevice聲明的所有方法。

frida -U -l hello.js com.android.bluetooth

通過frida hook就能夠對想要hook的方法進行控制了。這裡舉一個簡單的例子,我想讓所有的getName()都返回&34;。

setTimeout(function (){ Java.perform(function (){ console.log(&34;); var targetClass = Java.use(&34;); targetClass.getName.implementation=function(){ var x = this.getName(); return &39;; } Java.choose(&34;,{ onMatch: function (instance){ console.log(&34;+&34;+&39;&34;&34;); // console.log(Java.cast(instance,Java.use(&34;) ).getName()); console.log(instance.getName()); // bluetoothDeviceInfo(instance); }, onComplete: function() { console.log(&34;);} }); }); });

或者

import frida,sysdef on_message(message, data): if message[&39;] == &39;: print(&34;.format(message[&39;])) else: print(message)passsession = frida.get_usb_device().attach(&34;)jscode = &34;&34;android.bluetooth.BluetoothDevice&39;Redmi&34;&34;script = session.create_script(jscode)script.on(&34;, on_message)print(&39;)script.load()sys.stdin.read()

前者的運行結果:

微信運動的步數,支付寶的總金額等都可以用類似的方式進行修改。

完整的代碼

setTimeout(function (){ Java.perform(function (){ console.log(&34;); Java.enumerateLoadedClasses({ onMatch: function(_className){ if(_className.split(&34;)[1] == &34;){ console.log(&39;&34;&34;); } }, onComplete: function(){ console.log(&34;); } }); Java.choose(&34;,{ onMatch: function (instance){ console.log(&34;+&34;+&39;&34;&34;); // console.log(Java.cast(instance,Java.use(&34;) ).getName()); console.log(instance.getName()); // bluetoothDeviceInfo(instance); }, onComplete: function() { console.log(&34;);} }); function enumMethods(targetClass) { var hook = Java.use(targetClass); var ownMethods = hook.class.getDeclaredMethods(); hook.$dispose; return ownMethods; } var a = enumMethods(&34;) a.forEach(function(s) { console.log(s); }); }); });

參考

https://frida.re/docs/javascript-api/

公眾號

更多Frida相關的文章,歡迎關注我的公眾號:無情劍客。


相關焦點

  • frida學習筆記3 之hook so中的方法
    hook so 常用工具SubstrateCydia-需rootfrida--需rootVA系列-非root(VA、VXP、SandVXposed)frida方式hook材料準備heibaobao.apk
  • 當Frida來「敲」門
    0x2 fridafrida是平臺原生app的Greasemonkey,說的專業一點,就是一種動態插樁工具,可以插入一些代碼到原生app的內存空間去,(動態地監視和修改其行為),這些原生平臺可以是Win、Mac、Linux、Android
  • 詳解Hook框架frida,讓你在逆向工作中效率成倍提升!
    (2) frida CLI是安裝的frida的其中一個工具,也是最常用的一個工具。2.frida serverfrida-server需要我們單獨下載,在 frida項目的github上可以直接下載對應系統已經編譯好的frida server
  • Frida全平臺使用
    pip install frida-tools網上狠毒文章說使用 pip install frida 和 pip install frida-tools 進行安裝,但是現在一條命名就夠了。如果不嫌麻煩,也可以通過源碼進行安裝。
  • Frida進階-API
    同時簡單介紹下HOOK 系統函數的利器frida-trace。內存,內存還是內存。>function frida_Java$new(Java.array(&39;, [ 0x48, 0x65, 0x69 ])); });} setImmediate(frida_Java,0);部分運行結果如下:
  • Frida API進階
    as a stringaddress: absolute address as a NativePointerslot: memory location where the import is stored, as a NativePointer //枚舉模塊中所有中的所有導入表(Import)函數 function frida_Module_import
  • 天天酷跑腳本精靈怎麼用 天天酷跑腳本精靈教程
    天天酷跑腳本精靈怎麼用 天天酷跑腳本精靈教程 來源:www.18183.com作者:集落時間:2014-02-12 天天酷跑腳本精靈使用教程!本文小編為您帶來天天酷跑腳本精靈怎麼用 天天酷跑腳本精靈教程。
  • 《Bash 腳本教程》發布了
    過去三個月,我一直在寫《Bash 腳本教程》[1],現在終於寫完了。網上找不到簡明扼要的中文教程,我很早就想整理一個,方便自己日後使用。這個教程是開源的,你可以克隆它的代碼倉庫[4],放在本地,也可以提交 issue 和 pull request,反饋感想,幫助我提高教程質量。
  • 安卓安全從零到一: FRIDA hook Native基礎
    __B7dnZ2xKBdXTNE_sklZg 提取碼:ljt1https://github.com/nickmyb/android_security_zero_to_one/blob/master/src/3_frida_hook_native/frida_hook_native_basic.md1.
  • Frida常用API
    instance.getName()); // bluetoothDeviceInfo(instance); }, onComplete: function() { console.log(&34;);} }); }); });通過下面的命令運行程序frida
  • Frida API的"樂器"
    == null); }})運行上面的程序(在Win10系統下),使用腳本 frida-l hello.jsCalculator.exe運行, 結果如下,每一步運行過程中都有相應的指令輸出。gc(); var id = WeakRef.bind(wm, function(){ console.log("finish gc"); WeakRef.unbind(id); })})運行腳本
  • 光遇測試服怎麼開腳本?最新測試服開腳本教程[多圖]
    光遇測試服開腳本會封號嗎?具體要怎麼開腳本?測試服管得沒有正式服嚴格,所以很多人都會選擇在測試服開腳本,那麼具體要怎麼開腳本呢?今天小編會將測試服開腳本的操作方法分享在下方,不懂的可以查看下面的開腳本教程。
  • 安卓逆向——Frida hook java層
    各位愛好安卓逆向的大佬們早上好,今天呢小弟不才在這裡拙劣的給大家講解一下咱們frida hook我們先輸入我們的hook方法命令我們代碼的一個腳本拖到我們的cmd窗口裡面,然後回車。
  • 油猴插件怎麼用 安裝使用腳本教程
    tampermonkey油猴插件是Chrome上最流行的用戶腳本管理插件了,可以通過安裝腳本實現破解vip視頻、百度網盤資源直接下載等實用功能,堪稱神器。   tampermonkey安裝使用腳本教程:   先介紹一下win10系統Edge瀏覽器的安裝方法,很簡單,可以直接去應用商店下載安裝。
  • 牛逼,安卓應用層抓包通殺腳本
    今天推薦的這個項目是「r0capture」,是一個安卓應用層抓包通殺腳本。這個項目基於 https://github.com/BigFaceCat2017/frida_ssl_logger,原項目的側重點在於抓 ssl 和跨平臺,本項目的側重點是抓到所有的包。
  • iOS節奏大師瘋狂刷分教程 最新腳本攻略
    iOS節奏大師瘋狂刷分教程 最新腳本攻略 2013-11-18 16:18 | 作者:SORA | 來源:265G QQ群號:624022706 | 我要分享:
  • VB腳本編程教程匯總:西門子觸控螢幕博途Wincc腳本其實沒那麼難
    2:博途Wincc如何利用VB腳本,實現觸控螢幕畫面內靜態文本域輸出提示信息!3:博途Wincc如何組態圖形動畫,VB循環修改變量值,實現個性化鎖屏!4:博途Wincc如何多語言組態,切換顯示語言,讓你的西門子觸控螢幕看上去更加高大上!
  • Frida使用之資料庫
    下面的代碼查詢buglydb資料庫信息:function frida_Java() { Java.perform(function () { var db, smt, row, id, tm; db = SqliteDatabase.open
  • 天天連萌ios安卓雙用觸摸精靈腳本刷分教程
    天天連萌ios安卓雙用觸摸精靈腳本刷分教程 2013-09-04 15:05 | 作者:瑪莎 | 來源:Youxi正能量 QQ群號:624022706 | 我要分享: