forked from dyninst/dyninst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
125 lines (81 loc) · 2.6 KB
/
main.cpp
File metadata and controls
125 lines (81 loc) · 2.6 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// DynInst
#include "BPatch.h"
#include "BPatch_binaryEdit.h"
#include "BPatch_image.h"
#include "BPatch_function.h"
#include "BPatch_object.h"
#include "BPatch_point.h"
#include "BPatch_Vector.h"
#include <stdlib.h>
#include <stdio.h>
#include <iterator>
#include <string>
#include <vector>
#include <iterator>
// patchAPI
#include "PatchMgr.h"
using namespace std;
using namespace Dyninst;
using namespace PatchAPI;
// a simple example, just insert a bunch of no ops
class NoopSnippet : public Snippet {
public:
bool generate(Point *pt, Buffer &buffer){
uint8_t byte = 0x90;
cout << "inserting a no op @" << pt << endl;
for(int i = 0; i < 10; i++){
buffer.push_back(byte);
}
return true;
}
};
int main(int argc, const char *argv[]) {
if(argc != 3){
cerr << "Usage:\n\t" << argv[0] << " <input binary> <output binary path>" << endl;
return 1;
}
const char* input_binary = argv[1];
const char* output_binary = argv[2];
BPatch bpatch;
BPatch_binaryEdit* app = bpatch.openBinary(input_binary, false);
if(app == NULL){
return 0;
}
cout << "app OK" << endl;
BPatch_image* image = app->getImage();
if(image == NULL){
return 0;
}
cout << "image OK" << endl;
PatchMgrPtr patchMgr = PatchAPI::convert(image);
vector<BPatch_object*> objects;
image->getObjects(objects);
int ocount = objects.size();
cout << "objects: " << ocount << endl;
if(ocount <= 0){
return 0;
}
BPatch_object* batchObj = objects[0];
// Not mentioned in A.2 of https://dyninst.org/sites/default/files/manuals/dyninst/patchAPI.pdf
// But found in the header file: "BPatch_object.h"
PatchObject* binobj = PatchAPI::convert(batchObj);
Patcher patcher(patchMgr);
NoopSnippet::Ptr snippet = NoopSnippet::create(new NoopSnippet);
vector<PatchFunction*> functions;
binobj->funcs(back_inserter(functions));
for(vector<PatchFunction*>::iterator funIter = functions.begin(); funIter != functions.end(); funIter++){
PatchFunction *fun = *funIter;
vector<Point*> f_entryPoints;
patchMgr->findPoints(PatchAPI::Scope(fun), PatchAPI::Point::FuncEntry, back_inserter(f_entryPoints));
cout << fun->name() << " has:\n\t" << f_entryPoints.size() << " entry points" << endl;
for(vector<Point*>::iterator pointIter = f_entryPoints.begin(); pointIter!= f_entryPoints.end(); pointIter++){
Point* point = *pointIter;
cerr << "Patching @ " << point << endl;
patcher.add(PushBackCommand::create(point, snippet));
}
}
patcher.commit();
cout << "Commited" << endl;
app->writeFile(output_binary);
cout << "Written" << endl;
}