[Cheat Sheet] Frida
작업환경
| 1 2 3 | PC : Ubuntu 17.10(VMware) Android : Google Nexus 7 1st Generation(android 4.2.1)      // Java API 후킹 안됨 Android : Samsung Galaxy S5(android 4.4.2) | 
 
설치
PC와 안드로이드(물론, Frida는 다른 플랫폼도 지원한다.)에 각각 작업이 필요하다.
| 1 2 | pip install frida // frida frida-ps frida-trace 등이 설치됨 | 
안드로이드 플랫폼에 맞는 frida-server 파일을 다운로드(다운로드 링크)한다.
| 1 2 3 4 5 | (venv) xiphiasilver@ubuntu:~/android_tools/frida$ xz -d frida-server-10.6.21-android-arm.xz (venv) xiphiasilver@ubuntu:~/android_tools/frida$ adb push frida-server-10.6.21 /data/local/tmp (venv) xiphiasilver@ubuntu:~/android_tools/frida$ adb shell shell@android:/ $ su     // root 권한으로 실행하는 것이 추천됨 root@android:/ # /data/local/tmp/frida-server-10.6.21      // frida 서버 실행 | 
 
frida-ps
| 1 2 | frida-ps -U  // -U 옵션은 usb로 연결된 단말을 의미함. frida가 정상적으로 설치되었다면, android의 process list가 출력됨. frida-ps -a  // -a 옵션은 앱만 표시하는 것을 의미함. 앱 이름과 패키지 이름이 표시됨. | 
 
Java API Hooking
먼저, 후킹 대상 앱은 Frida 튜토리얼에서도 언급된 seccon2015.rock_paper_scissors(다운로드 링크)이다.
Frida를 이용하여 Java API를 후킹하는 방법은 두 가지가 있다. 첫 번째는 PC에서 frida 플랫폼을 실행시키고 자바스크립트를 이용하여 후킹하는 방법이다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | (venv) xiphiasilver@ubuntu:~/android_tools/frida$ cat rps_hook.js      // 자바스크립트를 준비 Java.perform(function () {  // Function to hook is defined here  var MainActivity = Java.use('com.example.seccon2015.rock_paper_scissors.MainActivity');  // Whenever button is clicked  MainActivity.onClick.implementation = function (v) {  // Show a message to know that the function got called  send('onClick');  // Call the original onClick handler  this.onClick(v);  // Set our values after running the original onClick handler  this.m.value = 0;  this.n.value = 1;  this.cnt.value = 999;  // Log to the console that it's done, and we should have the flag!  console.log('Done:' + JSON.stringify(this.cnt));  }; }); (venv) xiphiasilver@ubuntu:~/android_tools/frida$ frida -U com.example.seccon2015.rock_paper_scissors      // Frida 플랫폼 실행, -U 옵션은 usb 연결을 의미하며, 뒤에 패키지 이름이나 PID를 명시해야함.  ____  / _ | Frida 10.6.21 - A world-class dynamic instrumentation framework  | (_| |  > _ | Commands:  /_/ |_| help -> Displays the help system  . . . . object? -> Display information about 'object'  . . . . exit/quit -> Exit  . . . .  . . . . More info at http://www.frida.re/docs/home/ [Samsung SM-G900K::com.example.seccon2015.rock_paper_scissors]-> %load rps_hook.js      // 자바스크립트 로드 [Samsung SM-G900K::com.example.seccon2015.rock_paper_scissors]-> message: {u'type': u'send', u'payload': u'onClick'} data: None      // 후킹 결과 출력 Done:{"value":999,"fieldType":2,"fieldReturnType":{"className":"int","name":"I","type":"int32","size":1,"byteSize":4}} | 
두 번째 방법은 파이썬을 이용한 방법이다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | (venv) xiphiasilver@ubuntu:~/android_tools/frida$ cat rps_hook.py import frida, sys def on_message(message, data):     if message['type'] == 'send':         print("[*] {0}".format(message['payload']))     else:         print(message) jscode = """ Java.perform(function () {     // Function to hook is defined here     var MainActivity = Java.use('com.example.seccon2015.rock_paper_scissors.MainActivity');     // Whenever button is clicked     MainActivity.onClick.implementation = function (v) {         // Show a message to know that the function got called         send('onClick');         // Call the original onClick handler         this.onClick(v);         // Set our values after running the original onClick handler         this.m.value = 0;         this.n.value = 1;         this.cnt.value = 999;         // Log to the console that it's done, and we should have the flag!         console.log('Done:' + JSON.stringify(this.cnt));     }; }); """ process = frida.get_usb_device().attach('com.example.seccon2015.rock_paper_scissors') script = process.create_script(jscode) script.on('message', on_message) print('[*] Running CTF') script.load() sys.stdin.read() (venv) xiphiasilver@ubuntu:~/android_tools/frida$ python rps_hook.py [*] Running CTF [*] onClick Done:{"value":999,"fieldType":2,"fieldReturnType":{"className":"int","name":"I","type":"int32","size":1,"byteSize":4}} | 
 
참고문서
http://www.hahwul.com/2017/08/hacking-frida-hooking-to-multi-platform.html
http://blog.securekim.com/2016/10/android-frida.html
http://www.ninoishere.com/frida-learn-by-example/
https://www.frida.re/docs/examples/android/