-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigmaker.c
More file actions
517 lines (398 loc) · 10.9 KB
/
Copy pathsigmaker.c
File metadata and controls
517 lines (398 loc) · 10.9 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <getopt.h>
#include <unistd.h>
#include <ctype.h>
#define ARR_SIZE(arr) (sizeof(arr) / sizeof(*arr))
struct signature
{
char *bytes;
size_t idx;
size_t size;
size_t len;
size_t offset;
};
struct instruction
{
/* x86_64 max number of bytes for an instruction is 15. */
/* 15 * 3, beacuse there is whitespace between every byte + one byte is
* two characters. + 1 for null terminator.*/
#define INST_SIZE ((15 * 3) + 1)
char bytes[INST_SIZE];
/* size should be more then enough */
char mnemonic[32];
char op1[64];
char op2[64];
struct instruction *next;
};
struct user_input
{
char *file_path;
char *target;
int max_sig_len;
int offset;
};
static long get_file_size(FILE *f);
static char *get_file_content(const char *path);
static char *get_location(const char *haystack, const char *needle);
static char *get_func_data(const char *fcont, const char *target);
static struct instruction *parse_line(char *line);
static struct instruction *get_instructions(char *func_data, size_t *nbytes);
static int is_call(char *op);
static int is_lea(char *op);
static int is_missing_bytes(struct instruction *inst);
static void append_inst_bytes(struct instruction *dst, struct instruction *src);
static int is_dynamic(struct instruction *inst);
static void sig_add_wildcard(struct signature *sig);
static void sig_add_byte(struct signature *sig, char *byte);
static void process_instruction(struct instruction *inst, struct signature *sig);
static struct signature make_signature(char *func_data, int max_len, int offset);
static void display_sig(struct signature *sig);
static struct user_input get_user_input(int argc, char **argv);
int main(int argc, char **argv)
{
struct user_input uin = get_user_input(argc, argv);
char *fcont = get_file_content(uin.file_path);
char *func_data = get_func_data(fcont, uin.target);
free(fcont);
struct signature sig = make_signature(func_data, uin.max_sig_len, uin.offset);
display_sig(&sig);
free(func_data);
free(sig.bytes);
return 0;
}
static struct user_input get_user_input(int argc, char **argv)
{
struct user_input uin = {NULL, NULL, -1, 0};
if(argc <= 2)
goto usage_err;
uin.file_path = argv[1];
uin.target = argv[2];
int opt;
while((opt = getopt(argc, argv, "l:o:h")) != -1) {
switch(opt) {
case 'l':
uin.max_sig_len = atoi(optarg);
break;
case 'o':
uin.offset = atoi(optarg);
break;
case 'h':
goto usage_err;
break;
default:
goto usage_err;
}
}
return uin;
usage_err:
fprintf(stderr, "Usage: \n\t%s [objdump file] [function name]\n"
"\nOptions:\n"
"\t[-l max signature length]\n"
"\t[-o offset into the signature (in number of instructions)]\n", argv[0]);
exit(EXIT_FAILURE);
}
static void pretty_print_sig(const char *bytes)
{
size_t ws_count = 1;
printf("\t\"");
for(size_t i = 0; i < strlen(bytes); i++) {
printf("%c", bytes[i]);
if(bytes[i] == ' ' && ws_count++ % 18 == 0)
printf("\"\n\t\"");
}
printf("\";");
}
static void display_sig(struct signature *sig)
{
printf("[#] SIGNATURE: \n");
pretty_print_sig(sig->bytes);
printf("\n");
printf("[#] LEN: %zu\n", sig->len);
printf("[#] OFFSET: %zu\n", sig->offset);
}
static int is_call(char *op)
{
if(strcmp(op, "call") == 0)
return 1;
return 0;
}
static int is_lea(char *op)
{
if(strcmp(op, "lea") == 0)
return 1;
return 0;
}
static struct instruction *get_invalid_inst()
{
struct instruction *inst = malloc(sizeof(*inst));
if(inst == NULL)
return NULL;
strncpy(inst->mnemonic, "None", ARR_SIZE(inst->mnemonic));
strncpy(inst->op1, "None", ARR_SIZE(inst->op1));
strncpy(inst->op2, "None", ARR_SIZE(inst->op2));
inst->bytes[0] = '\0';
inst->next = NULL;
return inst;
}
static void trim_wspace(char *bytes)
{
size_t len = strlen(bytes);
int i = len;
while(i >= 0 && isalnum(bytes[i]) == 0) {
if(isspace(bytes[i]) || isblank(bytes[i]))
bytes[i] = '\0';
i -= 1;
}
}
/* not my proudest work */
static struct instruction *parse_line(char *line)
{
struct instruction *inst = get_invalid_inst();
assert(inst != NULL);
/* throw away */
char *tok = strtok(line, "\t");
if(tok == NULL)
return inst;
char *bytes = strtok(NULL, "\t");
if(bytes == NULL)
return inst;
trim_wspace(bytes);
assert(bytes != NULL && strlen(bytes) <= ARR_SIZE(inst->bytes));
strcpy(inst->bytes, bytes);
char *mnemonic = strtok(NULL, " ");
/* just bytes of the previous line */
if(mnemonic == NULL)
return inst;
assert(strlen(mnemonic) <= ARR_SIZE(inst->mnemonic));
strcpy(inst->mnemonic, mnemonic);
char *op1 = strtok(NULL, ",");
/* no first operand ? */
if(op1 == NULL)
return inst;
assert(strlen(op1) <= ARR_SIZE(inst->op1));
strcpy(inst->op1, op1);
char *op2 = strtok(NULL, " \n");
/* no second operand ? */
if(op2 == NULL)
return inst;
assert(strlen(op2) <= ARR_SIZE(inst->op2));
strcpy(inst->op2, op2);
return inst;
}
static int is_missing_bytes(struct instruction *inst)
{
if(strcmp(inst->mnemonic, "None") == 0)
return 1;
return 0;
}
static void append_inst_bytes(struct instruction *dst, struct instruction *src)
{
size_t dst_blen = strlen(dst->bytes);
size_t src_blen = strlen(src->bytes);
size_t size = ARR_SIZE(dst->bytes);
/* + 2 for one whitespace and nullterminator. */
assert((dst_blen + src_blen + 2) < size);
dst->bytes[dst_blen] = ' ';
for(size_t i = 0; i < src_blen + 1; i++) {
dst->bytes[dst_blen + 1 + i] = src->bytes[i];
}
}
/* not my proudest work either. Who cares. */
static struct instruction *get_instructions(char *func_data, size_t *nbytes)
{
struct instruction *cur = NULL;
struct instruction *head = NULL;
/* consume function name */
char *line = strtok(func_data, "\n");
assert(line != NULL);
/* get actual first line */
line = strtok(NULL, "\n");
while(line != NULL) {
/* "process_instruction()" calls strtok aswell, so we save where we
* need to continue with strtok to get the next line. */
char *cont = (char *)((uintptr_t)line + 1 + (uintptr_t)strlen(line));
struct instruction *tmp = parse_line(line);
/* track the number of bytes the entire function is made of */
*nbytes += strlen(tmp->bytes) + 1;
/* for large instructions, some bytes will be written on the
* next line. Append the missing bytes if that is the case. */
if(is_missing_bytes(tmp) == 1) {
assert(head != NULL && cur != NULL);
append_inst_bytes(cur, tmp);
free(tmp);
}
/* if not, then we have a new instrcution. */
else {
if(head == NULL) {
head = tmp;
cur = tmp;
}
else {
cur->next = tmp;
cur = cur->next;
}
}
line = strtok(cont, "\n");
}
return head;
}
static int is_dynamic(struct instruction *inst)
{
if((is_call(inst->mnemonic) == 1) ||
(is_lea(inst->mnemonic) == 1)) {
return 1;
}
return 0;
}
static void sig_add_wildcard(struct signature *sig)
{
sig->bytes[sig->idx++] = '?';
sig->bytes[sig->idx++] = '?';
sig->bytes[sig->idx++] = ' ';
}
static void sig_add_byte(struct signature *sig, char *byte)
{
assert(strlen(byte) == 2);
sig->bytes[sig->idx++] = byte[0];
sig->bytes[sig->idx++] = byte[1];
sig->bytes[sig->idx++] = ' ';
}
static void process_instruction(struct instruction *inst, struct signature *sig)
{
/* wildcards:
- lea with [rip + 0x0]
- call with 00 00 00 00
*/
char buf[INST_SIZE];
assert(sig->idx < sig->size);
size_t len = strlen(inst->bytes);
assert(sig->idx + len < sig->size);
strncpy(buf, inst->bytes, ARR_SIZE(buf));
char *byte = strtok(buf, " ");
while(byte != NULL) {
if(strlen(byte) != 2)
return;
if(is_dynamic(inst) == 1) {
if(byte[0] == '0' && byte[1] == '0') {
sig_add_wildcard(sig);
}
else {
sig_add_byte(sig, byte);
}
}
else {
sig_add_byte(sig, byte);
}
sig->len += 1;
byte = strtok(NULL, " ");
}
}
static struct signature make_signature(char *func_data, int max_len, int offset)
{
size_t nbytes = 0;
struct instruction *inst = get_instructions(func_data, &nbytes);
struct signature sig = {0};
sig.bytes = malloc(sizeof(*sig.bytes) * nbytes);
assert(sig.bytes != NULL);
sig.size = nbytes;
size_t prv_len = 0;
/* throw the first <offset> number of instructions away */
for(int i = 0; i < offset; i++) {
if(inst == NULL)
break;
sig.offset += ((strlen(inst->bytes) + 1)/ 3);
struct instruction *tmp = inst;
inst = inst->next;
free(tmp);
}
int keep_processing = 1;
struct instruction *crawler = inst;
while(crawler != NULL) {
if(keep_processing == 1)
process_instruction(crawler, &sig);
/* if max_len is -1 (or just < 0), then there is no limit on the
* signature length. */
if(max_len >= 0 && sig.len >= (size_t)max_len) {
keep_processing = 0;
sig.len = prv_len;
}
prv_len = sig.len;
struct instruction *tmp = crawler;
crawler = crawler->next;
free(tmp);
}
/* overwrite last whitespace with null terminator. */
sig.bytes[sig.idx-1] = '\0';
return sig;
}
static char *get_func_data(const char *fcont, const char *func_name)
{
char buf[255];
size_t buf_size = ARR_SIZE(buf);
int len = snprintf(buf, buf_size, "<%s>:", func_name);
assert(len > 0 && len < (int)buf_size);
/* find starting location of "<func_name>:" in the file. */
char *sloc = get_location(fcont, buf);
/* find end location of "<func_name>:".I assume that a function will
* follow the x86_64 calling convention and end with a "ret" instruction.
* */
char *eloc = strstr(sloc, "ret");
assert(eloc != NULL);
eloc += strlen("ret");
size_t nbytes = (size_t)((uintptr_t)eloc - (uintptr_t)sloc);
/* + 1 for nullterminator. */
char *func_data = malloc((sizeof(*func_data) * nbytes) + 1);
assert(func_data != NULL);
memcpy(func_data, sloc, nbytes);
func_data[nbytes] = '\0';
return func_data;
}
static char *get_location(const char *haystack, const char *needle)
{
/* search for "<func_name>" in file. There should only be one
* occourance. */
char *loc = strstr(haystack, needle);
if(loc == NULL) {
fprintf(stderr, "Error: Function name not found\n");
exit(EXIT_FAILURE);
}
/* search for the needle ("<func_name>") again to make sure that there
* are no duplicates. */
char *tmp = strstr(loc + 1, needle);
if(tmp != NULL) {
fprintf(stderr, "Error: Function name is not unique\n");
exit(EXIT_FAILURE);
}
return loc;
}
/* returns a null terminated string of the file content. */
static char *get_file_content(const char *path)
{
FILE *f = fopen(path, "r");
assert(f != NULL);
long fsize = get_file_size(f);
assert(fsize != -1);
char *fcont = NULL;
/* + 1 for the null terminator. */
size_t fcont_size = (sizeof(*fcont) * fsize) + 1;
fcont = malloc(fcont_size);
assert(fcont != NULL);
size_t ret = fread(fcont, sizeof(*fcont), fsize, f);
assert(ret > 0 && ret == (size_t)fsize);
fcont[ret] = '\0';
fclose(f);
return fcont;
}
static long get_file_size(FILE *f)
{
int ret = fseek(f, 0, SEEK_END);
assert(ret == 0);
long fsize = ftell(f);
assert(fsize != -1);
rewind(f);
return fsize;
}