-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgadget.cpp
356 lines (253 loc) · 8.79 KB
/
gadget.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <iostream>
#include <string>
#include <regex>
#include <tuple>
#include <queue>
#include "gadget.h"
#include <capstone/capstone.h>
using namespace std;
vector<vector<Ending> > myvect{
{
Ending("\xc3", 1, 1),
Ending("\xc2([\x00-\x7f\x80-\xff]{2})", 14, 3)
},
{
Ending("\x0f\x05", 2, 2),
Ending("\x0f\x05\xc3", 3, 3)
},
{
Ending("\xff[\x20\x21\x22\x23\x26\x27]", 9, 2),
Ending("\xff[\xe0\xe1\xe2\xe3\xe4\xe6\xe7]", 10, 2),
Ending("\xff[\x10\x11\x12\x13\x16\x17]", 9, 2),
Ending("\xff[\xd0\xd1\xd2\xd3\xd4\xd6\xd7]", 10, 2),
Ending("\xff[\x14\x24]\x24", 6, 3),
Ending("\xff\x55\x00", 3, 3),
Ending("\xff\x65\x00", 3, 3),
Ending("\xff[\xa0\xa1\xa2\xa3\xa6\xa7][\x00-\x7f\x80-\xff]{4}", 20, 6),
Ending("\xff\xa4\x24[\x00-\x7f\x80-\xff]{4}", 14, 7),
Ending("\xff[\x50-\x53\x55-\x57][\x00-\x7f\x80-\xff]{1}", 20, 3),
Ending("\xff[\x60-\x63\x65-\x67][\x00-\x7f\x80-\xff]{1}", 20, 3),
Ending("\xff[\x90\x91\x92\x93\x94\x96\x97][\x00-\x7f\x80-\xff]{4}", 21, 6)
}
};
#define BAD_INST_COUNT 22
string _badInstructions[] = {"retf","enter","loop","loopne","int3", "db", "ret", "jmp",
"les", "lds", "jle","jl", "jb","jbe","jg","jge","ja","jae",
"jne", "je", "js", "jrcxz"};
bool find_bad_inst(char *mnem) {
for (int i=0; i < BAD_INST_COUNT; i++) { // BAD_INST_COUNT defined in x86inst.h
if(_badInstructions[i].compare(mnem) == 0)
return true;
}
return false;
}
int disas_raw_code2(unsigned char *custom_code, size_t code_size, unsigned long start_addr){
csh handle;
cs_insn *insn;
size_t count;
//printf("%ld\n", sizeof(custom_code));
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
return -1;
count = cs_disasm(handle, custom_code, code_size, start_addr, 0, &insn);
if (count > 0) {
size_t j;
for (j = 0; j < count; j++) {
printf("0x%" PRIx64 ":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,
insn[j].op_str);
printf("%" PRIu16 "\n", insn[j].size);
}
printf("\n\n");
cs_free(insn, count);
} else
printf("ERROR: Failed to disassemble given code!\n");
cs_close(&handle);
return 0;
}
Gadget create_gadget(char *code, int start, int end, unsigned long start_addr, Ending ending) {
Gadget to_return, empty_gadget;
csh handle;
cs_insn *insn;
size_t count;
//printf("%d\n", end-start);
unsigned char *new_code = (unsigned char *)&code[start];
//disas_raw_code2(new_code, end-start, start_addr);
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
return empty_gadget; //gadget is empty at this point
count = cs_disasm(handle, new_code, end-start, start_addr, 0, &insn);
regex r2e(ending.rgx, ending.rlen, std::regex_constants::ECMAScript);
bool hasret = false;
if (count > 0) {
size_t j;
for (j = 0; j < count; j++) {
char *ibytes = (char *)insn[j].bytes;
//cout << "Match = " << regex_match(ibytes, ibytes+ending.blen, r2e) << endl;
/*
//for(int li=0; li<3; li++)printf("%x ", ibytes[li]);
//printf("\n");
auto match_start = cregex_iterator(&ibytes[0], &ibytes[ending.blen], r2e);
auto match_end = cregex_iterator();
//cout << "Number of matches " << distance(match_start, match_end) << endl;
if (distance(match_start, match_end) > 0)
hasret = true;*/
if (regex_match(ibytes, ibytes+ending.blen, r2e))
hasret = true;
//if(memcmp(ending.rgx, insn[j].bytes, ending.clen()) == 0)
// hasret = true;
if (hasret || !find_bad_inst(insn[j].mnemonic)) {
//printf("0x%"PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,
// insn[j].op_str);
//printf("strlen = %d\n", strlen(insn[j].op_str));
to_return.append(insn[j].address, insn[j].mnemonic, insn[j].op_str);
}
if (hasret || find_bad_inst(insn[j].mnemonic)) {
//printf("In break %s\n", insn[j].mnemonic);
break;
}
}
//printf("\n\n");
cs_free(insn, count);
} else {
//printf("ERROR: Failed to disassemble given code!\n");
cs_close(&handle);
return empty_gadget; // gadget is empty at this point
}
cs_close(&handle);
if (hasret && to_return.length() > 0)
return to_return;
return empty_gadget;
}
vector<string> gather_gadget_by_ending(char *code, size_t size, unsigned long start_addr, Ending ending, int inst_count) {
vector<string> to_return;
int index, cindex, none_count, offset_tmp = 0;
int align = 1;
int x, last_match;
//cmatch m;
//regex e ("[\x00-\xff]\\{2\\}"); // matches words beginning by "sub"
//char cReg[] = "\xc2([^\x00-\x7f]{2})";
//char cReg[] = "\xc2([\x00-\x7f]?[^\x80-\xff]?[\x00-\x7f]?[^\x80-\xff]?[\x00-\x7f]?[^\x80-\xff]?[\x00-\x7f]?[^\x80-\xff]?)";
//regex r2e(cReg,16+39, std::regex_constants::ECMAScript);
//char cReg[] = "\xc2([^\x00-\x7f]{2})";
//char cReg[] = "\xc3";
//regex r2e(cReg, 1, std::regex_constants::ECMAScript);
regex r2e(ending.rgx, ending.rlen, std::regex_constants::ECMAScript);
//char *pattern = "\xc3";
//int patlen = 1;
Gadget new_gadget;
cindex = 0;
int i = 0;
offset_tmp = 0;
index = 0;
auto match_start = std::cregex_iterator(&code[0], &code[size-1], r2e);
auto match_end = std::cregex_iterator();
//std::cout << "Found "
// << std::distance(match_start, match_end)
// << " words:\n";
last_match = 0;
for (std::cregex_iterator it = match_start; it != match_end; ++it) {
std::cmatch match = *it;
//cout << "Found at " << match.position() << endl;
index = match.position();
offset_tmp += index;
cindex = index - last_match;
if (offset_tmp % align == 0) {
none_count = 0;
//disas_raw_code2((unsigned char*)&code[index], 3, start_addr);
//printf("%d\n", cindex);
for (x = 0; x <= cindex; x += align) {
new_gadget = create_gadget(code, index-x, index+ending.blen, /*start_addr+index-x*/offset_tmp-x, ending);
if(!new_gadget.is_empty()) {
if (new_gadget.length() > inst_count)
break;
//cout << new_gadget.get_gadget() << endl;
to_return.push_back(new_gadget.get_gadget());
none_count = 0;
} else {
none_count += 1;
if (none_count == 8) //arch.maxInvalid
break;
}
}
}
last_match = index;
//std::string match_str = match.str();
//std::cout << match_str << '\n';
}
return to_return;
/*
cindex = 0;
i = 0;
offset_tmp = 0;
index = 0;
while (index < size) {
if(memcmp(&code[index], pattern, patlen) == 0){
printf("===========Found at %d===============\n", index);
offset_tmp += cindex;
//printf("%d\n", offset_tmp);
if (offset_tmp % align == 0){
none_count = 0;
printf("%d\n", cindex);
for (x = 0; x <= cindex; x += align) {
new_gadget = create_gadget(code, index-x, index+patlen, start_addr+index-x, pattern);
if(!new_gadget.is_empty()) {
if (new_gadget.length() > inst_count)
break;
cout << new_gadget.get_gadget() << endl;
none_count = 0;
} else {
none_count += 1;
if (none_count == 8) //arch.maxInvalid
break;
}
}
}
cindex = 0;
index += align;
continue;
}
index++;
cindex++;
}*/
}
bool operator<(const Ending& e1, const Ending& e2) {
return e2.blen < e1.blen;
}
vector<string> create_gadgets(
uint8_t *dest_buffer,
size_t size,
unsigned long target_address,
int inst_count,
int gtype)
{
priority_queue<Ending> pq;
vector<string> to_return, result;
if (gtype == 3) { //For all gadgets
for (int i = 0; i < myvect.size(); i++) {
for (int j = 0; j < myvect[i].size(); j++)
pq.push(myvect[i][j]);
}
}else {
for (int j = 0; j < myvect[gtype].size(); j++)
pq.push(myvect[gtype][j]);
}
//int total = 0;
while (!pq.empty()){
//cout << pq.top().blen << " ";
result = gather_gadget_by_ending((char *)dest_buffer, size, target_address, pq.top(), inst_count);
//total += result.size();
copy(result.begin(), result.end(), back_inserter(to_return));
result.clear();
pq.pop();
}
//for(int i=0; i<to_return.size(); i++)
// cout << "PFV: " << to_return[i] << endl;
return to_return;
//printf("%d\n", total);
//printf("%d\n", to_return.size());
//cout << endl;
//Ending test("\xc3", 1, 1);
// Ending test("\xc2([\x00-\x7f]?[^\x80-\xff]?[\x00-\x7f]?[^\x80-\xff]?[\x00-\x7f]?[^\x80-\xff]?[\x00-\x7f]?[^\x80-\xff]?)", 55, 3, 1);
//Ending test("\xc2([\x00-\x7f\x80-\xff]{2})", 14, 3, 1);
//gather_gadget_by_ending((char *)dest_buffer, size, target_address, test, 5);
//printf("%0x %ld\n", target_address, size);
//disas_raw_code2(dest_buffer, size, target_address);
}