Last active
January 14, 2023 08:46
-
-
Save brant-ruan/3e2280e6b5d28c45d3cf45abb069a3a3 to your computer and use it in GitHub Desktop.
Pawnyable LK06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/bpf.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#include <sys/syscall.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
// https://pawnyable.cafe/linux-kernel/LK06/distfiles/bpf_insn.h | |
#include "bpf_insn.h" | |
void fatal(const char *msg) { | |
perror(msg); | |
exit(1); | |
} | |
int bpf(int cmd, union bpf_attr *attrs) { | |
return syscall(__NR_bpf, cmd, attrs, sizeof(*attrs)); | |
} | |
int main() { | |
char verifier_log[0x10000]; | |
// prepare BPF program | |
struct bpf_insn insns[] = { | |
BPF_MOV64_IMM(BPF_REG_0, 4), | |
BPF_EXIT_INSN(), | |
}; | |
// set usage | |
union bpf_attr prog_attr = { | |
.prog_type = BPF_PROG_TYPE_SOCKET_FILTER, | |
.insn_cnt = sizeof(insns) / sizeof(insns[0]), | |
.insns = (uint64_t)insns, | |
.license = (uint64_t) "GPL v2", | |
.log_level = 2, | |
.log_size = sizeof(verifier_log), | |
.log_buf = (uint64_t)verifier_log}; | |
// load BPF program | |
int progfd = bpf(BPF_PROG_LOAD, &prog_attr); | |
if (progfd == -1) | |
fatal("bpf(BPF_PROG_LOAD)"); | |
// create socket | |
int socks[2]; | |
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, socks)) | |
fatal("socketpair"); | |
if (setsockopt(socks[0], SOL_SOCKET, SO_ATTACH_BPF, &progfd, sizeof(int))) | |
fatal("setsockopt"); | |
// use socket | |
write(socks[1], "Hello", 5); | |
char buf[0x10] = {}; | |
read(socks[0], buf, 0x10); | |
printf("Received: %s\n", buf); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment