-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsc-test.c
90 lines (81 loc) · 2.06 KB
/
sc-test.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
static void
flush_stream(FILE *strm) {
while (getc(strm) != EOF) { }
}
static void
croak(const char *msg) {
fprintf(stderr, "%s\n", msg);
flush_stream(stderr);
}
static void
usage(const char *program) {
fprintf(stderr, "\nExecute code : %s -e <file-containing-shellcode>\n", program);
fprintf(stderr, "Convert code : %s -p <file-containing-shellcode>\n\n", program);
flush_stream(stderr);
exit(EXIT_FAILURE);
}
static void
barf(const char *msg) {
if (errno)
perror(msg);
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv) {
FILE *fp;
unsigned char code[BUFSIZ];
size_t i;
int arg, l, m = 15 /* max # of bytes to print on one line */;
struct stat sbuf;
size_t flen;
void (*fptr)(void);
memset(&sbuf, '\0', sizeof(struct stat));
if (argc < 3) usage(argv[0]);
if (stat(argv[2], &sbuf)) {
fprintf(stderr, "file: %s\n", argv[2]);
barf("failed to stat");
}
errno = 0;
if (!S_ISREG(sbuf.st_mode)) barf(NULL);
flen = (long) sbuf.st_size;
if (flen > BUFSIZ) barf("file to big");
if (!(fp = fopen(argv[2], "rb"))) barf("failed to open file");
if (fread(code, 1, flen, fp) != flen) barf("failed to slurp file");
if (fclose(fp)) barf("failed to close file");
while ((arg = getopt(argc, argv, "e:p:")) != -1) {
switch (arg) {
case 'e':
croak("Calling code ...");
fptr = (void (*)(void)) code;
(*fptr)();
break;
case 'p':
printf("\n/* The following shellcode is %zu bytes long */\n", flen);
printf("\nchar shellcode[] =\n");
l = m;
for (i = 0; i < flen; i++) {
if (l >= m) {
if (i) printf("\"\n");
printf("\t\"");
l = 0;
}
++l;
printf("\\x%02x", ((unsigned char *)code)[i]);
}
printf("\";\n\n\n");
break;
default:
usage(argv[0]);
break;
}
}
return (EXIT_SUCCESS);
}