FRIDA

Sử dụng Frida để hook apk trên Android

Step 1: Cài đặt emulator

Chúng ta cần một emulator hoạt động được để có thể chạy apk, mình có một số hướng để làm như sau:

  1. Android Studio
  • Download và cài đặt Android Studio từ trang chủ Android Studio
  • Tiếp theo, ta tạo 1 project đơn giản (có thể debug APK nếu cần File -> Profile or debug APK)
    -Ctrl + Shift + A và search "Android SDK" hoặc Tool -> SDK manager sau đó cài đặt các SDK cần thiết. (Có thể sẽ bị lỗi Error: Please select android sdk do chưa cài SDK)
  • Tạo 1 emulator đơn giản, ở đây mình sẽ dùng Pixel 6 Pro API 34

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Sau khi có emulator, chỉ cần đơn giản kéo thả file APK là nó sẽ tự động install.

Khuyến khích nên dùng cách này, vì chúng ta cần dùng adb, mà trong Android SDK của Android Studio sẽ cài luôn cho chúng ta nên khỏi phải đi kiếm ở đâu nữa, emulator nó cũng khá dễ dùng và mượt, sau này chắc chắn có thể làm rất nhiều thứ với Android Studio nữa.

  1. Docker Android
  • repos: https://github.com/budtmo/docker-android
  • Cách này thì đơn giản hơn, chỉ cần pull docker về rồi run thôi. Sau khi run thì emulator sẽ open ở port mặc định 6080. Khi muốn dùng adb hay frida chỉ cần connect tới là được, còn về cách cài apk hay một số thứ liên quan có hướng dẫn rất chi tiết trong github. Thậm chí có cả video hướng dẫn chi tiết trên youtube: Easy Android Emulator in Docker

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Sau khi cài emulator, thường là có sẵn root, tuy nhiên chúng ta cần enable debugging để có thể sử dụng debug.

Vào Setting của emulator, tìm kiếm phần About emulated device và tap khoản 7 lần để enable Developer options.

Tiếp theo, tìm System -> Developer options, tại đây:
- Enable USB debugging
- Enable Wireless debugging

Vậy là chúng đã có một emulator đơn để execute và debug APK.

Step 2: Sử dụng adb

Tìm và sử dụng adb, thường nằm ở:

C:\Users\username\AppData\Local\Android\Sdk\platform-tools\adb.exe

Đối với remote emulator

  • Ta có thể dùng adb connect ip:port. IP, Port này thường ta dễ dàng thấy ở trong Setting của emulator.

Hoặc adb tự động kết nối tới emulator

  • Sử dụng adb devices -l để list ra những device hiện có:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Ngoài ra còn một số command khác có thể dùng như:

adb root
adb shell  
adb shell <command>
adb push <file> <path> 
...

Step 3: Sử dụng Frida

Cài đặt

Install: pip install frida-tools

Mình sẽ dùng phiên bản mới nhất trong lúc viết bài này, frida 16.3.1

Optional:

  • Trong trường hợp bị lỗi:

    ​​​​[!]load_script Exception: need Gadget to attach on jailed Android; its default location is: C:\Users\XXX\AppData\Local\Microsoft\Windows\INetCache\frida\gadget-android-arm64.so
    

    Ta có thể tìm và tải gadget trên repos của frida, tải đúng version và arch của emulator, sau đó đổi tên thành gadget-android-arm64.so rồi thêm vào path như trên.

Setup frida_server

Tìm và tải frida-server trên Frida release.

Trong trường hợp của mình là android x64_86, mình sẽ tải frida-server-16.3.1-android-x86_64.xz

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Extract file vừa tải ra và đổi tên executatble thành frida-server

Push frida-server lên emulator

$ 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 &"

https://frida.re/docs/android/

Từ đây, chúng ta có thể dùng frida để spawn process hoặc attach process.

spawn: $frida -U -l hook.js -f <package> -F
attach: $frida -U -l hook.js -f <package>

Demo

Ở đây mình sẽ thử đơn giản với 1 file note_taking từ giải corCTF

Sau khi install, mình load vào jadx để decompile APK.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Mình thấy hàm performDecryption có nhận vào 1 argument là 1 string. Để cho đơn giản, mình sẽ chỉ hook vào hàm này rồi print ra giá trị của argument đó.

hook.js

Java.perform(function () {
    console.log("[-] Starting hook");

    var class_decrypt = Java.use("com.example.note_taking.decrypt"); //classname
    
    class_decrypt.performDecryption.overload('java.lang.String').implementation =  function (arg1) {
        console.log("[*] function hooked!");
        console.log("[*] input: ",arg1);
        return this.performDecryption(arg1);
    }
});

Sử dụng command: frida -U -l hook.js -f com.example.note_taking -F mình thành công attach vào file và hooked.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Hàm performDecryption, sẽ bị triggered bởi button decrypt data, do đó input của chúng ta thấy sẽ là từ ô text nhập vào.

Done

Vậy là mình đã demo đơn giản cách sử dụng frida, lúc làm có thể có bug nhưng vẫn fix được, chủ yếu để mình có thể note lại và dùng lúc cần.

Other note: https://lephuduc.github.io/posts/note/