Diff
14 years ago
.htaccess
14 years ago
Diff.php
14 years ago
IPTraf.php
14 years ago
diffResult.php
14 years ago
dropAll.php
14 years ago
email_genericAlert.php
14 years ago
email_newIssues.php
14 years ago
email_unlockRequest.php
14 years ago
menu_activity.php
14 years ago
menu_blockedIPs.php
14 years ago
menu_config.php
14 years ago
menu_options.php
14 years ago
menu_scan.php
14 years ago
sysinfo.php
14 years ago
viewFullActivityLog.php
14 years ago
wf503.php
14 years ago
wfAPI.php
14 years ago
wfAction.php
14 years ago
wfBrowscap.php
14 years ago
wfBrowscapCache.php
14 years ago
wfConfig.php
14 years ago
wfCrawl.php
14 years ago
wfDB.php
14 years ago
wfDict.php
14 years ago
wfIssues.php
14 years ago
wfLockedOut.php
14 years ago
wfLog.php
14 years ago
wfModTracker.php
14 years ago
wfRate.php
14 years ago
wfScanEngine.php
14 years ago
wfSchema.php
14 years ago
wfUnlockMsg.php
14 years ago
wfUtils.php
14 years ago
wfViewResult.php
14 years ago
wordfenceClass.php
14 years ago
wordfenceConstants.php
14 years ago
wordfenceHash.php
14 years ago
wordfenceScanner.php
14 years ago
wordfenceURLHoover.php
14 years ago
wordfenceHash.php
124 lines
| 1 | <?php |
| 2 | require_once('wordfenceClass.php'); |
| 3 | class wordfenceHash { |
| 4 | private $whitespace = array("\n","\r","\t"," "); |
| 5 | public $totalData = 0; //To do a sanity check, don't use 'du' because it gets sparse files wrong and reports blocks used on disk. Use : find . -type f -ls | awk '{total += $7} END {print total}' |
| 6 | public $totalFiles = 0; |
| 7 | public $totalDirs = 0; |
| 8 | public $linesOfPHP = 0; |
| 9 | public $linesOfJCH = 0; //lines of HTML, CSS and javascript |
| 10 | public $striplen = 0; |
| 11 | private $hashes = array(); |
| 12 | public function __construct($striplen){ |
| 13 | $this->striplen = $striplen; |
| 14 | } |
| 15 | public function hashPaths($path, $only = array()){ //base path and 'only' is a list of files and dirs in the bast that are the only ones that should be processed. Everything else in base is ignored. If only is empty then everything is processed. |
| 16 | if($path[strlen($path) - 1] != '/'){ |
| 17 | $path .= '/'; |
| 18 | } |
| 19 | if(! is_readable($path)){ |
| 20 | wordfence::status(1, 'error', "Could not read directory $path to do sacn."); |
| 21 | exit(); |
| 22 | } |
| 23 | $files = scandir($path); |
| 24 | foreach($files as $file){ |
| 25 | if(sizeof($only) > 0 && (! in_array($file, $only))){ |
| 26 | continue; |
| 27 | } |
| 28 | $file = $path . $file; |
| 29 | wordfence::status(2, 'info', "Hashing item in base dir: $file"); |
| 30 | $this->_dirHash($file); |
| 31 | } |
| 32 | return $this->hashes; |
| 33 | } |
| 34 | private function _dirHash($path){ |
| 35 | if(substr($path, -3, 3) == '/..' || substr($path, -2, 2) == '/.'){ |
| 36 | return; |
| 37 | } |
| 38 | if(! is_readable($path)){ return; } //Applies to files and dirs |
| 39 | if(is_dir($path)){ |
| 40 | $this->totalDirs++; |
| 41 | if($path[strlen($path) - 1] != '/'){ |
| 42 | $path .= '/'; |
| 43 | } |
| 44 | $cont = scandir($path); |
| 45 | for($i = 0; $i < sizeof($cont); $i++){ |
| 46 | if($cont[$i] == '.' || $cont[$i] == '..'){ continue; } |
| 47 | $file = $path . $cont[$i]; |
| 48 | if(is_file($file)){ |
| 49 | $this->processFile($file); |
| 50 | } else if(is_dir($file)) { |
| 51 | $this->_dirHash($file); |
| 52 | } |
| 53 | } |
| 54 | } else { |
| 55 | if(is_file($path)){ |
| 56 | $this->processFile($path); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | private function processFile($file){ |
| 61 | if(@filesize($file) > WORDFENCE_MAX_FILE_SIZE_TO_PROCESS){ |
| 62 | wordfence::status(2, 'info', "Skipping file larger than 50 megs: $file"); |
| 63 | return; |
| 64 | } |
| 65 | if(function_exists('memory_get_usage')){ |
| 66 | wordfence::status(2, 'info', "Scanning: $file (Mem:" . sprintf('%.1f', memory_get_usage(true) / (1024 * 1024)) . "M)"); |
| 67 | } else { |
| 68 | wordfence::status(2, 'info', "Scanning: $file"); |
| 69 | } |
| 70 | $wfHash = $this->wfHash($file, true); |
| 71 | if($wfHash){ |
| 72 | $this->hashes[substr($file, $this->striplen)] = $wfHash; |
| 73 | //Now that we know we can open the file, lets update stats |
| 74 | if(preg_match('/\.(?:js|html|htm|css)$/i', $file)){ |
| 75 | $this->linesOfJCH += sizeof(file($file)); |
| 76 | } else if(preg_match('/\.php$/i', $file)){ |
| 77 | $this->linesOfPHP += sizeof(file($file)); |
| 78 | } |
| 79 | $this->totalFiles++; |
| 80 | $this->totalData += filesize($file); |
| 81 | } else { |
| 82 | wordfence::status(2, 'error', "Could not gen hash for file: $file"); |
| 83 | } |
| 84 | } |
| 85 | public function wfHash($file, $binary = true){ |
| 86 | $md5 = @md5_file($file, $binary); |
| 87 | if(! $md5){ return false; } |
| 88 | //$sha = @hash_file('sha256', $file, $binary); |
| 89 | //if(! $sha){ return false; } |
| 90 | $fp = @fopen($file, "rb"); |
| 91 | if(! $fp){ |
| 92 | return false; |
| 93 | } |
| 94 | $ctx = hash_init('sha256'); |
| 95 | while (!feof($fp)) { |
| 96 | hash_update($ctx, str_replace($this->whitespace,"",fread($fp, 65536))); |
| 97 | } |
| 98 | $shac = hash_final($ctx, $binary); |
| 99 | //Taking out $sha for now because we don't use it on the scanning server side |
| 100 | return array($md5, '', $shac, filesize($file) ); |
| 101 | } |
| 102 | public static function bin2hex($hashes){ |
| 103 | function wf_func1($elem){ |
| 104 | return array( |
| 105 | bin2hex($elem[0]), |
| 106 | bin2hex($elem[1]), |
| 107 | bin2hex($elem[2]) |
| 108 | ); |
| 109 | } |
| 110 | return array_map('wf_func1', $hashes); |
| 111 | } |
| 112 | public static function hex2bin($hashes){ |
| 113 | function wf_func2($elem){ |
| 114 | return array( |
| 115 | pack('H*', $elem[0]), |
| 116 | pack('H*', $elem[1]), |
| 117 | pack('H*', $elem[2]) |
| 118 | ); |
| 119 | } |
| 120 | return array_map('wf_func2', $hashes); |
| 121 | } |
| 122 | } |
| 123 | ?> |
| 124 |