天天看點

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

聲明

本文章中所有内容僅供學習交流使用,不用于其他任何目的,不提供完整代碼,抓包内容、敏感網址、資料接口等均已做脫敏處理,嚴禁用于商業用途和非法用途,否則由此産生的一切後果均與作者無關!

本文章未經許可禁止轉載,禁止任何修改後二次傳播,擅自使用本文講解的技術而導緻的任何意外,作者均不負責,若有侵權,請在公衆号【K哥爬蟲】聯系作者立即删除!

逆向目标

  • 裝置:Google Pixel4,Android 10,已 root
  • APP:UnCrackable-Level1.apk(可在公衆号回複 APP 擷取)
  • APP 檢測了 root,如果手機 root 了,會強制退出 APP,過了 root 檢測後,還需要輸入一個字元串進行校驗。

安裝 ADB

adb(Android Debug Bridge)即安卓調試橋,安裝後可以在電腦上與手機進行互動,Android Studio 等工具裡面會自帶 adb,有時候我們并不想下載下傳這麼大的工具,是以這裡介紹一下 Android SDK Platform-Tools,它是 Android SDK 的一個元件,它包括與 Android 平台互動的工具,主要是 adb 和 fastboot,官方下載下傳位址:https://developer.android.com/studio/releases/platform-tools ,下載下傳完成後将該目錄添加到環境變量,USB 連接配接手機,手機上設定允許 USB 調試,使用指令

adb version

可檢視版本資訊,

adb devices

可以檢視目前連接配接的裝置,如下圖所示:

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

安裝 Frida

Frida 是一款基于 Python + JavaScript 的 Hook 與調試架構,首先電腦端使用指令

pip install frida-tools

安裝 frida 子產品(此指令預設會安裝最新版的 frida 和 frida-tools,如),然後下載下傳 frida-server,下載下傳位址:https://github.com/frida/frida/releases

frida-server 要根據你電腦端安裝的 frida 版本和手機的 CPU 架構來選擇對應的,使用指令

frida --version

可以檢視 frida 版本,使用指令

adb shell

進入手機,輸入

getprop ro.product.cpu.abi

檢視 CPU 架構,如下圖所示,我這裡 frida 是 15.2.2 版本,手機 CPU 為 arm64,是以我下載下傳的是

frida-server-15.2.2-android-arm64.xz

某些 Android 低版本使用高版本 frida 可能有問題,遇到問題可嘗試降低 frida 版本來解決。

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位
【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

将下載下傳好的 frida-server 使用

adb push

指令傳到手機的

/data/local/tmp/

目錄下,并給予 777 讀、寫、執行的權限,然後直接運作 frida-server,正常不會有任何輸出,當然也可以使用 & 等方式讓其在背景運作。

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

然後另開一個 cmd 使用指令

frida-ps -U

可檢視手機程序,有輸出則正常。

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

逆向分析

使用

adb install

指令安裝 UnCrackable-Level1.apk,打開該 APP,會檢測到 root,出現

Root detected!

的提示,如下圖所示:

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

使用 JEB、JADX、GDA 等工具反編譯 apk,直接搜尋關鍵字

Root detected!

即可定位到檢測的地方:

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

可以看到圖中有三個檢測方法

c.a()

c.b()

c.c()

,其中一個傳回為真,則彈出

Root detected!

,然後前面還有一個

onClick

方法,如果點選 OK 按鈕,則觸發

System.exit(0);

,即退出 APP,先點進三個檢測方法看看:

a()

方法通過檢測 Android 系統環境變量中是否有 su 檔案來判斷是否被 root;

b()

方法通過檢測

Build.TAGS

中是否包含字元串

test-keys

來判斷是否被 root;

c()

方法通過檢測指定路徑下是否包含指定的檔案來判斷是否被 root。

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

是以我們這裡就有多種過掉檢測的方法:

方法一:Hook 三個檢測方法,讓它們都傳回 false,不再執行後續的 a 方法,就不會退出 APP 了:

Java.perform(
    function(){
        console.log("[*] Hook begin")
        var vantagePoint = Java.use("sg.vantagepoint.a.c")
        vantagePoint.a.implementation = function(){
            console.log("[*] Hook vantagepoint.a.c.a")
            this.a();
            return false;
        }
        vantagePoint.b.implementation = function(){
            console.log("[*] Hook vantagepoint.a.c.b")
            this.b();
            return false;
        }
        vantagePoint.c.implementation = function(){
            console.log("[*] Hook vantagepoint.a.c.c")
            this.c();
            return false;
        }
    }
)
           

方法二:Hook

a()

方法,置空,什麼都不做,不彈出對話框,也不退出 APP:

Java.perform(
    function(){
        console.log("[*] Hook begin")
        var mainActivity = Java.use("sg.vantagepoint.uncrackable1.MainActivity");
        mainActivity.a.implementation = function(){
            console.log("[*] Hook mainActivity.a")
        }
    }
)
           

方法三:Hook

onClick()

方法,點選 OK 後不讓其退出 APP,注意這裡是内部類的 Hook 寫法:

Java.perform(
    function(){
        console.log("[*] Hook begin")
        var mainActivity$1 = Java.use("sg.vantagepoint.uncrackable1.MainActivity$1");
        mainActivity$1.onClick.implementation = function(){
            console.log("[*] Hook mainActivity$1.onClick")
        }
    }
)
           

方法四:Hook

System.exit()

方法,點選 OK 後不讓其退出 APP:

Java.perform(
    function(){
        console.log("[*] Hook begin")
        var javaSystem = Java.use("java.lang.System");
        javaSystem.exit.implementation = function(){
            console.log("[*] Hook system.exit")
        }
    }
)
           

root 檢測過掉之後,APP 還要輸入一個字元串,輸入錯誤會提示

That's not it. Try again.

,如下圖所示:

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

分析 Java 代碼,有一個

if-else

判斷,obj 為輸入的字元串,

a.a(obj)

判斷為真,就表示輸入正确。

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

跟到

a.a()

方法,可以看到

bArr

是内置的字元串,通過

equals()

方法比較輸入的

str

是否和

bArr

相等:

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

bArr

的值,主要經過

sg.vantagepoint.a.a.a()

方法處理後得到,繼續跟進去可以發現是 AES 加密算法:

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

這裡就可以直接 Hook

sg.vantagepoint.a.a.a()

,直接拿到加密後的值,也就是我們要的正确字元串,由于這裡傳回的是 ASCII 碼,是以我們還需要在 JavaScript 代碼中使用

String.fromCharCode()

将其轉換成正常字元,Hook 代碼如下:

Java.perform(
    function(){
        var cryptoAES = Java.use("sg.vantagepoint.a.a");
        cryptoAES.a.implementation = function(bArr, bArr2){
            console.log("[*] Hook cryptoAES")
            var secret = "";
            var decryptValue = this.a(bArr, bArr2);
            console.log("[*] DecryptValue:", decryptValue)
            for (var i=0; i < decryptValue.length; i++){
              secret += String.fromCharCode(decryptValue[i]);
            }
            console.log("[*] Secret:", secret)
            return decryptValue;
        }
    }
)
           

運作 Hook 腳本有兩種方式,一是結合 Python 使用,二是直接通過 frida 指令使用腳本,注入 Hook 代碼也有個時機問題,有時候需要在 APP 啟動就開始 Hook,有時候可以等 APP 啟動加載完畢了再 Hook,本例中,過 root 檢測的時候,如果采用第一、二種方法,即 Hook 三個檢測方法或者 a 方法,那就需要在 APP 啟動的時候就 Hook,如果采用第三、四種方法,即 Hook

onClick()

或者

System.exit()

方法,那麼等 APP 啟動了再 Hook 也可以。

結合 Python 使用

首先來看一下結合 Python 怎麼使用,JavaScript 代碼如下(frida-hook.js):

/* ==================================
# @Time    : 2022-08-29
# @Author  : 微信公衆号:K哥爬蟲
# @FileName: frida-hook.js
# @Software: PyCharm
# ================================== */


