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:
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)Pixel 6 Pro API 34
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.
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.
Tìm và sử dụng adb
, thường nằm ở:
C:\Users\username\AppData\Local\Android\Sdk\platform-tools\adb.exe
adb connect ip:port
. IP, Port này thường ta dễ dàng thấy ở trong Setting
của emulator.adb devices -l
để list ra những device hiện có: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>
...
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.
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
Extract file vừa tải ra và đổi tên executatble thành frida-server
$ 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>
Ở đâ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.
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.
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.
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/