Frida ํŠœํ† ๋ฆฌ์–ผ

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ

์„ค์น˜

frida tools ์„ค์น˜:

pip install frida-tools
pip install frida

๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์„ค์น˜ํ•˜์„ธ์š” android์— frida server (Download the latest release).
adb๋ฅผ root ๋ชจ๋“œ๋กœ ์žฌ์‹œ์ž‘ํ•˜๊ณ  ์—ฐ๊ฒฐํ•œ ๋’ค frida-server๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ  ์‹คํ–‰ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜์—ฌ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•˜๋Š” ํ•œ ์ค„ ๋ช…๋ น:

adb root; adb connect localhost:6000; sleep 1; adb push frida-server /data/local/tmp/; adb shell "chmod 755 /data/local/tmp/frida-server"; adb shell "/data/local/tmp/frida-server &"

ํ™•์ธ ๊ทธ๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š”์ง€:

frida-ps -U #List packages and processes
frida-ps -U | grep -i <part_of_the_package_name> #Get all the package name

frida-ui (๋ธŒ๋ผ์šฐ์ € ๊ธฐ๋ฐ˜ Frida ์ปจํŠธ๋กค๋Ÿฌ)

frida-ui๋Š” http://127.0.0.1:8000์—์„œ devices/apps๋ฅผ ๋‚˜์—ดํ•˜๊ณ  scripts๋กœ targets์— attachํ•˜๊ฑฐ๋‚˜ spawnํ•  ์ˆ˜ ์žˆ๋Š” ์›น UI๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค (CLI ๋ถˆํ•„์š”).

  • ์„ค์น˜ (device server ๋ฒ„์ „์— ๋งž๊ฒŒ frida๋ฅผ ๊ณ ์ •):
uv tool install frida-ui --with frida==16.7.19
# pipx install frida-ui
# pip install frida-ui
  • ์‹คํ–‰:
frida-ui
frida-ui --host 127.0.0.1 --port 8000 --reload
  • ๊ธฐ๋Šฅ: USB/๋กœ์ปฌ ์žฅ์น˜ ๊ฒ€์ƒ‰, ์›๊ฒฉ ์„œ๋ฒ„ ์ถ”๊ฐ€ (192.168.1.x:27042), ๊ทธ๋ฆฌ๊ณ  Attach, Spawn, Spawn & Run (์ดˆ๊ธฐ onCreate() ๋กœ์ง ์ด์ „์— ํ›…์„ ๊ฑธ๊ธฐ ์œ„ํ•ด)์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
  • ์Šคํฌ๋ฆฝํŒ…: ํŽธ์ง‘๊ธฐ, .js ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญ, CodeShare ๊ฐ€์ ธ์˜ค๊ธฐ, ์Šคํฌ๋ฆฝํŠธ ๋ฐ ์„ธ์…˜ ๋กœ๊ทธ ๋‹ค์šด๋กœ๋“œ.
  • ์›๊ฒฉ ์„œ๋ฒ„: ./frida-server -l 0.0.0.0:27042 -D๋Š” ๋„คํŠธ์›Œํฌ์— ๋…ธ์ถœ์‹œ์ผœ frida-ui๊ฐ€ ADB ์—†์ด ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

Frida server vs. Gadget (root vs. no-root)

Frida๋กœ Android ์•ฑ์„ ๊ณ„์ธกํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•:

  • Frida server (rooted devices): ๋„ค์ดํ‹ฐ๋ธŒ ๋ฐ๋ชฌ์„ ํ‘ธ์‹œํ•˜๊ณ  ์‹คํ–‰ํ•˜์—ฌ ์–ด๋–ค ํ”„๋กœ์„ธ์Šค์—๋„ attachํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
  • Frida Gadget (no root): Frida๋ฅผ APK ๋‚ด๋ถ€์— ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ฒˆ๋“คํ•˜๊ณ  ๋Œ€์ƒ ํ”„๋กœ์„ธ์Šค์—์„œ ์ž๋™์œผ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.

Frida server (rooted)

