-
Notifications
You must be signed in to change notification settings - Fork 90
/
shellcode_wrapper_linux.c
84 lines (67 loc) · 1.65 KB
/
shellcode_wrapper_linux.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
/**
* Q'n'd shellcode wrapper for Linux
*
* @_hugsy_
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define MAX_SC_SIZE (1<<16)
void *sc_base;
void* copy_from_file(char* filename)
{
ssize_t n = 0;
void *off;
int fd;
fd = open(filename, O_RDONLY);
if (fd<0){
perror("open");
return NULL;
}
sc_base = mmap(NULL, MAX_SC_SIZE, PROT_READ|PROT_EXEC|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS, 0, 0);
if (sc_base == (void*) -1){
perror("mmap");
return NULL;
}
memset(sc_base, 0x00, MAX_SC_SIZE);
off = sc_base;
do {
n = read(fd, off, 128);
if (n<0) {
perror("read");
close(fd);
munmap(sc_base, MAX_SC_SIZE);
return NULL;
}
off += n;
} while (n > 0);
printf("[+] %ld bytes copied to 0x%p\n", (off-sc_base), sc_base);
return sc_base;
}
void trigger(void)
{
int (*func)();
func = (int (*)()) sc_base;
(*func)();
munmap(sc_base, MAX_SC_SIZE);
return;
}
int main(int argc, char **argv)
{
if (argc != 2) {
printf("Missing filename\n");
return -1;
}
if (copy_from_file(argv[1])) {
trigger();
return EXIT_SUCCESS;
} else {
return EXIT_FAILURE;
}
}