loaded_mu_plugin_files(); if ( empty( $files ) ) { return $findings; } foreach ( $files as $path ) { $entry = basename( $path ); if ( ! is_file( $path ) || 'php' !== strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ) || ! is_readable( $path ) ) { continue; } $content = ACHWPA_Preview::read_limited_file( $path ); $size = @filesize( $path ); $mtime = @filemtime( $path ); $hash = @hash_file( 'sha256', $path ); $signals = array(); $score = 0; if ( preg_match( '/(?:^|[-_.])[a-f0-9]{8,}(?:[-_.]|\.php$)/i', $entry ) ) { $signals[] = 'random-or-hex filename'; $score++; } if ( preg_match( '/\b(?:base64_decode|gzinflate|gzuncompress|str_rot13|eval|assert)\s*\(/i', $content ) ) { $signals[] = 'obfuscation/execution primitive'; $score++; } if ( preg_match( '/\b(?:file_put_contents|fopen|rename|copy|unlink)\s*\(/i', $content ) ) { $signals[] = 'filesystem write primitive'; $score++; } if ( preg_match( '/\b(?:get_option|get_site_option|update_option|set_transient|get_transient)\s*\(/i', $content ) ) { $signals[] = 'options/transients reference'; $score++; } if ( preg_match( '/\b(?:wp_schedule_event|wp_schedule_single_event|wp_next_scheduled)\s*\(/i', $content ) ) { $signals[] = 'cron reference'; $score++; } $ioc_hits = ACHWPA_IOC::matches( $entry . ' ' . $content ); if ( ! empty( $ioc_hits ) ) { $signals[] = 'known indicator'; $score += 2; } if ( 0 === $score ) { continue; } $level = $score >= 4 ? 'Review' : 'Info'; if ( $score >= 6 ) { $level = 'Warning'; } $metadata = sprintf( "File: %s\nSize: %s bytes\nModified: %s\nSHA-256: %s\n\n%s", $entry, false === $size ? 'unknown' : (string) $size, false === $mtime ? 'unknown' : gmdate( 'c', $mtime ), is_string( $hash ) ? $hash : 'unavailable', $content ); $findings[] = new ACHWPA_Finding( array( 'category' => 'MU-plugin', 'level' => $level, 'location' => 'wp-content/mu-plugins/' . $entry, 'indicator' => implode( ', ', array_unique( array_merge( $signals, $ioc_hits ) ) ), 'explanation' => __( 'The MU-plugin contains one or more persistence-related indicators. MU-plugins load automatically, so combinations involving obfuscation, database access, cron or file writes deserve additional attention.', 'aiutocomputerhelp-persistence-audit' ), 'preview' => ACHWPA_Preview::sanitize( $metadata, 1900 ), 'artifacts' => array_merge( array( 'FILE:' . $entry, 'PATH:wp-content/mu-plugins/' . $entry ), $this->extract_artifacts( $content ) ), 'identifiers' => $this->extract_identifiers( $entry . ' ' . $content ), ) ); } return $findings; } private function extract_artifacts( $content ) { $out = array(); if ( preg_match_all( '/[A-Za-z0-9_.-]{3,}\.php\b/', $content, $matches ) ) { foreach ( $matches[0] as $file ) { $out[] = 'FILE:' . basename( $file ); } } if ( preg_match_all( '/(?:_site_transient_|_transient_|wp_cache_|wp_compat_|_wp_)[A-Za-z0-9_.-]{3,}/i', $content, $matches ) ) { foreach ( $matches[0] as $name ) { $out[] = 'OPTION:' . $name; } } if ( preg_match_all( '/\b(?:wp_[A-Za-z0-9_]{5,}|[A-Za-z][A-Za-z0-9_]{7,})\b/', $content, $matches ) ) { foreach ( $matches[0] as $hook ) { if ( false !== stripos( $hook, 'cron' ) || false !== stripos( $hook, 'cache' ) || false !== stripos( $hook, 'heal' ) || false !== stripos( $hook, 'restore' ) ) { $out[] = 'HOOK:' . $hook; } } } return array_slice( array_values( array_unique( $out ) ), 0, 60 ); } private function extract_identifiers( $text ) { $ids = array(); if ( preg_match_all( '/\b[a-f0-9]{6,32}\b/i', $text, $matches ) ) { foreach ( $matches[0] as $candidate ) { if ( ! ctype_digit( $candidate ) ) { $ids[] = strtolower( $candidate ); } } } return array_slice( array_values( array_unique( $ids ) ), 0, 40 ); } private function loaded_mu_plugin_files() { if ( ! function_exists( 'get_mu_plugins' ) ) { $plugin_api = ACHWPA_Paths::wp_admin_file( 'includes/plugin.php' ); if ( '' === $plugin_api || ! is_file( $plugin_api ) ) { return array(); } require_once $plugin_api; } $registered = get_mu_plugins(); if ( empty( $registered ) ) { return array(); } $included = array_map( 'wp_normalize_path', get_included_files() ); $files = array(); foreach ( $registered as $plugin_file => $plugin_data ) { $expected_name = isset( $plugin_data['Name'] ) ? (string) $plugin_data['Name'] : ''; foreach ( $included as $candidate ) { if ( basename( $candidate ) !== basename( $plugin_file ) || ! is_file( $candidate ) ) { continue; } $candidate_data = get_plugin_data( $candidate, false, false ); $candidate_name = isset( $candidate_data['Name'] ) ? (string) $candidate_data['Name'] : ''; if ( '' === $candidate_name && basename( $plugin_file ) === $expected_name ) { $files[] = $candidate; break; } if ( '' !== $expected_name && $candidate_name === $expected_name ) { $files[] = $candidate; break; } } } return array_values( array_unique( $files ) ); } }