Frida 튜토리얼
Reading time: 8 minutes
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 지원하기
- 구독 계획 확인하기!
- **💬 디스코드 그룹 또는 텔레그램 그룹에 참여하거나 트위터 🐦 @hacktricks_live를 팔로우하세요.
- HackTricks 및 HackTricks Cloud 깃허브 리포지토리에 PR을 제출하여 해킹 트릭을 공유하세요.
설치
다음 항목을 설치하세요: frida tools
pip install frida-tools
pip install frida
다운로드 및 설치 안드로이드에 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 server vs. Gadget (root vs. no-root)
Frida로 Android 앱을 계측하는 두 가지 일반적인 방법:
- Frida server (rooted devices): 네이티브 daemon을 push하고 실행하여 어떤 process에도 attach할 수 있게 해줍니다.
- Frida Gadget (no root): Frida를 shared library로 APK 내부에 번들하고 target process 내에서 자동으로 로드합니다.
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)
- APK의 압축을 풀고, gadget .so와 config를 추가:
- libfrida-gadget.so를 lib/
/에 배치하세요 (예: 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" }
}
- gadget을 참조/로드하여 조기에 초기화되도록 합니다:
- 가장 쉬운 방법: Application.onCreate()에 System.loadLibrary("frida-gadget")를 호출하는 작은 Java 스텁을 추가하거나, 이미 존재하는 네이티브 라이브러리 로딩을 사용하세요.
- 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
- host에서 gadget process에 Attach:
frida-ps -Uai
frida -U -n com.example.app
참고
- Gadget은 일부 보호 기능에 의해 탐지됩니다; 필요하면 이름/경로를 은폐하고 늦게/조건부로 로드하세요.
- 보호가 강화된 앱에서는 rooted testing을 server + late attach로 수행하거나 Magisk/Zygisk 숨김과 결합하는 것을 권장합니다.
Tutorials
Tutorial 1
출처: https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1
APK: https://github.com/t0thkr1s/frida-demo/releases
Source Code: 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를 따라가세요.
You can find more Awesome Frida scripts here: https://codeshare.frida.re/
빠른 예제
커맨드라인에서 Frida 호출
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 Script
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 functions — 매개변수 없음
클래스 sg.vantagepoint.a.c
의 함수 a()
를 Hook하세요.
Java.perform(function () {
; rootcheck1.a.overload().implementation = function() {
rootcheck1.a.overload().implementation = function() {
send("sg.vantagepoint.a.c.a()Z Root check 1 HIT! su.exists()");
return false;
};
});
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)
}
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 함수의 매개변수와 반환값 가져오기
복호화 함수에 Hooking하기. 입력을 출력하고, 원본 함수를 호출해 입력을 복호화한 다음, 평문 데이터를 출력합니다:
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
}
Hooking functions 및 우리 입력으로 호출하기
string을 받는 function을 Hook하여 다른 string으로 호출한다 (from here)
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의 객체를 얻는 방법과, 해당 객체의 private 속성을 출력하는 .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 튜토리얼
- https://github.com/DERE-ad2001/Frida-Labs
- Part 1 of Advanced Frida Usage blog series: IOS Encryption Libraries
참고 자료
- Build a Repeatable Android Bug Bounty Lab: Emulator vs Magisk, Burp, Frida, and Medusa
- Frida Gadget documentation
- Frida releases (server binaries)
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 지원하기
- 구독 계획 확인하기!
- **💬 디스코드 그룹 또는 텔레그램 그룹에 참여하거나 트위터 🐦 @hacktricks_live를 팔로우하세요.
- HackTricks 및 HackTricks Cloud 깃허브 리포지토리에 PR을 제출하여 해킹 트릭을 공유하세요.