2016-05-05 07:35:23 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 Gabor Gyorvari
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class MalwareScanner
|
|
|
|
|
{
|
|
|
|
|
const ANSI_GREEN = "\033[32m";
|
|
|
|
|
const ANSI_RED = "\033[31m";
|
|
|
|
|
const ANSI_YELLOW = "\033[33m";
|
|
|
|
|
const ANSI_OFF = "\033[0m";
|
|
|
|
|
|
|
|
|
|
private $extension = '.php';
|
|
|
|
|
private $flagHideOk = false;
|
|
|
|
|
private $flagHideWhitelist = false;
|
2016-12-27 17:51:39 +01:00
|
|
|
private $extraCheck = false;
|
2016-05-05 07:35:23 +02:00
|
|
|
private $whitelist = array();
|
2017-01-11 19:10:59 +01:00
|
|
|
private $ignore = array();
|
2016-05-05 07:35:23 +02:00
|
|
|
private $stat = array(
|
|
|
|
|
'directories' => 0,
|
|
|
|
|
'files_scanned' => 0,
|
|
|
|
|
'files_infected' => 0,
|
|
|
|
|
);
|
2016-12-27 17:51:39 +01:00
|
|
|
private $followSymlink = false;
|
2016-05-05 07:35:23 +02:00
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
2017-01-11 19:10:59 +01:00
|
|
|
$options = getopt('hd:e::i::', array('hide-ok', 'hide-whitelist', 'extra-check', 'follow-symlink'));
|
2016-05-05 07:35:23 +02:00
|
|
|
if (isset($options['h'])) {
|
|
|
|
|
$this->showHelp();
|
|
|
|
|
} else {
|
|
|
|
|
if (isset($options['e'])) {
|
|
|
|
|
$ext = $options['e'];
|
|
|
|
|
if ($ext[0] != '.') {
|
|
|
|
|
$ext = '.' . $ext;
|
|
|
|
|
}
|
2016-12-29 08:31:27 +01:00
|
|
|
$this->extension = strtolower($ext);
|
2016-05-05 07:35:23 +02:00
|
|
|
}
|
2017-01-11 19:10:59 +01:00
|
|
|
if (isset($options['i'])) {
|
|
|
|
|
$this->ignore = is_array($options['i']) ? $options['i'] : array($options['i']);
|
|
|
|
|
}
|
2016-05-05 07:35:23 +02:00
|
|
|
if (isset($options['hide-ok'])) {
|
|
|
|
|
$this->flagHideOk = true;
|
|
|
|
|
}
|
|
|
|
|
if (isset($options['hide-whitelist'])) {
|
|
|
|
|
$this->flagHideWhitelist = true;
|
|
|
|
|
}
|
2016-12-10 22:20:57 +11:00
|
|
|
if (isset($options['extra-check'])) {
|
2016-12-27 17:51:39 +01:00
|
|
|
$this->extraCheck = true;
|
|
|
|
|
}
|
|
|
|
|
if (isset($options['follow-symlink'])) {
|
|
|
|
|
$this->followSymlink = true;
|
2016-12-10 22:20:57 +11:00
|
|
|
}
|
2016-05-05 07:35:23 +02:00
|
|
|
if (isset($options['d'])) {
|
|
|
|
|
$this->run($options['d']);
|
|
|
|
|
} else {
|
|
|
|
|
$this->out(MalwareScanner::ANSI_RED, 'ER', 'No directory specified');
|
|
|
|
|
$this->showHelp();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function run($dir)
|
|
|
|
|
{
|
|
|
|
|
$dir = rtrim($dir, '/');
|
|
|
|
|
if (!is_dir($dir)) {
|
|
|
|
|
$this->out(self::ANSI_RED, 'ER', 'Specified path is not a directory: ' . $dir);
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
$start = time();
|
|
|
|
|
$this->loadWhitelist();
|
|
|
|
|
$this->process($dir . '/');
|
|
|
|
|
$this->report($start, $dir . '/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function loadWhitelist()
|
|
|
|
|
{
|
|
|
|
|
if (!is_file(__DIR__ . '/whitelist.txt')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$fp = fopen(__DIR__ . '/whitelist.txt', 'r');
|
|
|
|
|
while (!feof($fp)) {
|
|
|
|
|
$line = fgets($fp);
|
|
|
|
|
$this->whitelist[] = substr($line, 0, 32);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function inWhitelist($hash)
|
|
|
|
|
{
|
|
|
|
|
return in_array($hash, $this->whitelist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function process($dir)
|
|
|
|
|
{
|
|
|
|
|
$dh = opendir($dir);
|
|
|
|
|
if (!$dh) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->stat['directories']++;
|
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
|
if ($file == '.' || $file == '..') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-01-11 19:10:59 +01:00
|
|
|
if ($this->isIgnored($dir . $file)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-12-27 17:51:39 +01:00
|
|
|
if (!$this->followSymlink && is_link($dir . $file)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-05-05 07:35:23 +02:00
|
|
|
if (is_dir($dir . $file)) {
|
|
|
|
|
$this->process($dir . $file . '/');
|
|
|
|
|
} elseif (is_file($dir . $file)) {
|
2016-12-29 08:31:27 +01:00
|
|
|
$ext = strtolower(substr($file, strrpos($file, '.')));
|
2016-05-05 07:35:23 +02:00
|
|
|
if ($ext == $this->extension) {
|
|
|
|
|
$this->scan($dir . $file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
closedir($dh);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function report($start, $dir)
|
|
|
|
|
{
|
|
|
|
|
$end = time();
|
|
|
|
|
echo 'Start time: ' . strftime('%Y-%m-%d %H:%M:%S', $start) . PHP_EOL;
|
|
|
|
|
echo 'End time: ' . strftime('%Y-%m-%d %H:%M:%S', $end) . PHP_EOL;
|
|
|
|
|
echo 'Total execution time: ' . ($end - $start) . PHP_EOL;
|
|
|
|
|
echo 'Base directory: ' . $dir . PHP_EOL;
|
|
|
|
|
echo 'Total directories scanned: ' . $this->stat['directories'] . PHP_EOL;
|
|
|
|
|
echo 'Total files scanned: ' . $this->stat['files_scanned'] . PHP_EOL;
|
|
|
|
|
echo 'Total malware identified: ' . $this->stat['files_infected'] . PHP_EOL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-22 13:56:09 +01:00
|
|
|
private function loadPatterns($file)
|
|
|
|
|
{
|
|
|
|
|
$list = array();
|
|
|
|
|
if (is_readable($file)) {
|
|
|
|
|
foreach (file($file) as $pattern) {
|
|
|
|
|
$list[] = trim($pattern);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $list;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 07:35:23 +02:00
|
|
|
private function scan($path)
|
|
|
|
|
{
|
|
|
|
|
$this->stat['files_scanned']++;
|
|
|
|
|
|
|
|
|
|
$fileContent = file_get_contents($path);
|
|
|
|
|
$found = false;
|
2017-02-22 13:56:09 +01:00
|
|
|
$toSearch = '';
|
|
|
|
|
$patterns = $this->loadPatterns(dirname(__FILE__) . '/patterns_raw.txt');
|
2016-12-27 17:51:39 +01:00
|
|
|
if ($this->extraCheck) {
|
2016-12-27 17:45:47 +01:00
|
|
|
array_push($patterns, "googleBot", "htaccess");
|
|
|
|
|
}
|
2016-05-05 07:35:23 +02:00
|
|
|
foreach ($patterns as $toSearch) {
|
2017-07-25 23:55:37 -06:00
|
|
|
if (!$toSearch) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ($toSearch[0] === '#') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-07-26 05:00:04 -06:00
|
|
|
if (strpos($fileContent, $toSearch) !== FALSE){
|
2016-05-05 07:35:23 +02:00
|
|
|
$found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-26 05:17:53 -06:00
|
|
|
|
|
|
|
|
if (!$found) {
|
|
|
|
|
$patterns = $this->loadPatterns(dirname(__FILE__) . '/patterns_iraw.txt');
|
|
|
|
|
foreach ($patterns as $toSearch) {
|
|
|
|
|
if (!$toSearch) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ($toSearch[0] === '#') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (stripos($fileContent, $toSearch) !== FALSE){
|
|
|
|
|
$found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-05 07:35:23 +02:00
|
|
|
|
|
|
|
|
if (!$found) {
|
2017-02-22 13:56:09 +01:00
|
|
|
$patterns = $this->loadPatterns(dirname(__FILE__) . '/patterns_re.txt');
|
2016-05-05 07:35:23 +02:00
|
|
|
foreach ($patterns as $toSearch) {
|
2017-07-26 03:09:47 -06:00
|
|
|
if (!$toSearch) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ($toSearch[0] === '#') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-05-05 07:35:23 +02:00
|
|
|
if (preg_match('/' . $toSearch . '/is', $fileContent)) {
|
|
|
|
|
$found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$found) {
|
|
|
|
|
if (!$this->flagHideOk) {
|
|
|
|
|
$this->out(self::ANSI_GREEN, 'OK', $path);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// file hash is on whithelist hash then skip
|
|
|
|
|
$hash = md5($fileContent);
|
|
|
|
|
if ($found && $this->inWhitelist($hash)) {
|
|
|
|
|
if (!$this->flagHideWhitelist) {
|
|
|
|
|
$this->out(self::ANSI_YELLOW, 'WL', $path);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($found) {
|
|
|
|
|
$this->stat['files_infected']++;
|
|
|
|
|
$this->out(self::ANSI_RED, 'ER', $path . ' -> ' . $toSearch . ' ' . $hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 19:10:59 +01:00
|
|
|
private function isIgnored($pathname)
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->ignore as $pattern) {
|
|
|
|
|
$match = $this->pathMatches($pathname, $pattern);
|
|
|
|
|
if ($match) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-22 13:56:09 +01:00
|
|
|
// @see http://stackoverflow.com/a/13914119
|
2017-01-11 19:10:59 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 07:35:23 +02:00
|
|
|
private function out($color, $serv, $text)
|
|
|
|
|
{
|
|
|
|
|
echo $color . ' ' . $serv . ' ' . self::ANSI_OFF . $text . PHP_EOL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function showHelp()
|
|
|
|
|
{
|
2017-01-11 19:10:59 +01:00
|
|
|
echo 'Usage scan.php -d <directory> [-i=<directory|file>] [-e=.php] [--hide-ok] [--hide-whitelist]' . PHP_EOL;
|
|
|
|
|
echo ' -d Directory for searching' . PHP_EOL;
|
|
|
|
|
echo ' -e=.php Extension' . PHP_EOL;
|
|
|
|
|
echo ' -i=<directory|file> 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;
|
2016-05-05 07:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new MalwareScanner();
|