# Download the matching frida-server binary for your device's arch
# https://github.com/frida/frida/releases
adb root
adb push frida-server-<ver>-android-<arch> /data/local/tmp/frida-server
adb shell chmod 755 /data/local/tmp/frida-server
adb shell /data/local/tmp/frida-server &    # run at boot via init/magisk if desired

# From host, list processes and attach
frida-ps -Uai
frida -U -n com.example.app

Frida Gadget (no-root)

  1. APK์˜ ์••์ถ•์„ ํ’€๊ณ  gadget .so์™€ ์„ค์ •์„ ์ถ”๊ฐ€:
  • libfrida-gadget.so๋ฅผ lib/<abi>/ (e.g., lib/arm64-v8a/)์— ๋„ฃ์Šต๋‹ˆ๋‹ค
  • assets/frida-gadget.config๋ฅผ ์Šคํฌ๋ฆฝํŠธ ๋กœ๋”ฉ ์„ค์ •์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค

Example frida-gadget.config

{
"interaction": { "type": "script", "path": "/sdcard/ssl-bypass.js" },
"runtime": { "logFile": "/sdcard/frida-gadget.log" }
}
  1. gadget๋ฅผ ์ฐธ์กฐ/๋กœ๋“œํ•˜์—ฌ ์กฐ๊ธฐ์— ์ดˆ๊ธฐํ™”๋˜๋„๋ก ํ•จ:
  • ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•: Application.onCreate()์— System.loadLibrary(โ€œfrida-gadgetโ€)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ž‘์€ Java ์Šคํ…์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋„ค์ดํ‹ฐ๋ธŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋กœ๋”ฉ์„ ์‚ฌ์šฉํ•˜์„ธ์š”.
  1. APK๋ฅผ ์žฌํŒจํ‚ค์ง•ํ•˜๊ณ  ์„œ๋ช…ํ•œ ๋‹ค์Œ ์„ค์น˜:
apktool d app.apk -o app_m
# ... add gadget .so and config ...
apktool b app_m -o app_gadget.apk
uber-apk-signer -a app_gadget.apk -o out_signed
adb install -r out_signed/app_gadget-aligned-debugSigned.apk
  1. host์—์„œ gadget ํ”„๋กœ์„ธ์Šค์— Attach:
frida-ps -Uai
frida -U -n com.example.app

Notes

  • Gadget๋Š” ์ผ๋ถ€ ๋ณดํ˜ธ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ์˜ํ•ด ํƒ์ง€๋ฉ๋‹ˆ๋‹ค; ํ•„์š” ์‹œ ์ด๋ฆ„/๊ฒฝ๋กœ๋ฅผ ์€๋ฐ€ํ•˜๊ฒŒ ํ•˜๊ณ  ๋Šฆ๊ฒŒ/์กฐ๊ฑด๋ถ€๋กœ ๋กœ๋“œํ•˜์„ธ์š”.
  • ํ•˜๋“œ๋‹๋œ ์•ฑ์—์„œ๋Š” rooted ํ…Œ์ŠคํŠธ๋ฅผ server + late attach๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•˜๊ฑฐ๋‚˜ Magisk/Zygisk ์ˆจ๊น€๊ณผ ๊ฒฐํ•ฉํ•˜์„ธ์š”.

JDWP ๊ธฐ๋ฐ˜ Frida ์ฃผ์ž… (root/repackaging ๋ถˆํ•„์š”) (frida-jdwp-loader)

APK๊ฐ€ debuggable(android:debuggable=โ€œtrueโ€)์ด๋ฉด, JDWP๋กœ attachํ•˜์—ฌ Java breakpoint์—์„œ native library๋ฅผ injectํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. No root ๋ฐ APK repackaging์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค.

  • Repo: https://github.com/frankheat/frida-jdwp-loader
  • Requirements: ADB, Python 3, USB/Wireless debugging. App๋Š” debuggable์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค (emulator์—์„œ ro.debuggable=1, rooted device์—์„œ resetprop ์‚ฌ์šฉ, ๋˜๋Š” manifest ์žฌ๋นŒ๋“œ).

Quick start:

git clone https://github.com/frankheat/frida-jdwp-loader.git
cd frida-jdwp-loader
# Inject frida-gadget.so into a debuggable target
python frida-jdwp-loader.py frida -n com.example.myapplication
# Keep the breakpoint thread suspended for early hooks
python frida-jdwp-loader.py frida -n com.example.myapplication -s
# Networkless: run a local agent script via Gadget "script" mode
python frida-jdwp-loader.py frida -n com.example.myapplication -i script -l script.js

์ฐธ๊ณ 

  • ๋ชจ๋“œ: spawn (Application.onCreate์—์„œ ์ค‘๋‹จ) ๋˜๋Š” attach (Activity.onStart์—์„œ ์ค‘๋‹จ). ํŠน์ • Java ๋ฉ”์„œ๋“œ๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด -b๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , Gadget ๋ฒ„์ „/๊ฒฝ๋กœ๋ฅผ ์„ ํƒํ•˜๋ ค๋ฉด -g, JDWP ํฌํŠธ๋ฅผ ์„ ํƒํ•˜๋ ค๋ฉด -p๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • Listen ๋ชจ๋“œ: ํ•„์š” ์‹œ Gadget์„ ํฌ์›Œ๋”ฉ(๊ธฐ๋ณธ 127.0.0.1:27042): adb forward tcp:27042 tcp:27042; ๊ทธ๋Ÿฐ ๋‹ค์Œ frida-ps -H 127.0.0.1:27042.
  • ์ด ๋ฐฉ๋ฒ•์€ JDWP ๋””๋ฒ„๊น…์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ—˜์€ debuggable builds๋ฅผ ๋ฐฐํฌํ•˜๊ฑฐ๋‚˜ JDWP๋ฅผ ๋…ธ์ถœ์‹œํ‚ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Self-contained agent + Gadget embedding (Frida 17+; automated with Objection)

Frida 17๋Š” GumJS์—์„œ ๋‚ด์žฅ๋œ Java/ObjC ๋ธŒ๋ฆฌ์ง€๋ฅผ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์—์ด์ „ํŠธ๊ฐ€ Java๋ฅผ ํ›„ํ‚นํ•œ๋‹ค๋ฉด, Java bridge๋ฅผ ๋ฒˆ๋“ค์— ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  1. Frida agent (TypeScript)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Java bridge๋ฅผ ํฌํ•จํ•˜์„ธ์š”
# Scaffolding
frida-create -t agent -o mod
cd mod && npm install
# Install the Java bridge for Frida 17+
npm install frida-java-bridge
# Dev loop (optional live-reload via REPL)
npm run watch

์ตœ์†Œํ•œ์˜ Java hook (์ฃผ์‚ฌ์œ„ ๊ตด๋ฆผ์„ 1๋กœ ๊ฐ•์ œ):

import Java from "frida-java-bridge";

Java.perform(function () {
var dicer = Java.use("org.secuso.privacyfriendlydicer.dicer.Dicer");
dicer.rollDice.implementation = function (numDice: number, numFaces: number) {
return Array(numDice).fill(1);
};
});

์ž„๋ฒ ๋”ฉ์„ ์œ„ํ•œ ๋‹จ์ผ ๋ฒˆ๋“ค ๋นŒ๋“œ:

npm run build    # produces _agent.js via frida-compile

๊ฐ„๋‹จํ•œ USB ํ…Œ์ŠคํŠธ (์„ ํƒ ์‚ฌํ•ญ):

frida -U -f org.secuso.privacyfriendlydicer -l _agent.js
  1. Gadget๊ฐ€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž๋™์œผ๋กœ ๋กœ๋“œํ•˜๋„๋ก ๊ตฌ์„ฑ Objection์˜ patcher๋Š” Gadget config๋ฅผ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค; script mode๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” APK lib dir ๋‚ด๋ถ€์˜ ๋””์Šคํฌ์ƒ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜์„ธ์š”:
{
"interaction": {
"type": "script",
"path": "libfrida-gadget.script.so"
}
}
  1. Objection์œผ๋กœ APK patching ์ž๋™ํ™”
# Embed Gadget, config, and your compiled agent into the APK; rebuild and sign
objection patchapk -s org.secuso.privacyfriendlydicer.apk \
-c gadget-config.json \
-l mod/_agent.js \
--use-aapt2

patchapk๊ฐ€ ํ•˜๋Š” ์ผ (๊ฐœ์š”):

  • ์žฅ์น˜ ABI(์˜ˆ: arm64-v8a)๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ์ผ์น˜ํ•˜๋Š” Gadget์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค
  • ํ•„์š”ํ•œ ๊ฒฝ์šฐ android.permission.INTERNET ๊ถŒํ•œ์„ ์„ ํƒ์ ์œผ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
  • launch activity์— System.loadLibrary(โ€œfrida-gadgetโ€)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” static class initializer๋ฅผ ์ฃผ์ž…ํ•ฉ๋‹ˆ๋‹ค
  • ๋‹ค์Œ ํŒŒ์ผ๋“ค์„ lib/<abi>/ ์•„๋ž˜์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค:
  • libfrida-gadget.so
  • libfrida-gadget.config.so (์ง๋ ฌํ™”๋œ ์„ค์ •)
  • libfrida-gadget.script.so (์‚ฌ์šฉ์ž์˜ _agent.js)

์ฃผ์ž…๋œ smali ์˜ˆ์‹œ (์ •์  ์ดˆ๊ธฐํ™”์ž):

.method static constructor <clinit>()V
.locals 1
const-string v0, "frida-gadget"
invoke-static {v0}, Ljava/lang/System;->loadLibrary(Ljava/lang/String;)V
return-void
.end method
  1. repack์„ ๊ฒ€์ฆ
apktool d org.secuso.privacyfriendlydicer.apk
apktool d org.secuso.privacyfriendlydicer.objection.apk
# Inspect differences
diff -r org.secuso.privacyfriendlydicer org.secuso.privacyfriendlydicer.objection

์˜ˆ์ƒ ๋ณ€๊ฒฝ ์‚ฌํ•ญ:

  • AndroidManifest.xml์— <uses-permission android:name="android.permission.INTERNET"/>๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Œ
  • ์œ„์™€ ๊ฐ™์ด lib/<abi>/ ์•„๋ž˜์— ์ƒˆ๋กœ์šด ๋„ค์ดํ‹ฐ๋ธŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ถ”๊ฐ€๋จ
  • Launchable activity์˜ smali์—๋Š” static <clinit>๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ System.loadLibrary(โ€œfrida-gadgetโ€)๋ฅผ ํ˜ธ์ถœํ•จ
  1. Split APKs
  • ๊ธฐ๋ณธ APK( the one that declares MAIN/LAUNCHER activity)๋ฅผ ํŒจ์น˜
  • ๋‚˜๋จธ์ง€ splits๋ฅผ ๋™์ผํ•œ ํ‚ค๋กœ ์žฌ์„œ๋ช…:
objection signapk split1.apk split2.apk ...
  • splits์„ ํ•จ๊ป˜ ์„ค์น˜:
adb install-multiple split1.apk split2.apk ...
  • ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด, splits๋ฅผ APKEditor๋กœ ๋‹จ์ผ APK๋กœ ๋ณ‘ํ•ฉํ•œ ๋‹ค์Œ align/signํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

๋™์  ๋ถ„์„ ์ค‘ FLAG_SECURE ์ œ๊ฑฐ

getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์•ฑ์€ ์Šคํฌ๋ฆฐ์ƒท, ์›๊ฒฉ ๋””์Šคํ”Œ๋ ˆ์ด ๋ฐ ์‹ฌ์ง€์–ด Android์˜ ์ตœ๊ทผ ์ž‘์—… ์Šค๋ƒ…์ƒท์„ ์ฐจ๋‹จํ•ฉ๋‹ˆ๋‹ค. Freedom Chat๊ฐ€ ์ด ํ”Œ๋ž˜๊ทธ๋ฅผ ์ ์šฉํ–ˆ์„ ๋•Œ leaks๋ฅผ ๋ฌธ์„œํ™”ํ•˜๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ ๋Ÿฐํƒ€์ž„์— ์œˆ๋„์šฐ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ„ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  • ํ”Œ๋ž˜๊ทธ๋ฅผ ๋‹ค์‹œ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  Window ์˜ค๋ฒ„๋กœ๋“œ(setFlags, addFlags, setAttributes)๋ฅผ Hookํ•˜๊ณ  ๋น„ํŠธ 0x00002000 (WindowManager.LayoutParams.FLAG_SECURE)์„ ๋งˆ์Šคํฌํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ activity๊ฐ€ resume๋œ ํ›„, UI ์Šค๋ ˆ๋“œ์—์„œ clearFlags(FLAG_SECURE)๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ์˜ˆ์•ฝํ•˜์—ฌ ์ดํ›„ ์ƒ์„ฑ๋˜๋Š” Dialogs/Fragments๊ฐ€ ์ž ๊ธˆ ํ•ด์ œ๋œ ์ƒํƒœ๋ฅผ ์ƒ์†๋ฐ›๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
  • React Native / Flutter๋กœ ๋นŒ๋“œ๋œ ์•ฑ์€ ์ข…์ข… ์ค‘์ฒฉ๋œ ์œˆ๋„์šฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค; ์—ฌ์ „ํžˆ black frames๊ฐ€ ๋ณด์ด๋ฉด android.app.Dialog/android.view.View ํ—ฌํผ๋ฅผ hookํ•˜๊ฑฐ๋‚˜ getWindow().peekDecorView()๋ฅผ ์ˆœํšŒํ•˜์„ธ์š”.
Frida hook clearing Window.FLAG_SECURE ```javascript Java.perform(function () { var LayoutParams = Java.use("android.view.WindowManager$LayoutParams"); var FLAG_SECURE = LayoutParams.FLAG_SECURE.value; var Window = Java.use("android.view.Window"); var Activity = Java.use("android.app.Activity");

function strip(value) { var masked = value & (~FLAG_SECURE); if (masked !== value) { console.log(โ€œ[-] Stripped FLAG_SECURE from 0xโ€ + value.toString(16)); } return masked; }

Window.setFlags.overload(โ€˜intโ€™, โ€˜intโ€™).implementation = function (flags, mask) { return this.setFlags.call(this, strip(flags), strip(mask)); };

Window.addFlags.implementation = function (flags) { return this.addFlags.call(this, strip(flags)); };

Window.setAttributes.implementation = function (attrs) { attrs.flags.value = strip(attrs.flags.value); return this.setAttributes.call(this, attrs); };

Activity.onResume.implementation = function () { this.onResume(); var self = this; Java.scheduleOnMainThread(function () { try { self.getWindow().clearFlags(FLAG_SECURE); console.log(โ€œ[+] Cleared FLAG_SECURE on โ€œ + self.getClass().getName()); } catch (err) { console.log(โ€[!] clearFlags failed: โ€œ + err); } }); }; });

</details>

Run the script with `frida -U -f <package> -l disable-flag-secure.js --no-pause`, interact with the UI, and screenshots/recordings will work again. Because everything happens on the UI thread there is no flicker, and you can still combine the hook with HTTP Toolkit/Burp to capture the traffic that revealed the `/channel` PIN leak.

## clsdumper(Frida)๋ฅผ ์ด์šฉํ•œ Dynamic DEX ๋คํ•‘/์–ธํŒจํ‚น

`clsdumper`๋Š” Frida ๊ธฐ๋ฐ˜์˜ ๋™์  **DEX/class dumper**๋กœ, anti-Frida pre-stage์™€ native ๋ฐ Java discovery strategies๋ฅผ ๊ฒฐํ•ฉํ•ด ํ•˜๋“œ๋‹๋œ ์•ฑ์„ ์šฐํšŒํ•ฉ๋‹ˆ๋‹ค(์‹ฌ์ง€์–ด `Java.perform()`์ด ์ฃฝ์–ด๋„ ๋™์ž‘). ์š”๊ตฌ์‚ฌํ•ญ: Python 3.10+, ๋ฃจํŒ…๋œ ๋””๋ฐ”์ด์Šค์—์„œ `frida-server`๊ฐ€ ์‹คํ–‰ ์ค‘์ด์–ด์•ผ ํ•˜๋ฉฐ, USB ๋˜๋Š” `--host` TCP ์—ฐ๊ฒฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

**์„ค์น˜ ๋ฐ ๋น ๋ฅธ ์‚ฌ์šฉ๋ฒ•**
```bash
pip install clsdumper
# Attach to a running app
clsdumper com.example.app
# Spawn first (hooks before early loaders)
clsdumper com.example.app --spawn
# Select strategies
clsdumper com.example.app --strategies fart_dump,oat_extract

CLI ์˜ต์…˜ (๊ฐ€์žฅ ์œ ์šฉํ•œ)

  • target: ํŒจํ‚ค์ง€ ์ด๋ฆ„ ๋˜๋Š” PID.
  • --spawn: attach ๋Œ€์‹  spawn.
  • --host <ip>: ์›๊ฒฉ frida-server์— ์—ฐ๊ฒฐ.
  • --strategies <comma>: ์ถ”์ถœ๊ธฐ ์ œํ•œ/์„ ํƒ; ๊ธฐ๋ณธ๊ฐ’์€ mmap_hook(๋น„์šฉ์ด ํผ)์„ ์ œ์™ธํ•œ ๋ชจ๋‘.
  • --no-scan / --deep-scan: deep ๋ฉ”๋ชจ๋ฆฌ ์Šค์บ” ๋น„ํ™œ์„ฑํ™” ๋˜๋Š” ๋А๋ฆฌ๊ฒŒ ์‹คํ–‰ (CDEX ์Šค์บ” ์ถ”๊ฐ€).
  • --extract-classes: androguard๋ฅผ ์‚ฌ์šฉํ•ด ๋คํ”„๋ฅผ .smali๋กœ ํ›„์ฒ˜๋ฆฌ.
  • --no-anti-frida: pre-hook bypass ๋‹จ๊ณ„๋ฅผ ๊ฑด๋„ˆ๋œ€.
  • --list / --list-apps: ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค ๋˜๋Š” ์„ค์น˜๋œ ํŒจํ‚ค์ง€ ๋‚˜์—ด.

Anti-instrumentation bypass (๋‹จ๊ณ„ 0)

  • sigaction/signal์„ ํ›„ํฌํ•˜์—ฌ crash/anti-debug ํ•ธ๋“ค๋Ÿฌ ๋“ฑ๋ก์„ ์ฐจ๋‹จ.
  • memfd_create๋ฅผ ํ†ตํ•ด ํ•„ํ„ฐ๋ง๋œ /proc/self/maps๋ฅผ ์ œ๊ณตํ•˜์—ฌ Frida ์˜์—ญ์„ ์ˆจ๊น€.
  • pthread_create๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜์—ฌ Frida๋ฅผ ํƒ์ง€ํ•˜๋Š” ์›Œ์น˜๋… ์Šค๋ ˆ๋“œ๋ฅผ ํฌ์ฐฉ/๋ฌด๋ ฅํ™”.

DEX discovery (๋‹จ๊ณ„ 1โ€“2) โ€” ๋‹ค์ค‘ ๋ณด์™„ ์ „๋žต๊ณผ ๊ฐ ์ ์ค‘์— ๋Œ€ํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ + ์ค‘๋ณต ์ œ๊ฑฐ(์—์ด์ „ํŠธ ์ธก djb2, ํ˜ธ์ŠคํŠธ ์ธก SHA-256):

  • Native (Java bridge ๋ถˆํ•„์š”): art_walk (ART Runtimeโ†’ClassLinkerโ†’DexFile ์ˆœํšŒ), open_common_hook (ํ›„ํฌ DexFile::OpenCommon), memory_scan (์ฝ๊ธฐ ๊ฐ€๋Šฅํ•œ ๋งต์—์„œ DEX magic), oat_extract (.vdex/.oat ๋งคํ•‘ ํŒŒ์‹ฑ), fart_dump (ํ›„ํฌ DefineClass + class_table_ ์ˆœํšŒ), dexfile_constructor (ํ›„ํฌ OatDexFile ์ƒ์„ฑ์ž), mmap_hook (mmap/mmap64 ๊ฐ์‹œ, ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ฑ๋Šฅ ๋•Œ๋ฌธ์— ๋น„ํ™œ์„ฑํ™”).
  • Java (๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ): cookie (ClassLoaders์—์„œ mCookie ์ฝ๊ธฐ), classloader_hook (loadClass, DexClassLoader, InMemoryDexClassLoader ๋ชจ๋‹ˆํ„ฐ๋ง).

Output layout

dump_<target>/
dex/classes_001.dex ...
classes/                 # only when --extract-classes
metadata.json            # strategy per hit + hashes

ํŒ: ๋ณดํ˜ธ๋œ ์•ฑ์€ ์ข…์ข… ์—ฌ๋Ÿฌ ์†Œ์Šค์—์„œ ์ฝ”๋“œ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค (in-memory payload, vdex/oat, custom loaders). ๊ธฐ๋ณธ multi-strategy ์„ธํŠธ์™€ --spawn์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์ปค๋ฒ„๋ฆฌ์ง€๋ฅผ ์ตœ๋Œ€ํ™”ํ•ฉ๋‹ˆ๋‹ค; ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ํ”ผํ•˜๋ ค๋ฉด ํ•„์š”ํ•  ๋•Œ๋งŒ --deep-scan์„ ํ™œ์„ฑํ™”ํ•˜์„ธ์š”.

ํŠœํ† ๋ฆฌ์–ผ

Tutorial 1

์ถœ์ฒ˜: https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1
APK: https://github.com/t0thkr1s/frida-demo/releases
์†Œ์Šค ์ฝ”๋“œ: https://github.com/t0thkr1s/frida-demo

์ฝ์œผ๋ ค๋ฉด link to read it.

Tutorial 2

์ถœ์ฒ˜: https://11x256.github.io/Frida-hooking-android-part-2/ (Parts 2, 3 & 4)
APKs and Source code: https://github.com/11x256/frida-android-examples

์ฝ์œผ๋ ค๋ฉด link to read it.

Tutorial 3

์ถœ์ฒ˜: https://joshspicer.com/android-frida-1
APK: https://github.com/OWASP/owasp-mstg/blob/master/Crackmes/Android/Level_01/UnCrackable-Level1.apk

์ฝ์œผ๋ ค๋ฉด link to read it.

๋” ๋งŽ์€ Frida ์Šคํฌ๋ฆฝํŠธ๋Š” ์—ฌ๊ธฐ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: https://codeshare.frida.re/

๋น ๋ฅธ ์˜ˆ์ œ

Calling Frida from command line

frida-ps -U

#Basic frida hooking
frida -l disableRoot.js -f owasp.mstg.uncrackable1

#Hooking before starting the app
frida -U --no-pause -l disableRoot.js -f owasp.mstg.uncrackable1
#The --no-pause and -f options allow the app to be spawned automatically,
#frozen so that the instrumentation can occur, and the automatically
#continue execution with our modified code.

๊ธฐ๋ณธ Python ์Šคํฌ๋ฆฝํŠธ

import frida, sys

jscode = open(sys.argv[0]).read()
process = frida.get_usb_device().attach('infosecadventures.fridademo')
script = process.create_script(jscode)
print('[ * ] Running Frida Demo application')
script.load()
sys.stdin.read()

Hooking: ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜

ํด๋ž˜์Šค sg.vantagepoint.a.c์˜ ํ•จ์ˆ˜ a()๋ฅผ Hookํ•˜์„ธ์š”.

Java.perform(function () {
rootcheck1.a.overload().implementation = function () {
return false;
};
});

Hook java exit()

