콘텐츠로 이동

[Frida] js 파일 따로 작성해서 실행하는 방법

서론

Frida Script는 Javscript API를 이용하여 코딩하고 실행하게 된다.
하지만 python 코드 내의 Javascript는 문자열이기에
에디터의 Syntax Highlight가 적용되지 않는다.
앱을 Spawn하면서 js 파일을 불러오는 방법에 대해 소개한다.

Syntax Highlight

아래의 코드는 공식 Frida 사이트의 안드로이드 Example이다.

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
  var onClick = MainActivity.onClick;
  onClick.implementation = function (v) {
    // Show a message to know that the function got called
    send('onClick');

    // Call the original onClick handler
    onClick.call(this, 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()

에디터에서 위처럼 Javascript 코드가 Syntax Highlighting이 되지 않는다.
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
  var onClick = MainActivity.onClick;
  onClick.implementation = function (v) {
    // Show a message to know that the function got called
    send('onClick');

    // Call the original onClick handler
    onClick.call(this, 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));
  };
});

Syntax Highlighting이 되어 이렇게 예쁘게(?) 보인다.
그래서 보통 js파일을 따로 만드는 것이 편하다.

방법 1. frida CLI 사용

frida-tools를 설치했다면 frida를 실행하여 frida CLI 사용이 가능하다.

>frida -U -l [js파일] -f [앱] --no-pause

옵션 설명

-U, --usb : USB로 연결
-l, --load : 스크립트 불러오기
-f, --file : 해당 앱 Spawn 하기
--no-pause : 자동 시작

주의

frida 실행이 안되면 pip install frida-tools로 설치하자.
--no-pause 옵션이 없으면 앱이 계속 멈춰 있는다.

방법 2. js 파일을 불러오는 py 코드 짜기

import frida, sys

bundle = "[앱]"
scriptfile = "[js파일]"

with open("./" + scriptfile, 'r') as f:
    script = f.read()

device = frida.get_usb_device(5)
target_process = device.spawn(bundle)
process_session = device.attach(target_process)

script = process_session.create_script(script)
script.load()

device.resume(target_process)
sys.stdin.read()

이 py 파일을 실행하면 동일한 결과를 얻을 수 있다.

결론

앱 Spawn, Frida js 파일 실행

  1. frida -U -l [js파일] -f [앱] --no-pause
  2. js 파일 읽는 py 실행

끝.


마지막 업데이트: 2022-05-13

댓글