array( 'severity' => 'critical', 'category' => 'code_execution', 'title' => 'Code Execution Function', 'explanation' => 'The eval language construct runs arbitrary PHP code. It is the number-one function used in backdoors.', 'score' => 30, ), 'assert(' => array( 'severity' => 'critical', 'category' => 'code_execution', 'title' => 'Alternative Code Execution', 'explanation' => 'The assert function can run code much like eval. It is often used to bypass security filters.', 'score' => 28, ), 'create_function(' => array( 'severity' => 'critical', 'category' => 'code_execution', 'title' => 'Dynamic Function Creation', 'explanation' => 'create_function() creates anonymous functions from strings. Deprecated and dangerous.', 'score' => 25, ), 'call_user_func(' => array( 'severity' => 'high', 'category' => 'code_execution', 'title' => 'Indirect Function Call', 'explanation' => 'Allows calling functions by name stored in variables. Can be used to hide malicious calls.', 'score' => 15, ), 'call_user_func_array(' => array( 'severity' => 'high', 'category' => 'code_execution', 'title' => 'Indirect Function Call with Array', 'explanation' => 'Like call_user_func but with array arguments. Common in obfuscated malware.', 'score' => 15, ), 'preg_replace' => array( 'severity' => 'medium', 'category' => 'code_execution', 'title' => 'Regex Replace', 'explanation' => 'With /e modifier (deprecated), this can execute code. Check for /e in the pattern.', 'score' => 10, 'context_check' => '/e', ), // Encoding/Obfuscation - HIGH 'base64_decode(' => array( 'severity' => 'high', 'category' => 'obfuscation', 'title' => 'Base64 Decoding', 'explanation' => 'Decodes hidden content. Found in 73% of PHP backdoors to hide malicious code.', 'score' => 20, ), 'gzinflate(' => array( 'severity' => 'high', 'category' => 'obfuscation', 'title' => 'Compressed Content', 'explanation' => 'Decompresses gzip data. Used to hide large amounts of malicious code.', 'score' => 22, ), 'gzuncompress(' => array( 'severity' => 'high', 'category' => 'obfuscation', 'title' => 'Compressed Content', 'explanation' => 'Another decompression function used to hide malicious payloads.', 'score' => 22, ), 'gzdecode(' => array( 'severity' => 'high', 'category' => 'obfuscation', 'title' => 'Gzip Decoding', 'explanation' => 'Decodes gzip compressed data. Often chained with base64_decode.', 'score' => 20, ), 'str_rot13(' => array( 'severity' => 'medium', 'category' => 'obfuscation', 'title' => 'ROT13 Encoding', 'explanation' => 'Simple letter substitution. Used for basic obfuscation.', 'score' => 12, ), 'rawurldecode(' => array( 'severity' => 'low', 'category' => 'obfuscation', 'title' => 'URL Decoding', 'explanation' => 'Decodes URL-encoded strings. Sometimes used for obfuscation.', 'score' => 5, ), // Shell/System access - CRITICAL 'shell_exec(' => array( 'severity' => 'critical', 'category' => 'system_access', 'title' => 'Shell Command Execution', 'explanation' => 'Executes system commands. Gives full server access to attackers!', 'score' => 35, ), 'system(' => array( 'severity' => 'critical', 'category' => 'system_access', 'title' => 'System Command', 'explanation' => 'Executes system commands and returns output. Very dangerous!', 'score' => 35, ), 'passthru(' => array( 'severity' => 'critical', 'category' => 'system_access', 'title' => 'Command Passthrough', 'explanation' => 'Executes commands and outputs raw results. Used in web shells.', 'score' => 35, ), 'exec(' => array( 'severity' => 'critical', 'category' => 'system_access', 'title' => 'Execute Command', 'explanation' => 'Executes external programs. Can run any command on your server!', 'score' => 35, ), 'popen(' => array( 'severity' => 'critical', 'category' => 'system_access', 'title' => 'Process Open', 'explanation' => 'Opens a process for reading/writing. Can execute commands.', 'score' => 30, ), 'proc_open(' => array( 'severity' => 'critical', 'category' => 'system_access', 'title' => 'Process Control', 'explanation' => 'Advanced process execution with full I/O control.', 'score' => 30, ), 'pcntl_exec(' => array( 'severity' => 'critical', 'category' => 'system_access', 'title' => 'Process Execution', 'explanation' => 'Executes program in current process space.', 'score' => 30, ), // File operations - HIGH 'file_put_contents(' => array( 'severity' => 'high', 'category' => 'file_operation', 'title' => 'File Writing', 'explanation' => 'Writes data to files. Can be used to create new backdoors.', 'score' => 18, ), 'fwrite(' => array( 'severity' => 'high', 'category' => 'file_operation', 'title' => 'File Writing', 'explanation' => 'Writes to file handles. Used to modify or create files.', 'score' => 15, ), 'fputs(' => array( 'severity' => 'high', 'category' => 'file_operation', 'title' => 'File Writing', 'explanation' => 'Alias of fwrite(). Writes data to files.', 'score' => 15, ), 'move_uploaded_file(' => array( 'severity' => 'medium', 'category' => 'file_operation', 'title' => 'File Upload Handler', 'explanation' => 'Moves uploaded files. Could allow uploading malicious files.', 'score' => 12, ), 'chmod(' => array( 'severity' => 'medium', 'category' => 'file_operation', 'title' => 'Permission Change', 'explanation' => 'Changes file permissions. May make files executable.', 'score' => 10, ), // Network - HIGH 'curl_exec(' => array( 'severity' => 'high', 'category' => 'network', 'title' => 'External Connection', 'explanation' => 'Makes HTTP requests. Can download malware or send stolen data.', 'score' => 15, ), 'fsockopen(' => array( 'severity' => 'high', 'category' => 'network', 'title' => 'Socket Connection', 'explanation' => 'Opens network connections. Can be used for reverse shells.', 'score' => 18, ), 'pfsockopen(' => array( 'severity' => 'high', 'category' => 'network', 'title' => 'Persistent Socket', 'explanation' => 'Opens persistent network connection.', 'score' => 18, ), 'stream_socket_client(' => array( 'severity' => 'high', 'category' => 'network', 'title' => 'Stream Socket', 'explanation' => 'Creates socket connection to remote server.', 'score' => 15, ), // User input - MEDIUM (context dependent) '$_REQUEST[' => array( 'severity' => 'medium', 'category' => 'user_input', 'title' => 'User Input (REQUEST)', 'explanation' => 'Accepts user input. Combined with eval/exec = backdoor.', 'score' => 8, ), '$_GET[' => array( 'severity' => 'medium', 'category' => 'user_input', 'title' => 'User Input (GET)', 'explanation' => 'URL parameter input. Can pass commands to backdoors.', 'score' => 6, ), '$_POST[' => array( 'severity' => 'medium', 'category' => 'user_input', 'title' => 'User Input (POST)', 'explanation' => 'POST data input. Often used for backdoor commands.', 'score' => 6, ), '$_COOKIE[' => array( 'severity' => 'medium', 'category' => 'user_input', 'title' => 'Cookie Input', 'explanation' => 'Cookie data. Can contain hidden commands.', 'score' => 5, ), '$_SERVER["HTTP_' => array( 'severity' => 'medium', 'category' => 'user_input', 'title' => 'HTTP Header Input', 'explanation' => 'HTTP headers can be manipulated by attackers.', 'score' => 5, ), '$GLOBALS[' => array( 'severity' => 'medium', 'category' => 'user_input', 'title' => 'Global Variable', 'explanation' => 'Accesses global variables. Can hide malicious data.', 'score' => 8, ), // WordPress specific - MEDIUM to HIGH 'wp_insert_user(' => array( 'severity' => 'high', 'category' => 'wordpress', 'title' => 'User Creation', 'explanation' => 'Creates WordPress users. Backdoors create admin accounts!', 'score' => 20, ), 'add_action' => array( 'severity' => 'low', 'category' => 'wordpress', 'title' => 'WordPress Hook', 'explanation' => 'Normal WordPress function. Check what it hooks into.', 'score' => 2, ), 'switch_to_blog(' => array( 'severity' => 'medium', 'category' => 'wordpress', 'title' => 'Multisite Switch', 'explanation' => 'Switches between sites. Could affect all sites in network.', 'score' => 10, ), // Obfuscation indicators '\\x' => array( 'severity' => 'high', 'category' => 'obfuscation', 'title' => 'Hexadecimal Encoding', 'explanation' => 'Hex-encoded characters hide readable code. Common in malware.', 'score' => 15, ), 'chr(' => array( 'severity' => 'medium', 'category' => 'obfuscation', 'title' => 'Character Codes', 'explanation' => 'Builds strings from ASCII codes. Hides function names.', 'score' => 10, ), 'ord(' => array( 'severity' => 'low', 'category' => 'obfuscation', 'title' => 'Character to ASCII', 'explanation' => 'Converts characters to ASCII. Part of encoding schemes.', 'score' => 5, ), ); /** * Dangerous pattern combinations that significantly increase risk */ private static $dangerous_combos = array( array( 'patterns' => array( 'eval(', 'base64_decode(' ), 'title' => 'Classic Backdoor Pattern', 'explanation' => 'Running a base64-decoded payload through eval is THE most common PHP backdoor. Found in 73% of infections!', 'bonus_score' => 30, ), array( 'patterns' => array( 'eval(', 'gzinflate(' ), 'title' => 'Compressed Backdoor', 'explanation' => 'Hidden code compressed with gzip then executed. Very suspicious!', 'bonus_score' => 25, ), array( 'patterns' => array( 'eval(', '$_POST[' ), 'title' => 'Remote Code Execution', 'explanation' => 'Executes code received via POST request. Active backdoor!', 'bonus_score' => 35, ), array( 'patterns' => array( 'eval(', '$_GET[' ), 'title' => 'URL-based Backdoor', 'explanation' => 'Executes code from URL parameters. Very dangerous!', 'bonus_score' => 35, ), array( 'patterns' => array( 'eval(', '$_REQUEST[' ), 'title' => 'Request-based Backdoor', 'explanation' => 'Accepts commands from any HTTP request type.', 'bonus_score' => 35, ), array( 'patterns' => array( 'assert(', 'base64_decode(' ), 'title' => 'Encoded Assert Backdoor', 'explanation' => 'Uses the assert function instead of eval to bypass filters.', 'bonus_score' => 28, ), array( 'patterns' => array( 'shell_exec(', '$_' ), 'title' => 'Web Shell', 'explanation' => 'Executes system commands from user input. Full server access!', 'bonus_score' => 40, ), array( 'patterns' => array( 'system(', '$_' ), 'title' => 'Command Injection', 'explanation' => 'System commands controlled by user input.', 'bonus_score' => 40, ), array( 'patterns' => array( 'exec(', '$_' ), 'title' => 'Command Execution', 'explanation' => 'Server commands from external input.', 'bonus_score' => 40, ), array( 'patterns' => array( 'file_put_contents(', '$_' ), 'title' => 'File Upload Backdoor', 'explanation' => 'Allows writing arbitrary files to your server.', 'bonus_score' => 30, ), array( 'patterns' => array( 'wp_insert_user(', 'administrator' ), 'title' => 'Admin User Creation', 'explanation' => 'Creates administrator account! Backdoor gaining access.', 'bonus_score' => 35, ), array( 'patterns' => array( 'preg_replace(', '/e' ), 'title' => 'Regex Code Execution', 'explanation' => 'Uses deprecated /e modifier to execute code.', 'bonus_score' => 25, ), ); /** * Check if a pattern match is in a "safe" context (documentation, strings, comments, or HTML output) * This helps reduce false positives when security plugins or documentation contain pattern examples * * @param string $content Full file content * @param string $pattern The pattern that was found * @param int $position Position where pattern was found * @return bool True if pattern is in safe context (should be ignored or reduced severity) */ private static function is_pattern_in_safe_context( $content, $pattern, $position = null ) { // FIX (audit 2026-06-10): SECURITY — the previous heuristic was trivially // evadable and let real backdoors hide: // (a) it only inspected the FIRST occurrence, so a benign decoy // "// eval('1');" suppressed every later, live eval(); // (b) "safe" was decided by over-broad substrings ("// ", "* ", "__(", // "echo '") that appear in almost every PHP file; // (c) a naive 500-byte quote-parity test flipped on any stray // apostrophe in a preceding comment. // We now use PHP's tokenizer to identify the byte ranges that are genuine // comments or string literals, and treat the pattern as "safe" ONLY when // EVERY occurrence falls inside such a range (documentation, never // executed code). If even one occurrence is live code, it is flagged. $ranges = self::php_safe_ranges( $content ); // No comment/string ranges (non-PHP file, or tokenizer unavailable): // do NOT suppress — better a reviewable false positive than a hidden // backdoor. if ( empty( $ranges ) ) { return false; } $lc_content = strtolower( $content ); $lc_pattern = strtolower( $pattern ); $plen = strlen( $pattern ); if ( $plen === 0 ) { return false; } $found_any = false; $offset = 0; // HARDENING (2026-06-15): bound the occurrence loop so a crafted file with a // huge number of pattern hits cannot blow up scan time. If a pattern occurs // more than this many times we stop verifying and treat it as live code // (conservative — the file is flagged for review rather than silently skipped). // Far above any realistic legitimate occurrence count in a <=512KB file. $max_occurrences = 2000; $seen = 0; while ( ( $pos = strpos( $lc_content, $lc_pattern, $offset ) ) !== false ) { if ( ++$seen > $max_occurrences ) { return false; } $found_any = true; $in_safe = false; foreach ( $ranges as $r ) { if ( $pos >= $r[0] && $pos < $r[1] ) { $in_safe = true; break; } } if ( ! $in_safe ) { // At least one occurrence is live code -> NOT safe context. return false; } $offset = $pos + $plen; } // Every occurrence was inside a comment/string literal. return $found_any; } /** * FIX (audit 2026-06-10): tokenizer-derived map of byte ranges that are * comments or string literals in PHP source. Used to decide, soundly, * whether a matched pattern is real code or documentation. Cached per * content hash (token_get_all is relatively expensive). * * @param string $content * @return array list of [startByte, endByte) ranges */ private static function php_safe_ranges( $content ) { $key = md5( $content ); if ( isset( self::$safe_ranges_cache[ $key ] ) ) { return self::$safe_ranges_cache[ $key ]; } $ranges = array(); if ( function_exists( 'token_get_all' ) && strpos( $content, ' 64 ) { self::$safe_ranges_cache = array(); } self::$safe_ranges_cache[ $key ] = $ranges; return $ranges; } /** * Scan a file for suspicious patterns * * @param string $filepath Full path to file * @return array Scan results */ public static function scan_file( $filepath ) { $result = array( 'filepath' => $filepath, 'risk_score' => 0, 'findings' => array(), 'combos' => array(), 'summary' => '', 'recommendation' => '', // FIX (audit 2026-06-10): always carry a 'level' so the early-return // paths below (file missing, scan-limit, too-large, unreadable) don't // leave it unset — callers treated a missing level as "clean" (score // 0), which could mark an unscanned large/infected file as safe. // 'unknown' = "not actually scanned"; generate_summary() overrides it // with clean/low/medium/high/critical for files that ARE scanned. 'level' => 'unknown', ); // ACG-082-H1 / ACG-084-H1 (audit 2026-06-27, MED): never read THROUGH a symlink (or symlinked PARENT, or any // non-regular file). file_exists()/filesize()/file_get_contents() FOLLOW a link to its external target, so a // symlinked in-install path would be scanned against content OUTSIDE the install. ACG-084-H1: adopt the SHARED // host-aware no-follow contract (tolerates a symlink AT a trusted root such as a host-published wp-content/ // uploads, refuses one deeper) when available; the v83 lstat + ABSPATH-containment below is the fallback. if ( class_exists( 'GuardPress_FS_Helpers' ) && method_exists( 'GuardPress_FS_Helpers', 'regular_file_in_abspath_no_symlink_components' ) ) { if ( ! GuardPress_FS_Helpers::regular_file_in_abspath_no_symlink_components( $filepath ) ) { return $result; // symlink component / non-regular / outside install → not scanned } } else { clearstatcache( false, $filepath ); if ( @is_link( $filepath ) || ( @file_exists( $filepath ) && ! @is_file( $filepath ) ) ) { return $result; } if ( defined( 'ABSPATH' ) ) { $rp = @realpath( $filepath ); $ra = @realpath( ABSPATH ); if ( false !== $rp && false !== $ra && 0 !== strpos( str_replace( '\\', '/', $rp ), rtrim( str_replace( '\\', '/', $ra ), '/' ) . '/' ) ) { return $result; // resolves outside the install → not scanned } } } // Check file exists and is readable if ( ! file_exists( $filepath ) || ! is_readable( $filepath ) ) { return $result; } // Performance limit - don't scan too many files per request if ( self::$scan_count >= self::MAX_FILES_PER_REQUEST ) { $result['summary'] = __( 'Scan limit reached for this request', 'ai-cyberguard' ); return $result; } // Get file size - skip very large files $filesize = filesize( $filepath ); if ( $filesize > 512 * 1024 ) { // 512KB max $result['summary'] = __( 'File too large for pattern scanning', 'ai-cyberguard' ); return $result; } // Cache key based on file path and modification time $cache_key = md5( $filepath . filemtime( $filepath ) ); // Return cached result if available if ( isset( self::$cache[ $cache_key ] ) ) { return self::$cache[ $cache_key ]; } // Increment scan counter self::$scan_count++; // Read file content $content = @file_get_contents( $filepath ); if ( $content === false ) { return $result; } // Track found patterns for combo detection $found_patterns = array(); // Scan for each pattern foreach ( self::$patterns as $pattern => $info ) { if ( stripos( $content, $pattern ) !== false ) { // Count occurrences $count = substr_count( strtolower( $content ), strtolower( $pattern ) ); // Check context if needed if ( isset( $info['context_check'] ) ) { if ( stripos( $content, $info['context_check'] ) === false ) { continue; // Context not found, skip } } // Skip patterns in safe contexts (documentation, comments, strings) // This prevents false positives in security plugins and documentation if ( self::is_pattern_in_safe_context( $content, $pattern ) ) { continue; // Pattern is in documentation/safe context, skip } $found_patterns[] = $pattern; $result['risk_score'] += $info['score']; $result['findings'][] = array( 'pattern' => $pattern, 'count' => $count, 'severity' => $info['severity'], 'category' => $info['category'], 'title' => $info['title'], 'explanation' => $info['explanation'], 'score' => $info['score'], ); } } // === Threat Feed Patterns (Real-time threat intelligence) === if ( class_exists( 'GuardPress_Threat_Feed' ) ) { $feed_patterns = GuardPress_Threat_Feed::get_signatures(); if ( is_array( $feed_patterns ) && ! empty( $feed_patterns ) ) { foreach ( $feed_patterns as $sig ) { // Skip if no pattern if ( empty( $sig['pattern'] ) ) { continue; } // Try regex pattern $pattern = $sig['pattern']; $matches = array(); // Suppress errors for invalid patterns if ( @preg_match( $pattern, $content, $matches ) ) { $found_patterns[] = $sig['id'] ?? 'feed_pattern'; // Map severity to score $severity_scores = array( 'critical' => 30, 'high' => 20, 'medium' => 10, 'low' => 5, ); $severity = $sig['severity'] ?? 'medium'; $score = $severity_scores[ $severity ] ?? 10; $result['risk_score'] += $score; $result['findings'][] = array( 'pattern' => $sig['name'] ?? 'Threat Feed Match', 'count' => 1, 'severity' => $severity, 'category' => $sig['category'] ?? 'threat_feed', 'title' => $sig['name'] ?? __( 'Threat Intelligence Match', 'ai-cyberguard' ), 'explanation' => $sig['description'] ?? __( 'Matched a signature from the AI CyberGuard threat database.', 'ai-cyberguard' ), 'score' => $score, 'source' => 'threat_feed', ); } } } // === Check file hash against known malware === $file_hash = md5_file( $filepath ); if ( $file_hash ) { $malware_info = GuardPress_Threat_Feed::check_hash( $file_hash ); if ( $malware_info ) { $result['risk_score'] += 50; // Known malware = very high score $result['findings'][] = array( 'pattern' => 'Known Malware Hash', 'count' => 1, 'severity' => 'critical', 'category' => 'known_malware', /* translators: %s is the malware name from the signature database. */ 'title' => sprintf( __( 'Known Malware: %s', 'ai-cyberguard' ), $malware_info['name'] ?? 'Unknown' ), 'explanation' => __( 'This file\'s hash matches a known malware signature in our database.', 'ai-cyberguard' ), 'score' => 50, 'source' => 'threat_feed_hash', 'malware_name' => $malware_info['name'] ?? '', ); } } } // Check for dangerous combinations foreach ( self::$dangerous_combos as $combo ) { $all_found = true; foreach ( $combo['patterns'] as $required_pattern ) { $pattern_found = false; foreach ( $found_patterns as $fp ) { if ( stripos( $fp, $required_pattern ) !== false || stripos( $required_pattern, $fp ) !== false ) { $pattern_found = true; break; } } // Also check content directly for partial matches. // FIX (audit 2026-06-10): respect safe-context here too, otherwise // a combo (e.g. eval(+base64_decode() could fire purely on // documentation/comments that the per-pattern pass correctly // suppressed, re-introducing the false positives we just removed. if ( ! $pattern_found && stripos( $content, $required_pattern ) !== false && ! self::is_pattern_in_safe_context( $content, $required_pattern ) ) { $pattern_found = true; } if ( ! $pattern_found ) { $all_found = false; break; } } if ( $all_found ) { $result['risk_score'] += $combo['bonus_score']; $result['combos'][] = array( 'patterns' => $combo['patterns'], 'title' => $combo['title'], 'explanation' => $combo['explanation'], 'bonus_score' => $combo['bonus_score'], ); } } // Additional checks $result = self::check_additional_indicators( $content, $filepath, $result ); // Generate summary and recommendation $result = self::generate_summary( $result ); // Save to cache self::$cache[ $cache_key ] = $result; return $result; } /** * Additional checks beyond pattern matching */ private static function check_additional_indicators( $content, $filepath, $result ) { $filename = basename( $filepath ); // Check for very long lines (obfuscated code) $lines = explode( "\n", $content ); foreach ( $lines as $line ) { if ( strlen( $line ) > 1000 ) { $result['risk_score'] += 15; $result['findings'][] = array( 'pattern' => 'Very long line', 'count' => 1, 'severity' => 'high', 'category' => 'obfuscation', 'title' => __( 'Extremely Long Code Line', 'ai-cyberguard' ), 'explanation' => __( 'Lines over 1000 characters often contain obfuscated malicious code.', 'ai-cyberguard' ), 'score' => 15, ); break; // Only count once } } // Check for high entropy (randomized variable names) if ( preg_match_all( '/\$[a-zA-Z0-9]{15,}/', $content, $matches ) ) { if ( count( $matches[0] ) > 3 ) { $result['risk_score'] += 12; $result['findings'][] = array( 'pattern' => 'Random variable names', 'count' => count( $matches[0] ), 'severity' => 'medium', 'category' => 'obfuscation', 'title' => __( 'Randomized Variable Names', 'ai-cyberguard' ), 'explanation' => __( 'Very long random variable names suggest obfuscated malware.', 'ai-cyberguard' ), 'score' => 12, ); } } // Check for encoded strings (high ratio of non-alphanumeric) $alpha = preg_match_all( '/[a-zA-Z0-9]/', $content ); $total = strlen( $content ); if ( $total > 500 && $alpha / $total < 0.3 ) { $result['risk_score'] += 18; $result['findings'][] = array( 'pattern' => 'Encoded content', 'count' => 1, 'severity' => 'high', 'category' => 'obfuscation', 'title' => __( 'Heavily Encoded Content', 'ai-cyberguard' ), 'explanation' => __( 'File contains mostly non-readable characters. Likely encoded malware.', 'ai-cyberguard' ), 'score' => 18, ); } // Check for hidden files if ( strpos( $filename, '.' ) === 0 && $filename !== '.htaccess' ) { $result['risk_score'] += 10; $result['findings'][] = array( 'pattern' => 'Hidden file', 'count' => 1, 'severity' => 'medium', 'category' => 'suspicious_name', 'title' => __( 'Hidden File (starts with dot)', 'ai-cyberguard' ), 'explanation' => __( 'Hidden files can contain backdoors that are easy to miss.', 'ai-cyberguard' ), 'score' => 10, ); } // Check location-based risk $path_lower = strtolower( $filepath ); if ( strpos( $path_lower, '/uploads/' ) !== false && pathinfo( $filepath, PATHINFO_EXTENSION ) === 'php' ) { $result['risk_score'] += 25; $result['findings'][] = array( 'pattern' => 'PHP in uploads', 'count' => 1, 'severity' => 'critical', 'category' => 'location', 'title' => __( 'PHP File in Uploads Folder', 'ai-cyberguard' ), 'explanation' => __( 'PHP files should NEVER be in the uploads folder. This is a major red flag!', 'ai-cyberguard' ), 'score' => 25, ); } return $result; } /** * Generate summary and recommendation */ private static function generate_summary( $result ) { $score = $result['risk_score']; $findings_count = count( $result['findings'] ); $combos_count = count( $result['combos'] ); if ( $score >= 70 ) { $result['level'] = 'critical'; $result['summary'] = sprintf( /* translators: 1: number of suspicious patterns found, 2: number of dangerous pattern combinations, 3: numeric risk score. */ __( 'CRITICAL: %1$d suspicious patterns found with %2$d dangerous combinations. Risk score: %3$d', 'ai-cyberguard' ), $findings_count, $combos_count, $score ); $result['recommendation'] = __( 'Immediately quarantine this file and investigate. Do NOT execute it. Consider restoring from backup or deleting.', 'ai-cyberguard' ); } elseif ( $score >= 40 ) { $result['level'] = 'high'; $result['summary'] = sprintf( /* translators: 1: number of suspicious patterns found, 2: numeric risk score. */ __( 'HIGH RISK: %1$d suspicious patterns found. Risk score: %2$d', 'ai-cyberguard' ), $findings_count, $score ); $result['recommendation'] = __( 'Review this file carefully. Compare with original version if available. Consider using AI Analysis for deeper inspection.', 'ai-cyberguard' ); } elseif ( $score >= 20 ) { $result['level'] = 'medium'; $result['summary'] = sprintf( /* translators: 1: number of patterns detected, 2: numeric risk score. */ __( 'MEDIUM RISK: %1$d patterns detected. Risk score: %2$d', 'ai-cyberguard' ), $findings_count, $score ); $result['recommendation'] = __( 'Patterns found may be legitimate but warrant review. Check if file is part of a known plugin/theme.', 'ai-cyberguard' ); } elseif ( $score > 0 ) { $result['level'] = 'low'; $result['summary'] = sprintf( /* translators: 1: number of minor patterns found, 2: numeric risk score. */ __( 'LOW RISK: %1$d minor patterns found. Risk score: %2$d', 'ai-cyberguard' ), $findings_count, $score ); $result['recommendation'] = __( 'Likely safe. Patterns found are common in legitimate code but monitor for changes.', 'ai-cyberguard' ); } else { $result['level'] = 'safe'; $result['summary'] = __( 'No suspicious patterns detected.', 'ai-cyberguard' ); $result['recommendation'] = __( 'File appears clean. Continue monitoring for changes.', 'ai-cyberguard' ); } return $result; } /** * Get all patterns (for settings/documentation) */ public static function get_patterns() { return self::$patterns; } /** * Get dangerous combinations */ public static function get_dangerous_combos() { return self::$dangerous_combos; } /** * Get risk level from score */ public static function get_risk_level( $score ) { if ( $score >= 70 ) return 'critical'; if ( $score >= 40 ) return 'high'; if ( $score >= 20 ) return 'medium'; if ( $score > 0 ) return 'low'; return 'safe'; } /** * Quick check - returns just the score without full details */ public static function quick_score( $filepath ) { $result = self::scan_file( $filepath ); return $result['risk_score']; } }