Frida 기본 예제 ( Windows )
# Frida 활용 예제 ( Windows )
- Frida는 scriptable한 DBI 프레임워크로 http://www.frida.re에서 무료 로 다운로드 할 수 있다.
- 바이너리 조작은 자바스크립트로 이용해야 하여 C/S 구조로 되어 있다.
- 처음에 바이너리에 프레임워크 라이브러리르 인젝션하여 파이프를 만들어 놓고 그 파이프를 통해서 명령을 주고 받으며 바이너리를 조사할 수 있다.
- Frida의 가장 큰 장점은 다양한 플랫폼을 지원한다. Intel 뿐 만 아니라 Android, IOS 앱에도 적용 가능하다.
- 함수 후킹 예제 - 기본
- 테스트 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include <windows.h>
void f(int n) { printf("Number : %d \n",n); }
int main() { int i = 0;
printf("f() is at %p \n", f);
while (1) { f(i++); Sleep(1000); }
return 0; } |
- Frida 예제 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from __future__ import print_function import frida import sys
session = frida.attach("frida_test.exe") script = session.create_script(""" Interceptor.attach(ptr("%s"), { onEnter: function(args) { send(args[0].toInt32()); } }); """ % int(sys.argv[1], 16)) def on_message(message, data): print(message) script.on('message', on_message) script.load() sys.stdin.read() |
- 테스트 프로그램 실행
- F() 함수의 주소 : 00F612B2
- Frida 스크립트 실행
- F() 함수의 주소를 입력하면 그 주소를 후킹하며, Frida가 후킹된 함수가 받는 인자 값을 가져와서 출력하는 것을 확인할 수 있다.
- 함수 후킹 예제 - Argument 수정
- Frida 예제 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import frida import sys
session = frida.attach("hello") script = session.create_script(""" Interceptor.attach(ptr("%s"), { onEnter: function(args) { args[0] = ptr("1337"); } }); """ % int(sys.argv[1], 16)) script.load() sys.stdin.read() |
- Frida 스크립트 실행
- F() 함수의 주소를 입력하면 그 주소를 후킹하며, Frida가 후킹된 함수가 받는 인자 값을 임의 값(1337)으로 넣었다. 그 결과, 임의 값(1337)이 출력된 것을 확인할 수 있다.
- 함수 후킹 예제 - Function 호출
- Frida 예제 코드
1 2 3 4 5 6 7 8 9 10 11 |
import frida import sys
session = frida.attach("hello") script = session.create_script(""" var f = new NativeFunction(ptr("%s"), 'void', ['int']); f(1911); f(1911); f(1911); """ % int(sys.argv[1], 16)) script.load() |
- Frida 스크립트 실행
- f() 함수의 주소를 가지고 f() 함수를 호출한다. 이 때 인자를 '1911' 이란 임의 값을 넣으면, 1911이라는 값이 출력된다.
- 함수 후킹 예제 - String 삽입 및 Function 호출
- Frida 예제 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from __future__ import print_function import frida import sys
session = frida.attach("frida_test.exe") script = session.create_script(""" var st = Memory.allocUtf8String("TESTMEPLZ!"); var f = new NativeFunction(ptr("%s"), 'int', ['pointer']); // In NativeFunction param 2 is the return value type, // and param 3 is an array of input types f(st); """ % int(sys.argv[1], 16)) def on_message(message, data): print(message) script.on('message', on_message) script.load() |
- Frida 스크립트 실행
- f() 함수의 주소를 가지고 f() 함수를 호출한다. 이 때 인자에 "TESTMEPLZ!" 문자열을 할당한 주소를 주면 "TESTMEPLZ!"가 출력된다.
'■ Security > - System Hacking' 카테고리의 다른 글
Argc 및 Argv에 대한 어셈블리어 (0) | 2023.08.17 |
---|