mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
120 lines
4.2 KiB
Markdown
120 lines
4.2 KiB
Markdown
# Frida Tutorial 1
|
|
|
|
{{#include ../../../banners/hacktricks-training.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)\
|
|
**소스 코드**: [https://github.com/t0thkr1s/frida-demo](https://github.com/t0thkr1s/frida-demo)
|
|
|
|
## Python
|
|
|
|
Frida는 실행 중인 애플리케이션의 함수 내에 **JavaScript 코드를 삽입**할 수 있게 해줍니다. 하지만 **python**을 사용하여 **후크를 호출**하고 **후크와 상호작용**할 수도 있습니다.
|
|
|
|
이것은 이 튜토리얼의 모든 제안된 예제와 함께 사용할 수 있는 간단한 python 스크립트입니다:
|
|
```python
|
|
#hooking.py
|
|
import frida, sys
|
|
|
|
with open(sys.argv[1], 'r') as f:
|
|
jscode = f.read()
|
|
process = frida.get_usb_device().attach('infosecadventures.fridademo')
|
|
script = process.create_script(jscode)
|
|
print('[ * ] Running Frida Demo application')
|
|
script.load()
|
|
sys.stdin.read()
|
|
```
|
|
스크립트를 호출하세요:
|
|
```bash
|
|
python hooking.py <hookN.js>
|
|
```
|
|
python을 frida와 함께 사용하는 방법을 아는 것은 유용하지만, 이 예제에서는 명령줄 frida 도구를 사용하여 직접 Frida를 호출할 수도 있습니다:
|
|
```bash
|
|
frida -U --no-pause -l hookN.js -f infosecadventures.fridademo
|
|
```
|
|
## Hook 1 - Boolean Bypass
|
|
|
|
여기에서 클래스 _infosecadventures.fridademo.utils.PinUtil_의 **boolean** 메서드 (_checkPin_)를 **hook**하는 방법을 볼 수 있습니다.
|
|
```javascript
|
|
//hook1.js
|
|
Java.perform(function () {
|
|
console.log("[ * ] Starting implementation override...")
|
|
var MainActivity = Java.use("infosecadventures.fridademo.utils.PinUtil")
|
|
MainActivity.checkPin.implementation = function (pin) {
|
|
console.log("[ + ] PIN check successfully bypassed!")
|
|
return true
|
|
}
|
|
})
|
|
```
|
|
|
|
```
|
|
python hooking.py hook1.js
|
|
```
|
|
보세요: 함수는 매개변수로 String을 받습니다. 오버로드가 필요하지 않나요?
|
|
|
|
## Hook 2 - Function Bruteforce
|
|
|
|
### Non-Static Function
|
|
|
|
클래스의 비정적 함수를 호출하려면 **먼저 해당 클래스의 인스턴스가 필요**합니다. 그런 다음, 그 인스턴스를 사용하여 함수를 호출할 수 있습니다.\
|
|
이를 위해 **기존 인스턴스를 찾아서** 사용할 수 있습니다:
|
|
```javascript
|
|
Java.perform(function () {
|
|
console.log("[ * ] Starting PIN Brute-force, please wait...")
|
|
Java.choose("infosecadventures.fridademo.utils.PinUtil", {
|
|
onMatch: function (instance) {
|
|
console.log("[ * ] Instance found in memory: " + instance)
|
|
for (var i = 1000; i < 9999; i++) {
|
|
if (instance.checkPin(i + "") == true) {
|
|
console.log("[ + ] Found correct PIN: " + i)
|
|
break
|
|
}
|
|
}
|
|
},
|
|
onComplete: function () {},
|
|
})
|
|
})
|
|
```
|
|
이 경우 인스턴스가 없고 함수가 정적이기 때문에 작동하지 않습니다.
|
|
|
|
### 정적 함수
|
|
|
|
함수가 정적이라면, 그냥 호출할 수 있습니다:
|
|
```javascript
|
|
//hook2.js
|
|
Java.perform(function () {
|
|
console.log("[ * ] Starting PIN Brute-force, please wait...")
|
|
var PinUtil = Java.use("infosecadventures.fridademo.utils.PinUtil")
|
|
|
|
for (var i = 1000; i < 9999; i++) {
|
|
if (PinUtil.checkPin(i + "") == true) {
|
|
console.log("[ + ] Found correct PIN: " + i)
|
|
}
|
|
}
|
|
})
|
|
```
|
|
## Hook 3 - 인수 및 반환 값 가져오기
|
|
|
|
함수를 후킹하여 **전달된 인수**의 값과 **반환 값**의 값을 **출력**하도록 만들 수 있습니다:
|
|
```javascript
|
|
//hook3.js
|
|
Java.perform(function () {
|
|
console.log("[ * ] Starting implementation override...")
|
|
|
|
var EncryptionUtil = Java.use(
|
|
"infosecadventures.fridademo.utils.EncryptionUtil"
|
|
)
|
|
EncryptionUtil.encrypt.implementation = function (key, value) {
|
|
console.log("Key: " + key)
|
|
console.log("Value: " + value)
|
|
var encrypted_ret = this.encrypt(key, value) //Call the original function
|
|
console.log("Encrypted value: " + encrypted_ret)
|
|
return encrypted_ret
|
|
}
|
|
})
|
|
```
|
|
## 중요
|
|
|
|
이 튜토리얼에서는 메서드의 이름과 _.implementation_을 사용하여 메서드를 후킹했습니다. 그러나 **같은 이름을 가진 메서드가 여러 개** 있을 경우, 후킹하려는 **메서드를 지정**해야 하며 **인수의 유형을 표시**해야 합니다.
|
|
|
|
다음 튜토리얼에서 이를 확인할 수 있습니다.
|