北京SEO

Linux服务器 php木马排查及增强安全措施

2019/10/10/17:46:04  阅读:1734  来源:谷歌SEO算法  标签: 搜索引擎

本文我们来介绍一下用php排查linux服务器木马及加增安全防护,开源的程序如discuz x漏洞比较明显,很容易上传websehll,要把漏洞补到最小才能安全使用.

网站频繁被挂马?做一些改进,基本上能把这个问题解决,因为discuz x等程序存在漏洞,被上传了websehll,每次被删除过段时间又出来了,最终查到所有的木马.

从以下几个方面查找并加强,如果能不开启会员功能,不给任何上传入口,保护好后台密码,加固好PHP,一般就没什么问题了.

1.根据特征码查找,php木马一般含有如下代码:

<?php eval($_POST[cmd]);?>或者:

  1. <?phpassert($_POST[cmd]);?>
  2. find/wwwroot/*-typef-name"*.php"|xargsgrep"eval(">/wwwroot/scan.txt

结果就查出很多明显的webshell,并且发现都藏在attachment等目录下.

2.利用网上的一个php代码,搜索最近被修改的文件 scandir.php 代码如下:

  1. <?php
  2. set_time_limit(0);//防止超时
  3. /**
  4. *
  5. *php目录扫描监控增强版
  6. *
  7. *@authorlssbing(lssbing#gmail.com)
  8. *@date2010-1-18
  9. *@licenseBSD
  10. *@version1.0
  11. *
  12. 下面几个变量使用前需要手动设置
  13. *
  14. **/
  15. /*=====================程序配置=====================*/
  16. $pass="12345";//设置密码
  17. $jkdir=".";//设置监控扫描的目录,当前目录为'.',上一级目录为'..',也可以设置绝对路径,后面不要加斜杠,默认为当前目录
  18. $logfilename="./m.log";//设置存储log的路径,可以放置在任意位置
  19. $exclude=array('data','images');//排除目录
  20. $danger='eval|cmd|passthru|gzuncompress';//设置要查找的危险的函数以确定是否木马文件
  21. $suffix='php|inc';//设置要扫描文件的后缀
  22. /*=====================配置结束=====================*/
  23. $filename=$_GET['filename'];
  24. $check=$_GET['check'];
  25. $jumpoff=false;
  26. $url=$_SERVER['PHP_SELF'];
  27. $thisfile=end(explode('/',$url));
  28. $jump="{$thisfile}|".implode('|',$exclude);
  29. $jkdir_num=$file_num=$danger_num=0;
  30. define('M_PATH',$jkdir);
  31. define('M_LOG',$logfilename);
  32. if($check=='check')
  33. {
  34. $safearr=explode("|",$jump);
  35. $start_time=microtime(true);
  36. safe_check($jkdir);
  37. $end_time=microtime(true);
  38. $total=$end_time-$start_time;
  39. $file_num=$file_num-$jkdir_num;
  40. $message="文件数:".$file_num;
  41. $message.="文件夹数:".$jkdir_num;
  42. $message.="可疑文件数:".$danger_num;
  43. $message.="执行时间:".$total;
  44. echo$message;
  45. }else{
  46. if($_GET['m']=="del")Delete();//处理文件删除
  47. //读取文件内容
  48. if(isset($_GET['readfile'])){
  49. //输出查看密码,密码校验正确以后输出文件内容
  50. if(emptyempty($_POST['passchack'])){
  51. echo"<formid="form1"name="form1"method="post">"
  52. ."<label>pass"
  53. ."<inputtype="text"name="passchack"/>"
  54. ."</label>"
  55. ."<inputtype="submit"name="Submit"value="提交"/>"
  56. ."</form>"
  57. ."";
  58. exit;
  59. }elseif(isset($_POST['passchack'])&&$_POST['passchack']==$pass){
  60. $code=file_get_contents($_GET['readfile']);
  61. echo"<textareaname="code"cols="150"rows="30"id="code"style='width:100%;height:450px;background:#cccccc;'>{$code}</textarea>";
  62. exit;
  63. }else{
  64. exit;
  65. }
  66. }else{
  67. record_md5(M_PATH);
  68. if(file_exists(M_LOG)){
  69. $log=unserialize(file_get_contents(M_LOG));
  70. }else{
  71. $log=array();
  72. }
  73. if($_GET['savethis']==1){
  74. //保存当前文件md5到日志文件
  75. @unlink(M_LOG);
  76. file_put_contents(M_LOG,serialize($file_list));
  77. echo"<ahref='scandir.php'>保存成功!点击返回</a>";
  78. exit;
  79. }
  80. if(emptyempty($log)){
  81. echo"当前还没有创建日志文件!点击[保存当前]创建日志文件!";
  82. }else{
  83. if($file_list==$log){
  84. echo"本文件夹没有做过任何改动!";
  85. }else{
  86. if(count($file_list)>0){
  87. foreach($file_listas$file=>$md5){
  88. if(!isset($log[$file])){
  89. echo"新增文件:<ahref={$file}target='_blank'>".$file."</a>"."创建时间:".date("Y-m-dH:i:s",filectime($file))."修改时间:".date("Y-m-dH:i:s",filemtime($file))."<ahref=?readfile={$file}target='_blank'>源码</a><ahref='?m=del&filename={$file}'target='_blank'>删除</u></a><br/>";
  90. }else{
  91. if($log[$file]!=$md5){
  92. echo"修改文件:<ahref={$file}target='_blank'>".$file."</a>"."创建时间:".date("Y-m-dH:i:s",filectime($file))."修改时间:".date("Y-m-dH:i:s",filemtime($file))."<ahref=?readfile={$file}target='_blank'>源码</a><br/>";
  93. unset($log[$file]);
  94. }else{
  95. unset($log[$file]);
  96. }
  97. }
  98. }
  99. }
  100. if(count($log)>0){
  101. foreach($logas$file=>$md5){
  102. echo"删除文件:<ahref={$file}target='_blank'>".$file."</a><br/>";
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. //计算md5
  110. functionrecord_md5($jkdir){
  111. global$file_list,$exclude;
  112. if(is_dir($jkdir)){
  113. $file=scandir($jkdir);
  114. foreach($fileas$f){
  115. if($f!='.'&&$f!='..'&&!in_array($f,$exclude)){
  116. $path=$jkdir.'/'.$f;
  117. if(is_dir($path)){
  118. record_md5($path);
  119. }else{
  120. $file_list[$path]=md5_file($path);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. functionSafe_Check($jkdir)//遍历文件
  127. {
  128. global$danger,$suffix,$jkdir_num,$file_num,$danger_num;
  129. $hand=@dir($jkdir)ordie('文件夹不存在');
  130. while($file=$hand->read())
  131. {
  132. $filename=$jkdir.'/'.$file;
  133. if(!$jumpoff){
  134. if(Jump($filename))continue;
  135. }
  136. if(@is_dir($filename)&&$file!='.'&&$file!='..'&&$file!='./..')
  137. {$jkdir_num++;
  138. Safe_Check($filename);
  139. }
  140. if(preg_match_all("/.($suffix)/i",$filename,$out))
  141. {
  142. $str='';
  143. $fp=@fopen($filename,'r')ordie('没有权限');
  144. while(!feof($fp))
  145. {
  146. $str.=fgets($fp,1024);
  147. }
  148. fclose($fp);
  149. if(preg_match_all("/($danger)[rnt]{0,}([[(])/i",$str,$out))
  150. {
  151. echo"<fontcolor='green'style='font-size:14px'>可疑文件:{$filename}</font>"."创建时间:".date("Y-m-dH:i:s",filectime($filename))."修改时间:".date("Y-m-dH:i:s",filemtime($filename))."<ahref='?readfile={$filename}'target='_blank'><u>查看代码</u></a><ahref='?m=del&filename=$filename'target='_blank'>删除</u></a><br>";
  152. $danger_num++;
  153. }
  154. }
  155. $file_num++;
  156. }
  157. }
  158. functionEdit()//查看可疑文件
  159. {
  160. global$filename;
  161. $filename=str_replace("..","",$filename);
  162. $file=$filename;
  163. $content="";
  164. if(is_file($file))
  165. {
  166. $fp=fopen($file,"r")ordie('没有权限');
  167. $content=fread($fp,filesize($file));
  168. fclose($fp);
  169. $content=htmlspecialchars($content);
  170. }
  171. echo"<textareaname='str'style='width:100%;height:450px;background:#cccccc;'>$content</textarea>rn";
  172. exit();
  173. }
  174. functionDelete()//删除文件
  175. {global$filename,$pass;
  176. if(emptyempty($_POST['passchack'])){
  177. echo"<formid="form1"name="form1"method="post">"
  178. ."<label>pass"
  179. ."<inputtype="text"name="passchack"/>"
  180. ."</label>"
  181. ."<inputtype="submit"name="Submit"value="提交"/>"
  182. ."</form>"//phpfensi.com
  183. ."";
  184. exit;
  185. }elseif(isset($_POST['passchack'])&&$_POST['passchack']==$pass){
  186. (is_file($filename))?($mes=unlink($filename)?'删除成功':'删除失败查看权限'):'';
  187. echo$mes;
  188. exit();
  189. }else{
  190. echo'密码错误!';
  191. exit;
  192. }
  193. }
  194. functionJump($file)//跳过文件
  195. {
  196. global$jump,$safearr;
  197. if($jump!='')
  198. {
  199. foreach($safearras$v)
  200. {
  201. if($v=='')continue;
  202. if(eregi($v,$file))returntrue;
  203. }
  204. }
  205. returnfalse;
  206. }
  207. ?>
  208. <a href="scandir.php">[查看文件改动]</a>|<a href="scandir.php?savethis=1">[保存当前文件指纹]</a>|<a href="scandir.php?check=check">[扫描可疑文件]</a>

执行后能看到最近被修改的文件,具有参加价值.

3.修改php.ini,限制以下函数:

  1. disable_functions=phpinfo,passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocke,popen,proc_close,curl_exec,curl_multi_exec,parse_ini_file,show_source,dl,escapeshellarg,escapeshellcmd

4.修改nginx.conf,限制一些目录执行php文件,代码如下:

  1. server
  2. {
  3. listen80;
  4. server_namewww.***.com;
  5. indexindex.htmindex.htmlindex.php;
  6. root/wwwroot/;
  7. rewrite^([^.]*)/topic-(.+).html$$1/portal.php?mod=topic&topic=$2last;
  8. rewrite^([^.]*)/article-([0-9]+)-([0-9]+).html$$1/portal.php?mod=view&aid=$2&page=$3last;
  9. rewrite^([^.]*)/forum-(w+)-([0-9]+).html$$1/forum.php?mod=forumdisplay&fid=$2&page=$3last;
  10. rewrite^([^.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$$1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3last;
  11. rewrite^([^.]*)/group-([0-9]+)-([0-9]+).html$$1/forum.php?mod=group&fid=$2&page=$3last;
  12. rewrite^([^.]*)/space-(username|uid)-(.+).html$$1/home.php?mod=space&$2=$3last;
  13. rewrite^([^.]*)/([a-z]+)-(.+).html$$1/$2.php?rewrite=$3last;
  14. rewrite^([^.]*)/topic-(.+).html$$1/portal.php?mod=topic&topic=$2last;
  15. location~^/images/.*.(php|php5)$
  16. {
  17. denyall;
  18. }
  19. location~^/static/.*.(php|php5)$
  20. {
  21. denyall;
  22. }
  23. location~*^/data/(attachment|avatar)/.*.(php|php5)$
  24. {
  25. denyall;
  26. }
  27. location~.*.(php|php5)?$
  28. {
  29. fastcgi_pass127.0.0.1:9000;
  30. fastcgi_indexindex.php;
  31. includefcgi.conf;
  32. }
  33. error_page400/404.html;
  34. error_page403/404.html;
  35. error_page404/404.html;
  36. error_page405/404.html;
  37. error_page408/404.html;
  38. error_page410/404.html;
  39. error_page411/404.html;
  40. error_page412/404.html;
  41. error_page413/404.html;
  42. error_page414/404.html;
  43. error_page415/404.html;
  44. error_page500/404.html;
  45. error_page501/404.html;
  46. error_page502/404.html;
  47. error_page503/404.html;
  48. error_page506/404.html;
  49. //phpfensi.com
  50. log_formatacclog"$remote_addr$request_time$http_x_readtime[$time_local]"$request_methodhttp://$host$request_uri"$status$body_bytes_sent"$http_referer""$http_user_agent"";
  51. access_log/logs/access.logacclog;
  52. }

此处需要注意的是如下代码:

  1. location~^/images/.*.(php|php5)$
  2. {
  3. denyall;
  4. }

这些目录的限制必须写在如下代码:

  1. location~.*.(php|php5)?$
  2. {
  3. fastcgi_pass127.0.0.1:9000;
  4. fastcgi_indexindex.php;
  5. includefcgi.conf;
  6. }

的前面,否则限制不生效.

广告内容

Linux服务器 php木马排查及增强安全措施 Linux服务器 php木马排查及增强安全措施 Linux服务器 php木马排查及增强安全措施

相关阅读

热门评论

SEO探索者团队 SEO探索者团队

SEO服务&网站优化

总篇数182

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法