使用Frida攻擊安卓應用程式
大家好,今天我想給大家展示的是如何使用Frida工具來攻擊安卓應用程式。我可是花了好幾個小時的時間才把這個Frida工具安裝好的,最後發現,其實並不是很難,我只是沒有找到一個對於像我這樣的移動安全初學者的良好教程。所以我才決定做這個教程,給移動安全初學者提供一個參考,如果你知道Frida是什麼,你可以直接跳到step0:開始安裝Frida。
Frida是什麼?
Frida官網上是這麼說的:
它是針對本地APP的類似油猴外掛的東西,用更專業的術語來說,它是一個動態程式碼檢測toolkit。它可以讓你注入JavaScript程式碼片段或者你自己的庫到Windows中的APP中,也可以注入到macOS,GNU/Linux,iOS,Android和QNX的APP中。Frida還提供了一些構建在Frida之上的簡單工具。這些工具你可以直接使用,也可以根據自己的需求來調整,或者是作為如何使用API的示例。
簡而言之,Frida就是一個讓你可以注入指令碼到本地APP(此案例我們將注入到安卓APP中)中的工具,從而修改APP的行為(在這裡例子中,我們可以繞過ssl pinning並執行中間人攻擊,即使APP使用的是HTTPS/SSL連線),並且實時的進行動態測試。
宣告:此方法不適用於使用HSTS協議的APP,比如Facebook,Instagram,Twitter,PayPal和銀行APP,不過不用擔心,現在絕大多數APP還沒有使用這個HSTS協議。
step0—環境配置
電腦端
·Python2.7
· Python pip
· adb工具
· 本地代理(Burpsuite)
Android端
· root過的安卓手機(我的是一加手機 Android8.1)
· 安卓模擬器,Android4.4.4到8.1
step1—電腦安裝Frida
# installing frida via terminal, sometimes you need to run this command as sudo pip install frida-tools
step2—裝置上安裝frida-server
由於安卓裝置有很多種不同的架構,所以我們需要搞清楚我們的裝置是什麼處理器,我們需要把手機跟電腦連線(要先啟用USB除錯選項),然後執行下列命令:
# getting the processor arquitecture in this case is ARM, there are also x86, x86_64, etc ... adb shell getprop ro.product.cpu.abi ouput: armeabi-v7a
OK,知道架構之後,我們就可以下載對應的Frida伺服器版本了,下載的GitHub連結在這裡 https://github.com/frida/frida/releases ,由於最新的版本不好用,這裡我建議大家下載frida-server-12.0.5-android-arm.xz,下載好之後,我們需要解壓這個frida server,然後將它複製到裝置中。
# extracting frida-server binary from the xz file # for linux distributions tar -xJf frida-server-12.0.5-android-arm.xz # for macOS or BSD based unxz frida-server-12.0.5-android-arm.xz # then we need to copy the frida-server binary to the device with adb adb push ./frida-server-12.0.5-android-arm /data/local/tmp/
step3—frida中的hello程序
一旦我們在電腦端安裝好了frida,在安卓端安裝好了frida-server,我們就可以使用下面的命令與frida進行互動了:
# first we need to start frida-server with this adb command # the last '&' is to run the command in background # disable SELinux is very important I was looking about 4 hours trying to see what happened and SELinux was preventing the success frida-server execution, also frida-server must run as root adb shell 'su -c setenforce 0' adb shell 'su -c /data/local/tmp/frida-server-12.0.5-android-arm &' # then if everything works you can see frida's hello world with # frida-ps is for list the devices process and -U flag is for usb devices frida-ps -U
step4—配置Burpsuite
在我們的裝置之間建立連線的最快的方式就是將安卓裝置和電腦連線到同一個WiFi中,所以我們需要在安卓裝置中把WiFi連線修改為手動配置代理,然後設定Burpsuite代理為本機IP,具體配置如下圖:
當然,我們還需要安裝Burpsuite證書。只要安卓裝置的代理設定好了,我們就可以在瀏覽器中訪問 https://burp 這個連結,然後點選CA證書按鈕下載證書(注意:你需要將證書後綴der改成cer)如圖:
最後一步—繞過SSL Pinning
現在,我們已經安裝好了frida,Frida-server,Burpsuite也配置好了。下一步就是執行“通用安卓SSL Pinning Bypass No.2”指令碼來開始嗅探應用程式的連線了,我們先要下載這個指令碼並儲存在本地。這裡有一篇 博文 是講解該指令碼的(你可以從這個 repo 裡新增多個指令碼到frida裡,也可以自定義指令碼)
/* Universal Android SSL Pinning Bypass by Mattia Vinci and Maurizio Agazzini $ frida -U -f org.package.name -l universal-ssl-check-bypass.js --no-pause https://techblog.mediaservice.net/2018/11/universal-android-ssl-check-bypass-2/ */ Java.perform(function() { var array_list = Java.use("java.util.ArrayList"); var ApiClient = Java.use('com.android.org.conscrypt.TrustManagerImpl'); ApiClient.checkTrustedRecursive.implementation = function(a1, a2, a3, a4, a5, a6) { // console.log('Bypassing SSL Pinning'); var k = array_list.$new(); return k; } }, 0);
現在我們唯一要做的就是儲存指令碼,這裡我們儲存為“Frida-ssl-2.js”,然後執行下列命令:
# the -l flag is to run custom script, in this case ssl pinning 2 script # the -f flag is for the apk package name, --no-paus option to not interrupt # the app startup at all and still leave the spawning of the process to Frida. frida -U -l frida-ssl-2.js --no-paus -f com.example.application
然後程式就開始運行了,我們可以在Burpsuite中看到結果,如圖: