mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
241 lines
9.3 KiB
Markdown
241 lines
9.3 KiB
Markdown
# Frida 튜토리얼
|
|
|
|
{{#include ../../../banners/hacktricks-training.md}}
|
|
|
|
|
|
## 설치
|
|
|
|
다음으로 **frida tools**를 설치하세요:
|
|
```bash
|
|
pip install frida-tools
|
|
pip install frida
|
|
```
|
|
Android에 **frida server**를 **다운로드 및 설치**하세요 ([Download the latest release](https://github.com/frida/frida/releases)).\
|
|
adb를 root 모드로 재시작하고 기기에 연결한 후 frida-server를 업로드하고 실행 권한을 부여한 뒤 백그라운드에서 실행하는 한 줄 명령:
|
|
```bash
|
|
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 &"
|
|
```
|
|
**확인**해 보세요: 해당 항목이 **작동하는지**
|
|
```bash
|
|
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): 어떤 프로세스에도 attach할 수 있게 해주는 네이티브 데몬을 푸시하고 실행한다.
|
|
- Frida Gadget (no root): Frida를 공유 라이브러리로 APK 내부에 번들하고 대상 프로세스 내에서 자동으로 로드한다.
|
|
|
|
Frida server (rooted)
|
|
```bash
|
|
# 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와 config를 추가하세요:
|
|
- libfrida-gadget.so를 lib/<abi>/ (예: lib/arm64-v8a/)에 배치합니다
|
|
- assets/frida-gadget.config 파일을 생성하고 스크립트 로드 설정을 추가합니다
|
|
|
|
예시 frida-gadget.config
|
|
```json
|
|
{
|
|
"interaction": { "type": "script", "path": "/sdcard/ssl-bypass.js" },
|
|
"runtime": { "logFile": "/sdcard/frida-gadget.log" }
|
|
}
|
|
```
|
|
2) gadget를 참조/로드하여 조기에 초기화되도록:
|
|
- 가장 쉬운 방법: Application.onCreate()에 System.loadLibrary("frida-gadget")를 호출하는 작은 Java stub을 추가하거나, 기존의 네이티브 라이브러리 로딩을 사용하세요.
|
|
|
|
3) APK를 재패키징하고 서명한 후 설치:
|
|
```bash
|
|
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
|
|
```
|
|
4) 호스트에서 gadget 프로세스에 연결:
|
|
```bash
|
|
frida-ps -Uai
|
|
frida -U -n com.example.app
|
|
```
|
|
노트
|
|
- Gadget은 일부 보호 메커니즘에 의해 탐지될 수 있으니 이름/경로를 은닉하고 필요하면 늦게 또는 조건부로 로드하세요.
|
|
- 하드닝된 앱의 경우 server + late attach를 이용한 rooted testing을 선호하거나 Magisk/Zygisk 은닉과 결합하세요.
|
|
|
|
## 튜토리얼
|
|
|
|
### [Tutorial 1](frida-tutorial-1.md)
|
|
|
|
**출처**: [https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1](https://medium.com/infosec-adventures/introduction-to-frida-5a3f51595ca1)\
|
|
**APK**: [https://github.com/t0thkr1s/frida-demo/releases](https://github.com/t0thkr1s/frida-demo/releases)\
|
|
**Source Code**: [https://github.com/t0thkr1s/frida-demo](https://github.com/t0thkr1s/frida-demo)
|
|
|
|
**읽으려면 [링크를 확인하세요.](frida-tutorial-1.md)**
|
|
|
|
### [Tutorial 2](frida-tutorial-2.md)
|
|
|
|
**출처**: [https://11x256.github.io/Frida-hooking-android-part-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](https://github.com/11x256/frida-android-examples)
|
|
|
|
**읽으려면 [링크를 확인하세요.](frida-tutorial-2.md)**
|
|
|
|
### [Tutorial 3](owaspuncrackable-1.md)
|
|
|
|
**출처**: [https://joshspicer.com/android-frida-1](https://joshspicer.com/android-frida-1)\
|
|
**APK**: [https://github.com/OWASP/owasp-mstg/blob/master/Crackmes/Android/Level_01/UnCrackable-Level1.apk](https://github.com/OWASP/owasp-mstg/blob/master/Crackmes/Android/Level_01/UnCrackable-Level1.apk)
|
|
|
|
**읽으려면 [링크를 확인하세요.](owaspuncrackable-1.md)**
|
|
|
|
**추가 Awesome Frida 스크립트는 여기에서 찾을 수 있습니다:** [**https://codeshare.frida.re/**](https://codeshare.frida.re)
|
|
|
|
## 빠른 예제
|
|
|
|
### 명령줄에서 Frida 호출하기
|
|
```bash
|
|
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 스크립트
|
|
```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하세요
|
|
```javascript
|
|
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()` 후킹
|
|
```javascript
|
|
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()`
|
|
```javascript
|
|
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()`
|
|
```javascript
|
|
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 함수를 Hooking합니다. 입력을 출력하고, 원본 함수를 호출해 입력을 decrypt한 다음, 마지막으로 평문 데이터를 출력합니다:
|
|
```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
|
|
}
|
|
```
|
|
### Hooking functions and calling them with our input
|
|
|
|
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
|
|
}
|
|
```
|
|
### 이미 생성된 클래스 객체 가져오기
|
|
|
|
이미 생성된 객체의 속성(attribute)을 추출하려면 이것을 사용할 수 있습니다.
|
|
|
|
이 예제에서는 클래스 my_activity의 객체를 가져오는 방법과 객체의 private 속성을 출력하는 .secret() 함수를 호출하는 방법을 보여줍니다:
|
|
```javascript
|
|
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](https://github.com/DERE-ad2001/Frida-Labs)
|
|
- [Advanced Frida Usage 블로그 시리즈 1부: iOS 암호화 라이브러리](https://8ksec.io/advanced-frida-usage-part-1-ios-encryption-libraries-8ksec-blogs/)
|
|
|
|
|
|
## 참고자료
|
|
|
|
- [재현 가능한 Android Bug Bounty Lab 구축: Emulator vs Magisk, Burp, Frida, and Medusa](https://www.yeswehack.com/learn-bug-bounty/android-lab-mobile-hacking-tools)
|
|
- [Frida Gadget 문서](https://frida.re/docs/gadget/)
|
|
- [Frida 릴리스 (server binaries)](https://github.com/frida/frida/releases)
|
|
|
|
{{#include ../../../banners/hacktricks-training.md}}
|