DaemonHK

NoN Production WP Virus Scan

Nov 1st, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.91 KB | None | 0 0
  1. <?php
  2. /*
  3.  * 1. Сканируем всю корневую директорию +
  4.  * 2. Ищем файлы без расширений и неизвестных форматов +
  5.  * 3. Проверяем папки на содержимое в поисках вирусов
  6.  * 4. Проверяем размеры файлов
  7.  */
  8. class wpStructure{
  9.     public static $data=array(
  10.         "wp-admin"=>array(
  11.             "type"=>"folder",
  12.             "normalExt"=>array("htaccess","php"),
  13.             "child"=>array(
  14.                 "css"=>array(
  15.                     "type"=>"folder",
  16.                     "normalExt"=>array("css"),
  17.                     "child"=>array(
  18.                         "colors"=>array(
  19.                             "type"=>"folder",
  20.                             "normalExt"=>array("scss"),
  21.                             "child"=>array()
  22.                         ),
  23.                     ),
  24.                 ),
  25.             )
  26.         ),
  27.     );
  28. }
  29. class check{
  30.     private static $data=array();
  31.     private static $parentDir=".";
  32.     private static $normalExt=array(
  33.         "htaccess","md","db",
  34.         "css","scss",
  35.         "ico","png","jpg","jpeg","gif",
  36.         "svg","psd","swf",
  37.         "mp4","avi","mp3","ogg","wav",
  38.         "js","php","txt","htm","html","rtf","pdf",
  39.         "csv","xml","json","sql",
  40.         "ini","dat","002","icc","crt","ds_store",
  41.         "ttf","eot","woff","otf",
  42.         "mo","po","pot","xap",
  43.         "gz",
  44.     );
  45.     private static $badNames=array(
  46.         "global",
  47.     );
  48.     private static $badWords=array(
  49.         "eval","base64_decode","base64_encode","gzinflate","str_rot13","exec","mail"
  50.     );
  51.     private static $normalFiles=array("Makefile");
  52.     private static $typeErrors=array(
  53.         1=>"Возможный вирус",
  54.         2=>"Неизвестный формат",
  55.     );
  56.     public static $errors=array();
  57.  
  58.     private static function parseSite($path,$isRoot=false){
  59.         $pathFiles=scandir($path);
  60.         foreach($pathFiles as $key=>$value){
  61.             if(!in_array($value,array(".","..",".idea","@ban","parser.php"))){
  62.                 $file=explode(".",$value);
  63.                 if(count($file)==1 && !is_dir($path.$value) && !in_array($value,self::$normalFiles)){
  64.                     $fileData=pathinfo($path.$value);
  65.                     if(!isset($fileData["extension"])){
  66.                         $filename=$fileData["dirname"]."/".$fileData["filename"];
  67.                         $fileInfo=self::fileInfo($filename);
  68.                         self::setErrors($fileInfo,1);
  69.                     }
  70.                 }
  71.                 if(!isset($file[1]) && !$isRoot){
  72.                     $value=$path.$value;
  73.                 }else{
  74.                     if(count($file)>1){
  75.                         $ext=strtolower($file[count($file)-1]);
  76.                         if(!in_array($ext,self::$normalExt)){
  77.                             $fileInfo=self::fileInfo($value);
  78.                             self::setErrors($fileInfo,2);
  79.                         }
  80.                     }
  81.                 }
  82.                 if(is_dir($value)){
  83.                     $fullPath=$isRoot?"./{$value}/":"{$value}/";
  84.                     self::$data[$fullPath]=array();
  85.                     self::parseSite($fullPath);
  86.                 }else{
  87.                     $path=$isRoot?"./":$path;
  88.                     $fullPath=$path.$value;
  89.                     self::$data[$path][]=$fullPath;
  90.                 }
  91.             }
  92.             unset($key,$value);
  93.         }
  94.     }
  95.  
  96.     private static function fillArray($data){
  97.         $outArray=array();
  98.         foreach($data as $key=>$value){
  99.             $keys=explode("/",$key);
  100.             $keys=array_slice($keys,0,count($keys)-1);
  101.             $outArray=self::setElements($outArray,$keys,$value);
  102.             unset($key,$value);
  103.         }
  104.         return $outArray;
  105.     }
  106.  
  107.     private static function setElements(&$array,$key,$value,$layer=1){
  108.         $current=$key[$layer-1];
  109.         if(!isset($array[$current])){
  110.             $array[$current]=array();
  111.         }
  112.         if($layer==count($key)){
  113.             $array[$current]=$value;
  114.         }else{
  115.             $array[$current]=self::setElements($array[$current],$key,$value,$layer+1);
  116.         }
  117.         return $array;
  118.     }
  119.  
  120.     private static function setErrors($fileInfo,$type=null){
  121.         if(!empty($type)){
  122.             $fileInfo["type"]=self::$typeErrors[$type];
  123.         }
  124.         self::$errors[]=$fileInfo;
  125.     }
  126.  
  127.     public static function setTable($isClosed=false){
  128.         echo $isClosed?"</table>":"<table>";
  129.     }
  130.  
  131.     public static function fileInfo($file){
  132.         $fileInfo=array();
  133.         $fileInfo["file"]=$file;
  134.         $fileInfo["date"]=date ("d.m.Y H:i:s",filemtime($file));
  135.         $fileInfo["chmod"]=substr(sprintf("%o",fileperms($file)),-4);
  136.         return $fileInfo;
  137.     }
  138.  
  139.     public static function getReport($type=1){
  140.         self::parseSite(check::$parentDir,true);
  141.         $objData=null;
  142.         if($type==1){
  143.             $objData=json_decode(json_encode(check::$data),false);
  144.         }elseif($type==2){
  145.             $objData=json_decode(json_encode(self::fillArray(check::$data)),false);
  146.         }
  147.         return check::$data;
  148.     }
  149. }
  150. function preDump($data){
  151.     echo "<meta charset=\"UTF-8\">";
  152.     echo "<pre>";
  153.     var_dump($data);
  154.     echo "</pre>";
  155. }
  156.  
  157. $outData=check::getReport();
  158. //if(!empty(check::$errors)){
  159. //  preDump(check::$errors);
  160. //}
  161. //var_dump(check::getReport());
  162. foreach($outData as $key=>$files){
  163.     foreach($files as $file){
  164.         $data=explode(".",$file);
  165.         if($data[count($data)-1]=="php"){
  166.             $text=file($file);
  167.             $firstStrings=array();
  168.             if(isset($text[1]) && !in_array(trim($text[1]),array("/**","/*"))){
  169.                 for($i=0;$i<=5;){
  170.                     if(isset($text[$i])){
  171.                         $firstStrings[$i]=$text[$i];
  172.                     }
  173.                     $i++;
  174.                 }
  175.             }
  176.             if(!empty($firstStrings)){
  177.                 $firstStrings=join("<br />",$firstStrings);
  178.                 echo "<strong>{$file}</strong><br />";
  179.                 preDump($firstStrings);
  180.             }
  181.         }
  182.         unset($file);
  183.     }
  184.     unset($key,$files);
  185. }
Add Comment
Please, Sign In to add comment