From acb58f1c2ee6e32dee8142eaec4f2cfeee585a0d Mon Sep 17 00:00:00 2001 From: Gabor Gyorvari Date: Wed, 11 Jan 2017 19:10:59 +0100 Subject: [PATCH] New ignore argument to exclude files and folders with glob style matching --- README.md | 17 ++++++++------- scan.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d8ff30d..832af26 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,18 @@ How to use? ``` $ php ./scan.php -h -Usage scan.php -d [-e=.php] [--hide-ok] [--hide-whitelist] - -d Directory for searching - -e=.php Extension - --hide-ok Hide OK aka not infected messages - --hide-whitelist Hide whitelisted messages - --extra-check Adds GoogleBot and htaccess to Scan List - --follow-symlink Follow symlinked directories +Usage scan.php -d [-i=] [-e=.php] [--hide-ok] [--hide-whitelist] + -d Directory for searching + -e=.php Extension + -i= Directory of file to igonre + --hide-ok Hide OK aka not infected messages + --hide-whitelist Hide whitelisted messages + --extra-check Adds GoogleBot and htaccess to Scan List + --follow-symlink Follow symlinked directories ``` +Ignore argument could be used multiple times and accept glob style matching ex.: "cache*", "??-cache.php" or "/cache" etc. + Whitelisting ------------ diff --git a/scan.php b/scan.php index b0614b7..45be34a 100644 --- a/scan.php +++ b/scan.php @@ -28,6 +28,7 @@ class MalwareScanner private $flagHideWhitelist = false; private $extraCheck = false; private $whitelist = array(); + private $ignore = array(); private $stat = array( 'directories' => 0, 'files_scanned' => 0, @@ -37,7 +38,7 @@ class MalwareScanner public function __construct() { - $options = getopt('hd:e::', array('hide-ok', 'hide-whitelist', 'extra-check', 'follow-symlink')); + $options = getopt('hd:e::i::', array('hide-ok', 'hide-whitelist', 'extra-check', 'follow-symlink')); if (isset($options['h'])) { $this->showHelp(); } else { @@ -48,6 +49,9 @@ class MalwareScanner } $this->extension = strtolower($ext); } + if (isset($options['i'])) { + $this->ignore = is_array($options['i']) ? $options['i'] : array($options['i']); + } if (isset($options['hide-ok'])) { $this->flagHideOk = true; } @@ -110,6 +114,9 @@ class MalwareScanner if ($file == '.' || $file == '..') { continue; } + if ($this->isIgnored($dir . $file)) { + continue; + } if (!$this->followSymlink && is_link($dir . $file)) { continue; } @@ -384,6 +391,45 @@ class MalwareScanner return true; } + private function isIgnored($pathname) + { + foreach ($this->ignore as $pattern) { + $match = $this->pathMatches($pathname, $pattern); + if ($match) { + return true; + } + } + return false; + } + + /** + * @see http://stackoverflow.com/a/13914119 + */ + private function pathMatches($path, $pattern, $ignoreCase = false) + { + $expr = preg_replace_callback( + '/[\\\\^$.[\\]|()?*+{}\\-\\/]/', + function ($matches) { + switch ($matches[0]) { + case '*': + return '.*'; + case '?': + return '.'; + default: + return '\\' . $matches[0]; + } + }, + $pattern + ); + + $expr = '/' . $expr . '/'; + if ($ignoreCase) { + $expr .= 'i'; + } + + return (bool)preg_match($expr, $path); + } + private function out($color, $serv, $text) { echo $color . ' ' . $serv . ' ' . self::ANSI_OFF . $text . PHP_EOL; @@ -391,13 +437,14 @@ class MalwareScanner private function showHelp() { - echo 'Usage scan.php -d [-e=.php] [--hide-ok] [--hide-whitelist]' . PHP_EOL; - echo ' -d Directory for searching' . PHP_EOL; - echo ' -e=.php Extension' . PHP_EOL; - echo ' --hide-ok Hide OK aka not infected messages' . PHP_EOL; - echo ' --hide-whitelist Hide whitelisted messages' . PHP_EOL; - echo ' --extra-check Adds GoogleBot and htaccess to Scan List' . PHP_EOL; - echo ' --follow-symlink Follow symlinked directories' . PHP_EOL; + echo 'Usage scan.php -d [-i=] [-e=.php] [--hide-ok] [--hide-whitelist]' . PHP_EOL; + echo ' -d Directory for searching' . PHP_EOL; + echo ' -e=.php Extension' . PHP_EOL; + echo ' -i= Directory of file to igonre' . PHP_EOL; + echo ' --hide-ok Hide OK aka not infected messages' . PHP_EOL; + echo ' --hide-whitelist Hide whitelisted messages' . PHP_EOL; + echo ' --extra-check Adds GoogleBot and htaccess to Scan List' . PHP_EOL; + echo ' --follow-symlink Follow symlinked directories' . PHP_EOL; } }