Advertisement
Guest User

Fix for WP Timthumb.php Vulnerability Scanner Plugin

a guest
Nov 2nd, 2011
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2.  
  3. /***
  4.  
  5. FILENAME       : cg-tvs-filescanner.php
  6. PART OF PLUGIN : "WordPress Timthumb.php Vulnerability Scanner Plugin"
  7. PLUGIN VERSION : 1.3
  8. DESCRIPTION    : This file fixes the plugin so it works on webservers running on Windows, such as WAMP.
  9. PLUGIN URI     : http://codegarage.com/blog/2011/09/wordpress-timthumb-vulnerability-scanner-plugin/
  10.  
  11. ***/
  12.  
  13. class CG_FileScanner {
  14.  
  15.     public $BaseDir;
  16.     public $Errors;
  17.     public $Inventory;
  18.     public $VulnerableFiles;
  19.     public $SafeFiles;
  20.     private $RunningOnWindows = false;
  21.  
  22.     function __construct( $base_dir ) {
  23.         if ( is_file( $base_dir ) || is_dir( $base_dir ) ) {
  24.  
  25.             if ( strtoupper(substr(php_uname('s'), 0, 3)) === 'WIN' ) {
  26.                 $this->RunningOnWindows = true;
  27.                 $base_dir = str_replace('/', '\\', $base_dir);
  28.             }
  29.  
  30.             $this->BaseDir = $base_dir;
  31.            
  32.         } else {
  33.             die();
  34.         }
  35.     }
  36.  
  37.   function generate_inventory() {
  38.     $this->Inventory = $this->get_dir_contents( $this->BaseDir, true );
  39.   }
  40.  
  41. function get_dir_contents( $path ) {
  42.     $inventory = array();
  43.     if ( ! $dir_handle = @opendir( $path ) ) {
  44.         $this->Errors[] = "Couldn't open $path";
  45.         return false;
  46.     }
  47.     while ( $file = readdir( $dir_handle ) ) {
  48.         if ( $file == '.' || $file == '..' ) continue;
  49.         if ( is_dir( $path . DIRECTORY_SEPARATOR . $file ) ) {
  50.             $inventory = array_merge( $inventory, $this->get_dir_contents( $path . DIRECTORY_SEPARATOR . $file ) );
  51.         } else {
  52.             $inventory[] = $path . DIRECTORY_SEPARATOR . $file;
  53.         }
  54.     }
  55.     closedir( $dir_handle );
  56.     return $inventory;
  57. }
  58.  
  59. function file_stat( $file ) {
  60.     $file_info['path']  = $file;
  61.     if( is_dir( $file ) ) {
  62.         $file_info['type'] = 'directory';
  63.     }else{
  64.         $file_info['type'] = 'file';
  65.     }
  66.  
  67.     $file_info['readable'] = is_readable( $file );
  68.     if ( $this->Type == 'structure' ) {
  69.         if ( $file_info['type'] == 'directory' ) {
  70.             $dir_stat = $this->dir_stat( $file );
  71.             $file_info['mtime']       = $dir_stat['mtime'];
  72.             $file_info['child_nodes'] = $dir_stat['child_nodes'];
  73.             $file_info['ctime']       = filectime( $file );
  74.             $file_info['atime']       = fileatime( $file );
  75.             return $file_info;
  76.         } else {
  77.             return '';
  78.         }
  79.     }
  80.  
  81.     if ( $this->Type != 'index' && $this->Type != 'structure' ) {
  82.         $file_info['mtime'] = filemtime( $file );
  83.         $file_info['ctime'] = filectime( $file );
  84.         $file_info['atime'] = fileatime( $file );
  85.         $file_info['size']  = filesize( $file );
  86.         $file_info['hash']  = md5_file( $file );
  87.     }
  88.  
  89.     return $file_info;
  90. }
  91.  
  92. function scan_inventory() {
  93.     $pattern_1 = "TimThumb script created by Tim McDaniels and Darren Hoyt|TimThumb script created by Ben Gillbanks\, originally created by Tim McDaniels and Darren Hoyt|TimThumb by Ben Gillbanks";
  94.     $pattern_2 = 'define\s*\(\'VERSION\',\s*\'[23456789]\.[0-9]';
  95.  
  96.     foreach( $this->Inventory as $path ) {
  97.         $path_parts = pathinfo( $path );
  98.  
  99.         $curdir = dirname(__FILE__);
  100.         if ( $this->RunningOnWindows ) {
  101.             $curdir = str_replace('\\', '\\\\', $curdir);
  102.         }
  103.  
  104.         // Don't scan this plugin's files
  105.         if( preg_match( "~^$curdir~", $path ) ) {
  106.             continue;
  107.         }
  108.         if( $path_parts['extension'] == 'php' ) {
  109.             if( $file_handle = @fopen( $path, 'r' ) ) {
  110.                 $contents = @fread( $file_handle, filesize( $path ) );
  111.                 if ( preg_match( "~$pattern_1~", $contents ) ) {
  112.                     // We have a timthumb script.  Now check to see if it is version 2.0 or greater.
  113.                     if ( ! preg_match( "~$pattern_2~", $contents ) ) {
  114.                         $this->VulnerableFiles[] = $path;
  115.                     } else {
  116.                         $this->SafeFiles[] = $path;
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.     }
  122. }
  123.  
  124. }
  125. ?>
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement