以下提到的所有步驟均在 macOS Mojave 上進行
使用 pip3 安裝frida-tools
執行指令$ pip --version
和$ pip3 --version
來檢查哪個pip來自Python 3x 。例如,您應該看到如下所示的版本資訊:
$ pip 19.0.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
然後執行正確的pip指令來安裝frida-tools
,例如pip3
$ pip3 install frida-tools
成功輸出:
....
Successfully built frida-tools frida
Installing collected packages: colorama, frida, six, wcwidth, prompt-toolkit, pygments, frida-tools
Successfully installed colorama-0.4.1 frida-12.4.7 frida-tools-1.3.2 prompt-toolkit-2.0.9 pygments-2.3.1 six-1.12.0 wcwidth-0.1.7
測試您的安裝
將cat
進位檔案複製到臨時資料夾,例如/tmp/cat
然後從該目錄執行cat
:
$ mkdir ~ /frida
$ cp /bin/cat /frida/cat
$ /frida/cat
在另一個終端機中,建立一個包含以下內容的檔案example.py
:
import frida
def on_message ( message , data ):
print ( "[on_message] message:" , message , "data:" , data )
session = frida . attach ( "cat" )
script = session . create_script ( """'use strict';
rpc.exports.enumerateModules = function () {
return Process.enumerateModulesSync();
};
""" )
script . on ( "message" , on_message )
script . load ()
print ([ m [ "name" ] for m in script . exports . enumerate_modules ()])
然後使用以下命令運行example.py
腳本
$ python3 example.py
輸出應該與此類似(取決於您的平台和庫版本):
[ u'cat' , …, u'ld-2.15.so' ]
安卓adb
指令
確保adb
可以看到您的設備:
$ adb devices -l
這也將確保 adb 守護程序在您的桌面上運行,這使得 Frida 能夠發現您的裝置並與您通信,無論您是否透過 USB 還是 WiFi 連接它。
frida-server
以下步驟是基於 macOS Majave 上的Android 模擬器 Nexus 6P (x86) api 23。
首先,從 frida-server 發布頁面下載最新的 Android frida-server
,例如,對於 Android x86
模擬器,您應該下載frida-server-12.4.7-android-x86.xz,並讓它在您的模擬器上運行:
$ adb root # might be required
$ adb push frida-server /data/local/tmp/
$ adb shell " chmod 755 /data/local/tmp/frida-server "
$ adb shell " /data/local/tmp/frida-server & "
對於最後一步,如果您看到以下錯誤:
Unable to load SELinux policy from the kernel: Failed to open file “/sys/fs/selinux/policy”: Permission denied
然後你需要使用 root shell 來執行frida-server
,例如
$ adb shell
angler:/ $ su
angler:/ # /data/local/tmp/frida-server &
[1] 12089
angler:/ #
[1] 12089
是frida-server
的進程 ID。
現在,是時候在您的桌面上確保基本功能正常運作了。跑步:
frida-ps -U
這應該會給你一個進程列表,大致如下:
PID Name
----- ---------------------------------------------------
721 ATFWD-daemon
4450 adbd
730 [email protected]
407 [email protected]
408 [email protected]
409 [email protected]
410 [email protected]
406 [email protected]
竄改Java原始碼中的Boolean
值,即Boolean bTamperingSucces = false;
,或您感興趣的其他代碼。
apktool
可以從 Apktool 網站取得。只需按照本頁內的步驟安裝 apktool 即可。
adb
隨 Android SDK 提供,可以在目錄<your-some-path>/Android/sdk/platform-tools/adb
找到
apksigner
是使用金鑰庫檔案對您的 apk 進行簽署。此工具可在目錄<ANDROID_HOME>/Android/sdk/build-tools/28.0.3/apksigner
中找到,其用法記錄在命令列 apksigner 中。
從 DecompileApk 克隆範例專案。
找到已經編譯好的apk檔DecompileApk/app/release/app-release.apk
。
使用apktool反編譯它。
$ cd < your-path > /DecompileApk/app/release/
$ apktool d --no-res -f app-release.apk
您將看到以下輸出
I: Using Apktool 2.4.0 on app-release.apk
I: Copying raw resources...
I: Baksmaling classes.dex...
I: Copying assets and libs...
I: Copying unknown files...
I: Copying original files...
在smali程式碼目錄下尋找DecompileApk/app/release/app-release/smali/com/arophix/decompileapk/MainActivity.smali
,找到以下程式碼
const / 4 p1 , 0x0
只需將0x0
(表示false
)更改為0x1
(表示true
)並儲存檔案。
使用apktool建構被竄改的apk。
apktool b app - release
您應該會看到下面的輸出
I: Using Apktool 2.4.0
I: Checking whether sources has changed...
I: Smaling smali folder into classes.dex...
I: Checking whether resources has changed...
I: Copying raw resources...
I: Copying libs... (/lib)
I: Building apk file...
I: Copying unknown files/dir...
I: Built apk...
從dist
目錄中找到新建的apk DecompileApk/app/release/app-release/dist/app-release.apk
使用位於DecompileApk/app/decompileapk.jks
apksigner和金鑰庫對 apk 進行簽署(請根據您自己的情況相應修改金鑰庫和 apk 的路徑),
$ < ANDROID_HOME > /sdk/build-tools/28.0.3/apksigner sign --ks ../decompileapk.jks app-release.apk
您應該看到以下輸出並輸入密碼123456
Keystore password for signer # 1:
使用 adb 指令安裝簽署的 apk。
$ adb install < path-to-the-tampered-apk > /app-release.apk
現在您應該在螢幕上看到"Hello, Android reverse engineer!"
,而不是從螢幕上看到"Hello from C++"
。
使用 Frida 掛鉤 Android Java 原始碼。
從 hook_java_methods.py 中找到腳本並使用以下命令運行它,然後按一下已啟動的 Android 應用程式的按鈕。
$ python3 hook_java_methods.py
[ * ] Running CTF
[ * ] onClick
[ * ] Called - isPhoneRooted ()
[ * ] onClick
[ * ] Called - isPhoneRooted ()
如果您看到以下錯誤:
$ frida.ServerNotRunningError: unable to connect to remote frida-server: closed
請記住,您已經在模擬器上啟動了frida-server
。
同時,在模擬器螢幕上,如果成功,您應該會看到 toast 訊息更改為Device not rooted
。
使用 Frida 掛鉤 Android C 原始碼。
反編譯APK
首先,使用 apktool 反編譯 apk,提取共享函式庫,即libnative-lib.so
。
$ cd DecompileApk/app/release
$ apktool d --no-res app-release.apk
找到目標JNI方法
其次,使用以下指令尋找要掛鉤的 JNI 函數。
$ nm --demangle --dynamic app-release/lib/x86/libnative-lib.so
您應該看到以下輸出:
00004d80 T Java_com_arophix_decompileapk_MainActivity_stringFromJNI
000090b0 T std::bad_typeid::what () const
00005cf0 T std::bad_exception::what () const
00005e70 T std::bad_array_length::what () const
00005df0 T std::bad_array_new_length::what () const
00008ff0 T std::bad_cast::what () const
...
按名稱掛鉤 C 函數
從 hooknative-by-function-name.py 中找到腳本並使用以下命令運行它,然後點擊已啟動的 Android 應用程式的按鈕。
$ python3 hooknative-by-function-name.py
[ * ] Running Arophix Hook Test ...
Java_com_arophix_decompileapk_MainActivity_stringFromJNI called with:
ret: 0x200019
如果您看到以下錯誤:
$ frida.ServerNotRunningError: unable to connect to remote frida-server: closed
請記住,您已經在模擬器上啟動了frida-server
。
同時,在模擬器畫面上,您應該看到Frida is hooking this displayed text from Native layer by function name.
如果成功的話。
透過地址掛鉤 C 函數
從 hooknative-by-function-address.py 中找到腳本並使用以下命令運行它,然後點擊已啟動的 Android 應用程式的按鈕。
$ python3 hooknative-by-function-address.py
[ * ] Running Arophix Hook Test ...
[+] membase: 0xb2acd000
[+] addressOfStringFromJni: 0xb2ad1d80
[++] addressOfStringFromJni: 0xb2ad1d80
ret: 0x19
如果您看到以下錯誤:
$ frida.ServerNotRunningError: unable to connect to remote frida-server: closed
請記住,您已經在模擬器上啟動了frida-server
。
同時,在模擬器畫面上,您應該看到Frida is hooking this displayed text from Native layer by function address.
如果成功的話。