-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlookup.cpp
More file actions
387 lines (308 loc) · 11 KB
/
lookup.cpp
File metadata and controls
387 lines (308 loc) · 11 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <set>
#include <algorithm>
#include "lookup.h"
#include "rgx.h"
using namespace std;
// trim from start
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
vector<string> format_gadgets(vector<string> gadgets) {
vector<string> to_return;
string fmtted_gadget = "";
for(int i=0; i<gadgets.size(); i++) {
fmtted_gadget = gadgets[i].substr(gadgets[i].find(":")+2, gadgets[i].length());
//cout << fmtted_gadget << endl;
to_return.push_back(rtrim(fmtted_gadget));
}
return to_return;
}
vector<string> delete_duplicate_gadgets(vector<string> gadgets) {
vector<string> to_return;
//printf("%lu\n", gadgets.size());
for(int i=0; i<gadgets.size(); i++) {
if ( std::find(to_return.begin(), to_return.end(), gadgets[i]) == to_return.end() ) {
to_return.push_back(gadgets[i]);
}
}
return to_return;
}
int find_gadgets(vector<string> gadgets, string pattern) {
smatch m;
regex rgx(pattern);
int total = 0;
for (int i=0; i<gadgets.size(); i++) {
if (regex_search(gadgets[i], m, rgx)) {
total += 1;
//cout << gadgets[i] << endl;
}
}
return total;
}
int find_gadgets_MOVTC(vector<string> gadgets, string pattern, set<string> *regset) {
smatch m, m2;
regex rgx(pattern);
regex rgx2(R"([er|r][abcdsi1-9]?[xip1-9])"); //for extracting registers
string gitem;
int total = 0;
for (int i=0; i<gadgets.size(); i++) {
if (regex_search(gadgets[i], m, rgx)) {
total += 1;
//cout << gadgets[i] << endl;
gitem = gadgets[i];
while (regex_search(gitem, m2, rgx2)){
//cout << m2.str() << " ";
regset->insert(m2.str());
gitem = m2.suffix();
}
//cout << endl;
}
}
return total;
}
int find_gadgets_MOVTC_count(vector<string> gadgets, string pattern, set<string> *gadget_set, set<string> *regset) {
smatch m, m2;
regex rgx(pattern);
regex rgx2(R"([er|r][abcdsi1-9]?[xip1-9])"); //for extracting registers
string gitem;
int total = 0;
for (int i=0; i<gadgets.size(); i++) {
if (regex_search(gadgets[i], m, rgx)) {
total += 1;
//cout << gadgets[i] << endl;
gadget_set->insert(gadgets[i]);
gitem = gadgets[i];
while (regex_search(gitem, m2, rgx2)){
//cout << m2.str() << " ";
regset->insert(m2.str());
gitem = m2.suffix();
}
//cout << endl;
}
}
return total;
}
int get_gadget_size(int which) {
jitrop_timing category = (jitrop_timing) which;
switch(category) {
case JITROP_TIME_TC: return TC_GADGETS; //TC set
case JITROP_TIME_PRIORITY: return PRIORITY_GADGETS; //Priority set
case JITROP_TIME_MOVTC: return MOVTC_GADGETS; //MOV TC set
case JITROP_TIME_MOVTC_COUNT: return MOVTC_GADGETS; //MOV TC set
case JITROP_TIME_PAYLOAD1: return PAYLOAD_ONE_GADGETS; //Payload one
default: return TC_GADGETS; //TC set
}
}
//TC gadget set, total 11
pair<string, string> TC_GADGET_SET[TC_GADGETS] = {
make_pair("LM_footprint", LM_FT_RELAX),
make_pair("SM_footprint", SM_FT_RELAX),
make_pair("LR_footprint", LR_FT),
make_pair("MR_footprint", MR_FT),
make_pair("AM_footprint", AM_FT),
make_pair("AMLD_footprint", AMLD_FT_RELAX),
make_pair("AMST_footprint", AMST_FT_RELAX),
make_pair("LOGIC_footprint", LOGIC_FT),
make_pair("JMP_footprint", JMP_FT),
make_pair("CALL_footprint", CALL_FT),
make_pair("SYS_footprint", SYS_FT)
};
//ALL gadget set, total 11
pair<string, string> ALL_GADGET_SET[ALL_GADGETS] = {
make_pair("LM_footprint", LM_FT_STRICT),
make_pair("SM_footprint", SM_FT_STRICT),
make_pair("LR_footprint", LR_FT),
make_pair("MR_footprint", MR_FT),
make_pair("AM_footprint", AM_FT),
make_pair("AMLD_footprint", AMLD_FT_STRICT),
make_pair("AMST_footprint", AMST_FT_STRICT),
make_pair("LOGIC_footprint", LOGIC_FT),
make_pair("JMP_footprint", JMP_FT),
make_pair("CALL_footprint", CALL_FT),
make_pair("SYS_footprint", SYS_FT),
make_pair("SP_footprint", CP_FT),
make_pair("CP_footprint", CP_FT),
//make_pair("RF_footprint", RF_FT),
make_pair("CS2_footprint", CS2_FT),
make_pair("EP_footprint", EP_FT),
make_pair("LM_non_footprint", LM_NONFT),
make_pair("SM_non_footprint", SM_NONFT),
make_pair("LR_non_footprint", LR_NONFT),
make_pair("MR_non_footprint", MR_NONFT),
make_pair("AM_non_footprint", AM_NONFT),
make_pair("AMLD_non_footprint", AMLD_NONFT),
make_pair("AMST_non_footprint", AMST_NONFT),
make_pair("LOGIC_non_footprint", LOGIC_NONFT),
make_pair("JMP_non_footprint", JMP_NONFT),
make_pair("CALL_non_footprint", CALL_NONFT),
make_pair("SYS_non_footprint", SYS_NONFT),
make_pair("SP_non_footprint", SP_NONFT),
make_pair("CP_non_footprint", CP_NONFT),
//make_pair("RF_non_footprint", RF_NONFT),
make_pair("CS2_non_footprint", CS2_NONFT)
};
// Priority gadget set, total 14
pair<string, string> PRIORITY_GADGET_SET[PRIORITY_GADGETS] = {
make_pair("PRIORITY_gadgets_1", PRIORITY_gadgets_1),
make_pair("PRIORITY_gadgets_2", PRIORITY_gadgets_2),
//make_pair("PRIORITY_gadgets_3", PRIORITY_gadgets_3),
make_pair("PRIORITY_gadgets_4", PRIORITY_gadgets_4),
//make_pair("PRIORITY_gadgets_5", PRIORITY_gadgets_5),
make_pair("PRIORITY_gadgets_6", PRIORITY_gadgets_6),
make_pair("PRIORITY_gadgets_7", PRIORITY_gadgets_7),
make_pair("PRIORITY_gadgets_8", PRIORITY_gadgets_8),
make_pair("PRIORITY_gadgets_9", PRIORITY_gadgets_9),
//make_pair("PRIORITY_gadgets_10", PRIORITY_gadgets_10),
//make_pair("PRIORITY_gadgets_11", PRIORITY_gadgets_11),
make_pair("PRIORITY_gadgets_12", PRIORITY_gadgets_12),
make_pair("PRIORITY_gadgets_13", PRIORITY_gadgets_13),
make_pair("PRIORITY_gadgets_14", PRIORITY_gadgets_14),
make_pair("PRIORITY_gadgets_15", PRIORITY_gadgets_15),
make_pair("PRIORITY_gadgets_16", PRIORITY_gadgets_16),
make_pair("PRIORITY_gadgets_17", PRIORITY_gadgets_17)
};
// MOVTC gadget set, total 7 excluding the system gadget
pair<string, string> MOVTC_GADGET_SET[MOVTC_GADGETS] = {
make_pair("MOVTC_MR", MOVTC_MR),
make_pair("MOVTC_MRCONST", MOVTC_MRCONST),
make_pair("MOVTC_ST", MOVTC_ST),
make_pair("MOVTC_STCONSTEX", MOVTC_STCONSTEX),
make_pair("MOVTC_STCONST", MOVTC_STCONST),
make_pair("MOVTC_LM", MOVTC_LM),
make_pair("MOVTC_LMEX", MOVTC_LMEX),
make_pair("MOVTC_SYS", MOVTC_SYS)
};
//gadgets for payload one, total 10
pair<string, string> PAYLOAD_ONE_GADGET_SET[PAYLOAD_ONE_GADGETS] = {
make_pair("GADGET_PAYLOAD_ONE_1", GADGET_PAYLOAD_ONE_1),
make_pair("GADGET_PAYLOAD_ONE_2", GADGET_PAYLOAD_ONE_2),
make_pair("GADGET_PAYLOAD_ONE_3", GADGET_PAYLOAD_ONE_3),
make_pair("GADGET_PAYLOAD_ONE_4", GADGET_PAYLOAD_ONE_4),
//make_pair("GADGET_PAYLOAD_ONE_5", GADGET_PAYLOAD_ONE_5),
make_pair("GADGET_PAYLOAD_ONE_6", GADGET_PAYLOAD_ONE_6),
make_pair("GADGET_PAYLOAD_ONE_7", GADGET_PAYLOAD_ONE_7),
make_pair("GADGET_PAYLOAD_ONE_8", GADGET_PAYLOAD_ONE_8),
make_pair("GADGET_PAYLOAD_ONE_9", GADGET_PAYLOAD_ONE_9),
make_pair("GADGET_PAYLOAD_ONE_10", GADGET_PAYLOAD_ONE_10),
make_pair("GADGET_PAYLOAD_ONE_11", GADGET_PAYLOAD_ONE_11)
};
void get_min_tc_set(vector<string> gadgets, int *tc_set) {
vector<string> result_unfmt = format_gadgets(gadgets);
vector<string> result = delete_duplicate_gadgets(result_unfmt);
for (int i = 0; i < TC_GADGETS; i++) {
if (!tc_set[i]) {
if(find_gadgets(result, TC_GADGET_SET[i].second) > 0) {
tc_set[i] = 1;
}
}
}
result_unfmt.clear();
result.clear();
}
void get_priority_set(vector<string> gadgets, int *tc_set) {
vector<string> result_unfmt = format_gadgets(gadgets);
vector<string> result = delete_duplicate_gadgets(result_unfmt);
for (int i = 0; i < PRIORITY_GADGETS; i++) {
if (!tc_set[i]) {
//cout << "!TC_SET " << i << endl;
if (find_gadgets(result, PRIORITY_GADGET_SET[i].second) > 0) {
tc_set[i] = 1;
}
}
}
result_unfmt.clear();
result.clear();
}
void get_mov_tc_set(vector<string> gadgets, int *tc_set, set<string> *regset) {
vector<string> result_unfmt = format_gadgets(gadgets);
vector<string> result = delete_duplicate_gadgets(result_unfmt);
for (int i = 0; i < MOVTC_GADGETS; i++) {
if (!tc_set[i] || regset->size() < 4) { //need four unique registers
if(find_gadgets_MOVTC(result, MOVTC_GADGET_SET[i].second, regset) > 0) {
tc_set[i] = 1;
}
}
}
result_unfmt.clear();
result.clear();
}
void get_payload_set_one(vector<string> gadgets, int *tc_set) {
vector<string> result_unfmt = format_gadgets(gadgets);
vector<string> result = delete_duplicate_gadgets(result_unfmt);
for (int i=0; i<PAYLOAD_ONE_GADGETS; i++) {
if (!tc_set[i]) {
//cout << i << endl;
if(find_gadgets(result, PAYLOAD_ONE_GADGET_SET[i].second) > 0) {
tc_set[i] = 1;
}
}
}
result_unfmt.clear();
result.clear();
}
void lookup_gadgets(vector<string> gadgets) {
vector<string> result_unfmt = format_gadgets(gadgets);
vector<string> result = delete_duplicate_gadgets(result_unfmt);
//printf("%lu\n", result.size());
//cout << find_gadgets(result, LM_FT_RELAX) << endl;
for (int i = 0; i < TC_GADGETS; i++) {
cout << TC_GADGET_SET[i].first << " " << find_gadgets(result, TC_GADGET_SET[i].second) << endl;
}
//for (int i = 0; i < ALL_GADGETS; i++) {
// cout << ALL_GADGET_SET[i].first << " " << find_gadgets(result, ALL_GADGET_SET[i].second) << endl;
//}
//for (int i=0; i<result.size(); i++)
// cout << result[i] << endl;
result_unfmt.clear();
result.clear();
}
void lookup_priority_gadgets(vector<string> gadgets) {
vector<string> result_unfmt = format_gadgets(gadgets);
vector<string> result = delete_duplicate_gadgets(result_unfmt);
for (int i = 0; i < PRIORITY_GADGETS; i++) {
cout << PRIORITY_GADGET_SET[i].first << " " << find_gadgets(result, PRIORITY_GADGET_SET[i].second) << endl;
}
result_unfmt.clear();
result.clear();
}
void lookup_movtc_gadgets(vector<string> gadgets, set<string> *gadget_set, set<string> *regset) {
vector<string> result_unfmt = format_gadgets(gadgets);
vector<string> result = delete_duplicate_gadgets(result_unfmt);
for (int i = 0; i < MOVTC_GADGETS; i++) {
cout << MOVTC_GADGET_SET[i].first << " "
<< find_gadgets_MOVTC_count(result, MOVTC_GADGET_SET[i].second, gadget_set, regset) << endl;
}
result_unfmt.clear();
result.clear();
}
void get_mov_tc_count(vector<string> gadgets, int *tc_set, set<string> *gadget_set, set<string> *regset) {
vector<string> result_unfmt = format_gadgets(gadgets);
vector<string> result = delete_duplicate_gadgets(result_unfmt);
int total = 0;
for (int i = 0; i < MOVTC_GADGETS; i++) {
//if (!tc_set[i] || regset->size() < 4) { //need four unique registers
total = find_gadgets_MOVTC_count(result, MOVTC_GADGET_SET[i].second, gadget_set, regset);
if( total > 0) {
tc_set[i] += total;
}
//}
}
result_unfmt.clear();
result.clear();
}