逆向工程 - Frida - Hook Process Function



參考資訊:
https://github.com/frida/frida
https://frida.re/docs/functions/
https://frida.re/docs/installation/

hello.c

#include <stdio.h>
#include <unistd.h>

static void print_value(int v)
{
    printf("%d\n", v);
}

int main(int argc, char *argv[])
{
    int cc = 0;

    printf("print_value()=%p\n", print_value);

    while (1) {
        print_value(cc++);
        usleep(1000000);
    }

    return 0;
}

hook.py

import sys
import frida

def on_message(message, data):
    print(message)

session = frida.attach("hello")

script = session.create_script("""
    Interceptor.attach(ptr("%s"), {
        onEnter(args) {
            send(args[0].toInt32());
        }
    });
""" % int(sys.argv[1], 16))

script.on('message', on_message)
script.load()
sys.stdin.read()

編譯、執行

$ gcc hello.c -o hello
$ ./hello&
    print_value()=0x555841a18149
    0
    1
    2
    3
    4
    5

$ python3 ./hook.py 0x555841a18149
    {'type': 'send', 'payload': 6}
    {'type': 'send', 'payload': 7}
    {'type': 'send', 'payload': 8}
    {'type': 'send', 'payload': 9}