-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellcode-generator.c
110 lines (103 loc) · 1.99 KB
/
shellcode-generator.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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#define MAX_LINE 128
void usage(char *progname, int exit_status)
{
printf("Usage: %s [options]\n", progname);
puts("-f <in_file> Use in_file as an input instead of stdin");
puts("-o <out_file> Redirect output to out_file");
puts("-h Prints this help and exits");
exit(exit_status);
}
void get_opcode(char *buf)
{
if (buf[0] != ' ') {
buf[0] = '\0';
return;
}
int i = 1, j = 0;
while (i < MAX_LINE && buf[i] != '\0' && buf[i] != '\t')
i++;
if (buf[i] == '\0') {
buf[0] = '\0';
return;
}
i++;
while (i+1 < MAX_LINE && !(isspace(buf[i]) && isspace(buf[i+1]))) {
if (isspace(buf[i])) {
i++;
} else {
buf[j] = buf[i];
j++;
i++;
}
}
buf[j] = '\0';
}
void print_output(FILE *in_file, FILE *out_file)
{
char buf[MAX_LINE];
char *s;
int i;
while (1) {
s = fgets(buf, MAX_LINE, in_file);
if (s != buf) {
if (ferror(in_file)) {
puts("Error occured while reading input\n");
fclose(in_file);
fclose(out_file);
exit(EXIT_FAILURE);
} else {
break;
}
}
get_opcode(buf);
i = 0;
while (i+1 < MAX_LINE && buf[i] != '\0') {
fprintf(out_file, "\\x%c%c", buf[i], buf[i+1]);
i += 2;
}
}
}
int main(int argc, char *argv[])
{
char opt;
char *in_path = NULL;
char *out_path = NULL;
FILE *in_file;
FILE *out_file;
opterr = 0;
while ((opt = getopt(argc, argv, "hf:o:")) != -1) {
switch (opt) {
case 'f' :
in_path = optarg;
break;
case 'o' :
out_path= optarg;
break;
case 'h' :
usage(argv[0], EXIT_SUCCESS);
break;
default :
usage(argv[0], EXIT_FAILURE);
}
}
if (argv[optind])
usage(argv[0], EXIT_FAILURE);
if ((in_file = fopen(in_path, "r")) == NULL)
in_file = stdin;
if ((out_file = fopen(out_path, "w")) == NULL) {
if (!out_path) {
out_file = stdout;
} else {
puts("Couldn't open file for write\n");
exit(EXIT_FAILURE);
}
}
print_output(in_file, out_file);
fclose(in_file);
fclose(out_file);
return EXIT_SUCCESS;
}