$plugin_slug, 'type' => 'plugin', 'status' => 'unknown', 'local_version' => '', 'repo_version' => '', 'is_from_repo' => false, 'modified_files' => array(), 'added_files' => array(), 'removed_files' => array(), 'message' => '', ); // Get local plugin info $local_plugin = self::get_local_plugin_info( $plugin_slug ); if ( ! $local_plugin ) { $result['status'] = 'not_found'; $result['message'] = __( 'Plugin not found locally', 'ai-cyberguard' ); return $result; } $result['local_version'] = $local_plugin['Version']; // Get repository info $repo_info = self::get_plugin_repo_info( $plugin_slug ); if ( ! $repo_info ) { $result['status'] = 'not_in_repo'; $result['message'] = __( 'Plugin not found in WordPress.org repository (premium/custom plugin)', 'ai-cyberguard' ); return $result; } $result['is_from_repo'] = true; $result['repo_version'] = $repo_info->version; // Check version match if ( version_compare( $local_plugin['Version'], $repo_info->version, '<' ) ) { $result['status'] = 'outdated'; $result['message'] = sprintf( /* translators: 1: installed plugin version, 2: available repository version */ __( 'Update available: %1$s → %2$s', 'ai-cyberguard' ), $local_plugin['Version'], $repo_info->version ); } elseif ( version_compare( $local_plugin['Version'], $repo_info->version, '>' ) ) { $result['status'] = 'newer'; $result['message'] = __( 'Local version is newer than repository (dev version?)', 'ai-cyberguard' ); } else { $result['status'] = 'current'; $result['message'] = __( 'Version matches repository', 'ai-cyberguard' ); } return $result; } /** * Get local plugin info */ private static function get_local_plugin_info( $plugin_slug ) { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugins = get_plugins(); foreach ( $plugins as $path => $info ) { if ( strpos( $path, $plugin_slug . '/' ) === 0 || $path === $plugin_slug . '.php' ) { return $info; } } return null; } /** * Get plugin info from WordPress.org */ private static function get_plugin_repo_info( $plugin_slug ) { $url = add_query_arg( array( 'action' => 'plugin_information', 'slug' => $plugin_slug, ), self::PLUGIN_API ); $response = wp_remote_get( $url, array( 'timeout' => 15, ) ); if ( is_wp_error( $response ) ) { return null; } $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body ); if ( ! $data || isset( $data->error ) ) { return null; } return $data; } /** * Verify WordPress core files * * @return array List of modified core files */ /** * ACG-084-H1 (audit 2026-06-27): the shared no-follow contract for this tool's core-file hashing. Prefers the * canonical GuardPress_FS_Helpers primitive; falls back to a self-contained, trusted-root-aware copy for an * isolated harness. True ONLY for a REGULAR file (no symlink/FIFO/dir/socket/device) with NO symlink path * component below the deepest trusted root (ABSPATH / WP_CONTENT_DIR / uploads basedir — host may symlink those, * not a deeper one) and a realpath that stays under that root. * * @param string $path Absolute filesystem path. * @return bool */ private static function is_safe_regular_file( $path ) { if ( class_exists( 'GuardPress_FS_Helpers' ) && method_exists( 'GuardPress_FS_Helpers', 'regular_file_in_abspath_no_symlink_components' ) ) { return GuardPress_FS_Helpers::regular_file_in_abspath_no_symlink_components( $path ); } $path = (string) $path; if ( '' === $path || ! defined( 'ABSPATH' ) ) { return false; } clearstatcache( false, $path ); if ( @is_link( $path ) || ! @is_file( $path ) ) { return false; } $norm = static function ( $p ) { return str_replace( '\\', '/', (string) $p ); }; $full = $norm( $path ); $roots = array(); if ( function_exists( 'wp_upload_dir' ) ) { $u = @wp_upload_dir( null, false ); if ( is_array( $u ) && ! empty( $u['basedir'] ) ) { $roots[] = $u['basedir']; } } if ( defined( 'WP_CONTENT_DIR' ) ) { $roots[] = WP_CONTENT_DIR; } $roots[] = ABSPATH; $base = null; foreach ( $roots as $r ) { $rn = rtrim( $norm( $r ), '/' ); if ( '' !== $rn && 0 === strpos( $full, $rn . '/' ) ) { $base = $rn; break; } } if ( null === $base ) { $ra = @realpath( ABSPATH ); $rp = @realpath( $path ); return ( false !== $ra && false !== $rp && 0 === strpos( $norm( $rp ), rtrim( $norm( $ra ), '/' ) . '/' ) ); } $rel = substr( $full, strlen( $base ) + 1 ); $cur = $base; foreach ( explode( '/', $rel ) as $p ) { if ( '' === $p || '.' === $p ) { continue; } if ( '..' === $p ) { return false; } $cur .= '/' . $p; clearstatcache( false, $cur ); if ( @is_link( $cur ) ) { return false; } } $rp = @realpath( $path ); $rb = @realpath( $base ); if ( false === $rp || false === $rb ) { return false; } return ( 0 === strpos( $norm( $rp ), rtrim( $norm( $rb ), '/' ) . '/' ) ); } public static function verify_core_files() { global $wp_version; $result = array( 'wp_version' => $wp_version, 'verified' => true, 'modified_files' => array(), 'missing_files' => array(), 'extra_files' => array(), // FIX (June 2026): GuardPress's own root-level helper files // (emergency.php, php-error.php) are picked up by the integrity // scan as "extra" — they are not WordPress core. Rather than // hide them silently, we identify them by content signature and // surface them in a friendly "trusted" group so the user knows // the scan found them AND that they are safe to keep. 'trusted_files' => array(), ); // Get core checksums from WordPress.org $checksums = self::get_core_checksums( $wp_version ); if ( ! $checksums ) { $result['verified'] = false; $result['message'] = __( 'Could not retrieve WordPress checksums', 'ai-cyberguard' ); return $result; } // Check each core file foreach ( $checksums as $file => $expected_hash ) { $filepath = ABSPATH . $file; // Skip wp-content (it's user content) if ( strpos( $file, 'wp-content/' ) === 0 ) { continue; } if ( ! file_exists( $filepath ) ) { $result['missing_files'][] = $file; continue; } // ACG-084-001 (audit 2026-06-27, HIGH): never md5_file() a core path that is itself a symlink — or // reachable only THROUGH a symlinked core DIRECTORY (e.g. wp-includes -> external tree). file_exists()/ // md5_file() FOLLOW the link, so an external target that happens to match the official checksum would be // counted "verified" while the genuine core file is gone. Gate on the shared no-symlink-component + // regular-file contract; an unsafe core path is reported MODIFIED (so 'verified' is forced false below), // never silently verified. Core paths are never legitimately links. if ( ! self::is_safe_regular_file( $filepath ) ) { $result['modified_files'][] = array( 'file' => $file, 'expected' => $expected_hash, 'actual' => 'symbolic link or special file', ); continue; } $actual_hash = md5_file( $filepath ); if ( $actual_hash !== $expected_hash ) { $result['modified_files'][] = array( 'file' => $file, 'expected' => $expected_hash, 'actual' => $actual_hash, ); } } // Check for extra files in wp-admin and wp-includes $core_dirs = array( 'wp-admin', 'wp-includes' ); foreach ( $core_dirs as $dir ) { $extra = self::find_extra_files( ABSPATH . $dir, $checksums, $dir ); $result['extra_files'] = array_merge( $result['extra_files'], $extra ); } // Check root for extra PHP-family files (RC-108-01 follow-up, round-9 // review): enumerate the full family so a root-level evil.php8 / shell.pht // is listed too, matching the Malware Scanner — not just .php. $root_files = array(); $rc_root_exts = class_exists( 'GuardPress_Malware_Scanner' ) ? GuardPress_Malware_Scanner::php_family_exts() : array( 'php', 'phtml', 'php3', 'php4', 'php5', 'php7', 'php8', 'phar', 'pht', 'phps', 'inc' ); foreach ( $rc_root_exts as $rc_fx ) { $rc_g = glob( ABSPATH . '*.' . $rc_fx ); if ( $rc_g ) { $root_files = array_merge( $root_files, $rc_g ); } } // Known legitimate files that plugins place in root $known_root_files = array( 'wp-config.php', // WordPress config 'wordfence-waf.php', // Wordfence WAF 'wp-cache-config.php', // WP Super Cache 'advanced-cache.php', // Various caching plugins 'db.php', // DB drop-in 'object-cache.php', // Object cache drop-in 'sunrise.php', // Multisite 'db-error.php', // Custom DB error 'maintenance.php', // Maintenance mode 'php-error.php', // PHP error handler 'wp-blog-header.php', // Core (should be in checksums) 'wp-cron.php', // Core (should be in checksums) 'wp-load.php', // Core (should be in checksums) 'wp-login.php', // Core (should be in checksums) 'wp-settings.php', // Core (should be in checksums) 'xmlrpc.php', // Core (should be in checksums) 'wp-activate.php', // Core (should be in checksums) 'wp-comments-post.php', // Core (should be in checksums) 'wp-links-opml.php', // Core (should be in checksums) 'wp-mail.php', // Core (should be in checksums) 'wp-signup.php', // Core (should be in checksums) 'wp-trackback.php', // Core (should be in checksums) ); foreach ( $root_files as $file ) { $relative = basename( $file ); if ( ! isset( $checksums[ $relative ] ) ) { // FIX (June 2026): identify GuardPress-installed root files // by content signature BEFORE the generic "known root files" // check. We don't trust the filename alone — an attacker // could drop a file named `emergency.php` with malware. The // signature check looks at the first 4KB for a marker // unique to GuardPress's installer (ASCII-art header that // contains 'GUARDPRESS' + 'EMERGENCY RECOVERY SYSTEM' or // an equivalent banner). $gp_label = self::guardpress_root_file_label( $file ); if ( $gp_label !== null ) { $result['trusted_files'][] = array( 'file' => $relative, 'label' => $gp_label, ); continue; } // Skip known legitimate files if ( ! in_array( $relative, $known_root_files, true ) ) { $result['extra_files'][] = $relative; } } } // RC-116-02 (audit 2026-06-18): a MISSING official core file must also mark the // result not-verified. A removed core file (compromise / partial cleanup / disk // failure) means core is no longer intact, so "verified" would be a false assurance. if ( ! empty( $result['modified_files'] ) || ! empty( $result['extra_files'] ) || ! empty( $result['missing_files'] ) ) { $result['verified'] = false; } return $result; } /** * FIX (June 2026): identify a root-level PHP file as a known GuardPress * helper by reading its first 4KB and matching content signatures unique * to the GuardPress installer. Returns a short human label (e.g. "Emergency * Recovery System") when matched, or null when the file is not ours. * * Why content-based and not filename-based: an attacker who can write to * the WP root can drop `emergency.php` containing malware. The signature * check refuses to mark that as trusted even though the filename matches. * * Capped read (4KB + file_size check) so a giant file or unreadable file * never blocks the scan. Safe to call on any local file path. * * @param string $filepath Absolute path to the candidate root file. * @return string|null Friendly label when trusted; null otherwise. */ private static function guardpress_root_file_label( $filepath ) { if ( ! is_string( $filepath ) || ! is_file( $filepath ) || ! is_readable( $filepath ) ) { return null; } // ACG-086 sweep (audit 2026-06-28): never read a root file's header THROUGH a symlink component or from a // non-regular file (FIFO would HANG fread). An unsafe path is NOT labelled as a trusted GuardPress root file. if ( ! self::is_safe_regular_file( $filepath ) ) { return null; } // Sanity cap — don't read absurdly large files. $size = @filesize( $filepath ); if ( $size === false || $size > 8 * 1024 * 1024 ) { return null; } $handle = @fopen( $filepath, 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- direct filesystem operation required by the malware scanner; WP_Filesystem is unsuitable here if ( ! $handle ) { return null; } $head = (string) @fread( $handle, 4096 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread -- direct filesystem operation required by the malware scanner; WP_Filesystem is unsuitable here @fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- direct filesystem operation required by the malware scanner; WP_Filesystem is unsuitable here if ( $head === '' ) { return null; } // Signature 1: emergency.php (PRO) — ASCII-art banner + version line. if ( stripos( $head, 'EMERGENCY RECOVERY SYSTEM' ) !== false && stripos( $head, 'GUARDPRESS' ) !== false ) { return __( 'GuardPress Emergency Recovery System (safe)', 'ai-cyberguard' ); } // Signature 2: php-error.php drop-in — only mark trusted when it // carries the GuardPress header. Generic php-error.php files from // other plugins/themes stay in the legacy `known_root_files` // allow-list and are silently skipped (no UI mention). if ( basename( $filepath ) === 'php-error.php' && stripos( $head, 'GuardPress' ) !== false ) { return __( 'GuardPress PHP error handler (safe)', 'ai-cyberguard' ); } return null; } /** * Get WordPress core checksums from API */ private static function get_core_checksums( $version ) { $locale = get_locale(); $url = 'https://api.wordpress.org/core/checksums/1.0/?' . http_build_query( array( 'version' => $version, 'locale' => $locale, ) ); // Debug: Log API call if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { guardpress_debug_log( 'GuardPress: Fetching checksums from: ' . $url ); } $response = wp_remote_get( $url, array( 'timeout' => 30 ) ); // Increased timeout if ( is_wp_error( $response ) ) { // Debug: Log error if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { guardpress_debug_log( 'GuardPress: API Error (locale: ' . $locale . '): ' . $response->get_error_message() ); } // Try en_US locale $url = 'https://api.wordpress.org/core/checksums/1.0/?' . http_build_query( array( 'version' => $version, 'locale' => 'en_US', ) ); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { guardpress_debug_log( 'GuardPress: Retrying with en_US: ' . $url ); } $response = wp_remote_get( $url, array( 'timeout' => 30 ) ); } if ( is_wp_error( $response ) ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { guardpress_debug_log( 'GuardPress: Final API Error: ' . $response->get_error_message() ); } return null; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); // If checksums is false or not array, try en_US fallback (locale might not have checksums) if ( ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { guardpress_debug_log( 'GuardPress: No valid checksums in response. Body type: ' . gettype( $body['checksums'] ?? null ) ); guardpress_debug_log( 'GuardPress: Response body preview: ' . substr( wp_remote_retrieve_body( $response ), 0, 300 ) ); } // Try en_US fallback if we haven't already if ( $locale !== 'en_US' ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { guardpress_debug_log( 'GuardPress: Locale ' . $locale . ' has no checksums, trying en_US fallback...' ); } $url = 'https://api.wordpress.org/core/checksums/1.0/?' . http_build_query( array( 'version' => $version, 'locale' => 'en_US', ) ); $response = wp_remote_get( $url, array( 'timeout' => 30 ) ); if ( ! is_wp_error( $response ) ) { $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['checksums'] ) && is_array( $body['checksums'] ) ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { guardpress_debug_log( 'GuardPress: en_US fallback successful! Got ' . count( $body['checksums'] ) . ' checksums' ); } return $body['checksums']; } } } return null; } if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { guardpress_debug_log( 'GuardPress: Got ' . count( $body['checksums'] ) . ' checksums' ); } return $body['checksums']; } /** * Find extra files not in checksums */ private static function find_extra_files( $dir, $checksums, $prefix = '' ) { $extra = array(); if ( ! is_dir( $dir ) ) { return $extra; } // SECURITY — ACG-069-005 (audit 2026-06-25): NO directory-prefix // allow-list. The official WordPress checksum map is the SOLE // authority for "is this core?". Any executable-family file whose // exact relative path is NOT a key in $checksums is an EXTRA file and // MUST be reported — including files under bundled-library subtrees // such as Requests/src/, PHPMailer/, sodium_compat/, SimplePie/src/, // ID3/, etc. // // Rationale: legitimate bundled-library files that genuinely ship with // core ARE present in $checksums and are already accepted by the // exact `isset( $checksums[ $relative ] )` test below — so they never // reach the "extra" branch. The old $safe_patterns prefix list could // therefore ONLY ever fire for files ABSENT from the checksums, i.e. // genuinely unknown/foreign files. An attacker who drops a new file // such as wp-includes/Requests/src/shell.php (which does NOT exist in // the official checksums) was filtered out and never reported, hiding // exactly the threat this tool exists to find. // // If a real compatibility false-positive is ever demonstrated, the // ONLY acceptable exception is an EXACT path bound to a signed // version/hash — never a directory prefix. None is needed today: a // clean core tree on the correct version/locale yields zero extras // purely from the exact-checksum test. // ACG-084-001 (audit 2026-06-27, HIGH): a core directory (wp-admin / wp-includes) is never legitimately a // symlink. If it is one, RecursiveDirectoryIterator would enumerate an EXTERNAL tree as "core extras". Refuse // to walk a symlinked core dir — the md5 loop in verify_core_files() already flags the symlinked core paths as // modified (forcing verified=false), so we simply do not descend an external target here. if ( @is_link( rtrim( $dir, '/\\' ) ) ) { return $extra; } $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS ), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD // FIX (audit 2026-06-10): skip unreadable subdirs, don't abort the walk ); foreach ( $iterator as $file ) { if ( $file->isFile() ) { $relative = $prefix . '/' . $iterator->getSubPathName(); $relative = str_replace( '\\', '/', $relative ); // RC-107-03 (audit 2026-06-17): align with the Malware Scanner's // core_dir_ext so the manual "Verify Core Files" tool and a scan // agree on what counts as an unknown core file (PHP-family + js + // svg), rather than this tool only seeing .php. The Malware Scanner's // check_core_integrity() remains the deeper authority (it also // inspects core .htaccess/asset execution chains). $rc_ext = strtolower( pathinfo( $relative, PATHINFO_EXTENSION ) ); $rc_exts = class_exists( 'GuardPress_Malware_Scanner' ) ? array_merge( GuardPress_Malware_Scanner::php_family_exts(), array( 'js', 'svg' ) ) : array( 'php', 'phtml', 'php3', 'php4', 'php5', 'php7', 'php8', 'phar', 'pht', 'phps', 'inc', 'js', 'svg' ); if ( in_array( $rc_ext, $rc_exts, true ) ) { // Sole authority: exact-path membership in the official // checksum map. Not a key => unknown/foreign executable => // report it. No prefix/substring allow-listing. if ( ! isset( $checksums[ $relative ] ) ) { $extra[] = $relative; } } } } return $extra; } /** * Compare all installed plugins with repository * * @return array Results for all plugins */ public static function compare_all_plugins() { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugins = get_plugins(); $results = array( 'total' => count( $plugins ), 'from_repo' => 0, 'modified' => 0, 'outdated' => 0, 'premium' => 0, 'plugins' => array(), ); foreach ( $plugins as $path => $info ) { // Get slug from path $parts = explode( '/', $path ); $slug = $parts[0]; // Skip GuardPress itself if ( $slug === 'ai-cyberguard' ) { continue; } $comparison = self::compare_plugin( $slug ); $results['plugins'][ $slug ] = $comparison; if ( $comparison['is_from_repo'] ) { $results['from_repo']++; if ( $comparison['status'] === 'outdated' ) { $results['outdated']++; } } else { $results['premium']++; } } return $results; } /** * Render repository comparison results */ public static function render_results_html( $core_results, $plugin_results = null ) { $html = '
' . esc_html( $mod['file'] ) . '' . esc_html__( 'These files are not part of official WordPress core. They may be from plugins, backups, or unauthorized additions. Please verify:', 'ai-cyberguard' ) . '
'; $html .= '' . esc_html( $file ) . ' ← ' . esc_html__( 'Verify this file', 'ai-cyberguard' ) . '' . esc_html( $name ) . '';
if ( $label !== '' ) {
$html .= ' — ' . esc_html( $label ) . '';
}
$html .= '