array( 'title' => __('The database server version is no longer supported', 'all-in-one-wp-content-security'), 'kind' => 'vulnerability', 'explanation' => __('The vendor of the database server no longer publishes security fixes for this version.', 'all-in-one-wp-content-security'), 'impact' => __('Security issues in the database server stay unfixed. The risk is lower when the database only accepts local connections, but upgrades usually become harder the longer they are postponed.', 'all-in-one-wp-content-security'), 'remediation' => __('Ask your host to move the site to a supported MySQL or MariaDB version, or upgrade the server yourself.', 'all-in-one-wp-content-security'), 'steps' => array( __('Take a database backup.', 'all-in-one-wp-content-security'), __('Upgrade to a supported release (for example MySQL 8.4 LTS or MariaDB 10.11 / 11.4 LTS) or ask your host to do so.', 'all-in-one-wp-content-security'), __('Re-run this check.', 'all-in-one-wp-content-security'), ), 'docs' => array( self::doc(__('MySQL end-of-life notices', 'all-in-one-wp-content-security'), 'https://www.mysql.com/support/eol-notice.html'), self::doc(__('MariaDB maintenance policy', 'all-in-one-wp-content-security'), 'https://mariadb.org/about/#maintenance-policy'), ), 'permission' => __('Hosting provider or server administrator', 'all-in-one-wp-content-security'), ), 'db.connection_tls' => array( 'title' => __('The database connection is not encrypted', 'all-in-one-wp-content-security'), 'explanation' => __('WordPress connects to a database on another host and the connection does not use TLS.', 'all-in-one-wp-content-security'), 'impact' => __('Anyone who can observe the network between the web server and the database server can read queries, results and the database password.', 'all-in-one-wp-content-security'), 'remediation' => __('Enable TLS for the database connection, or make sure the database is only reachable over a private network.', 'all-in-one-wp-content-security'), 'steps' => array( __('Ask your host whether the database traffic stays on a private network.', 'all-in-one-wp-content-security'), __('To require TLS, enable it on the database server and add define(\'MYSQL_CLIENT_FLAGS\', MYSQLI_CLIENT_SSL); to wp-config.php.', 'all-in-one-wp-content-security'), ), 'docs' => array(self::doc(__('wp-config.php: MYSQL_CLIENT_FLAGS', 'all-in-one-wp-content-security'), 'https://developer.wordpress.org/advanced-administration/wordpress/wp-config/#mysql-client-flags')), 'permission' => __('Database server administrator and access to wp-config.php', 'all-in-one-wp-content-security'), ), ); } public static function server_info() { global $wpdb; $info = method_exists($wpdb, 'db_server_info') ? (string) $wpdb->db_server_info() : (string) $wpdb->get_var('SELECT VERSION()'); $suppress = $wpdb->suppress_errors(true); $comment = (string) $wpdb->get_var('SELECT @@version_comment'); $wpdb->suppress_errors($suppress); return AOWPCS_EOL::parse_db_version($info, $comment) + array('raw' => $info, 'comment' => $comment); } /** * Whether DB_HOST points to this machine (local TCP or a socket) */ public static function host_type() { $host = defined('DB_HOST') ? DB_HOST : 'localhost'; $name = preg_replace('/:.*$/', '', $host); if ($name === '' || in_array(strtolower($name), array('localhost', '127.0.0.1', '::1', '[::1]'), true) || strpos($host, '/') === 0) { return 'local'; } if (AOWPCS_IP::is_valid($name)) { return AOWPCS_IP::is_private($name) ? 'private' : 'public'; } // Single-label host names (for example "db" in Docker) resolve on a private network return strpos($name, '.') === false ? 'private' : 'remote'; } public function run($context) { global $wpdb; $findings = array(); $server = self::server_info(); $names = array('mysql' => 'MySQL', 'mariadb' => 'MariaDB', 'percona' => 'Percona Server'); $vendor = isset($names[$server['vendor']]) ? $names[$server['vendor']] : $server['vendor']; $status = AOWPCS_EOL::db($server['vendor'], $server['branch']); $evidence = array( __('Server', 'all-in-one-wp-content-security') => trim($vendor . ' ' . $server['version']), __('Reported version string', 'all-in-one-wp-content-security') => $server['raw'], __('Support data as of', 'all-in-one-wp-content-security') => AOWPCS_EOL::DATA_DATE, ); if ($status['until'] !== '') { $evidence[__('Supported until', 'all-in-one-wp-content-security')] = $status['until']; } if ($status['status'] === 'eol') { /* translators: 1: vendor, 2: server branch */ $findings[] = $this->issue('db.version_eol', 'medium', 'database-server', sprintf(__('%1$s %2$s is no longer supported by its vendor.', 'all-in-one-wp-content-security'), $vendor, $server['branch']), array( 'detected' => trim($vendor . ' ' . $server['version']), 'expected' => __('A supported release', 'all-in-one-wp-content-security'), 'evidence' => $evidence, )); } elseif ($status['status'] === 'innovation') { /* translators: 1: vendor, 2: server branch */ $findings[] = $this->issue('db.version_eol', 'low', 'database-server', sprintf(__('%1$s %2$s is an "innovation" release that is only supported until the next one ships; an LTS release (8.4) is recommended for production.', 'all-in-one-wp-content-security'), $vendor, $server['branch']), array( 'kind' => 'hardening', 'confidence' => 'likely', 'detected' => trim($vendor . ' ' . $server['version']), 'expected' => 'MySQL 8.4 LTS', 'evidence' => $evidence, )); } elseif ($status['status'] === 'unknown') { /* translators: 1: vendor, 2: server version */ $findings[] = $this->unknown('db.version_eol', sprintf(__('The support status of %1$s %2$s is not in the bundled table, so it was not assessed.', 'all-in-one-wp-content-security'), $vendor, $server['version'])); } else { /* translators: 1: vendor, 2: server version, 3: status until */ $findings[] = $this->pass('db.version_eol', sprintf(__('%1$s %2$s is supported until %3$s.', 'all-in-one-wp-content-security'), $vendor, $server['version'], $status['until']), $evidence); } $type = self::host_type(); if ($type === 'local') { $findings[] = $this->pass('db.connection_tls', __('The database runs on the same machine (local connection), so its traffic does not cross the network.', 'all-in-one-wp-content-security')); } else { $suppress = $wpdb->suppress_errors(true); $row = $wpdb->get_row("SHOW SESSION STATUS LIKE 'Ssl_cipher'", ARRAY_N); $wpdb->suppress_errors($suppress); if (!is_array($row)) { $findings[] = $this->unknown('db.connection_tls', __('The database did not report whether the connection is encrypted.', 'all-in-one-wp-content-security')); } elseif (!empty($row[1])) { /* translators: %s: row 1 */ $findings[] = $this->pass('db.connection_tls', sprintf(__('The database connection is encrypted (%s).', 'all-in-one-wp-content-security'), $row[1])); } else { $private = $type === 'private'; $findings[] = $this->issue('db.connection_tls', $private ? 'low' : 'medium', 'db-connection', $private ? __('The database is on another host on what appears to be a private network, and the connection is not encrypted.', 'all-in-one-wp-content-security') : __('The database is on another host and the connection is not encrypted.', 'all-in-one-wp-content-security'), array( 'confidence' => $private ? 'likely' : 'confirmed', 'detected' => __('Unencrypted (Ssl_cipher is empty)', 'all-in-one-wp-content-security'), 'expected' => __('TLS, or a private network only', 'all-in-one-wp-content-security'), 'evidence' => array(__('Database host type', 'all-in-one-wp-content-security') => $private ? __('private network', 'all-in-one-wp-content-security') : __('remote', 'all-in-one-wp-content-security')), )); } } return $findings; } } /** * Effective privileges of the database user WordPress connects with */ class AOWPCS_Check_Db_Privileges extends AOWPCS_Check { public function id() { return 'database.privileges'; } public function category() { return 'database'; } public function rules() { return array( 'db.excessive_privileges' => array( 'title' => __('The WordPress database user has server-wide privileges', 'all-in-one-wp-content-security'), 'explanation' => __('WordPress only needs privileges on its own database. This user has privileges on all databases or administrative privileges (such as FILE, SUPER or GRANT OPTION).', 'all-in-one-wp-content-security'), 'impact' => __('An SQL injection in any plugin would let an attacker read or change every database on the server, read server files (FILE) or create new database users.', 'all-in-one-wp-content-security'), 'remediation' => __('Use a dedicated database user that only has privileges on the WordPress database.', 'all-in-one-wp-content-security'), 'steps' => array( __('In the hosting control panel (or with a database administrator), create a user with ALL PRIVILEGES on the WordPress database only — no global privileges.', 'all-in-one-wp-content-security'), __('Update DB_USER and DB_PASSWORD in wp-config.php.', 'all-in-one-wp-content-security'), __('Re-run this check. This plugin never changes database privileges itself.', 'all-in-one-wp-content-security'), ), 'docs' => array(self::doc(__('MySQL: privileges provided', 'all-in-one-wp-content-security'), 'https://dev.mysql.com/doc/refman/8.4/en/privileges-provided.html')), 'permission' => __('Database server administrator', 'all-in-one-wp-content-security'), ), ); } /** * Parse SHOW GRANTS rows. Returns array(global => privileges on *.*, grant_option => bool, raw => rows) */ public static function parse_grants($rows) { $global = array(); $grant_option = false; foreach ($rows as $row) { $row = (string) $row; if (preg_match('/^GRANT\s+(.+?)\s+ON\s+(\S+)\s+TO\s/i', $row, $m)) { $object = trim($m[2], '`'); if ($object === '*.*') { foreach (array_map('trim', explode(',', strtoupper($m[1]))) as $privilege) { if ($privilege !== 'USAGE') { $global[] = $privilege; } } } if (stripos($row, 'WITH GRANT OPTION') !== false && $object === '*.*') { $grant_option = true; } } } return array('global' => array_values(array_unique($global)), 'grant_option' => $grant_option); } public function run($context) { global $wpdb; $suppress = $wpdb->suppress_errors(true); $rows = $wpdb->get_col('SHOW GRANTS FOR CURRENT_USER()'); $wpdb->suppress_errors($suppress); if (empty($rows)) { return array($this->unknown('db.excessive_privileges', __('The database server did not allow the privileges to be listed (common on managed database services).', 'all-in-one-wp-content-security'))); } $grants = self::parse_grants($rows); if (empty($grants['global']) && !$grants['grant_option']) { return array($this->pass('db.excessive_privileges', __('The database user only has privileges on specific databases.', 'all-in-one-wp-content-security'))); } $dangerous = array(); foreach ($grants['global'] as $privilege) { if (in_array($privilege, array('ALL', 'ALL PRIVILEGES', 'FILE', 'SUPER', 'PROCESS', 'SHUTDOWN', 'CREATE USER', 'RELOAD', 'GRANT OPTION'), true) || preg_match('/_ADMIN$/', $privilege)) { $dangerous[] = $privilege; } } $severity = ($dangerous || $grants['grant_option']) ? 'high' : 'medium'; $evidence = array( __('Global privileges (*.*)', 'all-in-one-wp-content-security') => $grants['global'], 'WITH GRANT OPTION' => $grants['grant_option'] ? __('yes', 'all-in-one-wp-content-security') : __('no', 'all-in-one-wp-content-security'), ); return array($this->issue('db.excessive_privileges', $severity, 'db-user', $dangerous /* translators: %s: value */ ? sprintf(__('The database user has administrative privileges on the whole server: %s.', 'all-in-one-wp-content-security'), implode(', ', $dangerous)) : __('The database user has privileges on all databases of the server.', 'all-in-one-wp-content-security'), array( 'detected' => implode(', ', $grants['global']), 'expected' => __('Privileges on the WordPress database only', 'all-in-one-wp-content-security'), 'evidence' => $evidence, ))); } } /** * Table prefix and tables that do not belong to this WordPress installation */ class AOWPCS_Check_Db_Tables extends AOWPCS_Check { public function id() { return 'database.tables'; } public function category() { return 'database'; } public function rules() { return array( 'db.default_prefix' => array( 'title' => __('The default table prefix "wp_" is used', 'all-in-one-wp-content-security'), 'kind' => 'informational', 'explanation' => __('The database tables use the default "wp_" prefix.', 'all-in-one-wp-content-security'), 'impact' => __('Informational only. A different prefix does not stop SQL injection; it only defeats some poorly written automated attacks. Changing it on an existing site is risky and not recommended for security reasons alone.', 'all-in-one-wp-content-security'), 'remediation' => __('No action required. Use a unique prefix for new installations if you like.', 'all-in-one-wp-content-security'), 'steps' => array(__('No change needed. Focus on keeping plugins updated and on the other findings.', 'all-in-one-wp-content-security')), ), 'db.foreign_tables' => array( 'title' => __('The database contains tables that do not use this site\'s prefix', 'all-in-one-wp-content-security'), 'kind' => 'informational', 'explanation' => __('Some tables in the WordPress database do not start with this site\'s table prefix. They usually belong to another application, an old installation or a staging copy.', 'all-in-one-wp-content-security'), 'impact' => __('Old copies of a site in the same database often contain outdated user accounts and password hashes, and an SQL injection on this site exposes them too.', 'all-in-one-wp-content-security'), 'remediation' => __('Identify the tables; export and remove those that are no longer needed.', 'all-in-one-wp-content-security'), 'steps' => array( __('Find out which application uses the listed tables (phpMyAdmin or your host can help).', 'all-in-one-wp-content-security'), __('Back up and drop tables of abandoned installations, or move other applications to their own database.', 'all-in-one-wp-content-security'), ), 'permission' => __('Database access (phpMyAdmin or hosting panel)', 'all-in-one-wp-content-security'), ), ); } public function run($context) { global $wpdb; $findings = array(); if ($wpdb->base_prefix === 'wp_') { $findings[] = $this->issue('db.default_prefix', 'low', 'table_prefix', __('The tables use the default "wp_" prefix.', 'all-in-one-wp-content-security'), array( 'kind' => 'informational', 'detected' => 'wp_', 'expected' => __('Any prefix (informational)', 'all-in-one-wp-content-security'), )); } $tables = $wpdb->get_col('SHOW TABLES'); $foreign = array(); foreach ($tables as $table) { if (strpos($table, $wpdb->base_prefix) !== 0) { $foreign[] = $table; } } if ($foreign) { /* translators: 1: value, 2: value */ $findings[] = $this->issue('db.foreign_tables', 'low', 'foreign-tables', sprintf(_n('%1$d table does not use the prefix "%2$s".', '%1$d tables do not use the prefix "%2$s".', count($foreign), 'all-in-one-wp-content-security'), count($foreign), $wpdb->base_prefix), array( 'kind' => 'informational', 'confidence' => 'likely', 'evidence' => array(__('Tables', 'all-in-one-wp-content-security') => array_slice($foreign, 0, 50)), )); } return $findings; } } /** * Administrator accounts: hidden administrators and administrators created without a recorded change */ class AOWPCS_Check_Db_Admins extends AOWPCS_Check { public function id() { return 'database.admins'; } public function category() { return 'database'; } public function rules() { $review = array( __('Open Users → All Users and review the account (registration date, e-mail, posts).', 'all-in-one-wp-content-security'), __('If nobody on your team created it, delete it, change all administrator passwords and the database password, and look for modified files (File Integrity).', 'all-in-one-wp-content-security'), __('If it is legitimate, accept the finding on this page.', 'all-in-one-wp-content-security'), ); return array( 'db.hidden_admins' => array( 'title' => __('Administrator accounts are hidden from the user list', 'all-in-one-wp-content-security'), 'kind' => 'compromise', 'explanation' => __('The database contains more administrator accounts than WordPress shows. Malware commonly hides the administrator account it created by filtering the user list.', 'all-in-one-wp-content-security'), 'impact' => __('A hidden administrator gives an attacker permanent access to the site even after passwords are changed.', 'all-in-one-wp-content-security'), 'remediation' => __('Treat the site as compromised: find the code that hides the account, remove it and the account, and change all credentials.', 'all-in-one-wp-content-security'), 'steps' => array( __('Note the user IDs listed in the evidence.', 'all-in-one-wp-content-security'), __('Look for code using the pre_user_query or users_list_table_query_args filters in plugins, must-use plugins and the theme (File Integrity findings help).', 'all-in-one-wp-content-security'), __('Remove the malicious code, then delete the accounts (WP-CLI: wp user delete ID --reassign=1).', 'all-in-one-wp-content-security'), __('Change all administrator passwords, the database password and the keys and salts in wp-config.php.', 'all-in-one-wp-content-security'), ), 'docs' => array(self::doc(__('FAQ: My site was hacked', 'all-in-one-wp-content-security'), 'https://developer.wordpress.org/advanced-administration/security/hacking/')), ), 'db.unrecorded_admin' => array( 'title' => __('An administrator account appeared without a recorded change', 'all-in-one-wp-content-security'), 'kind' => 'compromise', 'explanation' => __('This account has administrator rights but was not an administrator when the plugin last recorded the list, and the plugin did not see it being created or promoted through WordPress.', 'all-in-one-wp-content-security'), 'impact' => __('Accounts written directly into the database (for example through SQL injection) are a classic sign of compromise. It can also happen legitimately while the plugin was deactivated or when an account was added with direct database access.', 'all-in-one-wp-content-security'), 'remediation' => __('Confirm who created the account; delete it if nobody did.', 'all-in-one-wp-content-security'), 'steps' => $review, ), 'db.new_admin' => array( 'title' => __('A new administrator account was added', 'all-in-one-wp-content-security'), 'kind' => 'informational', 'explanation' => __('An administrator account was created or promoted through WordPress since the last scan.', 'all-in-one-wp-content-security'), 'impact' => __('Informational: every administrator can do anything on the site, so new administrators should always be expected.', 'all-in-one-wp-content-security'), 'remediation' => __('Confirm that the account is expected, then accept the finding.', 'all-in-one-wp-content-security'), 'steps' => $review, ), ); } /** * Administrator user IDs read directly from usermeta (not affected by user query filters) */ public static function admin_ids_from_db() { global $wpdb; $key = $wpdb->get_blog_prefix(get_main_site_id()) . 'capabilities'; $rows = $wpdb->get_results($wpdb->prepare("SELECT m.user_id, m.meta_value FROM {$wpdb->usermeta} m INNER JOIN {$wpdb->users} u ON u.ID = m.user_id WHERE m.meta_key = %s AND m.meta_value LIKE %s", $key, '%administrator%')); $ids = array(); foreach ($rows as $row) { $caps = maybe_unserialize($row->meta_value); if (is_array($caps) && !empty($caps['administrator'])) { $ids[] = (int) $row->user_id; } } if (is_multisite()) { foreach (get_super_admins() as $login) { $user = get_user_by('login', $login); if ($user) { $ids[] = (int) $user->ID; } } } sort($ids); return array_values(array_unique($ids)); } public function run($context) { $findings = array(); $db_ids = self::admin_ids_from_db(); $listed =get_users(array('role' => 'administrator', 'fields' => 'ID', 'blog_id' => get_main_site_id())); $listed = array_map('intval', $listed); if (is_multisite()) { foreach (get_super_admins() as $login) { $user = get_user_by('login', $login); if ($user) { $listed[] = (int) $user->ID; } } } $hidden = array_values(array_diff($db_ids, $listed)); if ($hidden) { $evidence = array(); foreach ($hidden as $id) { $user = get_userdata($id); /* translators: %d: id */ $evidence[sprintf(__('User ID %d', 'all-in-one-wp-content-security'), $id)] = $user ? sprintf('%s (%s)', $user->user_login, AOWPCS_Plugin::format_time($user->user_registered)) : ''; } /* translators: Placeholder is a number or value shown in the message */ $findings[] = $this->issue('db.hidden_admins', 'critical', 'hidden-admins', sprintf(_n('%d administrator account exists in the database but is hidden from the WordPress user list.', '%d administrator accounts exist in the database but are hidden from the WordPress user list.', count($hidden), 'all-in-one-wp-content-security'), count($hidden)), array( 'confidence' => 'likely', /* translators: 1: db ids, 2: value */ 'detected' => sprintf(__('%1$d administrators in the database, %2$d listed by WordPress', 'all-in-one-wp-content-security'), count($db_ids), count(array_unique($listed))), 'expected' => __('The same number', 'all-in-one-wp-content-security'), 'evidence' => $evidence, )); } $baseline = AOWPCS_Settings::state('admin_baseline'); if (!is_array($baseline)) { AOWPCS_Settings::set_state('admin_baseline', array('ids' => $db_ids, 'time' => AOWPCS_Plugin::now())); /* translators: %d: db ids */ $findings[] = $this->pass('db.unrecorded_admin', sprintf(__('Recorded %d administrator accounts as the starting point for future scans.', 'all-in-one-wp-content-security'), count($db_ids))); return $findings; } $recorded = AOWPCS_Audit::admin_grants_since($baseline['time']); $new_baseline = $baseline['ids']; foreach (array_diff($db_ids, $baseline['ids']) as $id) { $user = get_userdata($id); $evidence = array( __('User', 'all-in-one-wp-content-security') => $user ? $user->user_login : '#' . $id, __('E-mail domain', 'all-in-one-wp-content-security') => $user ? substr(strrchr($user->user_email, '@'), 1) : '', __('Registered', 'all-in-one-wp-content-security') => $user ? AOWPCS_Plugin::format_time($user->user_registered) : '', ); if (isset($recorded[$id])) { $evidence[__('Recorded change', 'all-in-one-wp-content-security')] = $recorded[$id]; /* translators: %s: value */ $findings[] = $this->issue('db.new_admin', 'low', 'user:' . $id, sprintf(__('"%s" became an administrator.', 'all-in-one-wp-content-security'), $user ? $user->user_login : $id), array('kind' => 'informational', 'evidence' => $evidence)); } else { /* translators: %s: value */ $findings[] = $this->issue('db.unrecorded_admin', 'high', 'user:' . $id, sprintf(__('"%s" has administrator rights, but no creation or promotion was recorded.', 'all-in-one-wp-content-security'), $user ? $user->user_login : $id), array('confidence' => 'unverified', 'evidence' => $evidence)); } } // Accounts that are no longer administrators leave the baseline $new_baseline = array_values(array_intersect($new_baseline, $db_ids)); if ($new_baseline !== $baseline['ids']) { AOWPCS_Settings::set_state('admin_baseline', array('ids' => $new_baseline, 'time' => $baseline['time'])); } return $findings; } /** * Accepting a new administrator adds it to the baseline */ public function on_accept($record) { if (strpos($record->subject, 'user:') !== 0) { return; } $id = (int) substr($record->subject, 5); $baseline = AOWPCS_Settings::state('admin_baseline'); if (is_array($baseline) && !in_array($id, $baseline['ids'], true)) { $baseline['ids'][] = $id; sort($baseline['ids']); AOWPCS_Settings::set_state('admin_baseline', $baseline); } } } /** * Important options: changes made outside WordPress and known malware patterns */ class AOWPCS_Check_Db_Options extends AOWPCS_Check { public function id() { return 'database.options'; } public function category() { return 'database'; } public function cost() { return 'cheap'; } /** * Options whose changes are tracked */ public static function watched() { return array('siteurl', 'home', 'admin_email', 'users_can_register', 'default_role', 'template', 'stylesheet', 'active_plugins', 'permalink_structure', 'upload_path', 'upload_url_path'); } public function rules() { return array( 'db.option_changed' => array( 'title' => __('An important setting changed without a recorded update', 'all-in-one-wp-content-security'), 'kind' => 'integrity', 'explanation' => __('One of the settings that attackers typically change (site address, administrator e-mail, registration, default role, active plugins or theme) differs from the value the plugin recorded, and the change was not made through WordPress while the plugin was active.', 'all-in-one-wp-content-security'), 'impact' => __('Changing the site address redirects visitors; changing the administrator e-mail allows password resets; opening registration with a privileged role creates accounts. Changes made directly in the database bypass all logging.', 'all-in-one-wp-content-security'), 'remediation' => __('Review the change; revert it in Settings if nobody made it on purpose, and investigate how it was made.', 'all-in-one-wp-content-security'), 'steps' => array( __('Compare the recorded and current value in the evidence.', 'all-in-one-wp-content-security'), __('If the change was intended (for example a migration), accept the finding to record the new value.', 'all-in-one-wp-content-security'), __('Otherwise revert it and treat the site as possibly compromised: change passwords, review administrators and recent file changes.', 'all-in-one-wp-content-security'), ), ), 'db.option_suspicious' => array( 'title' => __('Options contain code patterns typical of malware', 'all-in-one-wp-content-security'), 'kind' => 'compromise', 'explanation' => __('Values stored in the options table contain patterns such as eval(base64_decode(…)) or injected script tags in the site title or description, which legitimate plugins rarely store.', 'all-in-one-wp-content-security'), 'impact' => __('Database-stored malware survives file clean-ups and can inject spam, redirects or backdoors into every page.', 'all-in-one-wp-content-security'), 'remediation' => __('Inspect the listed options and remove the injected content (a database backup first).', 'all-in-one-wp-content-security'), 'steps' => array( __('Back up the database.', 'all-in-one-wp-content-security'), __('Inspect each listed option (WP-CLI: wp option get NAME, or phpMyAdmin).', 'all-in-one-wp-content-security'), __('Remove the injected code or restore the option from a clean backup; if the option belongs to a plugin, reinstalling the plugin may be enough.', 'all-in-one-wp-content-security'), ), 'permission' => __('Database access (WP-CLI, phpMyAdmin)', 'all-in-one-wp-content-security'), ), 'db.active_plugin_invalid' => array( 'title' => __('The list of active plugins contains an invalid entry', 'all-in-one-wp-content-security'), 'kind' => 'integrity', 'explanation' => __('An entry in active_plugins does not point to a regular plugin file in the plugins directory.', 'all-in-one-wp-content-security'), 'impact' => __('Entries pointing outside the plugins directory (or using ../) are used by malware to load code on every request; entries for deleted plugins are harmless leftovers.', 'all-in-one-wp-content-security'), 'remediation' => __('Deactivate and re-activate your plugins from the Plugins screen, which rewrites the list.', 'all-in-one-wp-content-security'), 'steps' => array(__('Visit Plugins: WordPress deactivates entries whose file is missing. For entries containing ../ or absolute paths, remove them with WP-CLI (wp option get active_plugins) and investigate.', 'all-in-one-wp-content-security')), ), ); } public static function value_hash($value) { return sha1(maybe_serialize($value)); } /** * Short, safe description of a value for evidence (the admin e-mail is masked) */ public static function describe_value($name, $value) { if ($name === 'admin_email' && is_string($value) && strpos($value, '@') !== false) { list($local, $domain) = explode('@', $value, 2); return substr($local, 0, 2) . '…@' . $domain; } if ($name === 'active_plugins') { return is_array($value) ? implode(', ', array_map('dirname', $value)) : ''; } return is_scalar($value) ? substr((string) $value, 0, 200) : ''; } public function run($context) { global $wpdb; $findings = array(); // Change detection against the recorded baseline $baseline = AOWPCS_Settings::state('option_baseline'); $current = array(); foreach (self::watched() as $name) { $value = get_option($name); $current[$name] = array('hash' => self::value_hash($value), 'value' => self::describe_value($name, $value)); } if (!is_array($baseline)) { AOWPCS_Settings::set_state('option_baseline', array('values' => $current, 'time' => AOWPCS_Plugin::now())); } else { $logged = AOWPCS_Audit::option_changes_since($baseline['time']); $updated = $baseline['values']; foreach ($current as $name => $info) { if (!isset($baseline['values'][$name]) || $baseline['values'][$name]['hash'] === $info['hash']) { continue; } if (isset($logged[$name])) { // Made through WordPress while the plugin was active: recorded in the event log $updated[$name] = $info; continue; } /* translators: %s: name */ $findings[] = $this->issue('db.option_changed', in_array($name, array('siteurl', 'home', 'admin_email', 'users_can_register', 'default_role', 'active_plugins'), true) ? 'high' : 'medium', 'option:' . $name, sprintf(__('The setting "%s" changed without a recorded update.', 'all-in-one-wp-content-security'), $name), array( 'confidence' => 'unverified', 'detected' => $info['value'], 'expected' => $baseline['values'][$name]['value'], 'evidence' => array(__('Option', 'all-in-one-wp-content-security') => $name, __('Recorded on', 'all-in-one-wp-content-security') => AOWPCS_Plugin::format_time($baseline['time'])), 'meta' => array('option' => $name, 'hash' => $info['hash']), )); } if ($updated !== $baseline['values']) { AOWPCS_Settings::set_state('option_baseline', array('values' => $updated, 'time' => $baseline['time'])); } } // Malware patterns in options (names only are reported) $patterns = array('%eval(base64_decode(%', '%eval(gzinflate(%', '%eval(str_rot13(%', '%eval(gzuncompress(%', '%String.fromCharCode(%eval%', '%get_col($wpdb->prepare("SELECT option_name FROM {$wpdb->options} WHERE ($where) AND option_name NOT LIKE %s AND option_name NOT LIKE %s LIMIT 50", array_merge($patterns, array('\_transient%', '\_site\_transient%')))); foreach (array('blogname', 'blogdescription') as $name) { $value = (string) get_option($name); if (preg_match('/issue('db.option_suspicious', 'high', 'option:' . $name, sprintf(__('The option "%s" contains code typical of malware.', 'all-in-one-wp-content-security'), $name), array( 'confidence' => 'unverified', 'evidence' => array(__('Option', 'all-in-one-wp-content-security') => $name), )); } // Active plugin entries foreach ((array) get_option('active_plugins', array()) as $entry) { if (!is_string($entry)) { continue; } $traversal = strpos($entry, '..') !== false || strpos($entry, '/') === 0 || preg_match('/^[a-z]:/i', $entry); if ($traversal) { /* translators: %s: entry */ $findings[] = $this->issue('db.active_plugin_invalid', 'high', 'active:' . md5($entry), sprintf(__('active_plugins contains a path outside the plugins directory: %s', 'all-in-one-wp-content-security'), $entry), array('kind' => 'compromise', 'confidence' => 'likely', 'detected' => $entry)); } elseif (!preg_match('/\.php$/', $entry) || !file_exists(WP_PLUGIN_DIR . '/' . $entry)) { /* translators: %s: entry */ $findings[] = $this->issue('db.active_plugin_invalid', 'low', 'active:' . md5($entry), sprintf(__('active_plugins lists a plugin file that does not exist: %s', 'all-in-one-wp-content-security'), $entry), array('confidence' => 'confirmed', 'detected' => $entry)); } } return $findings; } /** * Accepting a changed option records its current value */ public function on_accept($record) { if (strpos($record->subject, 'option:') !== 0 || $record->check_id !== 'db.option_changed') { return; } $name = substr($record->subject, 7); $baseline = AOWPCS_Settings::state('option_baseline'); if (is_array($baseline) && isset($baseline['values'][$name])) { $value = get_option($name); $baseline['values'][$name] = array('hash' => self::value_hash($value), 'value' => self::describe_value($name, $value)); AOWPCS_Settings::set_state('option_baseline', $baseline); } } }