详细信息都可以在拦截器的Javascript API 文档中找到
下面是一个小演示
假设您有如下源代码,adder 函数将从 3 个地方调用,总共调用 26 次
#include <stdio.h>
int adder( int a , int b) {
return a+b;
}
int addonce (int a, int b) {
return adder(a,b);
}
int addtwice (int a, int b) {
return adder(a,b) + adder (a,b);
}
int addntimes(int a, int b, int c) {
int res = 0;
for (int i = 0; i < c; i++ ) {
res = res + adder(a,b);
}
return res;
}
void main(void) {
getchar();
printf("%d\n", addonce(2,3));
printf("%d\n", addtwice(2,3));
printf("%d\n", addntimes(2,3,10));
printf("%d\n", addonce(2,3)+addtwice(2,3)+addntimes(2,3,10));
}
用 vs 2017 社区编译并执行
cl /Zi /W4 /analyze /Od /EHsc mulcall.cpp /link / release
5
10
50
65
弗里达python脚本
import frida
import sys
session = frida.attach("mulcall.exe")
script = session.create_script("""
Interceptor.attach
(
ptr("%s"),
{
onEnter: function(args)
{
console.log("entering intercepted function will return to " + this.returnAddress);
} ,
onLeave: function(retval)
{
console.log( "leaving intercepted function returning " + retval.toInt32());
}
}
);
""" % int(sys.argv[1], 16))
def on_message(message, data):
print(message)
script.on('message', on_message)
script.load()
sys.stdin.read()
您需要必须传递的加法器函数的地址(在您的情况下为 sub_yyyy 的 5xxx 地址)请注意 ASLR 可能会起作用您总是需要正在运行的实例的新地址而不是过去实例的一些陈旧地址
你会像这样运行脚本
python friscript.py 7ff670901000
0x00007ff670901000 是我的 adder() 的地址我已经执行了 exe 并且它正在等待按键现在我运行它附加的上面的脚本并等待直到我在等待实例中按下一个键
这是弗里达的输出
python friscript.py 7ff670901000
entering intercepted function will return to 0x7ff670901039
leaving intercepted function returning 5
entering intercepted function will return to 0x7ff670901069
leaving intercepted function returning 5
entering intercepted function will return to 0x7ff67090107a
leaving intercepted function returning 5
entering intercepted function will return to 0x7ff6709010d4
leaving intercepted function returning 5 (10 times)
entering intercepted function will return to 0x7ff670901039
leaving intercepted function returning 5
entering intercepted function will return to 0x7ff670901069
leaving intercepted function returning 5
entering intercepted function will return to 0x7ff67090107a
leaving intercepted function returning 5
entering intercepted function will return to 0x7ff6709010d4
leaving intercepted function returning 5 (10 times)
编辑以解决评论
如果 eax 是指向某种类型的指针 (ansi,wide,utf8,utf16,bytearray,struct *)
在 onLeave {} 中使用适当的辅助函数
这里是返回 struct *
struct { int a , char * b } 的函数的实现
// hack for getting the next member of struct (adding pointer size of
// 32bit machine read documents to see if you can cast the return
// value to proper structure type
// so that we can use (foo *) (this.context.eax)->a
// instead of hacks like add(4)
foo = Memory.readPointer(this.context.eax.add(4))
blah = Memory.readCString(foo)
log( blah )