php Trojan horse webshell scanner code

  • 2020-05-10 17:53:51
  • OfStack

 
<?php 
/* 
+--------------------------------------------------------------------------+ 
| Codz by indexphp Version:0.01 | 
| (c) 2009 indexphp | 
| http://www.indexphp.org | 
+--------------------------------------------------------------------------+ 
*/ 
/*=====================  Application configuration  =====================*/ 
$dir='cms'; // Set the directory to scan  
$jumpoff=false;// Set the file to skip the check  
$jump='safe.php|g'; // Set the file or folder to skip the check  $jumpoff=false  When this setting is valid  
$danger='eval|cmd|passthru';// Sets the dangerous function to look for   To determine whether a Trojan file is present  
$suffix='php|inc';// Sets the suffix to scan the file  
$dir_num=0; 
$file_num=0; 
$danger_num=0; 
/*=====================  End of the configuration  =====================*/ 
extract (GetHttpVars()); 
if ($m=="edit") Edit(); 
if ($m=="del") Delete(); 
if ($check=='check') 
{ $safearr = explode("|",$jump); 
$start_time=microtime(true); 
safe_check($dir); 
$end_time=microtime(true); 
$total=$end_time-$start_time; 
$file_num=$file_num-$dir_num; 
$message= "  The file number :".$file_num; 
$message.= "  Number of folders: ".$dir_num; 
$message.= "  Number of suspicious files: ".$danger_num; 
$message.= "  Execution time: ".$total; 
echo $message; 
exit(); 
} 
function GetHttpVars() {// The global variable  
$superglobs = array( 
'_POST', 
'_GET', 
'HTTP_POST_VARS', 
'HTTP_GET_VARS'); 
$httpvars = array(); 
foreach ($superglobs as $glob) { 
global $$glob; 
if (isset($$glob) && is_array($$glob)) { 
$httpvars = $$glob; 
} 
if (count($httpvars) > 0) 
break; 
} 
return $httpvars; 
} 
function Safe_Check($dir)// Traverse the document  
{ 
global $danger ,$suffix ,$dir_num ,$file_num ,$danger_num; 
$hand=@dir($dir) or die(' Folder does not exist ') ; 
while ($file=$hand->read() ) 
{ 
$filename=$dir.'/'.$file; 
if (!$jumpoff) { 
if(Jump($filename))continue; 
} 
if(@is_dir($filename) && $file != '.' && $file!= '..'&& $file!='./..') 
{ $dir_num++; 
Safe_Check($filename); 
} 
if (preg_match_all ("/\.($suffix)/i",$filename,$out)) 
{ 
$str=''; 
$fp = @fopen($filename,'r')or die(' Have no legal power '); 
while(!feof($fp)) 
{ 
$str .= fgets($fp,1024); 
} 
fclose($fp); 
if( preg_match_all ("/($danger)[ \r\n\t]{0,}([\[\(])/i",$str,$out)) 
{ 
echo "<font color='green' style='font-size:14px'> Suspicious files: {$filename}</font> 
<a href='?m=edit&filename=$filename' target='_blank'><u> Look at the code </u></a> 
<a href='?m=del&filename=$filename' target='_blank'> delete </u></a><br>"; 
$danger_num++; 
} 
} 
$file_num++; 
} 
} 
function Edit()// View suspicious files  
{ 
global $filename; 
$filename = str_replace("..","",$filename); 
$file = $filename; 
$content = ""; 
if(is_file($file)) 
{ 
$fp = fopen($file,"r")or die(' Have no legal power '); 
$content = fread($fp,filesize($file)); 
fclose($fp); 
$content = htmlspecialchars($content); 
} 
echo "<textarea name='str' style='width:100%;height:450px;background:#cccccc;'>$content</textarea>\r\n"; 
exit(); 
} 
function Delete()// Delete the file  
{ 
global $filename; 
(is_file($filename))?($mes=unlink($filename)?' Delete the success ':' Delete failed   Check the permissions '):''; 
echo $mes; 
exit(); 
} 
function Jump($file)// Skip the file  
{ 
global $jump,$safearr; 
if($jump != '') 
{ 
foreach($safearr as $v) 
{ 
if($v=='') continue; 
if( eregi($v,$file) ) return true ; 
} 
} 
return false; 
} 
?> 
<form action="" > 
<input type="submit" value=" Starting test " /> 
<input type="hidden" name="check" value="check"/> 
</form> 

Related articles: