檢測php網站是否已經被攻破的方法

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   strong   檔案   

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 *(" --colorfind . -type f -name ‘*.php‘ | xargs grep -l "gzinflate *(" --colorfind . -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編碼

也可以使用Regex來搜尋檔案,尋找可以代碼:

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()Regex由修飾符"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檔案

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_filefind . -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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.