` or `wp aowpcs blocks clear`. * * @package All_in_one_WP_Content_Security */ class AOWPCS_CLI { public static function register() { WP_CLI::add_command('aowpcs', 'AOWPCS_CLI'); } /** * Run a security scan to completion. * * ## OPTIONS * * [--profile=] * : quick or full. * --- * default: full * options: * - quick * - full * --- * * [--category=] * : Only scan one category (core, php, database, filesystem, extensions, integrity, server, traffic). * * [--check=] * : Only run one check (for example php.config). * * [--quiet-progress] * : Do not print progress lines. */ public function scan($args, $assoc) { AOWPCS_Installer::maybe_upgrade(); $profile = isset($assoc['profile']) ? $assoc['profile'] : 'full'; $scope = ''; if (!empty($assoc['category'])) { $profile = 'category'; $scope = $assoc['category']; } elseif (!empty($assoc['check'])) { $profile = 'check'; $scope = $assoc['check']; } $scan_id = AOWPCS_Scan_Runner::start($profile, $scope, 'cli'); if (is_wp_error($scan_id)) { WP_CLI::error($scan_id->get_error_message()); } $quiet = !empty($assoc['quiet-progress']); $progress = AOWPCS_Scan_Runner::run_to_completion($scan_id, function ($p) use ($quiet) { if (!$quiet) { WP_CLI::log(sprintf('%3d%% %d/%d checks %s', $p['percent'], $p['checks_done'], $p['checks_total'], $p['stage'])); } }); if (is_wp_error($progress)) { WP_CLI::error($progress->get_error_message()); } WP_CLI::log(sprintf('Scan #%d %s: %d rules passed, %d with issues, %d could not be checked, %d errors. Score: %s', $progress['id'], $progress['status'], $progress['passed'], $progress['issues'], $progress['unknown'], $progress['errors'], $progress['score'] === null ? '-' : $progress['score'])); if ($progress['status'] === 'completed') { WP_CLI::success('Scan completed.'); } else { WP_CLI::warning('Scan ended early: ' . AOWPCS_Scan_Store::get_scan($scan_id)->status_reason); } } /** * Show the security status. */ public function status($args, $assoc) { $counts = AOWPCS_Scan_Store::severity_counts('open'); $latest = AOWPCS_Scan_Store::latest_scan(); WP_CLI::log(sprintf('Score: %d / 100', AOWPCS_Score::current())); WP_CLI::log(sprintf('Open issues: %d critical, %d high, %d medium, %d low', $counts['critical'], $counts['high'], $counts['medium'], $counts['low'])); WP_CLI::log(sprintf('Last scan: %s', $latest ? '#' . $latest->id . ' ' . $latest->status . ' ' . $latest->finished_at . ' UTC' : 'never')); WP_CLI::log(sprintf('Active blocks: %d', AOWPCS_Block_List::count_active())); WP_CLI::log(sprintf('Safe mode: %s', AOWPCS_Plugin::safe_mode() ? 'ON (blocking disabled)' : 'off')); } /** * List findings. * * ## OPTIONS * * [--state=] * : open, accepted, resolved, active (open + accepted) or all. * --- * default: active * --- * * [--format=] * : table, json, csv. * --- * default: table * --- */ public function findings($args, $assoc) { $rows = AOWPCS_Scan_Store::findings(array('state' => $assoc['state'], 'limit' => 1000)); $items = array(); foreach ($rows as $row) { $items[] = array('id' => $row->id, 'severity' => $row->severity, 'state' => $row->state, 'rule' => $row->check_id, 'subject' => $row->subject, 'title' => $row->title); } WP_CLI\Utils\format_items($assoc['format'], $items, array('id', 'severity', 'state', 'rule', 'subject', 'title')); } /** * Manage IP blocks. * * ## OPTIONS * * * : list or clear. */ public function blocks($args, $assoc) { if ($args[0] === 'clear') { AOWPCS_Block_List::clear_all(); WP_CLI::success('All blocks removed.'); return; } $items = array(); foreach (AOWPCS_Block_List::rows(500) as $row) { $items[] = array('ip' => $row->ip, 'scope' => $row->scope, 'reason' => $row->reason, 'expires' => $row->expires_at ? $row->expires_at . ' UTC' : 'never'); } WP_CLI\Utils\format_items('table', $items, array('ip', 'scope', 'reason', 'expires')); } /** * Remove the block of an IP address. * * ## OPTIONS * * * : The address (or range) to unblock. */ public function unblock($args, $assoc) { if (AOWPCS_Block_List::unblock($args[0])) { WP_CLI::success('Unblocked ' . $args[0]); } else { WP_CLI::warning('No block found for ' . $args[0]); } } /** * Download the vulnerability data now. */ public function vuln_sync($args, $assoc) { $result = AOWPCS_Vulnerability_Sync::start(true); if (is_wp_error($result)) { WP_CLI::error($result->get_error_message()); } do { $state = AOWPCS_Vulnerability_Sync::step(); WP_CLI::log(sprintf('%s offset %s, %d matching records', $state['status'], size_format((int) $state['offset']), (int) $state['matched'])); } while (in_array($state['status'], array('downloading', 'parsing'), true)); if ($state['status'] === 'error') { WP_CLI::error($state['error']); } WP_CLI::success('Vulnerability data updated.'); } /** * Read or change a security suite setting. * * ## OPTIONS * * * : Setting name. * * [] * : New value (true/false for switches). */ public function setting($args, $assoc) { $schema = AOWPCS_Settings::schema(); $key = $args[0]; if (!isset($schema[$key])) { WP_CLI::error('Unknown setting. Known: ' . implode(', ', array_keys($schema))); } if (!isset($args[1])) { $value = AOWPCS_Settings::get($key); WP_CLI::log($schema[$key][0] === 'secret' ? ($value !== '' ? '(set)' : '(empty)') : (is_array($value) ? implode(',', $value) : var_export($value, true))); return; } $value = $args[1]; if ($schema[$key][0] === 'bool') { $value = in_array(strtolower($value), array('1', 'true', 'on', 'yes'), true); } elseif ($schema[$key][0] === 'list') { $value = array_filter(explode(',', $value)); } AOWPCS_Settings::update(array($key => $value)); if ($key === 'xmlrpc_mode') { AOWPCS_Xmlrpc_Guard::apply_mode($value); } if (strpos($key, 'header') === 0) { AOWPCS_Header_Manager::sync_htaccess(); } WP_CLI::success($key . ' updated.'); } /** * Explain the emergency recovery options. * * @subcommand safe-mode */ public function safe_mode($args, $assoc) { WP_CLI::log('To disable all blocking and protective actions, add this line to wp-config.php above "That\'s all, stop editing!":'); WP_CLI::log(" define('AOWPCS_SAFE_MODE', true);"); WP_CLI::log('Or remove all blocks: wp aowpcs blocks clear'); WP_CLI::log('Safe mode is currently ' . (AOWPCS_Plugin::safe_mode() ? 'ON' : 'off') . '.'); } }