WEEK_IN_SECONDS, 'display' => __('Once Weekly', 'all-in-one-wp-content-security')); } return $schedules; } /** * Activation also runs the schema migration (it is idempotent) and restores the .htaccess * blocks this plugin removed when it was last deactivated */ public static function activate($network_wide = false) { self::maybe_upgrade(true); AOWPCS_Htaccess::restore_deactivated_blocks(); self::schedule_events(); } /** * Deactivation removes every .htaccess block this plugin wrote (the content is kept so that a * later activation can restore it) and unschedules the plugin's cron events */ public static function deactivate($network_wide = false) { AOWPCS_Htaccess::remove_blocks_for_deactivation(); foreach (array('aowpcs_cron_maintenance', 'aowpcs_cron_scheduled_scan', 'aowpcs_cron_digest', 'aowpcs_cron_vuln_sync', 'aowpcs_scan_step', 'aowpcs_vuln_sync_step', 'aowpcs_cron_verify_crawlers') as $hook) { wp_clear_scheduled_hook($hook); } AOWPCS_Plugin::delete_option('aowpcs_scan_lock'); AOWPCS_Plugin::delete_option('aowpcs_step_lock'); } public static function schedule_events() { if (!wp_next_scheduled('aowpcs_cron_maintenance')) { wp_schedule_event(time() + HOUR_IN_SECONDS, 'daily', 'aowpcs_cron_maintenance'); } AOWPCS_Scan_Runner::reschedule(); AOWPCS_Notifier::reschedule(); AOWPCS_Vulnerability_Sync::reschedule(); } public static function maybe_upgrade($force = false) { $installed = (int) AOWPCS_Plugin::get_option(self::VERSION_OPTION, 0); if (!$force && $installed >= AOWPCS_DB_VERSION) { return; } // Only one request performs the upgrade $lock = 'aowpcs_upgrade_lock'; if (!add_option($lock, time(), '', 'no')) { if ((int) get_option($lock) > time() - 300) { return; } update_option($lock, time(), false); } try { self::create_tables(); if ($installed < 1) { self::migrate_from_v2(); } AOWPCS_Plugin::update_option(self::VERSION_OPTION, AOWPCS_DB_VERSION, true); self::schedule_events(); } finally { delete_option($lock); } } /** * Create or update the plugin tables with dbDelta (one field per line, two spaces after PRIMARY KEY) */ public static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $tables = array(); $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('scans') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, profile varchar(20) NOT NULL DEFAULT 'full', scope varchar(191) NOT NULL DEFAULT '', trigger_type varchar(20) NOT NULL DEFAULT 'manual', status varchar(20) NOT NULL DEFAULT 'running', status_reason varchar(255) NOT NULL DEFAULT '', cancel_requested tinyint(1) NOT NULL DEFAULT 0, created_by bigint(20) unsigned NOT NULL DEFAULT 0, started_at datetime NOT NULL, finished_at datetime DEFAULT NULL, heartbeat_at datetime DEFAULT NULL, checks_total int(10) unsigned NOT NULL DEFAULT 0, checks_done int(10) unsigned NOT NULL DEFAULT 0, rules_passed int(10) unsigned NOT NULL DEFAULT 0, rules_failed int(10) unsigned NOT NULL DEFAULT 0, rules_unknown int(10) unsigned NOT NULL DEFAULT 0, rules_error int(10) unsigned NOT NULL DEFAULT 0, count_critical int(10) unsigned NOT NULL DEFAULT 0, count_high int(10) unsigned NOT NULL DEFAULT 0, count_medium int(10) unsigned NOT NULL DEFAULT 0, count_low int(10) unsigned NOT NULL DEFAULT 0, score smallint(5) DEFAULT NULL, coverage tinyint(3) unsigned DEFAULT NULL, cursor_data longtext, PRIMARY KEY (id), KEY status (status), KEY started_at (started_at) ) $charset;"; $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('findings') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, fingerprint char(40) NOT NULL, check_id varchar(64) NOT NULL, category varchar(20) NOT NULL DEFAULT '', severity varchar(10) NOT NULL DEFAULT 'low', kind varchar(20) NOT NULL DEFAULT 'misconfiguration', confidence varchar(12) NOT NULL DEFAULT 'confirmed', state varchar(12) NOT NULL DEFAULT 'open', subject varchar(255) NOT NULL DEFAULT '', title varchar(255) NOT NULL DEFAULT '', data longtext, evidence_hash char(40) NOT NULL DEFAULT '', first_seen datetime NOT NULL, last_seen datetime NOT NULL, last_verified datetime NOT NULL, resolved_at datetime DEFAULT NULL, accepted_by bigint(20) unsigned NOT NULL DEFAULT 0, accepted_at datetime DEFAULT NULL, accepted_note text, first_scan_id bigint(20) unsigned NOT NULL DEFAULT 0, last_scan_id bigint(20) unsigned NOT NULL DEFAULT 0, notified_hash char(40) NOT NULL DEFAULT '', PRIMARY KEY (id), UNIQUE KEY fingerprint (fingerprint), KEY state_severity (state,severity), KEY check_id (check_id), KEY category (category) ) $charset;"; $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('scan_results') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, scan_id bigint(20) unsigned NOT NULL, fingerprint char(40) NOT NULL, check_id varchar(64) NOT NULL, status varchar(10) NOT NULL, severity varchar(10) NOT NULL DEFAULT '', subject varchar(255) NOT NULL DEFAULT '', summary varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (id), KEY scan_id (scan_id), KEY fingerprint (fingerprint,scan_id), KEY check_id (check_id,scan_id) ) $charset;"; $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('events') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, created_at datetime NOT NULL, event_type varchar(40) NOT NULL, category varchar(20) NOT NULL DEFAULT '', severity varchar(10) NOT NULL DEFAULT 'low', ip varchar(45) NOT NULL DEFAULT '', method varchar(10) NOT NULL DEFAULT '', path varchar(255) NOT NULL DEFAULT '', status_code smallint(5) unsigned NOT NULL DEFAULT 0, user_id bigint(20) unsigned NOT NULL DEFAULT 0, rule varchar(64) NOT NULL DEFAULT '', action_taken varchar(20) NOT NULL DEFAULT 'logged', classification varchar(20) NOT NULL DEFAULT '', user_agent varchar(255) NOT NULL DEFAULT '', message varchar(255) NOT NULL DEFAULT '', data text, PRIMARY KEY (id), KEY created_at (created_at), KEY event_type (event_type,created_at), KEY ip (ip), KEY severity (severity), KEY category (category,created_at) ) $charset;"; $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('counters') . " ( bucket datetime NOT NULL, metric varchar(32) NOT NULL, dim varchar(191) NOT NULL DEFAULT '', hits int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (bucket,metric,dim), KEY metric (metric,bucket) ) $charset;"; $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('rate') . " ( rkey char(40) NOT NULL, window_start int(10) unsigned NOT NULL, hits int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (rkey), KEY window_start (window_start) ) $charset;"; $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('blocks') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, ip varchar(64) NOT NULL, scope varchar(10) NOT NULL DEFAULT 'login', reason varchar(255) NOT NULL DEFAULT '', rule varchar(64) NOT NULL DEFAULT '', created_at datetime NOT NULL, expires_at datetime DEFAULT NULL, created_by bigint(20) unsigned NOT NULL DEFAULT 0, hits int(10) unsigned NOT NULL DEFAULT 0, strikes tinyint(3) unsigned NOT NULL DEFAULT 1, PRIMARY KEY (id), UNIQUE KEY ip (ip), KEY expires_at (expires_at) ) $charset;"; $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('vulns') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, provider varchar(20) NOT NULL, vuln_id varchar(64) NOT NULL, software_type varchar(10) NOT NULL, slug varchar(191) NOT NULL, title text, cvss_score decimal(3,1) DEFAULT NULL, cvss_rating varchar(12) NOT NULL DEFAULT '', cve varchar(32) NOT NULL DEFAULT '', informational tinyint(1) NOT NULL DEFAULT 0, affected longtext, patched longtext, refs longtext, published datetime DEFAULT NULL, updated datetime DEFAULT NULL, import_id int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY vuln (provider,vuln_id,software_type,slug), KEY slug (software_type,slug) ) $charset;"; $tables[] = "CREATE TABLE " . AOWPCS_Plugin::table('file_hashes') . " ( path_hash char(40) NOT NULL, path varchar(1024) NOT NULL, scope varchar(191) NOT NULL DEFAULT '', size bigint(20) unsigned NOT NULL DEFAULT 0, mtime int(10) unsigned NOT NULL DEFAULT 0, sha1 char(40) NOT NULL DEFAULT '', first_seen datetime NOT NULL, seen_scan_id bigint(20) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (path_hash), KEY scope (scope) ) $charset;"; foreach ($tables as $sql) { dbDelta($sql); } } /** * First migration (version 2.x → 3.0). Only adds values; nothing is renamed, removed or * written to .htaccess, so existing behaviour is preserved. */ private static function migrate_from_v2() { $legacy = get_option(AOWPCS_Settings::LEGACY_OPTION); $existing_install = is_array($legacy) && !empty($legacy); if ($existing_install) { // Version 2.0 only stored keys that were switched on at least once foreach (array('block_selection', 'block_image_dragging', 'block_right_clicking', 'block_hacking_website', 'enhanced_web_security', 'disable_xmlrpc') as $key) { if (!isset($legacy[$key])) { $legacy[$key] = 'off'; } } // Version 2.x applied content protection to everyone, including administrators if (!isset($legacy['exempt_editors'])) { $legacy['exempt_editors'] = 'off'; } update_option(AOWPCS_Settings::LEGACY_OPTION, $legacy); } else { // New installations exempt editors and administrators. Stored explicitly, so later // writes to this option (XML-RPC mode, header fix) cannot change the default. update_option(AOWPCS_Settings::LEGACY_OPTION, array('exempt_editors' => 'on')); } $stored = AOWPCS_Plugin::get_option(AOWPCS_Settings::OPTION, false); if ($stored === false) { $settings = AOWPCS_Settings::defaults(); if ($existing_install) { // "Disable XML-RPC" becomes the server-level mode of the XML-RPC control if (isset($legacy['disable_xmlrpc']) && $legacy['disable_xmlrpc'] === 'on') { $settings['xmlrpc_mode'] = 'server'; } } AOWPCS_Plugin::update_option(AOWPCS_Settings::OPTION, $settings, true); AOWPCS_Settings::flush(); } if (AOWPCS_Settings::state('installed_at') === null) { AOWPCS_Settings::set_state('installed_at', AOWPCS_Plugin::now()); AOWPCS_Settings::set_state('upgraded_from', $existing_install ? '2.x' : ''); } } /** * Remove every table and option (uninstall with "delete data" enabled) */ public static function drop_everything() { global $wpdb; foreach (array('scans', 'findings', 'scan_results', 'events', 'counters', 'rate', 'blocks', 'vulns', 'file_hashes') as $table) { $wpdb->query('DROP TABLE IF EXISTS ' . AOWPCS_Plugin::table($table)); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- table name from a fixed list } foreach (array(AOWPCS_Settings::OPTION, AOWPCS_Settings::STATE_OPTION, self::VERSION_OPTION, 'aowpcs_active_blocks', 'aowpcs_htaccess_backups', 'aowpcs_vuln_sync', 'aowpcs_crawler_ranges', 'aowpcs_scan_lock', 'aowpcs_step_lock', 'aowpcs_quarantine') as $option) { AOWPCS_Plugin::delete_option($option); delete_option($option); } delete_option(AOWPCS_Settings::LEGACY_OPTION); } }