= count( self::STAGES ) ) { $db['stage'] = 'done'; return true; } $db['stage'] = self::STAGES[ $idx + 1 ]; $db['cursor'] = 0; } } return false; } public static function progress( $db ) { $idx = isset( $db['stage'] ) ? array_search( $db['stage'], self::STAGES, true ) : 0; return false === $idx ? 1.0 : $idx / count( self::STAGES ); } /* ------------------------------------------------------------------ */ private static function stage_posts( array &$db, array &$state, $deadline ) { global $wpdb; $like = array(); foreach ( array( 'esc_like( $needle ) . '%'; } $where = implode( ' OR ', array_fill( 0, count( $like ), 'post_content LIKE %s' ) ); while ( microtime( true ) < $deadline ) { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title, post_type, post_status, post_modified, post_content FROM {$wpdb->posts} WHERE ID > %d AND post_type NOT IN ('revision','attachment','nav_menu_item','oembed_cache','customize_changeset','user_request') AND ({$where}) ORDER BY ID ASC LIMIT 100", array_merge( array( (int) $db['cursor'] ), $like ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( ! $rows ) { return true; } foreach ( $rows as $row ) { $db['cursor'] = (int) $row['ID']; $hits = Softvolt_Ai_Bodyguard_Signatures::scan_db_text( (string) $row['post_content'], 'post' ); if ( ! $hits ) { continue; } self::record( $state, $hits, array( 'fingerprint' => md5( 'db|posts|' . $row['ID'] ), 'location' => sprintf( '%sposts #%d "%s" (%s, %s)', $wpdb->prefix, (int) $row['ID'], Softvolt_Ai_Bodyguard_Helpers::trim_text( $row['post_title'], 60 ), $row['post_type'], $row['post_status'] ), 'meta' => array( 'table' => 'posts', 'id' => (int) $row['ID'], 'post_type' => $row['post_type'], 'post_status' => $row['post_status'], 'modified' => $row['post_modified'], 'edit_link' => get_edit_post_link( (int) $row['ID'], 'raw' ), 'content' => Softvolt_Ai_Bodyguard_Helpers::trim_text( (string) $row['post_content'], 12000 ), ), ) ); } if ( count( $rows ) < 100 ) { return true; } } return false; } private static function stage_postmeta( array &$db, array &$state, $deadline ) { global $wpdb; $like = array(); foreach ( array( 'esc_like( $needle ) . '%'; } $where = implode( ' OR ', array_fill( 0, count( $like ), 'meta_value LIKE %s' ) ); while ( microtime( true ) < $deadline ) { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id, post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE meta_id > %d AND ({$where}) ORDER BY meta_id ASC LIMIT 150", array_merge( array( (int) $db['cursor'] ), $like ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( ! $rows ) { return true; } foreach ( $rows as $row ) { $db['cursor'] = (int) $row['meta_id']; $value = (string) $row['meta_value']; $hits = Softvolt_Ai_Bodyguard_Signatures::scan_db_text( $value, 'meta' ); if ( ! $hits ) { continue; } // Page-builder data legitimately carries HTML widgets; lower the confidence there. if ( preg_match( '/^_?(elementor|et_pb|fl_builder|vc_|wpb|cornerstone|brizy|oxygen|ct_builder|divi)/i', $row['meta_key'] ) ) { foreach ( $hits as &$h ) { $h['confidence'] = (int) round( $h['confidence'] * 0.7 ); if ( 'critical' !== $h['severity'] ) { $h['severity'] = 'medium'; } } unset( $h ); } self::record( $state, $hits, array( 'fingerprint' => md5( 'db|postmeta|' . $row['post_id'] . '|' . $row['meta_key'] ), 'location' => sprintf( '%spostmeta post #%d key "%s"', $wpdb->prefix, (int) $row['post_id'], Softvolt_Ai_Bodyguard_Helpers::trim_text( $row['meta_key'], 60 ) ), 'meta' => array( 'table' => 'postmeta', 'id' => (int) $row['post_id'], 'meta_key' => $row['meta_key'], 'content' => Softvolt_Ai_Bodyguard_Helpers::trim_text( $value, 12000 ), ), ) ); } if ( count( $rows ) < 150 ) { return true; } } return false; } private static function stage_options( array &$db, array &$state, $deadline ) { global $wpdb; $like = array(); foreach ( array( 'esc_like( $needle ) . '%'; } $where = implode( ' OR ', array_fill( 0, count( $like ), 'option_value LIKE %s' ) ); while ( microtime( true ) < $deadline ) { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT option_id, option_name, option_value FROM {$wpdb->options} WHERE option_id > %d AND option_name NOT LIKE %s AND option_name NOT LIKE %s AND option_name NOT LIKE %s AND ({$where}) ORDER BY option_id ASC LIMIT 100", array_merge( array( (int) $db['cursor'], $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( 'svabg_' ) . '%' ), $like ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber if ( ! $rows ) { return true; } foreach ( $rows as $row ) { $db['cursor'] = (int) $row['option_id']; $value = (string) $row['option_value']; $hits = Softvolt_Ai_Bodyguard_Signatures::scan_db_text( $value, 'option' ); if ( ! $hits ) { continue; } // Widgets and theme "custom code" boxes are the usual injection points, so raise those. if ( preg_match( '/^(widget_|theme_mods_|sidebars_widgets|wpcode|insert_headers|custom_css|ihaf|wp_head|footer)/i', $row['option_name'] ) ) { foreach ( $hits as &$h ) { $h['confidence'] = min( 100, $h['confidence'] + 10 ); } unset( $h ); } self::record( $state, $hits, array( 'fingerprint' => md5( 'db|options|' . $row['option_name'] ), 'location' => sprintf( '%soptions "%s"', $wpdb->prefix, Softvolt_Ai_Bodyguard_Helpers::trim_text( $row['option_name'], 80 ) ), 'meta' => array( 'table' => 'options', 'option_name' => $row['option_name'], 'content' => Softvolt_Ai_Bodyguard_Helpers::trim_text( $value, 12000 ), ), ) ); } if ( count( $rows ) < 100 ) { return true; } } return false; } private static function stage_options_special( array &$db, array &$state, $deadline ) { global $wpdb; $siteurl = (string) get_option( 'siteurl' ); $home = (string) get_option( 'home' ); $s_host = strtolower( (string) wp_parse_url( $siteurl, PHP_URL_HOST ) ); $h_host = strtolower( (string) wp_parse_url( $home, PHP_URL_HOST ) ); if ( $s_host && $h_host && $s_host !== $h_host && preg_replace( '/^www\./', '', $s_host ) !== preg_replace( '/^www\./', '', $h_host ) ) { self::simple( $state, 'db|siteurl_mismatch', sprintf( '%soptions "siteurl" / "home"', $wpdb->prefix ), __( 'Site URL and Home URL point at different hosts', 'ai-guardwall' ), 'critical', 85, sprintf( /* translators: 1: siteurl, 2: home */ __( 'siteurl is %1$s but home is %2$s. Redirect malware changes these to send visitors elsewhere. If this is not a deliberate setup, fix them under Settings > General.', 'ai-guardwall' ), $siteurl, $home ), 'fix_config', array( 'details' => $siteurl . ' | ' . $home ) ); } $req_host = strtolower( (string) preg_replace( '/:\d+$/', '', Softvolt_Ai_Bodyguard_Helpers::server( 'HTTP_HOST' ) ) ); if ( $req_host && $h_host && ! defined( 'WP_CLI' ) && ! wp_doing_cron() && $req_host !== $h_host && preg_replace( '/^www\./', '', $req_host ) !== preg_replace( '/^www\./', '', $h_host ) ) { self::simple( $state, 'db|home_host', sprintf( '%soptions "home"', $wpdb->prefix ), __( 'Home URL does not match the address this site was requested on', 'ai-guardwall' ), 'low', 25, sprintf( /* translators: 1: home, 2: request host */ __( 'Home URL is %1$s but the request came to %2$s. Normal on staging copies and reverse proxies; otherwise check Settings > General.', 'ai-guardwall' ), $home, $req_host ), 'manual_review' ); } $default_role = (string) get_option( 'default_role' ); if ( in_array( $default_role, array( 'administrator', 'editor', 'shop_manager' ), true ) ) { self::simple( $state, 'db|default_role', sprintf( '%soptions "default_role"', $wpdb->prefix ), __( 'New users get a privileged role by default', 'ai-guardwall' ), 'critical', 95, sprintf( /* translators: %s: default role */ __( 'The default role for new registrations is "%s". Anyone who registers gets control of the site. Set it back to Subscriber under Settings > General.', 'ai-guardwall' ), $default_role ), 'fix_config' ); } if ( get_option( 'users_can_register' ) ) { self::simple( $state, 'db|registration_open', sprintf( '%soptions "users_can_register"', $wpdb->prefix ), __( 'Anyone can register an account', 'ai-guardwall' ), 'subscriber' === $default_role ? 'info' : 'high', 30, __( 'Open registration is fine for membership sites; otherwise switch it off under Settings > General to shrink the attack surface.', 'ai-guardwall' ), 'fix_config' ); } // Active plugins that do not exist, live in uploads, or are not listed by WordPress. $active = (array) get_option( 'active_plugins', array() ); $known = Softvolt_Ai_Bodyguard_Integrity::installed_plugins(); foreach ( $active as $file ) { $file = (string) $file; $abs = Softvolt_Ai_Bodyguard_Helpers::site_dir( 'wp-content/plugins' ) . '/' . $file; if ( preg_match( '#(^|/)\.\.(/|$)#', $file ) || false !== strpos( $file, 'uploads/' ) ) { self::simple( $state, 'db|active_plugin|' . $file, sprintf( '%soptions "active_plugins" → %s', $wpdb->prefix, $file ), __( 'Active plugin entry points outside the plugins folder', 'ai-guardwall' ), 'critical', 90, __( 'A plugin file is loaded from an unexpected location. This is how malware keeps itself running after the plugins folder is cleaned.', 'ai-guardwall' ), 'remove_db_record', array( 'details' => $file ) ); } elseif ( ! file_exists( $abs ) ) { self::simple( $state, 'db|active_plugin|' . $file, sprintf( '%soptions "active_plugins" → %s', $wpdb->prefix, $file ), __( 'Active plugin whose file is missing', 'ai-guardwall' ), 'info', 20, __( 'The plugin was deleted by FTP while still active. Harmless, but WordPress will keep trying to load it; deactivate it from Plugins.', 'ai-guardwall' ), 'manual_review', array( 'details' => $file ) ); } elseif ( ! isset( $known[ $file ] ) ) { self::simple( $state, 'db|active_plugin|' . $file, sprintf( '%soptions "active_plugins" → %s', $wpdb->prefix, $file ), __( 'Active plugin without a plugin header (hidden from the Plugins screen)', 'ai-guardwall' ), 'high', 80, __( 'The file runs as an active plugin but WordPress does not list it, so it never appears under Plugins. Fake plugins hide this way.', 'ai-guardwall' ), 'manual_review', array( 'details' => $file, 'rel' => Softvolt_Ai_Bodyguard_Helpers::rel_path( $abs ) ) ); } } foreach ( array( 'template', 'stylesheet' ) as $opt ) { $theme = (string) get_option( $opt ); if ( '' !== $theme && ! wp_get_theme( $theme )->exists() ) { self::simple( $state, 'db|' . $opt, sprintf( '%soptions "%s"', $wpdb->prefix, $opt ), __( 'Active theme folder does not exist', 'ai-guardwall' ), 'medium', 50, sprintf( /* translators: 1: option, 2: theme */ __( 'The "%1$s" option points at "%2$s", which is not installed. The site falls back to a default theme.', 'ai-guardwall' ), $opt, $theme ), 'fix_config' ); } } $upload_path = (string) get_option( 'upload_path' ); if ( '' !== $upload_path && false === strpos( wp_normalize_path( $upload_path ), 'wp-content' ) ) { self::simple( $state, 'db|upload_path', sprintf( '%soptions "upload_path"', $wpdb->prefix ), __( 'Uploads are stored outside wp-content', 'ai-guardwall' ), 'low', 30, sprintf( /* translators: %s: upload path */ __( 'upload_path is "%s". Fine if you set it up; malware sometimes points uploads at a writable public folder.', 'ai-guardwall' ), $upload_path ), 'manual_review' ); } return true; } private static function stage_comments( array &$db, array &$state, $deadline ) { global $wpdb; $like = array(); foreach ( array( 'esc_like( $needle ) . '%'; } $where = implode( ' OR ', array_fill( 0, count( $like ), 'comment_content LIKE %s' ) ); while ( microtime( true ) < $deadline ) { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_post_ID, comment_author, comment_content, comment_approved FROM {$wpdb->comments} WHERE comment_ID > %d AND comment_approved = '1' AND ({$where}) ORDER BY comment_ID ASC LIMIT 150", array_merge( array( (int) $db['cursor'] ), $like ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( ! $rows ) { return true; } foreach ( $rows as $row ) { $db['cursor'] = (int) $row['comment_ID']; $hits = Softvolt_Ai_Bodyguard_Signatures::scan_db_text( (string) $row['comment_content'], 'comment' ); if ( ! $hits ) { continue; } self::record( $state, $hits, array( 'fingerprint' => md5( 'db|comments|' . $row['comment_ID'] ), 'location' => sprintf( '%scomments #%d by "%s" on post #%d', $wpdb->prefix, (int) $row['comment_ID'], Softvolt_Ai_Bodyguard_Helpers::trim_text( $row['comment_author'], 40 ), (int) $row['comment_post_ID'] ), 'meta' => array( 'table' => 'comments', 'id' => (int) $row['comment_ID'], 'content' => Softvolt_Ai_Bodyguard_Helpers::trim_text( (string) $row['comment_content'], 6000 ), ), ) ); } if ( count( $rows ) < 150 ) { return true; } } return false; } private static function stage_users( array &$db, array &$state, $deadline ) { global $wpdb; $cap_key = $wpdb->prefix . 'capabilities'; $admins = $wpdb->get_results( $wpdb->prepare( "SELECT u.ID, u.user_login, u.user_email, u.user_registered, u.display_name FROM {$wpdb->users} u INNER JOIN {$wpdb->usermeta} m ON m.user_id = u.ID AND m.meta_key = %s WHERE m.meta_value LIKE %s ORDER BY u.ID ASC", $cap_key, '%administrator%' ), ARRAY_A ); $admins = $admins ? $admins : array(); // Users hidden by a filter: the raw table count must match what WordPress reports. $raw_total = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->users}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wp_total = (int) count_users()['total_users']; if ( $raw_total !== $wp_total && ! is_multisite() ) { self::simple( $state, 'db|hidden_users', sprintf( '%susers', $wpdb->prefix ), __( 'User accounts are hidden from WordPress', 'ai-guardwall' ), 'critical', 80, sprintf( /* translators: 1: total, 2: WordPress total */ __( 'The users table holds %1$d accounts but WordPress reports %2$d. A plugin or injected code is filtering the user list, which is how rogue administrators stay invisible.', 'ai-guardwall' ), $raw_total, $wp_total ), 'manual_review', array( 'details' => $raw_total . ' vs ' . $wp_total ) ); } $last_scan = isset( $state['last_scan_time'] ) ? (int) $state['last_scan_time'] : 0; $list = array(); foreach ( $admins as $u ) { $registered = strtotime( $u['user_registered'] . ' UTC' ); $list[] = $u['user_login'] . ' <' . $u['user_email'] . '>'; $age_days = ( time() - $registered ) / DAY_IN_SECONDS; $random = (bool) preg_match( '/^[a-z0-9]{10,}$/', $u['user_login'] ) && ! preg_match( '/[aeiou]{1}[a-z]{2,}/', $u['user_login'] ); if ( $last_scan && $registered > $last_scan && $registered > $state['started_ts'] - WEEK_IN_SECONDS * 4 ) { self::simple( $state, 'db|new_admin|' . $u['ID'], sprintf( '%susers #%d "%s"', $wpdb->prefix, (int) $u['ID'], $u['user_login'] ), __( 'Administrator created since the last scan', 'ai-guardwall' ), 'high', 60, sprintf( /* translators: 1: user login, 2: user email, 3: user registered */ __( 'Account "%1$s" (%2$s) with the Administrator role was created on %3$s. If nobody on your team added it, it is an attacker\'s account.', 'ai-guardwall' ), $u['user_login'], $u['user_email'], $u['user_registered'] ), 'remove_user', array( 'user_id' => (int) $u['ID'], 'user_login' => $u['user_login'], 'user_email' => $u['user_email'], 'registered' => $u['user_registered'] ) ); } elseif ( $age_days <= 30 && count( $admins ) > 1 ) { self::simple( $state, 'db|recent_admin|' . $u['ID'], sprintf( '%susers #%d "%s"', $wpdb->prefix, (int) $u['ID'], $u['user_login'] ), __( 'Administrator account created in the last 30 days', 'ai-guardwall' ), 'medium', 40, sprintf( /* translators: 1: user login, 2: user email, 3: user registered */ __( 'Account "%1$s" (%2$s) was created on %3$s. Confirm it belongs to your team.', 'ai-guardwall' ), $u['user_login'], $u['user_email'], $u['user_registered'] ), 'manual_review', array( 'user_id' => (int) $u['ID'], 'user_login' => $u['user_login'], 'user_email' => $u['user_email'], 'registered' => $u['user_registered'] ) ); } elseif ( $random ) { self::simple( $state, 'db|random_admin|' . $u['ID'], sprintf( '%susers #%d "%s"', $wpdb->prefix, (int) $u['ID'], $u['user_login'] ), __( 'Administrator with a machine-generated username', 'ai-guardwall' ), 'low', 30, sprintf( /* translators: %s: user login */ __( 'Usernames like "%s" are typical of accounts created by exploit scripts.', 'ai-guardwall' ), $u['user_login'] ), 'manual_review', array( 'user_id' => (int) $u['ID'], 'user_login' => $u['user_login'] ) ); } if ( 'admin' === strtolower( $u['user_login'] ) ) { self::simple( $state, 'db|admin_username', sprintf( '%susers #%d "admin"', $wpdb->prefix, (int) $u['ID'] ), __( 'The administrator is called "admin"', 'ai-guardwall' ), 'high', 80, __( 'Every brute-force bot tries "admin" first. Rename the account on the Tools tab (nothing else changes).', 'ai-guardwall' ), 'fix_config', array( 'user_id' => (int) $u['ID'] ) ); } } $state['admins'] = $list; return true; } private static function stage_roles( array &$db, array &$state, $deadline ) { global $wpdb; $dangerous = array( 'manage_options', 'edit_plugins', 'edit_themes', 'install_plugins', 'install_themes', 'update_plugins', 'update_themes', 'update_core', 'delete_plugins', 'delete_themes', 'edit_users', 'create_users', 'delete_users', 'promote_users', 'unfiltered_html', 'unfiltered_upload', 'edit_files', 'import', 'export' ); $roles = wp_roles()->roles; foreach ( $roles as $slug => $role ) { if ( 'administrator' === $slug ) { continue; } $caps = isset( $role['capabilities'] ) && is_array( $role['capabilities'] ) ? array_keys( array_filter( $role['capabilities'] ) ) : array(); $found = array_values( array_intersect( $caps, $dangerous ) ); if ( ! $found ) { continue; } $expected = array( 'editor' => array( 'unfiltered_html' ), 'shop_manager' => array( 'edit_users', 'create_users', 'delete_users', 'promote_users', 'export', 'import', 'unfiltered_html' ) ); $unexpected = isset( $expected[ $slug ] ) ? array_diff( $found, $expected[ $slug ] ) : $found; if ( ! $unexpected ) { continue; } self::simple( $state, 'db|role|' . $slug, sprintf( '%soptions "%suser_roles" → %s', $wpdb->prefix, $wpdb->prefix, $slug ), sprintf( /* translators: %s: slug */ __( 'Role "%s" holds administrator-level capabilities', 'ai-guardwall' ), isset( $role['name'] ) ? $role['name'] : $slug ), 'critical', 85, sprintf( /* translators: %s: list of unexpected */ __( 'The role has: %s. Privilege-escalation malware edits roles so an ordinary account can install plugins or change settings.', 'ai-guardwall' ), implode( ', ', $unexpected ) ), 'fix_config', array( 'details' => implode( ', ', $unexpected ), 'role' => $slug ) ); } return true; } private static function stage_cron( array &$db, array &$state, $deadline ) { global $wpdb; $cron = _get_cron_array(); if ( ! is_array( $cron ) ) { return true; } $seen = array(); foreach ( $cron as $timestamp => $hooks ) { if ( ! is_array( $hooks ) ) { continue; } foreach ( $hooks as $hook => $events ) { if ( isset( $seen[ $hook ] ) ) { continue; } $seen[ $hook ] = true; $args_text = ''; foreach ( (array) $events as $event ) { if ( ! empty( $event['args'] ) ) { $args_text .= wp_json_encode( $event['args'] ) . ' '; } } $suspicious_args = (bool) preg_match( '/https?:\/\/|base64|eval|PD9waHA|<\?php/i', $args_text ); $random_name = (bool) preg_match( '/^[a-z0-9]{16,}$|_[a-f0-9]{8,}$/', $hook ) && ! preg_match( '/^(wp_|woocommerce|action_scheduler|jetpack|elementor|wpseo|aioseo|rank_math|wpforms|svabg|svava)/', $hook ); if ( $suspicious_args || $random_name ) { self::simple( $state, 'db|cron|' . $hook, sprintf( '%soptions "cron" → hook "%s"', $wpdb->prefix, $hook ), __( 'Suspicious scheduled task', 'ai-guardwall' ), 'high', $suspicious_args ? 70 : 50, sprintf( /* translators: 1: hook, 2: value */ __( 'Hook "%1$s" runs on a schedule%2$s. Malware uses WP-Cron to re-download its payload after cleanups.', 'ai-guardwall' ), $hook, $suspicious_args ? ' with arguments containing a URL or encoded code: ' . Softvolt_Ai_Bodyguard_Helpers::trim_text( $args_text, 200 ) : ' and has a machine-generated name' ), 'remove_cron', array( 'hook' => $hook, 'details' => Softvolt_Ai_Bodyguard_Helpers::trim_text( $args_text, 400 ) ) ); } elseif ( ! has_action( $hook ) && ! preg_match( '/^(wp_|woocommerce|action_scheduler|svabg)/', $hook ) ) { self::simple( $state, 'db|cron_orphan|' . $hook, sprintf( '%soptions "cron" → hook "%s"', $wpdb->prefix, $hook ), __( 'Scheduled task with no code attached', 'ai-guardwall' ), 'info', 15, sprintf( /* translators: %s: hook */ __( 'Nothing is registered for hook "%s" right now; usually a leftover from a removed plugin. It can be deleted.', 'ai-guardwall' ), $hook ), 'remove_cron', array( 'hook' => $hook ) ); } } } return true; } private static function stage_tables( array &$db, array &$state, $deadline ) { global $wpdb; $tables = $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->prefix ) . '%' ) ); $core = array( 'commentmeta', 'comments', 'links', 'options', 'postmeta', 'posts', 'term_relationships', 'term_taxonomy', 'termmeta', 'terms', 'usermeta', 'users', 'blogs', 'blogmeta', 'blog_versions', 'registration_log', 'signups', 'site', 'sitemeta' ); $known = '/^(woocommerce_|wc_|actionscheduler_|svabg_|svava_|svap_|yoast_|wpforms_|gf_|rg_|e_|elementor_|wpmailsmtp_|wfls_|wf[a-z]+|aiowps_|itsec_|sucuri|cf7|frm_|wpml_|icl_|edd_|bp_|wpr_|litespeed_|redirection_|cmplz_|wpstg|jet_|ewww|smush|rank_math|aioseo_|ms_snippets|snippets|wpcode|duplicator|updraft|backwpup|nf3?_|tec_|ninja_|wp_statistics|statistics_|mailpoet_|burst_|um_|pmxi_|pmxe_|wpvivid|lmfwc|wpforo|asp_|dlm_|wc_admin|wp_abtesting)/i'; $unknown = array(); foreach ( (array) $tables as $table ) { $short = substr( $table, strlen( $wpdb->prefix ) ); if ( in_array( $short, $core, true ) || preg_match( '/^\d+_/', $short ) || preg_match( $known, $short ) ) { continue; } $unknown[] = $short; } if ( $unknown ) { self::simple( $state, 'db|tables', sprintf( '%s* (%d tables)', $wpdb->prefix, count( $unknown ) ), __( 'Database tables not created by WordPress core', 'ai-guardwall' ), 'info', 10, sprintf( /* translators: %s: list of unknown */ __( 'For your review: %s. Plugins create their own tables; one you cannot match to an installed plugin deserves a look.', 'ai-guardwall' ), implode( ', ', array_slice( $unknown, 0, 60 ) ) . ( count( $unknown ) > 60 ? '…' : '' ) ), 'manual_review', array( 'details' => implode( ', ', $unknown ) ) ); } return true; } /* ------------------------------------------------------------------ */ private static function record( array &$state, $hits, $fields ) { $top = $hits[0]; $confirmed = false; $more = array(); foreach ( $hits as $h ) { if ( 'confirmed' === $h['verdict'] && $h['confidence'] >= 85 ) { $confirmed = true; } if ( $h['id'] !== $top['id'] ) { $more[] = $h['title']; } } $desc = $top['description'] . ( $more ? ' ' . __( 'Also:', 'ai-guardwall' ) . ' ' . implode( '; ', array_slice( array_unique( $more ), 0, 6 ) ) . '.' : '' ); $meta = $fields['meta']; $meta['hits'] = array_slice( $hits, 0, 12 ); Softvolt_Ai_Bodyguard_Findings::upsert( array( 'scan_id' => (int) $state['scan_id'], 'fingerprint' => $fields['fingerprint'], 'category' => 'db', 'location' => $fields['location'], 'line' => (int) $top['line'], 'severity' => $top['severity'], 'confidence' => (int) $top['confidence'], 'signature' => $top['id'], 'title' => $top['title'], 'description' => $desc, 'snippet' => $top['excerpt'], 'verdict' => $confirmed ? 'confirmed' : 'suspicious', 'remediation' => $top['remediation'], 'file_hash' => md5( isset( $meta['content'] ) ? $meta['content'] : $fields['location'] ), 'meta' => $meta, ) ); $state['findings']++; if ( 'critical' === $top['severity'] ) { $state['critical']++; } } private static function simple( array &$state, $key, $location, $title, $severity, $confidence, $description, $remediation, $meta = array() ) { Softvolt_Ai_Bodyguard_Findings::upsert( array( 'scan_id' => (int) $state['scan_id'], 'fingerprint' => md5( $key ), 'category' => 0 === strpos( $key, 'db|cron' ) ? 'cron' : ( ( false !== strpos( $key, 'admin' ) || false !== strpos( $key, 'users' ) ) ? 'user' : 'db' ), 'location' => $location, 'line' => 0, 'severity' => $severity, 'confidence' => (int) $confidence, 'signature' => substr( preg_replace( '/[^a-z0-9_]/', '_', strtolower( $key ) ), 0, 64 ), 'title' => $title, 'description' => $description, 'snippet' => '', 'verdict' => 'suspicious', 'remediation' => $remediation, 'file_hash' => md5( $description ), 'meta' => $meta, ) ); $state['findings']++; if ( 'critical' === $severity ) { $state['critical']++; } } }