var sysexit = Java.use("java.lang.System")
sysexit.exit.overload("int").implementation = function (var_0) {
send("java.lang.System.exit(I)V  // We avoid exiting the application  :)")
}

Hook MainActivity .onStart() & .onCreate()

var mainactivity = Java.use("sg.vantagepoint.uncrackable1.MainActivity")
mainactivity.onStart.overload().implementation = function () {
send("MainActivity.onStart() HIT!!!")
var ret = this.onStart.overload().call(this)
}
mainactivity.onCreate.overload("android.os.Bundle").implementation = function (
var_0
) {
send("MainActivity.onCreate() HIT!!!")
var ret = this.onCreate.overload("android.os.Bundle").call(this, var_0)
}

Hook android .onCreate()

var activity = Java.use("android.app.Activity")
activity.onCreate.overload("android.os.Bundle").implementation = function (
var_0
) {
send("Activity HIT!!!")
var ret = this.onCreate.overload("android.os.Bundle").call(this, var_0)
}

Hooking ํ•จ์ˆ˜๋“ค โ€” ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ฐ˜ํ™˜๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

decryption function์„ Hookingํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ์„ ์ถœ๋ ฅํ•˜๊ณ , ์›๋ณธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ์ž…๋ ฅ์„ decryptํ•œ ๋‹ค์Œ, ์ตœ์ข…์ ์œผ๋กœ ํ‰๋ฌธ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค:

Hooking a decryption function (Java) โ€” print inputs/outputs ```javascript function getString(data) { var ret = "" for (var i = 0; i < data.length; i++) { ret += data[i].toString() } return ret } var aes_decrypt = Java.use("sg.vantagepoint.a.a") aes_decrypt.a.overload("[B", "[B").implementation = function (var_0, var_1) { send("sg.vantagepoint.a.a.a([B[B)[B doFinal(enc) // AES/ECB/PKCS7Padding") send("Key : " + getString(var_0)) send("Encrypted : " + getString(var_1)) var ret = this.a.overload("[B", "[B").call(this, var_0, var_1) send("Decrypted : " + ret)

var flag = โ€œโ€ for (var i = 0; i < ret.length; i++) { flag += String.fromCharCode(ret[i]) } send(โ€œDecrypted flag: โ€œ + flag) return ret //[B }

</details>

### Hooking ํ•จ์ˆ˜๋“ค์„ ์šฐ๋ฆฌ ์ž…๋ ฅ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ธฐ

string์„ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ Hookํ•˜๊ณ  ๋‹ค๋ฅธ string์œผ๋กœ ํ˜ธ์ถœํ•˜์„ธ์š” (from [here](https://11x256.github.io/Frida-hooking-android-part-2/))
```javascript
var string_class = Java.use("java.lang.String") // get a JS wrapper for java's String class

my_class.fun.overload("java.lang.String").implementation = function (x) {
//hooking the new function
var my_string = string_class.$new("My TeSt String#####") //creating a new String by using `new` operator
console.log("Original arg: " + x)
var ret = this.fun(my_string) // calling the original function with the new String, and putting its return value in ret variable
console.log("Return value: " + ret)
return ret
}

์ด๋ฏธ ์ƒ์„ฑ๋œ ํด๋ž˜์Šค ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ

์ด๋ฏธ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์—์„œ ์–ด๋–ค ์†์„ฑ์„ ์ถ”์ถœํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์˜ˆ์ œ์—์„œ๋Š” ํด๋ž˜์Šค my_activity์˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ๊ฐ์ฒด์˜ ๋น„๊ณต๊ฐœ ์†์„ฑ์„ ์ถœ๋ ฅํ•˜๋Š” .secret() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค:

Java.choose("com.example.a11x256.frida_test.my_activity", {
onMatch: function (instance) {
//This function will be called for every instance found by frida
console.log("Found instance: " + instance)
console.log("Result of secret func: " + instance.secret())
},
onComplete: function () {},
})

๊ธฐํƒ€ Frida ํŠœํ† ๋ฆฌ์–ผ

์ฐธ๊ณ ์ž๋ฃŒ

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