-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-shellcode.c
48 lines (41 loc) · 995 Bytes
/
run-shellcode.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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#if __SIZEOF_LONG__ != 8
#error Build this as a 64-bit binary: use gcc -m64 ...
#endif
int
main(int ac, char **av)
{
if (ac != 2) {
printf("Usage: %s shellcode.bin\n", av[0]);
exit(-1);
}
void *buf;
int fd = open(av[1], O_RDONLY);
if (fd < 0)
perror("open");
struct stat st;
if (fstat(fd, &st) < 0)
perror("fstat");
buf = memalign(4096, st.st_size);
if (!buf)
perror("malloc");
ssize_t cc = read(fd, buf, st.st_size);
printf("st.st_size: %d\n", (int)st.st_size);
printf("cc size: %d\n", (int)cc);
if (cc < 0)
perror("read");
if (cc != st.st_size)
printf("incomplete read: %ld %ld\n", cc, st.st_size);
close(fd);
if (mprotect(buf, st.st_size, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
perror("mprotect");
void (*f)(void) __attribute__((noreturn)) = buf;
f();
}