Java.perform(
    function(){
        console.log("[*] Hook begin")

        // 方法一:Hook 三個檢測方法,讓它們都傳回 false,不再執行後續的 a 方法,就不會退出 APP 了
        // var vantagePoint = Java.use("sg.vantagepoint.a.c")
        // vantagePoint.a.implementation = function(){
        //     console.log("[*] Hook vantagepoint.a.c.a")
        //     this.a();
        //     return false;
        // }
        // vantagePoint.b.implementation = function(){
        //     console.log("[*] Hook vantagepoint.a.c.b")
        //     this.b();
        //     return false;
        // }
        // vantagePoint.c.implementation = function(){
        //     console.log("[*] Hook vantagepoint.a.c.c")
        //     this.c();
        //     return false;
        // }

        // 方法二:Hook a() 方法,置空,什麼都不做,不彈出對話框,也不退出 APP
        // var mainActivity = Java.use("sg.vantagepoint.uncrackable1.MainActivity");
        // mainActivity.a.implementation = function(){
        //    console.log("[*] Hook mainActivity.a")
        // }

        // 方法三:Hook onClick() 方法,點選 OK 後不讓其退出 APP
        // var mainActivity$1 = Java.use("sg.vantagepoint.uncrackable1.MainActivity$1");
        // mainActivity$1.onClick.implementation = function(){
        //     console.log("[*] Hook mainActivity$1.onClick")
        // }

        // 方法四:Hook System.exit 方法,點選 OK 後不讓其退出 APP
        var javaSystem = Java.use("java.lang.System");
        javaSystem.exit.implementation = function(){
            console.log("[*] Hook system.exit")
        }

        var cryptoAES = Java.use("sg.vantagepoint.a.a");
        cryptoAES.a.implementation = function(bArr, bArr2){
            console.log("[*] Hook cryptoAES")
            var secret = "";
            var decryptValue = this.a(bArr, bArr2);
            console.log("[*] DecryptValue:", decryptValue)
            for (var i=0; i < decryptValue.length; i++){
              secret += String.fromCharCode(decryptValue[i]);
            }
            console.log("[*] Secret:", secret)
            return decryptValue;
        }
    }
)
           

Python 代碼如下(frida-hook.py):

# ==================================
# --*-- coding: utf-8 --*--
# @Time    : 2022-08-29
# @Author  : 微信公衆号:K哥爬蟲
# @FileName: frida-hook.py
# @Software: PyCharm
# ==================================


import sys
import frida


def on_message(message, data):
    if message['type'] == 'send':
        print("[*] {0}".format(message['payload']))
    else:
        print(message)


with open("./frida-hook.js", "r", encoding="utf-8") as fp:
    hook_string = fp.read()

# 方式一:attach 模式,已經啟動的 APP
process = frida.get_usb_device(-1).attach("Uncrackable1")
script = process.create_script(hook_string)
script.on("message", on_message)
script.load()
sys.stdin.read()

# 方式二,spawn 模式,重新開機 APP
# device = frida.get_usb_device(-1)
# pid = device.spawn(["owasp.mstg.uncrackable1"])
# process = device.attach(pid)
# script = process.create_script(hook_string)
# script.on("message", on_message)
# script.load()
# device.resume(pid)
# sys.stdin.read()
           

Python 代碼中,attach 模式 Hook 已經存在的程序,spawn 模式會重新開機 APP,啟動一個新的程序并挂起,在啟動的同時注入 frida 代碼,适用于在程序啟動前的一些 Hook,attach 模式傳入的是 APP 名稱,spawn 模式傳入的是 APP 包名,檢視 APP 名稱和包名的方法有很多,這裡介紹兩個 frida 指令,

frida-ps -Uai

:列出安裝的程式,

frida-ps -Ua

:列出正在運作中的程式,如下圖所示,本例中

Uncrackable1

就是 APP 名稱,

owasp.mstg.uncrackable1

就是包名:

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

運作 Python 代碼,注意手機端也要啟動 frida-server,過掉 root 檢測後,先随便輸入字元串,點選 VERIFY 就會 Hook 到正确的字元串為

I want to believe

,再次輸入正确的字元串,即可驗證成功。

【APP 逆向百例】Frida 初體驗,root 檢測與加密字元串定位

frida 指令

不使用 Python,也可以直接使用 frida 指令來實作,和前面 Python 一樣也有兩種模式,同樣的一個是 APP 名一個是包名:

frida -U Uncrackable1 -l .\frida-hook.js

:attach 模式,APP 啟動後注入 frida 代碼;

繼續閱讀