当前位置:首页 > PHP教程 > php应用 > 列表

php网站被挂木马后的修复方法总结

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-24 14:18:55 浏览: 评论:0 

这篇文章主要介绍了php网站被挂木马后的修复方法,总结分析了Linux与Windows系统上木马的查杀方法,并给出了一个完整的木马查找工具实例,需要的朋友可以参考下。

本文实例总结了php网站被挂木马后的修复方法。分享给大家供大家参考。具体方法如下:

在linux中我们可以使用命令来搜查木马文件,到代码安装目录执行下面命令,代码如下:

find ./ -iname "*.php" | xargs grep -H -n "eval(base64_decode"

搜出来接近100条结果,这个结果列表很重要,木马都在里面,要一个一个文件打开验证是否是木马,如果是,马上删除掉

最后找到10个木马文件,存放在各种目录,都是php webshell,功能很齐全,用base64编码

如果你在windows中查找目录直接使用windows文件搜索就可以了,可以搜索eval或最近修改文件,然后如果是dedecms我们要查看最新dedecms漏洞呀然后修补。

下面给个php木马查找工具,直接放到你站点根目录,代码如下:

  1. <?php 
  2. /**************PHP Web木马扫描器************************/ 
  3. /* [+] 作者: alibaba */ 
  4. /* [+] MSN: weeming21@hotmail.com */ 
  5. /* [+] 首发: t00ls.net , 转载请注明t00ls */ 
  6. /* [+] 版本: v1.0 */ 
  7. /* [+] 功能: web版php木马扫描工具*/ 
  8. /* [+] 注意: 扫描出来的文件并不一定就是后门, */ 
  9. /* 请自行判断、审核、对比原文件。*/ 
  10. /* 如果你不确定扫出来的文件是否为后门,*/ 
  11. /* 欢迎你把该文件发给我进行分析。*/ 
  12. /*******************************************************/ 
  13. ob_start(); 
  14. set_time_limit(0); 
  15. $username = "t00ls"; //设置用户名 
  16. $password = "t00ls"; //设置密码 
  17. $md5 = md5(md5($username).md5($password)); 
  18. $version = "PHP Web木马扫描器v1.0"; 
  19.  
  20. PHP Web 木马扫描器 
  21. $realpath = realpath('./'); 
  22. $selfpath = $_SERVER['PHP_SELF']; 
  23. $selfpath = substr($selfpath, 0, strrpos($selfpath,'/')); 
  24. define('REALPATH', str_replace('//','/',str_replace('\','/',substr($realpath, 0, strlen($realpath) - strlen($selfpath))))); 
  25. define('MYFILE', basename(__FILE__)); 
  26. define('MYPATH', str_replace('\', '/', dirname(__FILE__)).'/'); 
  27. define('MYFULLPATH', str_replace('\', '/', (__FILE__))); 
  28. define('HOST', "http://".$_SERVER['HTTP_HOST']); 
  29. ?> 
  30. <html> 
  31. <head> 
  32. <title><?php echo $version?></title> 
  33. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  34. <style> 
  35. body{margin:0px;} 
  36. body,td{font: 12px Arial,Tahoma;line-height: 16px;} 
  37. a {color: #00f;text-decoration:underline;} 
  38. a:hover{color: #f00;text-decoration:none;} 
  39. .alt1 td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#f1f1f1;padding:5px 10px 5px 5px;} 
  40. .alt2 td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#f9f9f9;padding:5px 10px 5px 5px;} 
  41. .focus td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#ffffaa;padding:5px 10px 5px 5px;} 
  42. .head td{border-top:1px solid #fff;border-bottom:1px solid #ddd;background:#e9e9e9;padding:5px 10px 5px 5px;font-weight:bold;} 
  43. .head td span{font-weight:normal;} 
  44. </style> 
  45. </head> 
  46. <body> 
  47. <?php 
  48. if(!(isset($_COOKIE['t00ls']) && $_COOKIE['t00ls'] == $md5) && !(isset($_POST['username']) && isset($_POST['password']) && (md5(md5($_POST['username']).md5($_POST['password']))==$md5))) 
  49. { 
  50. echo '<form id="frmlogin" name="frmlogin" method="post" action="">用户名: <input type="text" name="username" id="username" /> 密码: <input type="password" name="password" id="password" /> <input type="submit" name="btnLogin" id="btnLogin" value="登陆" /></form>'; 
  51. } 
  52. elseif(isset($_POST['username']) && isset($_POST['password']) && (md5(md5($_POST['username']).md5($_POST['password']))==$md5)) 
  53. { 
  54. setcookie("t00ls", $md5, time()+60*60*24*365,"/"); 
  55. echo "登陆成功!"; 
  56. header( 'refresh: 1; url='.MYFILE.'?action=scan' ); 
  57. exit(); 
  58. } 
  59. else 
  60. { 
  61. setcookie("t00ls", $md5, time()+60*60*24*365,"/"); 
  62. $setting = getSetting(); 
  63. $action = isset($_GET['action'])?$_GET['action']:""; 
  64.  
  65. if($action=="logout") 
  66. { 
  67. setcookie ("t00ls", "", time() - 3600); 
  68. Header("Location: ".MYFILE); 
  69. exit(); 
  70. } 
  71. if($action=="download" && isset($_GET['file']) && trim($_GET['file'])!="") 
  72. { 
  73. $file = $_GET['file']; 
  74. ob_clean(); 
  75. if (@file_exists($file)) { 
  76. header("Content-type: application/octet-stream"); 
  77. header("Content-Disposition: filename="".basename($file)."""); 
  78. echo file_get_contents($file); 
  79. } 
  80. exit(); 
  81. } 
  82. ?> 
  83. <table border="0" cellpadding="0" cellspacing="0" width="100%"> 
  84. <tbody><tr class="head"> 
  85. <td><?php echo $_SERVER['SERVER_ADDR']?><span style="float: right; font-weight:bold;"><?php echo "<a href='http://www.t00ls.net/'>$version</a>"?></span></td> 
  86. </tr> 
  87. <tr class="alt1"> 
  88. <td><span style="float: right;"><?=date("Y-m-d H:i:s",mktime())?></span> 
  89. <a href="?action=scan">扫描</a> | 
  90. <a href="?action=setting">设定</a> | 
  91. <a href="?action=logout">登出</a> 
  92. </td> 
  93. </tr> 
  94. </tbody></table> 
  95. <br> 
  96. <?php 
  97. if($action=="setting") 
  98. { 
  99. if(isset($_POST['btnsetting'])) 
  100. { 
  101. $Ssetting = array(); 
  102. $Ssetting['user']=isset($_POST['checkuser'])?$_POST['checkuser']:"php | php? | phtml"; 
  103. $Ssetting['all']=isset($_POST['checkall'])&&$_POST['checkall']=="on"?1:0; 
  104. $Ssetting['hta']=isset($_POST['checkhta'])&&$_POST['checkhta']=="on"?1:0; 
  105. setcookie("t00ls_s", base64_encode(serialize($Ssetting)), time()+60*60*24*365,"/"); 
  106. echo "设置完成!"; 
  107. header( 'refresh: 1; url='.MYFILE.'?action=setting' ); 
  108. exit(); 
  109. } 
  110. ?> 
  111. <form name="frmSetting" method="post" action="?action=setting"> 
  112. <FIELDSET style="width:400px"> 
  113. <LEGEND>扫描设定</LEGEND> 
  114. <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
  115. <tr> 
  116. <td width="60">文件后缀:</td> 
  117. <td width="300"><input type="text" name="checkuser" id="checkuser" style="width:300px;" value="<?php echo $setting['user']?>"></td> 
  118. </tr> 
  119. <tr> 
  120. <td><label for="checkall">所有文件</label></td> 
  121. <td><input type="checkbox" name="checkall" id="checkall" <?php if($setting['all']==1) echo "checked"?>></td> 
  122. </tr> 
  123. <tr> 
  124. <td><label for="checkhta">设置文件</label></td> 
  125. <td><input type="checkbox" name="checkhta" id="checkhta" <?php if($setting['hta']==1) echo "checked"?>></td> 
  126. </tr> 
  127. <tr> 
  128. <td>&nbsp;</td> 
  129. <td> 
  130. <input type="submit" name="btnsetting" id="btnsetting" value="提交"> 
  131. </td> 
  132. </tr> 
  133. </table> 
  134. </fieldset> 
  135. </form> 
  136. <?php 
  137. } 
  138. else 
  139. { 
  140. $dir = isset($_POST['path'])?$_POST['path']:MYPATH; 
  141. $dir = substr($dir,-1)!="/"?$dir."/":$dir; 
  142. ?> 
  143. <form name="frmScan" method="post" action=""> 
  144. <table width="100%%" border="0" cellspacing="0" cellpadding="0"> 
  145. <tr> 
  146. <td width="35" style="vertical-align:middle; padding-left:5px;">扫描路径:</td> 
  147. <td width="690"> 
  148. <input type="text" name="path" id="path" style="width:600px" value="<?php echo $dir?>"> 
  149. &nbsp;&nbsp;<input type="submit" name="btnScan" id="btnScan" value="开始扫描"></td> 
  150. </tr> 
  151. </table(www.jb51.net)> 
  152. </form> 
  153. <?php 
  154. if(isset($_POST['btnScan'])) 
  155. { 
  156. $start=mktime(); 
  157. $is_user = array(); 
  158. $is_ext = ""; 
  159. $list = ""; 
  160.  
  161. if(trim($setting['user'])!="") 
  162. { 
  163. $is_user = explode("|",$setting['user']); 
  164. if(count($is_user)>0) 
  165. { 
  166. foreach($is_user as $key=>$value) 
  167. $is_user[$key]=trim(str_replace("?","(.)",$value)); 
  168. $is_ext = "(.".implode("($|.))|(.",$is_user)."($|.))"; 
  169. } 
  170. } 
  171. if($setting['hta']==1) 
  172. { 
  173. $is_hta=1; 
  174. $is_ext = strlen($is_ext)>0?$is_ext."|":$is_ext; 
  175. $is_ext.="(^.htaccess$)"; 
  176. } 
  177. if($setting['all']==1 || (strlen($is_ext)==0 && $setting['hta']==0)) 
  178. { 
  179. $is_ext="(.+)"; 
  180. } 
  181.  
  182. $php_code = getCode(); 
  183. if(!is_readable($dir)) 
  184. $dir = MYPATH; 
  185. $count=$scanned=0; 
  186. scan($dir,$is_ext); 
  187. $end=mktime(); 
  188. $spent = ($end - $start); 
  189. ?> 
  190. <div style="padding:10px; background-color:#ccc">扫描: <?php echo $scanned?> 文件| 发现: <?php echo $count?> 可疑文件| 耗时: <?php echo $spent?> 秒</div> 
  191. <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
  192. <tr class="head"> 
  193. <td width="15" align="center">No.</td> 
  194. <td width="48%">文件</td> 
  195. <td width="12%">更新时间</td> 
  196. <td width="10%">原因</td> 
  197. <td width="20%">特征</td> 
  198. <td>动作</td> 
  199. </tr> 
  200. <?php echo $list?> 
  201. </table> 
  202. <?php 
  203. } 
  204. } 
  205. } 
  206. ob_flush(); 
  207. ?> 
  208. </body> 
  209. </html> 
  210. <?php 
  211. function scan($path = '.',$is_ext){ 
  212. global $php_code,$count,$scanned,$list; 
  213. $ignore = array('.', '..' ); 
  214. $replace=array(" ","n","r","t"); 
  215. $dh = @opendir( $path ); 
  216. while(false!==($file=readdir($dh))){ 
  217. if( !in_array( $file, $ignore ) ){ 
  218. if( is_dir( "$path$file" ) ){ 
  219. scan("$path$file/",$is_ext); 
  220. } else { 
  221. $current = $path.$file; 
  222. if(MYFULLPATH==$current) continue; 
  223. if(!preg_match("/$is_ext/i",$file)) continue; 
  224. if(is_readable($current)) 
  225. { 
  226. $scanned++; 
  227. $content=file_get_contents($current); 
  228. $content= str_replace($replace,"",$content); 
  229. foreach($php_code as $key => $value) 
  230. { 
  231. if(preg_match("/$value/i",$content)) 
  232. { 
  233. $count++; 
  234. $j = $count % 2 + 1; 
  235. $filetime = date('Y-m-d H:i:s',filemtime($current)); 
  236. $reason = explode("->",$key); 
  237. $url = str_replace(REALPATH,HOST,$current); 
  238. preg_match("/$value/i",$content,$arr); 
  239. $list.=" 
  240. <tr class='alt$j' onmouseover='this.className="focus";' onmouseout='this.className="alt$j";'> 
  241. <td>$count</td> 
  242. <td><a href='$url' target='_blank'>$current</a></td> 
  243. <td>$filetime</td> 
  244. <td><font color=red>$reason[0]</font></td> 
  245. <td><font color=#090>$reason[1]</font></td> 
  246. <td><a href='?action=download&file=$current' target='_blank'>下载</a></td> 
  247. </tr>"; 
  248. //echo $key . "-" . $path . $file ."(" . $arr[0] . ")" ."<br />"; 
  249. //echo $path . $file ."<br />"; 
  250. break; 
  251. } 
  252. } 
  253. } 
  254. } 
  255. } 
  256. } 
  257. closedir( $dh ); 
  258. } 
  259. function getSetting() 
  260. { 
  261. $Ssetting = array(); 
  262. if(isset($_COOKIE['t00ls_s'])) 
  263. { 
  264. $Ssetting = unserialize(base64_decode($_COOKIE['t00ls_s'])); 
  265. $Ssetting['user']=isset($Ssetting['user'])?$Ssetting['user']:"php | php? | phtml | shtml"; 
  266. $Ssetting['all']=isset($Ssetting['all'])?intval($Ssetting['all']):0; 
  267. $Ssetting['hta']=isset($Ssetting['hta'])?intval($Ssetting['hta']):1; 
  268. } 
  269. else 
  270. { 
  271. $Ssetting['user']="php | php? | phtml | shtml"; 
  272. $Ssetting['all']=0; 
  273. $Ssetting['hta']=1; 
  274. setcookie("t00ls_s", base64_encode(serialize($Ssetting)), time()+60*60*24*365,"/"); 
  275. } 
  276. return $Ssetting; 
  277. } 
  278. function getCode() 
  279. { 
  280. return array( 
  281. '后门特征->cha88.cn'=>'cha88.cn', 
  282. '后门特征->c99shell'=>'c99shell', 
  283. '后门特征->phpspy'=>'phpspy', 
  284. '后门特征->Scanners'=>'Scanners', 
  285. '后门特征->cmd.php'=>'cmd.php', 
  286. '后门特征->str_rot13'=>'str_rot13', 
  287. '后门特征->webshell'=>'webshell', 
  288. '后门特征->EgY_SpIdEr'=>'EgY_SpIdEr', 
  289. '后门特征->tools88.com'=>'tools88.com', 
  290. '后门特征->SECFORCE'=>'SECFORCE', 
  291. '后门特征->eval("?>'=>'eval(('|")?>', 
  292. '可疑代码特征->system('=>'system(', 
  293. '可疑代码特征->passthru('=>'passthru(', 
  294. '可疑代码特征->shell_exec('=>'shell_exec(', 
  295. '可疑代码特征->exec('=>'exec(', 
  296. '可疑代码特征->popen('=>'popen(', 
  297. '可疑代码特征->proc_open'=>'proc_open', 
  298. '可疑代码特征->eval($'=>'eval(('|"|s*)\$', 
  299. '可疑代码特征->assert($'=>'assert(('|"|s*)\$', 
  300. '危险MYSQL代码->returns string soname'=>'returnsstringsoname', 
  301. '危险MYSQL代码->into outfile'=>'intooutfile', 
  302. '危险MYSQL代码->load_file'=>'select(s+)(.*)load_file', 
  303. '加密后门特征->eval(gzinflate('=>'eval(gzinflate(', 
  304. '加密后门特征->eval(base64_decode('=>'eval(base64_decode(', 
  305. '加密后门特征->eval(gzuncompress('=>'eval(gzuncompress(', 
  306. '加密后门特征->eval(gzdecode('=>'eval(gzdecode(', 
  307. '加密后门特征->eval(str_rot13('=>'eval(str_rot13(', 
  308. '加密后门特征->gzuncompress(base64_decode('=>'gzuncompress(base64_decode(', 
  309. '加密后门特征->base64_decode(gzuncompress('=>'base64_decode(gzuncompress(', 
  310. '一句话后门特征->eval($_'=>'eval(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  311. '一句话后门特征->assert($_'=>'assert(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  312. '一句话后门特征->require($_'=>'require(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  313. '一句话后门特征->require_once($_'=>'require_once(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  314. '一句话后门特征->include($_'=>'include(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  315. '一句话后门特征->include_once($_'=>'include_once(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  316. '一句话后门特征->call_user_func("assert"'=>'call_user_func(("|')assert("|')', 
  317. '一句话后门特征->call_user_func($_'=>'call_user_func(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  318. '一句话后门特征->$_POST/GET/REQUEST/COOKIE[?]($_POST/GET/REQUEST/COOKIE[?]'=>'$_(POST|GET|REQUEST|COOKIE)[([^]]+)](('|"|s*)\$_(POST|GET|REQUEST|COOKIE)[', 
  319. '一句话后门特征->echo(file_get_contents($_POST/GET/REQUEST/COOKIE'=>'echo(file_get_contents(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  320. '上传后门特征->file_put_contents($_POST/GET/REQUEST/COOKIE,$_POST/GET/REQUEST/COOKIE'=>'file_put_contents(('|"|s*)\$_(POST|GET|REQUEST|COOKIE)[([^]]+)],('|"|s*)\$_(POST|GET|REQUEST|COOKIE)', 
  321. '上传后门特征->fputs(fopen("?","w"),$_POST/GET/REQUEST/COOKIE['=>'fputs(fopen((.+),('|")w('|")),('|"|s*)\$_(POST|GET|REQUEST|COOKIE)[', 
  322. '.htaccess插马特征->SetHandler application/x-httpd-php'=>'SetHandlerapplication/x-httpd-php', 
  323. '.htaccess插马特征->php_value auto_prepend_file'=>'php_valueauto_prepend_file', 
  324. '.htaccess插马特征->php_value auto_append_file'=>'php_valueauto_append_file' 
  325. );//www.phpfensi.com 
  326. } 
  327. ?> 

希望本文所述对大家基于php的网站安全建设有所帮助。

Tags: php网站挂木马

分享到: