new AOWPCS_Provider_Wordfence())); } public static function provider() { $id = AOWPCS_Settings::get('vuln_provider'); $providers = self::providers(); return isset($providers[$id]) ? $providers[$id] : null; } public static function state() { $state = get_site_option(self::STATE, array()); return array_merge(array('status' => 'idle', 'file' => '', 'offset' => 0, 'top' => null, 'import_id' => 0, 'started' => '', 'last_success' => '', 'last_attempt' => '', 'matched' => 0, 'error' => '', 'validators' => array(), 'slugs_hash' => '', 'last_download' => 0, 'blocked_until' => 0, 'provider' => ''), is_array($state) ? $state : array()); } private static function save($state) { update_site_option(self::STATE, $state); } public static function reschedule() { $provider = self::provider(); if ($provider && $provider->is_configured()) { if (!wp_next_scheduled('aowpcs_cron_vuln_sync')) { wp_schedule_event(time() + 10 * MINUTE_IN_SECONDS, 'daily', 'aowpcs_cron_vuln_sync'); } } else { wp_clear_scheduled_hook('aowpcs_cron_vuln_sync'); } } public static function scheduled() { $result = self::start(false); if (!is_wp_error($result)) { self::cron_step(); } } /** * Installed software changed: refresh the data soon if new slugs need it */ public static function software_changed() { $state = self::state(); $provider = self::provider(); if (!$provider || !$provider->is_configured() || $state['slugs_hash'] === '' || $state['slugs_hash'] === AOWPCS_Inventory::slugs_hash()) { return; } $when = max(time() + 10 * MINUTE_IN_SECONDS, (int) $state['blocked_until'], (int) $state['last_download'] + self::MIN_INTERVAL); wp_schedule_single_event($when, 'aowpcs_cron_vuln_sync'); } /** * Begin a synchronisation. $manual relaxes the interval to one hour. */ public static function start($manual = true) { $provider = self::provider(); if (!$provider) { return new WP_Error('aowpcs_no_provider', __('No vulnerability data source is selected in Settings.', 'all-in-one-wp-content-security')); } if (!$provider->is_configured()) { /* translators: %s: label */ return new WP_Error('aowpcs_provider_config', sprintf(__('%s needs an API key (Settings → Vulnerability data).', 'all-in-one-wp-content-security'), $provider->label())); } $state = self::state(); if (in_array($state['status'], array('downloading', 'parsing'), true) && strtotime($state['last_attempt'] . ' UTC') > time() - 30 * MINUTE_IN_SECONDS) { return new WP_Error('aowpcs_sync_running', __('An update of the vulnerability data is already in progress.', 'all-in-one-wp-content-security')); } // Respect the provider: never more than one download per hour (manual) or six hours (scheduled) $allowed = max((int) $state['blocked_until'], (int) $state['last_download'] + ($manual ? HOUR_IN_SECONDS : self::MIN_INTERVAL)); if (time() < $allowed) { /* translators: %s: value */ return new WP_Error('aowpcs_sync_wait', sprintf(__('The next download is allowed in %s (provider rate limits).', 'all-in-one-wp-content-security'), human_time_diff(time(), $allowed))); } self::cleanup_file($state); $state['status'] = 'downloading'; $state['provider'] = $provider->id(); $state['started'] = AOWPCS_Plugin::now(); $state['last_attempt'] = AOWPCS_Plugin::now(); $state['error'] = ''; self::save($state); return true; } public static function cron_step() { $until = microtime(true) + 40; do { $state = self::step(); if (is_wp_error($state)) { return; } } while (in_array($state['status'], array('downloading', 'parsing'), true) && microtime(true) < $until); if (in_array($state['status'], array('downloading', 'parsing'), true)) { wp_schedule_single_event(time() + 5, 'aowpcs_vuln_sync_step'); } } /** * Advance the synchronisation; returns the state */ public static function step() { $state = self::state(); $provider = self::provider(); if (!$provider || $state['provider'] !== $provider->id()) { return $state; } @set_time_limit(300); if ($state['status'] === 'downloading') { $dir = get_temp_dir(); if (function_exists('disk_free_space')) { $free = @disk_free_space($dir); if ($free !== false && $free < 600 * MB_IN_BYTES) { /* translators: %s: dir */ return self::fail($state, sprintf(__('Not enough free disk space in %s to download the vulnerability data (about 600 MB needed).', 'all-in-one-wp-content-security'), $dir), HOUR_IN_SECONDS); } } if (!function_exists('wp_tempnam')) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $file = wp_tempnam('aowpcs-vulns'); $validators = ($state['slugs_hash'] === AOWPCS_Inventory::slugs_hash()) ? $state['validators'] : array(); $result = $provider->download($file, $validators); $state['last_download'] = time(); if (is_wp_error($result)) { @unlink($file); $data = $result->get_error_data(); return self::fail($state, $result->get_error_message(), isset($data['retry_after']) ? $data['retry_after'] : HOUR_IN_SECONDS); } if ($result['status'] === 'not_modified') { @unlink($file); $state['status'] = 'idle'; $state['last_success'] = AOWPCS_Plugin::now(); self::save($state); return $state; } $state['validators'] = $result['validators']; $state['file'] = $file; $state['offset'] = 0; $state['top'] = null; $state['import_id'] = time(); $state['matched'] = 0; $state['status'] = 'parsing'; $state['slugs_hash'] = AOWPCS_Inventory::slugs_hash(); self::save($state); return $state; } if ($state['status'] === 'parsing') { if ($state['file'] === '' || !file_exists($state['file'])) { return self::fail($state, __('The downloaded data file disappeared before it was processed; it will be downloaded again.', 'all-in-one-wp-content-security'), 0); } $installed = array(); foreach (AOWPCS_Inventory::slugs() as $type => $slugs) { $installed[$type] = array_flip($slugs); } $matched = 0; $import_id = (int) $state['import_id']; $provider_id = $provider->id(); $result = AOWPCS_Json_Stream::process_file($state['file'], (int) $state['offset'], $state['top'], microtime(true) + 15, function ($raw) use ($provider, $provider_id, $installed, $import_id, &$matched) { foreach ($provider->parse_record($raw, $installed) as $row) { AOWPCS_Vulnerability_Store::save($provider_id, $row, $import_id); $matched++; } }); if (is_wp_error($result)) { self::cleanup_file($state); return self::fail($state, $result->get_error_message(), HOUR_IN_SECONDS); } $state['offset'] = $result['offset']; $state['top'] = $result['top']; $state['matched'] += $matched; $state['last_attempt'] = AOWPCS_Plugin::now(); if ($result['done']) { AOWPCS_Vulnerability_Store::delete_stale($provider_id, $import_id); self::cleanup_file($state); $state['status'] = 'idle'; $state['last_success'] = AOWPCS_Plugin::now(); $state['file'] = ''; /* translators: 1: label, 2: state matched */ AOWPCS_Event_Log::log('vuln_data_updated', array('category' => 'scan', 'severity' => 'low', 'message' => sprintf(__('Vulnerability data updated from %1$s: %2$d records concern installed software.', 'all-in-one-wp-content-security'), $provider->label(), $state['matched']))); self::save($state); // Re-evaluate installed software against the new data $scan = AOWPCS_Scan_Runner::start('check', 'extensions.vulnerabilities', 'vuln_sync'); if (!is_wp_error($scan)) { wp_schedule_single_event(time(), 'aowpcs_scan_step', array($scan, 0)); } return $state; } self::save($state); return $state; } return $state; } private static function fail($state, $message, $retry_after) { $state['status'] = 'error'; $state['error'] = $message; $state['last_attempt'] = AOWPCS_Plugin::now(); if ($retry_after > 0) { $state['blocked_until'] = max((int) $state['blocked_until'], time() + (int) $retry_after); } self::save($state); AOWPCS_Event_Log::log('vuln_data_failed', array('category' => 'scan', 'severity' => 'medium', 'message' => AOWPCS_Redactor::text($message))); return $state; } private static function cleanup_file(&$state) { if (!empty($state['file']) && file_exists($state['file'])) { @unlink($state['file']); } $state['file'] = ''; } } /** * Stored vulnerability records (only those concerning installed software) */ class AOWPCS_Vulnerability_Store { public static function save($provider, $row, $import_id) { global $wpdb; $table = AOWPCS_Plugin::table('vulns'); $values = array( 'provider' => $provider, 'vuln_id' => $row['vuln_id'], 'software_type' => $row['software_type'], 'slug' => $row['slug'], 'title' => $row['title'], 'cvss_score' => $row['cvss_score'], 'cvss_rating' => $row['cvss_rating'], 'cve' => $row['cve'], 'informational' => (int) $row['informational'], 'affected' => wp_json_encode($row['affected']), 'patched' => wp_json_encode($row['patched']), 'refs' => wp_json_encode($row['refs']), 'published' => $row['published'], 'updated' => $row['updated'], 'import_id' => (int) $import_id, ); $existing = $wpdb->get_var($wpdb->prepare("SELECT id FROM $table WHERE provider = %s AND vuln_id = %s AND software_type = %s AND slug = %s", $provider, $row['vuln_id'], $row['software_type'], $row['slug'])); if ($existing) { $wpdb->update($table, $values, array('id' => (int) $existing)); } else { $wpdb->insert($table, $values); } } public static function delete_stale($provider, $import_id) { global $wpdb; $wpdb->query($wpdb->prepare('DELETE FROM ' . AOWPCS_Plugin::table('vulns') . ' WHERE provider = %s AND import_id <> %d', $provider, $import_id)); } public static function for_software($type, $slug) { global $wpdb; $rows = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . AOWPCS_Plugin::table('vulns') . ' WHERE software_type = %s AND slug = %s ORDER BY cvss_score DESC', $type, $slug)); foreach ($rows as $row) { $row->affected = json_decode((string) $row->affected, true) ?: array(); $row->patched = json_decode((string) $row->patched, true) ?: array(); $row->refs = json_decode((string) $row->refs, true) ?: array(); } return $rows; } public static function count() { global $wpdb; return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . AOWPCS_Plugin::table('vulns')); } /** * Severity from a CVSS score (or the provider's rating when no score is given) */ public static function severity($score, $rating) { if ($score !== null && $score !== '') { $score = (float) $score; if ($score >= 9.0) { return 'critical'; } if ($score >= 7.0) { return 'high'; } if ($score >= 4.0) { return 'medium'; } return 'low'; } $rating = strtolower((string) $rating); return in_array($rating, array('critical', 'high', 'medium', 'low'), true) ? $rating : 'medium'; } }