Advertisement
denik-od

JS Antivirus

Nov 2nd, 2016
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2.  
  3. // Get file list
  4. $filelist = "all_js_files_list.txt";
  5. //$filelist = "test_js_files.txt";
  6. $log_file = "js_antivirus.log";
  7.  
  8. $fp = fopen($filelist, "r");
  9. $fp_log = fopen($log_file, "a");
  10. if( $fp )
  11. {
  12.     fwrite($fp_log, "\n\n=========================\n");
  13.     fwrite($fp_log, "Start JS antivirus clean\n");
  14.     fwrite($fp_log, date("Y-m-d H:i:s")."\n");
  15.     fwrite($fp_log, "=========================\n");
  16.  
  17.     while( ($filename = fgets($fp)) !== false )
  18.     {
  19.         $filename = trim($filename);
  20.         if( file_exists($filename) )
  21.         {
  22.             // Run antivirus
  23.             $content = file_get_contents($filename);
  24.             $res = virus_clear($content);
  25.             if( $res )
  26.             {
  27.                 file_put_contents($filename, $content);
  28.                 echo "$filename - OK\n";
  29.                 fwrite($fp_log, "$filename - OK\n");
  30.             }
  31.             else
  32.             {
  33.                 echo "$filename - not found\n";
  34.                 fwrite($fp_log, "$filename - not found\n");
  35.             }
  36.         }
  37.         else
  38.         {
  39.             echo "File not exists: $filename\n";
  40.             fwrite($fp_log, "File not exists: $filename\n");
  41.         }
  42.     }
  43.  
  44.     if (!feof($fp))
  45.     {
  46.         echo "Error: unexpected fgets() fail\n";
  47.         fwrite($fp_log, "Error: unexpected fgets() fail\n");
  48.     }
  49.     fclose($fp_log);
  50.     fclose($fp);
  51. }
  52.  
  53.  
  54.  
  55.  
  56. function virus_clear(&$js)
  57. {
  58.     $js_lenght = strlen($js);
  59.     $max_length = min($js_lenght, 2000);
  60.  
  61.     $js_slice = substr($js, 0-$max_length);
  62.  
  63.     $keys = array();
  64.     $found = false;
  65.     if( preg_match_all("#([\d\w]+)\(([\d\w]+)\(([\d\w]+)\[\d+\]\)\);#is", $js_slice, $match) && isset($match[3][0]) )
  66.     {
  67.         foreach( $match[0] as $m_i=>$fns_full )
  68.         {
  69.             $fns1 = $match[1][$m_i];
  70.             $fns2 = $match[2][$m_i];
  71.             $var = $match[3][$m_i];
  72.  
  73.             if( preg_match("#var\s$var=\[[\"'\d,]{100,}\];#is", $js_slice, $match2) && isset($match2) )
  74.             {
  75.                 if( preg_match("#function\s$fns2\([\d\w]+\){return\s([\d\w]+)\(([\d\w]+)#is", $js_slice, $match3) && isset($match3) )
  76.                 {
  77.                     $keys = array(
  78.                         $fns_full,
  79.                         'function '.$fns1,
  80.                         'function '.$fns2,
  81.                         $match2[0],
  82.                         $match3[0],
  83.                         'function '.$match3[1],
  84.                         'function '.$match3[2]
  85.                     );
  86.                 }
  87.             }
  88.  
  89.             if( $keys )
  90.             {
  91.                 $pos = array();
  92.                 foreach( $keys as $key ) $pos[] = strpos($js_slice, $key);
  93.                 $pos = min($pos);
  94.                 //$lenght = strlen($js_slice)-$pos;
  95.  
  96.                 // Caught!
  97.                 //if( $pos && $lenght <= $max_length )
  98.                 //{
  99.                     $js_slice = substr($js_slice, 0, $pos);
  100.                     $found = true;
  101.                 //}
  102.             }
  103.         }
  104.     }
  105.  
  106.     if( $found )
  107.     {
  108.         $js = substr($js, 0, 0-$max_length) . $js_slice;
  109.     }
  110.  
  111.     return $found;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement