Advertisement
MarcHumer

Check WP-Scripts for "uncommon" code

Mar 22nd, 2025 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.14 KB | Cybersecurity | 0 0
  1. <?php
  2. if (session_status() === PHP_SESSION_NONE) {
  3.     ini_set('ignore_repeated_errors', TRUE);
  4.     ini_set('display_errors', FALSE);
  5.     ini_set('log_errors', TRUE);
  6.     ini_set('error_log', 'errors.txt');
  7.     ini_set('log_errors_max_len', 1024);
  8.     ini_set('set_time_out',500);
  9.     error_reporting(E_ALL);
  10.     session_start();
  11.     $_SESSION['monitoring']=1;
  12. }
  13. $ausgabe = array();
  14. if (!empty($_REQUEST['typ']) && isset($_REQUEST['typ']))
  15. {
  16.     $par = strtoupper(trim(strip_tags(stripslashes($_REQUEST['typ']))));
  17.     $par = explode(",",$par);
  18. } else $par = array('A','B','C','D','E','F');
  19.  
  20. if (isset($_REQUEST['sub']))
  21. {
  22.     $maxDepth = (int)($_REQUEST['sub']);
  23. } else $maxDepth = -1;
  24.  
  25.  
  26.  
  27. $basedir =
  28. $directory = './'; // Current directory
  29. echo '<h3>vMon - PHP auf potentielle Schwachstellen untersuchen</h3>
  30. <p>maximale Such-Tiefe: '.($maxDepth==-1?"unbegrenzt":$maxDepth).'</p>
  31.  
  32. <p>Parameter:
  33. <br>- sub=[Verzeichnis-Tiefe] (Default: alle Dateien)
  34. <br>- typ=[A-F] (Default: alle Typen)</p>';
  35. flush();
  36.  
  37. function getFilesInDirectoryAndSubdirectories($dir, $maxDepth = -1, $currentDepth = 0) {
  38.     if ($maxDepth>-1 && $currentDepth > $maxDepth) {
  39.     return [];
  40.     }
  41.     $files = [];
  42.     if ($dir!="./stats" && $dir != './awstats' && $dir != './cgi-bin' && $dir != './logs')
  43.     {
  44.         $iterator = new DirectoryIterator($dir);
  45.         foreach ($iterator as $file) {
  46.             if ($file->isDot()) continue;
  47.             if ($file->isFile()) {
  48.                 $files[] = $file->getPathname();
  49.             } elseif ($file->isDir()) {
  50.                 $subFiles = getFilesInDirectoryAndSubdirectories($file->getPathname(), $maxDepth, $currentDepth + 1);
  51.                 $files = array_merge($files, $subFiles);
  52.             }
  53.         }
  54.     }
  55.   return $files;
  56. }
  57.  
  58. function h($d) {
  59.     return str_replace(basename($d),'<b style="color:#00f;">'.basename($d).'</b>',$d);
  60. }
  61.  
  62. function verlinkung($d) {
  63.     global $basedir;
  64.     $x = str_replace(basename($_SERVER['REQUEST_URI']),"",$_SERVER['REQUEST_URI']).str_replace(array("\\",$_SERVER['DOCUMENT_ROOT']),array("/",''),($d));
  65.     $x = str_replace($basedir,'',$x);
  66.     return $x;
  67. }
  68.  
  69. function genLine($typ,$filePath,$fdate,$fsize,$pat,$hit,$ln) {
  70.     $s = '<td>'.$typ.'</td>';
  71.     $s .= '<td style="color:#666666;">'.h(verlinkung($filePath)).'</td>';
  72.     $s .= '<td>'.$fdate.'</td>';
  73.     $s .= '<td>'.$fsize.'</td>';
  74.     if ($hit==0)
  75.         $s .= '<td colspan="2">'.$pat.'</td>';
  76.     else {
  77.         $s .= '<td>'.$pat.'</td>';
  78.         $s .= '<td>'.$hit.'</td>';
  79.     }
  80.     $s .= '<td>'.$ln.'</td>';
  81.     return $s;
  82. }
  83.  
  84. $files = getFilesInDirectoryAndSubdirectories($directory, $maxDepth);
  85. echo '<h4>Durchsucht: '.count($files).' Dateien</h4>';
  86. echo '
  87. <p>A findet $GLOBALS[$var] (potentiell unsicher, Fall zu Fall prüfen)
  88. <br>B sucht ungewöhnlichen Auskommentierungen /* &hellip; */
  89. <br>C findet Zuordnungen von Globals, die verdächtig sind
  90. <br>D findet (@)includes mit Variablen als Namens-Übergabe
  91. <br>E findet ungewöhnlichen Zeichen-Kodierungen wie chr(&hellip;)
  92. <br>F findet Null-Byte-Files
  93. <br>&nbsp;</p>
  94. <p><B>Wichtig!</B> Natürlich sind nicht alle Funde Malware.<br>Oft ist der Code drum herum entscheidend, ob ein Konstrukt problematisch oder gar gefährlich ist.<br>Aber das Script zeigt potentielle Schwachstellen auf, die zu prüfen sich lohnt.</p>';
  95. flush();
  96.  
  97. foreach ($files as $file) {
  98.     if ($file != '.' && $file != '..' && !strpos($file,".gz")
  99.     && (strpos($file,".php")||strpos($file,".css")||strpos($file,".scss")||strpos($file,".png")||strpos($file,".js"))
  100.     && is_file($directory . $file)
  101.     && basename($file)!='vmon.php'  ) {
  102.         $filePath = $directory . $file;
  103.         $date = date("Y-m-d H:i:s", filemtime($filePath));
  104.         $size = filesize($filePath);
  105.  
  106.     if (in_array("F", $par)){
  107.         if ($size==0 && basename($filePath)!="index.php") {
  108.             $out = '<tr class="c6">';
  109.             $out .= genLine('F',$filePath,$date,0,'0-Byte-File',0,$zeile);
  110.             $out .= '</tr>';
  111.             $ausgabe[] = $out;
  112.             continue;
  113.         }
  114.     }
  115.  
  116.     $lines = explode("\n",file_get_contents($filePath));
  117.         $t = -1;
  118.         $zeile = 0;
  119.         foreach ($lines as $line) {
  120.             $zeile ++;
  121.             $line = str_replace('/***/','',$line)."\n";
  122.             $t = strpos($line,'$GLOBALS[');
  123.             if (in_array("A", $par) && $t !==FALSE && substr($line,$t+9,1)!='"' && substr($line,$t+9,1)!="'") {
  124.                 $firstOccurrence = htmlentities(substr($line,$t,30));
  125.                 $out = '<tr class="c1">';
  126.                 $out .= genLine('A',$filePath,$date,$size,$firstOccurrence,0,$zeile);
  127.                 $out .= '</tr>';
  128.                 $ausgabe[] = $out;
  129.             }
  130.             elseif (in_array("B", $par) && preg_match('#\/\*\w+\s\*\/#', $line, $matches)) {
  131.                 $firstOccurrence = htmlentities($matches[0]);
  132.                 $out = '<tr class="c2">';
  133.                 $out .= genLine('B',$filePath,$date,$size,$firstOccurrence,sizeof($matches),$zeile);
  134.                 $out .= '</tr>';
  135.                 $ausgabe[] = $out;
  136.             }
  137.                         elseif (in_array("C", $par) && preg_match('#\$(.*?)\s=\s\$GLOBALS(?:\s|;)\s*#', $line, $matches)) {
  138.                 $firstOccurrence = htmlentities($matches[0]);
  139.                 $out = '<tr class="c3">';
  140.                 $out .= genLine('C',$filePath,$date,$size,$firstOccurrence,sizeof($matches),$zeile);
  141.                 $out .= '</tr>';
  142.                 $ausgabe[] = $out;
  143.             }
  144.             elseif (in_array("D", $par) && preg_match('/@?include\s*\(\s*(\$\w+)\s*\)/', $line, $matches)) {
  145.             $a = strpos($line, "include");
  146.             $firstOccurrence = htmlentities(substr($line, $a, 30));
  147.             $out = '<tr class="c4">';
  148.                 $out .= genLine('D',$filePath,$date,$size,$firstOccurrence,sizeof($matches),$zeile);
  149.             $out .= '</tr>';
  150.             $ausgabe[] = $out;
  151.             }
  152.             elseif (in_array("E", $par) && preg_match('/chr\((\w+)[-](\w+)\)/', $line, $matches)) {
  153.                 $firstOccurrence = htmlentities($matches[0]);
  154.                 $out = '<tr class="c5">';
  155.                 $out .= genLine('E',$filePath,$date,$size,$firstOccurrence,sizeof($matches),$zeile);
  156.                 $out .= '</tr>';
  157.                 $ausgabe[] = $out;
  158.             }
  159.         }
  160.     }
  161. }
  162.  
  163. if (sizeof($ausgabe)>0)
  164. {
  165.     sort($ausgabe);
  166. echo<<<hd
  167. <style>
  168. td a{color:#000 !important}
  169. .c1{background-color:#ff9999}
  170. .c2{background-color:#99ff99}
  171. .c3{background-color:#9999ff}
  172. .c4{background-color:#ffff99}
  173. .c5{background-color:#99ffff}
  174. .c6{background-color:#ff99ff}
  175. </style>
  176. <table border="1"><tr><th>TYP</th><th style="width:300px;">Name</th><th>Datum/Zeit</th><th>Größe</th><th>Muster</th><th>Treffer</th><th>Zeile</th></tr>
  177. hd;
  178.     foreach($ausgabe as $zeile) echo $zeile;
  179.     echo '</table>';
  180. } else echo "Keine Treffer";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement