问题描述
使用Dyninst API,我们可以执行静态和动态二进制检测.
在静态检测中,我们可以重写二进制文件,并在特定位置注入额外的代码(代码段).
我已经测试了以下代码,以修补二进制代码.但是,此代码将片段插入了感兴趣的函数的输入区,即foo.
如何在特定地址插入代码段?
这是代码(链接到原始代码)
Using Dyninst API, we can perform both static and dynamic binary instrumentation.
In static instrumentation, we can rewrite a binary file and inject an extra code (snippets) at a specific points.
I have tested the following code in order to patch a binary code. However, this code inject the snippets at the entery of the interested function, i.e. foo.
How can we inject a snippets at a specific address?
this is the code (link to original code)
#include <stdio.h>
#include "BPatch.h"
#include "BPatch_addressSpace.h"
#include "BPatch_function.h"
#include "BPatch_binaryEdit.h"
#include "BPatch_point.h"
int main(int argc, const char *argv[]) {
// Use BPatch_* classes to initialize
BPatch bpatch;
BPatch_addressSpace *app = bpatch.openBinary("hello", true);
bool flag = false;
flag = app->loadLibrary("liblib.so");
BPatch_image* image = app->getImage();
std::vector<BPatch_function*> func;
image->findFunction("code_to_inject", func);
std::cout<<"\nFunction is: "<<func[0]->getName()<<std::endl;
std::vector<BPatch_snippet*> openArgs;
BPatch_funcCallExpr enter_call(*(func[0]), openArgs);
func.clear();
std::vector<BPatch_point *> *points;
image->findFunction("foo1", func);
points = func[0]->findPoint(BPatch_entry);
app->insertSnippet(enter_call, *points);
BPatch_binaryEdit *appBin = dynamic_cast<BPatch_binaryEdit *>(app);
appBin->writeFile("newbinary");
return 0;
}
我尝试过的事情:
我认为我们应该使用BPatch_function并传递一个特定的地址,但是我用字段来修饰修改后的代码.也许我们应该使用Dyninst :: Address.
感谢您的帮助
What I have tried:
I think we should use BPatch_function and pass a specific address, but i field to comiple the modified code. Maybe we should use Dyninst::Address.
Thanks for helps
推荐答案
这篇关于我们如何使用dyninst来执行特定指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!