0x00
Dyninst 学习暂时告一段落,学习的成果是为师兄做了一个二进制层面的静态插桩,之后也做了一些性能测试的工作。
0x01
主要以代码形式讲解和记录: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
37
38
39
40
41int main(int argc, char **argv)
{
//首先通过参数打开一个二进制文件
BPatch_addressSpace *app = bpatch->openBinary(argv[1]);
if (app == NULL)
{
exit(1);
}
//mgr 为patchAPI的主要操作对象
mgr = convert(app);
appImage = app->getImage();
Patcher patcher(mgr);
//获取modules和定义一个function的vector
vector<BPatch_module *> *modules = appImage->getModules();
vector<BPatch_function *> *functions;
//遍历所有modules,取出每一个modules的所有function
for (auto it = modules->begin(); it != modules->end(); ++it)
{
BPatch_module * module = *it;
functions = module->getProcedures();
}
//对每个function进行插桩
for (auto it = functions->begin(); it != functions->end(); ++it)
{
PatchFunction *func = convert(*it);
writeCanaryPoint(func);
}
//提交插桩结果,使插桩生效
patcher.commit();
cout << pts.size() << " inst points" << endl;
//重写二进制文件
string name = string(argv[1]);
finishInstrumenting(app, name.c_str());
cout << "Instrumentation Success!" << endl;
return 0;
}
1 | void writeCanaryPoint(PatchFunction *func) |
1 | void finishInstrumenting(BPatch_addressSpace *app, const char *newName) |
1 | bool overwritePoint(Point *pt, char *date, size_t size) |
源文件地址
safeCanary.cpp