-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.c
138 lines (128 loc) · 3.32 KB
/
main.c
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
#include "rop.h"
void usage(void)
{
printf("Usage:\n");
printf(" ropchain <file> [-p <bool>] [-o <offset>] [-b <badbyte>] [-l <length>] [-v <bool>]\n");
printf(" -p\tPrint all gadgets.\n");
printf(" -o\tAdd padding(offset) bytes to payload.\n");
printf(" -b\tBypass badbyte gadgets. ex: \"00|20|0a\"\n");
printf(" -l\tMaximum gadgets length\"\n");
printf(" -v\tVerbose gadgets messages\"\n");
exit (-1);
}
int parse_arg(int argc, char** argv, struct Arg *arg);
int main(int argc, char** argv)
{
FILE *fp;
unsigned char *binary;
unsigned long binary_len;
unsigned char *chain;
int chain_len = 0;
int i;
struct Arg *arg;
if(argc < 2)
{
usage();
}
fp = fopen(argv[1], "rb");
if(!fp)
{
fprintf(stderr ,"Fail to open file.\n");
return -1;
}
//Get file length
fseek(fp, 0, SEEK_END);
binary_len=ftell(fp);
fseek(fp, 0, SEEK_SET);
//Allocate memory
binary = (unsigned char *)malloc(binary_len+1);
if(!binary)
{
fprintf(stderr ,"malloc failed.\n");
return -1;
}
if(fread(binary, binary_len, 1, fp) == 0)
{
return -1;
}
argv++;
argc--;
//Arg init
arg = (struct Arg *)malloc(sizeof(struct Arg));
if(!arg)
{
fprintf(stderr ,"malloc failed.\n");
return -1;
}
parse_arg(argc, argv, arg);
chain_len = rop_chain(&chain, binary, arg);
if(chain_len > 0)
{
printf("\n--- Result ---\n");
for(i = 0; i < arg->offset; i++)
printf("\\x41");
for(i = 0; i < chain_len; i++)
printf("\\x%02x",chain[i]);
printf("\n");
}
free(binary);
free(arg);
fclose(fp);
return 0;
}
int parse_arg(int argc, char** argv, struct Arg *arg)
{
char *endptr;
char *pch;
arg->print = 1;
arg->verbose = 0;
arg->depth = 10;
arg->offset = 0;
arg->badbyte_no = 0;
//Parse command
while ((argc > 1) && (argv[1][0] == '-'))
{
if(argv[2])
{
switch (argv[1][1])
{
case 'p':
if(!strcmp(argv[2], "false") || !strcmp(argv[2], "0"))
{
arg->print = 0;
}
break;
case 'v':
if(!strcmp(argv[2], "True") || !strcmp(argv[2], "1"))
{
arg->verbose = 1;
}
break;
case 'o':
arg->offset = strtol(argv[2], &endptr, 10);
break;
case 'l':
arg->depth = strtol(argv[2], &endptr, 10);
break;
case 'b':
pch = strtok(argv[2], "|");
while(pch != NULL)
{
arg->badbyte[arg->badbyte_no] = (unsigned char)strtol(pch, NULL, 16);
arg->badbyte_no++;
pch = strtok(NULL, "|");
}
break;
default:
usage();
}
argv += 2;
argc -= 2;
}
else
{
fprintf(stderr, "Parser args failed.[%s]\n", argv[1]);
return -1;
}
}
}