Skip to content

Commit

Permalink
feat: add disable colors flag and fix update and header not showed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Oct 8, 2020
1 parent b9ff9f4 commit 6507785
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 44 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# AMWSCAN - PHP Antimalware Scanner

**Version:** 0.6.0.108 beta
**Version:** 0.6.0.109 beta

**Github:** https://github.com/marcocesarato/PHP-Antimalware-Scanner

Expand Down Expand Up @@ -139,13 +139,16 @@ Flags:
this is recommended for WordPress or others platforms
-s --only-signatures - Check only virus signatures (like exploit but more specific)
-h --help - Show the available flags and arguments
-l --log="" - Write a log file on 'scanner.log' or the specified filepath
-l --log="" - Write a log file on 'index.php.log' or the specified filepath
-r --report - Report scan only mode without check and remove malware (like --auto-skip).
It also write a report with all malware paths found
-b --backup - Make a backup of every touched files
-u --update - Update scanner to last version
-u --update - Update index.php to last version
-v --version - Get version number
--silent - No output and prompt
--disable-colors - Disable CLI colors
--limit="" - Set file mapping limit
--offset="" - Set file mapping offset
Expand All @@ -154,24 +157,22 @@ Flags:
Wildcards are enabled ex. /path/*/cache or /path/*.log
--filter-path/s="" - Filter path/s, for multiple value separate with comma
Wildcards are enabled ex. /path/*/htdocs or /path/*.php
--path-backups="" - Set backups path directory (default ./scanner-backups)
Is recommended put files outside the public document path
--path-logs="" - Set quarantine log file (default ./scanner.log)
--path-quarantine="" - Set quarantine path directory (default ./scanner-quarantine)
Is recommended put files outside the public document path
--path-report="" - Set report log file (default ./scanner-report.log)
--path-whitelist="" - Set whitelist file (default ./scanner-whitelist.json)
--exploits="" - Filter exploits
--functions="" - Define functions to search
--whitelist-only-path - Check on whitelist only file path and not line number
--list - Get default exploit and functions list
--list-exploits - Get default exploits list
--list-functions - Get default functions lists
--silent - No output and prompt
--definitions-list - Get default definitions exploit and functions list
--definitions-exploits - Get default definitions exploits list
--definitions-functions - Get default definitions functions lists
--auto-clean - Auto clean code (without confirmation, use with caution)
--auto-clean-line - Auto clean line code (without confirmation, use with caution)
Expand Down
Binary file modified dist/scanner
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.0.108
0.6.0.109
51 changes: 36 additions & 15 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public static function eol($n)
*/
public static function header()
{
if (!Scanner::isCli()) {
return;
}

$version = Scanner::getVersion();
self::newLine(2);
$header = <<<EOD
Expand Down Expand Up @@ -111,6 +115,14 @@ public static function header()
$title = self::title('');
self::display($title, 'black', 'green');
self::newLine(2);
$version = file_get_contents('https://raw.githubusercontent.com/marcocesarato/PHP-Antimalware-Scanner/master/dist/version');
if (!empty($version)) {
if (version_compare(Scanner::$version, $version, '<')) {
Console::write('New version', 'green');
Console::write(' ' . $version . ' ', 'green');
Console::writeLine('of the scanner available!', 2, 'green');
}
}
}

/**
Expand Down Expand Up @@ -332,11 +344,11 @@ public static function writeLine($string, $eol = 1, $foreground_color = 'white',
public static function write($string, $foreground_color = 'white', $background_color = null, $log = null, $escape = true)
{
$return_string = $string;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
if (!Scanner::isColorEnabled()) {
$foreground_color = null;
$background_color = null;
}
if (Scanner::isLogsEnabled() && $log === null) {
if (Scanner::isLogEnabled() && $log === null) {
$log = true;
}
if ($escape) {
Expand All @@ -352,10 +364,10 @@ public static function write($string, $foreground_color = 'white', $background_c
$colored_string .= $return_string . "\033[0m";

if (!Scanner::isSilentMode()) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo $return_string;
} else {
if (Scanner::isColorEnabled()) {
echo $colored_string;
} else {
echo $return_string;
}
}

Expand All @@ -375,7 +387,7 @@ public static function write($string, $foreground_color = 'white', $background_c
*/
public static function read($string, $foreground_color = 'white', $background_color = null)
{
if (stripos(PHP_OS, 'WIN') === 0) {
if (!Scanner::isColorEnabled()) {
$foreground_color = null;
$background_color = null;
}
Expand All @@ -391,14 +403,14 @@ public static function read($string, $foreground_color = 'white', $background_co
$read = null;

if (!Scanner::isSilentMode()) {
if (stripos(PHP_OS, 'WIN') === 0) {
echo Scanner::getName() . ' > ' . trim($string) . ' ';
} else {
if (Scanner::isColorEnabled()) {
echo Scanner::getName() . ' > ' . trim($colored_string) . ' ';
} else {
echo Scanner::getName() . ' > ' . trim($string) . ' ';
}
}

if (stripos(PHP_OS, 'WIN') === 0) {
if (Scanner::isWindows()) {
$read = stream_get_line(STDIN, 1024, PHP_EOL);
} else {
$in = array(STDIN);
Expand Down Expand Up @@ -491,6 +503,10 @@ public static function escape($string)
*/
public static function helplist($type = null)
{
if (!Scanner::isCli()) {
return;
}

$list = '';
if (empty($type) || $type === 'exploits') {
$exploit_list = implode(self::eol(1) . '- ', array_keys(Definitions::$EXPLOITS));
Expand All @@ -512,6 +528,10 @@ public static function helplist($type = null)
*/
public static function helper()
{
if (!Scanner::isCli()) {
return;
}

self::displayTitle('Help', 'black', 'cyan');
$dir = Scanner::currentDirectory();
$help = <<<EOD
Expand Down Expand Up @@ -539,6 +559,9 @@ public static function helper()
-u --update - Update index.php to last version
-v --version - Get version number
--silent - No output and prompt
--disable-colors - Disable CLI colors
--limit="" - Set file mapping limit
--offset="" - Set file mapping offset
Expand All @@ -560,11 +583,9 @@ public static function helper()
--functions="" - Define functions to search
--whitelist-only-path - Check on whitelist only file path and not line number
--list - Get default exploit and functions list
--list-exploits - Get default exploits list
--list-functions - Get default functions lists
--silent - No output and prompt
--definitions-list - Get default definitions exploit and functions list
--definitions-exploits - Get default definitions exploits list
--definitions-functions - Get default definitions functions lists
--auto-clean - Auto clean code (without confirmation, use with caution)
--auto-clean-line - Auto clean line code (without confirmation, use with caution)
Expand Down
83 changes: 65 additions & 18 deletions src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Scanner
*
* @var string
*/
public static $version = '0.6.0.107';
public static $version = '0.6.0.109';

/**
* Root path.
Expand Down Expand Up @@ -257,10 +257,10 @@ public function run($args = null)
$this->interrupt = false;

try {
// Initialize arguments
$this->arguments($args);
// Print header
Console::header();
// Initialize arguments
$this->arguments($args);
// Initialize
$this->init();
// Initialize modes
Expand Down Expand Up @@ -327,9 +327,9 @@ private function arguments($args = null)
self::$argv->addFlag('only-signatures', array('alias' => '-s', 'default' => false));
self::$argv->addFlag('only-exploits', array('alias' => '-e', 'default' => false));
self::$argv->addFlag('only-functions', array('alias' => '-f', 'default' => false));
self::$argv->addFlag('list', array('default' => false));
self::$argv->addFlag('list-exploits', array('default' => false));
self::$argv->addFlag('list-functions', array('default' => false));
self::$argv->addFlag('definitions-list', array('default' => false));
self::$argv->addFlag('definitions-exploits', array('default' => false));
self::$argv->addFlag('definitions-functions', array('default' => false));
self::$argv->addFlag('exploits', array('default' => false, 'has_value' => true));
self::$argv->addFlag('functions', array('default' => false, 'has_value' => true));
self::$argv->addFlag('whitelist-only-path', array('default' => false));
Expand All @@ -349,6 +349,7 @@ private function arguments($args = null)
self::$argv->addFlag('path-quarantine', array('default' => false, 'has_value' => true));
self::$argv->addFlag('path-logs', array('default' => false, 'has_value' => true));
self::$argv->addFlag('path-report', array('default' => false, 'has_value' => true));
self::$argv->addFlag('disable-colors', array('default' => false));
self::$argv->addArgument('path', array('var_args' => true, 'default' => ''));
self::$argv->parse($args);

Expand All @@ -364,18 +365,18 @@ private function arguments($args = null)
}

// List exploits
if (isset(self::$argv['list']) && self::$argv['list']) {
if (isset(self::$argv['definitions-list']) && self::$argv['definitions-list']) {
Console::helplist();
$this->interrupt();
}

// List exploits
if (isset(self::$argv['list-exploits']) && self::$argv['list-exploits']) {
if (isset(self::$argv['definitions-exploits']) && self::$argv['definitions-exploits']) {
Console::helplist('exploits');
}

// List functions
if (isset(self::$argv['list-functions']) && self::$argv['list-functions']) {
if (isset(self::$argv['definitions-functions']) && self::$argv['definitions-functions']) {
Console::helplist('functions');
}

Expand All @@ -400,6 +401,15 @@ private function arguments($args = null)
self::setSilentMode(true);
}

// Colors
if (isset(self::$argv['disable-colors']) && self::$argv['disable-colors']) {
self::setColors(false);
} else {
if (function_exists('ncurses_has_colors')) {
self::setColors(ncurses_has_colors());
}
}

// Max filesize
if (isset(self::$argv['max-filesize']) && is_numeric(self::$argv['max-filesize'])) {
self::setMaxFilesize(self::$argv['max-filesize']);
Expand Down Expand Up @@ -566,7 +576,7 @@ private function arguments($args = null)
}

// Check if logs and scan at the same time
if (self::isLogsEnabled() && self::isReportMode()) {
if (self::isLogEnabled() && self::isReportMode()) {
self::disableLogs();
}

Expand Down Expand Up @@ -1200,12 +1210,12 @@ public function update()
if (version_compare(self::$version, $version, '<')) {
Console::write('New version');
Console::write(' ' . $version . ' ');
Console::writeLine('of the index.php available!', 2);
Console::writeLine('of the scanner available!', 2);
$confirm = Console::read('You sure you want update the index.php to the last version [y|N]? ', 'purple');
Console::writeBreak();
if (strtolower($confirm) === 'y') {
$new_version = file_get_contents('https://raw.githubusercontent.com/marcocesarato/PHP-Antimalware-Scanner/master/dist/scanner');
file_put_contents(__FILE__, $new_version);
file_put_contents(self::currentFilename(), $new_version);
Console::write('Updated to last version');
Console::write(' (' . self::$version . ' => ' . $version . ') ');
Console::writeLine('with SUCCESS!', 2);
Expand Down Expand Up @@ -1250,15 +1260,24 @@ private static function convertToBytes($from)
* @return string|string[]|null
*/
public static function currentDirectory()
{
return dirname(self::currentFilename());
}

/**
* Return real current filename.
*
* @return string|string[]|null
*/
public static function currentFilename()
{
if (method_exists('Phar', 'running')) {
return dirname(Phar::running(false));
return Phar::running(false);
}
$string = __DIR__;
$string = pathinfo($string);
$dir = parse_url($string['dirname']);
$string = pathinfo(__FILE__);
$dir = parse_url($string['dirname'] . '/' . $string['basename']);

return realpath($dir['host'] . ':/' . $dir['path']);
return realpath($dir['path']);
}

/**
Expand All @@ -1278,6 +1297,16 @@ public static function isCli()
);
}

/**
* Is windows environment.
*
* @return bool
*/
public static function isWindows()
{
return stripos(PHP_OS, 'WIN') === 0;
}

/**
* Interrupt.
*/
Expand All @@ -1292,7 +1321,7 @@ protected function interrupt()
/**
* @return bool
*/
public static function isLogsEnabled()
public static function isLogEnabled()
{
return isset(self::$settings['log']) ? self::$settings['log'] : false;
}
Expand Down Expand Up @@ -1361,6 +1390,24 @@ public static function isSilentMode()
return isset(self::$settings['silent']) ? self::$settings['silent'] : false;
}

/**
* @return self
*/
public static function setColors($mode = true)
{
self::$settings['colors'] = $mode;

return new static();
}

/**
* @return bool
*/
public static function isColorEnabled()
{
return isset(self::$settings['colors']) ? self::$settings['colors'] : true;
}

/**
* @return self
*/
Expand Down

0 comments on commit 6507785

Please sign in to comment.