forked from yuyang-huang-90/mit-6.858-2014-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-shellcode.c
48 lines (39 loc) · 913 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__ != 4
#error Build this as a 32-bit binary: use gcc -m32 ...
#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);
if (cc < 0)
perror("read");
if (cc != st.st_size)
printf("incomplete read: %d %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();
}