'php_eval_input', 'title' => 'eval() executes attacker-supplied input', 'severity' => 'critical', 'confidence' => 97, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\beval\s*\([^;]{0,200}\$_(?:GET|POST|REQUEST|COOKIE|SERVER|FILES)\b/i', 'description' => 'Code taken directly from the request is executed with eval(). This is a remote code execution backdoor.', ); $r[] = array( 'id' => 'php_exec_input', 'title' => 'Shell command built from request input', 'severity' => 'critical', 'confidence' => 96, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\b(?:system|exec|shell_exec|passthru|popen|proc_open|pcntl_exec)\s*\([^;]{0,200}(?:\$_(?:GET|POST|REQUEST|COOKIE)\b|base64_decode\s*\(|stripslashes\s*\(\s*\$_)/i', 'description' => 'A system shell function receives attacker-controlled input. This is the signature of a web shell.', ); $r[] = array( 'id' => 'php_assert_input', 'title' => 'assert() used as a code-execution backdoor', 'severity' => 'critical', 'confidence' => 94, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\bassert\s*\([^;]{0,200}(?:\$_(?:GET|POST|REQUEST|COOKIE)\b|base64_decode\s*\(|stripslashes\s*\()/i', 'description' => 'assert() evaluates a string as PHP code; feeding it request data or decoded payloads is a classic backdoor.', ); $r[] = array( 'id' => 'php_input_as_function', 'title' => 'Request parameter called as a function', 'severity' => 'critical', 'confidence' => 93, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\$_(?:GET|POST|REQUEST|COOKIE)\s*\[[^\]]+\]\s*\(/i', 'description' => 'A value from the request is used as the function name (e.g. $_GET["f"]($_GET["a"])). Attackers can call any PHP function.', ); $r[] = array( 'id' => 'php_callback_exec', 'title' => 'Callback function points at eval/assert/system', 'severity' => 'critical', 'confidence' => 92, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\b(?:array_map|array_filter|array_walk|usort|uasort|uksort|call_user_func(?:_array)?|register_shutdown_function|ob_start|array_reduce|iterator_apply|preg_replace_callback)\s*\(\s*[\'"]?\s*(?:assert|eval|system|exec|passthru|shell_exec|create_function|popen|proc_open)\b/i', 'description' => 'Dangerous functions are smuggled in as string callbacks to evade simple scanners.', ); $r[] = array( 'id' => 'php_preg_replace_e', 'title' => 'preg_replace() with the /e modifier (code execution)', 'severity' => 'critical', 'confidence' => 90, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\bpreg_replace\s*\(\s*[\'"](?:[^\'"\\\\]|\\\\.)*\/[a-zA-Z]*e[a-zA-Z]*[\'"]\s*,/', 'description' => 'The deprecated /e modifier evaluates the replacement as PHP code. It is only ever seen in legacy malware.', ); $r[] = array( 'id' => 'php_backtick_input', 'title' => 'Backtick shell execution of request data', 'severity' => 'critical', 'confidence' => 92, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'code_only' => true, 'pattern' => '/`[^`\n]{0,100}\$_(?:GET|POST|REQUEST|COOKIE)[^`\n]{0,100}`/', 'description' => 'Backticks run the enclosed string as a shell command; here it contains request input.', ); $r[] = array( 'id' => 'php_base64_input', 'title' => 'Request data is base64-decoded before use', 'severity' => 'critical', 'confidence' => 88, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\bbase64_decode\s*\(\s*(?:stripslashes\s*\(\s*)?\$_(?:GET|POST|REQUEST|COOKIE)\b/i', 'description' => 'Decoding attacker input is how backdoors receive hidden commands.', ); $r[] = array( 'id' => 'php_md5_password_gate', 'title' => 'Password-protected backdoor (hashed secret compared with request)', 'severity' => 'critical', 'confidence' => 90, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\b(?:md5|sha1|crc32|hash)\s*\(\s*\$_(?:GET|POST|COOKIE|REQUEST)\s*\[[^\]]+\]\s*\)\s*[!=]==?\s*[\'"][a-f0-9]{8,}[\'"]/i', 'description' => 'A hashed secret from the request gates access; this is how web shells authenticate their operator.', ); $r[] = array( 'id' => 'php_cookie_backdoor', 'title' => 'Cookie value compared with a hard-coded hash', 'severity' => 'critical', 'confidence' => 85, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\$_COOKIE\s*\[[^\]]+\]\s*\)?\s*===?\s*[\'"][a-f0-9]{32}[\'"]/i', 'description' => 'Backdoors frequently authenticate through a secret cookie.', ); $r[] = array( 'id' => 'php_known_shell', 'title' => 'Known web shell family detected', 'severity' => 'critical', 'confidence' => 98, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php', 'text' ), 'pattern' => '/\b(?:c99shell|c99madshell|r57shell|b374k|FilesMan|WSO\s?[0-9.]+|IndoXploit|AnonymousFox|AlfaShell|ALFA TEAM|Mister Spy|K2ll33d|Priv8 Shell|Sh3ll|TryagShell|wso\.php|Web Shell by|Mini Shell|P\.A\.S\. v|uname\s*-a;\s*w;\s*id|safe_mode_bypass)\b/i', 'description' => 'The file identifies itself as a known web shell (c99, r57, WSO, b374k, AnonymousFox, Alfa, IndoXploit...).', ); $r[] = array( 'id' => 'php_shell_ui', 'title' => 'Command execution form (web shell UI)', 'severity' => 'critical', 'confidence' => 90, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/]+name\s*=\s*[\'"]?(?:cmd|command|shell|exec|c)[\'"]?[^>]*>/i', 'requires' => array( '/\b(?:system|exec|shell_exec|passthru|popen|proc_open)\s*\(/i' ), 'description' => 'An HTML form posts a command to a shell function: an interactive web shell.', ); $r[] = array( 'id' => 'php_wpvcd', 'title' => 'WP-VCD malware family', 'severity' => 'critical', 'confidence' => 97, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php', 'any' ), 'pattern' => '/\b(?:wp-vcd|wp_vcd|wp-tmp\.php|wp-feed\.php|class\.wp\.php|class\.theme-modules\.php|wp_cd_code|wp_class_check|theme_temp_setup|wp_temp_setupx)\b/i', 'description' => 'Strings unique to the WP-VCD family (spread through nulled themes/plugins; creates hidden admins and injects spam).', ); $r[] = array( 'id' => 'php_reversed_funcs', 'title' => 'Reversed function names (strrev obfuscation)', 'severity' => 'critical', 'confidence' => 93, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php', 'js' ), 'pattern' => '/\b(?:edoced_46esab|etalfnizg|ecalper_gerp|tressa|lave|noitcnuf_etaerc|sserpmocnuzg|31tor_rts)\b/', 'description' => 'base64_decode, gzinflate, eval etc. written backwards so simple greps miss them.', ); $r[] = array( 'id' => 'php_hidden_admin_creation', 'title' => 'Hidden administrator account creation', 'severity' => 'critical', 'confidence' => 88, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\b(?:wp_insert_user|wp_create_user)\s*\([^;]{0,400}?(?:administrator|[\'"]role[\'"]\s*=>\s*[\'"]administrator)/is', 'requires' => array( '/[\'"]user_pass[\'"]\s*=>\s*[\'"][^\'"]{3,}[\'"]|wp_create_user\s*\(\s*[\'"][^\'"]+[\'"]\s*,\s*[\'"][^\'"]+[\'"]/i' ), 'description' => 'An administrator is created with a hard-coded password. Legitimate code never ships admin credentials.', ); $r[] = array( 'id' => 'php_auto_login', 'title' => 'Automatic login as a privileged user', 'severity' => 'high', 'confidence' => 65, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'escalate' => array( 'upload' => 90, 'root' => 88, 'theme' => 88, 'other' => 88, 'dropin' => 85, 'mu' => 80 ), 'escalate_severity' => 'critical', 'escalate_verdict' => 'confirmed', 'types' => array( 'php' ), 'pattern' => '/\bwp_set_auth_cookie\s*\(\s*(?:1|\$_(?:GET|POST|REQUEST|COOKIE)[^)]*|\$(?:admin|user)s?\s*(?:\[\s*0\s*\])?\s*->\s*ID)\s*[,)]/i', 'requires' => array( '/\$_(?:GET|POST|REQUEST|COOKIE)\s*\[[^\]]+\]\s*[!=]==?|wp_set_current_user\s*\(\s*1\s*\)|get_users\s*\(\s*(?:array\s*\(|\[)[^)]*administrator/i' ), 'description' => 'A secret request parameter logs the visitor in as an administrator without a password (backdoor login).', ); $r[] = array( 'id' => 'php_hide_users_filter', 'title' => 'Users hidden from the admin user list', 'severity' => 'critical', 'confidence' => 86, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/add_(?:action|filter)\s*\(\s*[\'"](?:pre_user_query|views_users|wp_count_users|users_list_table_query_args)[\'"]/i', 'requires' => array( '/user_login\s*(?:!=|<>|NOT\s+IN)|->query_where\s*\.=|->query_where\s*=\s*str_replace/i' ), 'description' => 'Code rewrites the users query to hide a specific account from Users > All Users: this hides a rogue admin.', ); $r[] = array( 'id' => 'php_hide_plugin', 'title' => 'Plugin hides itself from the plugin list', 'severity' => 'high', 'confidence' => 60, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/add_filter\s*\(\s*[\'"]all_plugins[\'"]/i', 'requires' => array( '/\bunset\s*\(\s*\$\w+\s*\[/i' ), 'description' => 'The all_plugins filter is used to remove an entry from Plugins. Some management plugins do this, malware does it to hide.', ); $r[] = array( 'id' => 'php_remote_include', 'title' => 'Remote file inclusion', 'severity' => 'critical', 'confidence' => 92, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\b(?:include|require)(?:_once)?\s*\(?\s*[\'"](?:https?|ftps?|php):\/\//i', 'description' => 'PHP code is loaded from a remote URL or a php:// stream wrapper.', ); $r[] = array( 'id' => 'php_include_media', 'title' => 'PHP include of an image/text/icon file (hidden payload loader)', 'severity' => 'critical', 'confidence' => 92, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'code_only' => true, 'pattern' => '/(?:\'"-])(?:include|require)(?:_once)?\b\s*\(?[^;\n]{0,200}?\.(?:ico|png|jpe?g|gif|bmp|webp|txt|log|css|svg|dat|tmp|bak|zip)[\'"]\s*\)?\s*;/i', 'description' => 'Malware hides PHP inside files with harmless extensions (.ico, .png, .txt) and includes them from a loader.', ); $r[] = array( 'id' => 'php_include_uploads', 'title' => 'PHP include from the uploads directory', 'severity' => 'high', 'confidence' => 75, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\b(?:include|require)(?:_once)?\s*\(?[^;]{0,200}uploads\/[^;]{0,120}\.php/i', 'description' => 'Executable code is loaded from the uploads folder, which should only hold media.', ); $r[] = array( 'id' => 'php_dropper', 'title' => 'Writes a PHP file from request data (dropper)', 'severity' => 'high', 'confidence' => 60, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'escalate' => array( 'upload' => 90, 'root' => 88, 'theme' => 88, 'other' => 88, 'dropin' => 85, 'mu' => 80 ), 'escalate_severity' => 'critical', 'escalate_verdict' => 'confirmed', 'types' => array( 'php' ), 'pattern' => '/\b(?:file_put_contents|fwrite|fputs)\s*\([^;]{0,300}(? array( '/\$_(?:GET|POST|REQUEST|COOKIE|FILES)\b|base64_decode\s*\(|curl_exec\s*\(|file_get_contents\s*\(\s*[\'"]https?:/i' ), 'description' => 'Code writes a .php file whose content comes from the request or a remote server.', ); $r[] = array( 'id' => 'php_upload_handler', 'title' => 'Unrestricted file upload handler', 'severity' => 'high', 'confidence' => 70, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\bmove_uploaded_file\s*\(\s*\$_FILES\s*\[[^\]]+\]\s*\[\s*[\'"]tmp_name[\'"]\s*\]\s*,[^;]{0,120}\$_FILES\s*\[[^\]]+\]\s*\[\s*[\'"]name[\'"]\s*\]/i', 'description' => 'An uploaded file is saved under its original name with no validation: attackers use this to plant shells.', ); $r[] = array( 'id' => 'php_extract_input', 'title' => 'extract() of request variables', 'severity' => 'high', 'confidence' => 65, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\bextract\s*\(\s*\$_(?:GET|POST|REQUEST|COOKIE)\b/i', 'description' => 'extract() turns every request parameter into a variable; backdoors use it to receive commands.', ); $r[] = array( 'id' => 'php_unserialize_input', 'title' => 'unserialize() of untrusted input', 'severity' => 'high', 'confidence' => 70, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\bunserialize\s*\(\s*(?:base64_decode\s*\(\s*)?(?:stripslashes\s*\(\s*)?\$_(?:GET|POST|REQUEST|COOKIE)\b/i', 'description' => 'PHP object injection: attacker-controlled serialized data is unserialized.', ); $r[] = array( 'id' => 'php_mailer_script', 'title' => 'Spam mailer script', 'severity' => 'high', 'confidence' => 78, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\b(?:mail|wp_mail)\s*\(\s*(?:\$_(?:POST|GET|REQUEST)\s*\[|\$(?:to|email|recipient)s?\s*,\s*\$_(?:POST|GET|REQUEST))/i', 'description' => 'Email recipient and content come straight from the request: a classic spam relay.', ); $r[] = array( 'id' => 'php_reverse_shell', 'title' => 'Reverse shell / socket connection to a shell', 'severity' => 'critical', 'confidence' => 85, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\b(?:fsockopen|stream_socket_client|socket_create)\s*\(/i', 'requires' => array( '/\/bin\/(?:ba)?sh|proc_open\s*\(|popen\s*\(|exec\s*\(\s*[\'"](?:\/bin|cmd)/i' ), 'description' => 'A network socket is wired to a system shell: an attacker gets an interactive shell on the server.', ); $r[] = array( 'id' => 'php_cron_payload', 'title' => 'Scheduled task fetches and runs remote code', 'severity' => 'high', 'confidence' => 55, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'escalate' => array( 'upload' => 90, 'root' => 86, 'theme' => 86, 'other' => 86, 'dropin' => 84, 'mu' => 80 ), 'escalate_severity' => 'critical', 'escalate_verdict' => 'confirmed', 'types' => array( 'php' ), 'pattern' => '/\bwp_schedule_(?:single_)?event\s*\(|add_action\s*\(\s*[\'"](?:init|wp_loaded|shutdown)[\'"]/i', 'requires' => array( '/\b(?:wp_remote_get|wp_remote_post|file_get_contents|curl_exec)\s*\([^;]{0,200}/i', '/\b(?:eval|assert|base64_decode|gzinflate|str_rot13|create_function)\s*\(/i' ), 'description' => 'A hook or cron job downloads content from a remote server and executes decoded code.', ); /* ------------------------------------------------------------------ * Obfuscation & encoded payloads * ---------------------------------------------------------------- */ $r[] = array( 'id' => 'php_eval_decode', 'title' => 'eval() of an encoded/compressed payload', 'severity' => 'critical', 'confidence' => 92, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\b(?:eval|assert)\s*\(\s*(?:@\s*)?(?:base64_decode|gzinflate|gzuncompress|gzdecode|str_rot13|strrev|hex2bin|rawurldecode|urldecode|convert_uu(?:en|de)code|pack)\s*\(/i', 'description' => 'Executing code only after decoding it has no legitimate purpose; it hides the real payload from reviewers.', ); $r[] = array( 'id' => 'php_layered_decode', 'title' => 'Layered decoding chain (gzinflate/base64/rot13)', 'severity' => 'high', 'confidence' => 80, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\b(?:gzinflate|gzuncompress|gzdecode|str_rot13|strrev|hex2bin)\s*\(\s*(?:base64_decode|str_rot13|strrev|gzinflate|hex2bin)\s*\(/i', 'description' => 'Multiple decoding layers are stacked, a common way to hide payloads.', ); $r[] = array( 'id' => 'php_long_base64_decode', 'title' => 'Large base64 blob is decoded at runtime', 'severity' => 'high', 'confidence' => 70, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/[\'"][A-Za-z0-9+\/=]{400,}[\'"]/', 'requires' => array( '/\bbase64_decode\s*\(/i' ), 'description' => 'A long base64 string combined with base64_decode() usually hides code. (Embedded images without decode are not flagged.)', ); $r[] = array( 'id' => 'php_hex_strings', 'title' => 'Hex-escaped strings hide function names', 'severity' => 'high', 'confidence' => 75, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/(?:\\\\x[0-9a-fA-F]{2}){8,}|(?:\\\\[0-7]{3}){8,}/', 'description' => 'Long runs of \\x.. or octal escapes are used to spell out eval/base64_decode without writing them.', ); $r[] = array( 'id' => 'php_chr_chain', 'title' => 'String rebuilt character by character', 'severity' => 'high', 'confidence' => 78, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/(?:chr\s*\(\s*\d+\s*\)\s*\.\s*){6,}/i', 'description' => 'chr(101).chr(118)... reconstructs words like "eval" at runtime to defeat pattern matching.', ); $r[] = array( 'id' => 'php_obfuscated_vars', 'title' => 'Obfuscated variable names', 'severity' => 'high', 'confidence' => 72, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\$(?:[O0]{4,}|[Il1]{4,}|_0x[0-9a-fA-F]{3,}|[a-zA-Z]{1,2}[0-9]{5,})\b/', 'min_count' => 4, 'description' => 'Variables named like $O0O0O, $IlIl1 or $_0x1a2b are produced by obfuscators, not by developers.', ); $r[] = array( 'id' => 'php_globals_trick', 'title' => '${"GLOBALS"} obfuscation', 'severity' => 'high', 'confidence' => 72, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\$\{\s*[\'"]GLOBALS[\'"]\s*\}\s*\[|\$GLOBALS\s*\[\s*\$GLOBALS\s*\[/', 'description' => 'Variable-variable tricks through $GLOBALS are a hallmark of obfuscated malware.', ); $r[] = array( 'id' => 'php_concat_function_name', 'title' => 'Function name assembled from string fragments', 'severity' => 'medium', 'confidence' => 60, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\$\w+\s*=\s*(?:[\'"][a-zA-Z0-9_]{1,4}[\'"]\s*\.\s*){4,}[\'"][a-zA-Z0-9_]{1,4}[\'"]\s*;/', 'requires' => array( '/\$\w+\s*\(\s*(?:\$\w+|[\'"])/' ), 'description' => '"ba"."se6"."4_de"."code" style concatenation followed by a variable function call.', ); $r[] = array( 'id' => 'php_create_function_decode', 'title' => 'create_function() with decoded body', 'severity' => 'critical', 'confidence' => 90, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/\bcreate_function\s*\([^;]{0,200}(?:base64_decode|gzinflate|str_rot13|strrev|hex2bin)\s*\(/i', 'description' => 'create_function() compiles a decoded string into a function: an eval in disguise.', ); $r[] = array( 'id' => 'php_variable_function_input', 'title' => 'Variable function called with request data', 'severity' => 'high', 'confidence' => 68, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\$[a-zA-Z_]\w*\s*\(\s*(?:stripslashes\s*\(\s*)?\$_(?:GET|POST|REQUEST|COOKIE)\s*\[/i', 'description' => 'A function whose name is stored in a variable receives raw request input.', ); $r[] = array( 'id' => 'php_error_prelude', 'title' => 'Error-suppression prelude typical of malware', 'severity' => 'medium', 'confidence' => 55, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/@?error_reporting\s*\(\s*0\s*\)\s*;\s*@?ini_set\s*\(\s*[\'"]display_errors[\'"]\s*,\s*[\'"]?(?:0|off|false)[\'"]?\s*\)/i', 'requires' => array( '/\b(?:eval|assert|base64_decode|gzinflate|str_rot13|create_function|\$_(?:GET|POST|COOKIE|REQUEST))\b/i' ), 'description' => 'Malware silences all errors first so it never reveals itself in logs. Only reported together with other indicators.', ); $r[] = array( 'id' => 'php_encoded_loader', 'title' => 'Encoded PHP loader (ion' . 'Cube / Zend Guard / SourceGuardian)', 'severity' => 'info', 'confidence' => 20, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\b(?:ion[C]ube Loader|Zend Guard|SourceGuardian|extension_loaded\s*\(\s*[\'"]ion[C]ube)/i', 'description' => 'The file is commercially encoded and cannot be inspected. Confirm it belongs to a plugin you bought.', ); /* ------------------------------------------------------------------ * SEO spam, redirects, injected JavaScript * ---------------------------------------------------------------- */ $r[] = array( 'id' => 'spam_keywords', 'title' => 'SEO spam keywords (pharma / casino / adult / essay)', 'severity' => 'medium', 'confidence' => 45, 'verdict' => 'suspicious', 'remediation' => 'remove_spam', 'escalate' => array( 'upload' => 80, 'root' => 75, 'theme' => 72, 'other' => 72, 'db' => 70, 'core' => 80 ), 'types' => array( 'php', 'js', 'html', 'text', 'db' ), 'pattern' => '/\b' . self::SPAM_WORDS . '\b/i', 'min_count' => 3, 'description' => 'Repeated gambling, pharmacy, adult or essay-writing keywords indicate injected SEO spam.', ); $r[] = array( 'id' => 'hidden_links', 'title' => 'Hidden links (display:none / off-screen)', 'severity' => 'high', 'confidence' => 80, 'verdict' => 'confirmed', 'remediation' => 'remove_spam', 'types' => array( 'php', 'html', 'js', 'db' ), 'pattern' => '/<(?:div|span|p|ul|li)[^>]+style\s*=\s*[\'"][^\'"]*(?:display\s*:\s*none|visibility\s*:\s*hidden|position\s*:\s*absolute\s*;\s*(?:left|top)\s*:\s*-\d{3,}|text-indent\s*:\s*-\d{3,}|font-size\s*:\s*0|opacity\s*:\s*0)[^\'"]*[\'"][^>]*>\s*(?:<[^>]+>\s*){0,3} 'Links wrapped in invisible containers only exist to manipulate search engines.', ); $r[] = array( 'id' => 'hidden_iframe', 'title' => 'Hidden or zero-size iframe', 'severity' => 'high', 'confidence' => 82, 'verdict' => 'confirmed', 'remediation' => 'remove_js', 'types' => array( 'php', 'html', 'js', 'db' ), 'pattern' => '/]+(?:(?:width|height)\s*=\s*[\'"]?\s*0\s*[\'"]?|style\s*=\s*[\'"][^\'"]*(?:display\s*:\s*none|visibility\s*:\s*hidden|position\s*:\s*absolute\s*;\s*(?:left|top)\s*:\s*-\d{3,}))[^>]*>/i', 'description' => 'Invisible iframes load malware or ads from another site without the visitor noticing.', ); $r[] = array( 'id' => 'ua_cloaking_redirect', 'title' => 'Search-engine cloaking / conditional redirect', 'severity' => 'high', 'confidence' => 82, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'php' ), 'pattern' => '/HTTP_USER_AGENT[^;]{0,200}(?:googlebot|bingbot|yandex|slurp|baidu|duckduck)/i', 'requires' => array( '/header\s*\(\s*[\'"]Location\s*:|file_get_contents\s*\(\s*[\'"]https?:|curl_exec\s*\(| 'Different content/redirects are served to search engines than to humans: doorway spam or redirect malware.', ); $r[] = array( 'id' => 'mobile_redirect', 'title' => 'Conditional redirect for mobile visitors', 'severity' => 'high', 'confidence' => 72, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php', 'js' ), 'pattern' => '/(?:android|iphone|ipad|ipod|blackberry|windows phone|mobile)[^;]{0,300}(?:header\s*\(\s*[\'"]Location\s*:\s*https?:\/\/|location\.(?:href|replace)\s*(?:=|\()\s*[\'"]https?:\/\/)/i', 'description' => 'Mobile users are redirected elsewhere; malicious redirect campaigns target phones to avoid detection by site owners.', ); $r[] = array( 'id' => 'known_bad_host', 'title' => 'Reference to a known malware domain', 'severity' => 'critical', 'confidence' => 90, 'verdict' => 'confirmed', 'remediation' => 'remove_js', 'types' => array( 'php', 'js', 'html', 'css', 'text', 'db', 'htaccess' ), 'pattern' => '/' . self::BAD_HOSTS . '/i', 'description' => 'The domain is associated with the Balada Injector / fake-CDN malware campaigns.', ); $r[] = array( 'id' => 'ndsw_injection', 'title' => 'NDSW / NDSX JavaScript injection', 'severity' => 'critical', 'confidence' => 97, 'verdict' => 'confirmed', 'remediation' => 'remove_js', 'types' => array( 'js', 'php', 'html', 'db' ), 'pattern' => '/\bndsw\s*===?\s*undefined|\bndsx\b|\bndsj\b/', 'description' => 'The "ndsw===undefined" loader is a widespread redirect/malvertising injection appended to JavaScript files.', ); $r[] = array( 'id' => 'js_eval_decoded', 'title' => 'JavaScript eval() of decoded text', 'severity' => 'high', 'confidence' => 82, 'verdict' => 'suspicious', 'remediation' => 'remove_js', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/\beval\s*\(\s*(?:String\.fromCharCode|unescape|atob|decodeURIComponent|window\.atob)\s*\(/i', 'description' => 'Obfuscated JavaScript payload executed at runtime.', ); $r[] = array( 'id' => 'js_document_write_unescape', 'title' => 'document.write(unescape(...)) injection', 'severity' => 'high', 'confidence' => 80, 'verdict' => 'suspicious', 'remediation' => 'remove_js', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/document\.write\s*\(\s*(?:unescape|atob|decodeURIComponent)\s*\(/i', 'description' => 'Writing decoded HTML into the page is the classic way to inject hidden iframes and scripts.', ); $r[] = array( 'id' => 'js_fromcharcode_blob', 'title' => 'Long String.fromCharCode sequence', 'severity' => 'high', 'confidence' => 72, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/String\.fromCharCode\s*\(\s*(?:\d{2,3}\s*,\s*){25,}/i', 'description' => 'Text encoded as a list of character codes hides what the script does.', ); $r[] = array( 'id' => 'js_obfuscator_io', 'title' => 'Heavily obfuscated JavaScript (_0x arrays)', 'severity' => 'medium', 'confidence' => 55, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/\b_0x[a-f0-9]{4,6}\s*=\s*\[/i', 'requires' => array( '/\b(?:eval|Function|atob|unescape|fromCharCode|document\.write|location\.(?:href|replace))\b/i' ), 'description' => 'obfuscator.io style code combined with eval/redirect functions. Some licensed scripts look like this; verify the source.', ); $r[] = array( 'id' => 'js_redirect_external', 'title' => 'JavaScript redirect to an external site', 'severity' => 'medium', 'confidence' => 50, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/(?:window\.|top\.|document\.)?location(?:\.href|\.replace|\.assign)?\s*(?:=|\()\s*[\'"]https?:\/\/[^\'"]+[\'"]/i', 'requires' => array( '/\b(?:setTimeout|Math\.random|atob|localStorage|sessionStorage|document\.cookie|referrer)\b/i' ), 'description' => 'Delayed/randomised redirects to another domain are typical of redirect malware. Legitimate redirects rarely use timers or randomness.', ); $r[] = array( 'id' => 'js_script_bad_tld', 'title' => 'Script loaded from a suspicious domain', 'severity' => 'medium', 'confidence' => 55, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/]+src\s*=\s*[\'"](?:https?:)?\/\/[^\'"\/]+\.' . self::BAD_TLDS . '\/[^\'"]*[\'"]/i', 'description' => 'External scripts from these top-level domains are heavily abused by malware campaigns.', ); $r[] = array( 'id' => 'js_cryptominer', 'title' => 'Browser cryptocurrency miner', 'severity' => 'critical', 'confidence' => 92, 'verdict' => 'confirmed', 'remediation' => 'remove_js', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/\b(?:coinhive|coin-hive|CoinHive|cryptonight|jsecoin|webminepool|crypto-?loot|minero\.cc|coinimp|deepMiner|CryptoNoter|stratum\+tcp|monerominer|authedmine|coinhave|minr\.pw|webmine\.pro)\b/i', 'description' => 'Mines cryptocurrency in visitors\' browsers.', ); $r[] = array( 'id' => 'js_card_skimmer', 'title' => 'Possible payment-card skimmer', 'severity' => 'high', 'confidence' => 60, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'escalate' => array( 'upload' => 85, 'root' => 80, 'other' => 80, 'db' => 80 ), 'escalate_severity' => 'critical', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/\b(?:card[_-]?number|ccnumber|cc[_-]?num|cardnum|cvv2?|cvc|cardcode|card_cvc)\b/i', 'min_count' => 2, 'requires' => array( '/\b(?:btoa|encodeURIComponent|JSON\.stringify)\s*\(/i', '/\b(?:XMLHttpRequest|fetch\s*\(|sendBeacon|new\s+Image\s*\(\s*\)\s*\.src|\.src\s*=\s*[\'"]https?:\/\/)/i', '/https?:\/\/(?!(?:[^\/\'"]*\.)?(?:' . self::EMBED_HOSTS . '))[^\'"\s]+/i' ), 'description' => 'Script reads card fields, encodes them and sends them to an external URL (Magecart-style skimming).', ); $r[] = array( 'id' => 'js_hidden_iframe_builder', 'title' => 'Script creates a hidden iframe', 'severity' => 'high', 'confidence' => 78, 'verdict' => 'suspicious', 'remediation' => 'remove_js', 'types' => array( 'js', 'html', 'php', 'db' ), 'pattern' => '/createElement\s*\(\s*[\'"]iframe[\'"]\s*\)/i', 'requires' => array( '/\.style\.(?:display|visibility|opacity|height|width)\s*=\s*[\'"]?(?:none|hidden|0)/i' ), 'description' => 'Dynamically injected invisible iframes are used for drive-by downloads and ad fraud.', ); /* ------------------------------------------------------------------ * .htaccess / php.ini * ---------------------------------------------------------------- */ $r[] = array( 'id' => 'htaccess_php_handler', 'title' => 'Non-PHP extensions mapped to the PHP handler', 'severity' => 'critical', 'confidence' => 93, 'verdict' => 'confirmed', 'remediation' => 'manual_review', 'types' => array( 'htaccess' ), 'pattern' => '/^\s*(?:AddHandler|AddType|SetHandler|ForceType)\s+[^\n]*php[^\n]*\.(?:jpe?g|png|gif|ico|txt|css|js|html?|svg|pdf|zip|mp3|log|dat)\b/im', 'description' => 'Images or text files will be executed as PHP: this allows disguised backdoors to run.', ); $r[] = array( 'id' => 'ini_auto_prepend', 'title' => 'auto_prepend_file / auto_append_file set', 'severity' => 'critical', 'confidence' => 85, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'htaccess', 'ini' ), 'pattern' => '/(?:php_value|php_admin_value)\s+auto_(?:prepend|append)_file\s+(?!.*(?:wordfence-waf\.php|sucuri|ithemes|solid-security|patchstack|siteground|wp-content\/plugins\/softvoltai-ai-bodyguard))|^\s*auto_(?:prepend|append)_file\s*=\s*(?!\s*(?:none|["\']{2})?\s*$)(?!.*(?:wordfence-waf\.php|sucuri|ithemes|solid-security|patchstack|siteground))/im', 'description' => 'A file is silently executed before every PHP request. Only firewalls (Wordfence, Sucuri...) use this legitimately.', ); $r[] = array( 'id' => 'htaccess_remote_redirect', 'title' => 'Rewrite rule redirects to an external site', 'severity' => 'high', 'confidence' => 70, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'htaccess' ), 'pattern' => '/^\s*(?:RewriteRule|Redirect(?:Match|Permanent|Temp)?)\s+[^\n]*https?:\/\/[^\n]+/im', 'requires' => array( '/RewriteCond\s+%\{HTTP_(?:USER_AGENT|REFERER)\}[^\n]*(?:google|bing|yahoo|msn|yandex|android|iphone|mobile)/i' ), 'description' => 'Visitors coming from search engines or on mobile are redirected to another domain (cloaked redirect).', ); $r[] = array( 'id' => 'htaccess_errordocument_remote', 'title' => 'ErrorDocument points to a remote URL', 'severity' => 'high', 'confidence' => 72, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'htaccess' ), 'pattern' => '/^\s*ErrorDocument\s+\d{3}\s+https?:\/\//im', 'description' => 'Error pages are served from another server, a trick used to push spam and redirects.', ); $r[] = array( 'id' => 'htaccess_php_in_uploads_allowed', 'title' => 'PHP execution explicitly allowed in uploads', 'severity' => 'high', 'confidence' => 80, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'htaccess' ), 'pattern' => '/]*php[^>]*>\s*(?:[^<]*?(?:Allow\s+from\s+all|Require\s+all\s+granted))/is', 'description' => 'An .htaccess rule re-enables PHP in a folder where it should be blocked.', ); $r[] = array( 'id' => 'htaccess_php_code', 'title' => 'PHP code inside .htaccess / ini file', 'severity' => 'critical', 'confidence' => 90, 'verdict' => 'confirmed', 'remediation' => 'manual_review', 'types' => array( 'htaccess', 'ini' ), 'pattern' => '/<\?php|<\?=/i', 'description' => 'Configuration files never contain PHP; malware uses them as a hidden store.', ); /* ------------------------------------------------------------------ * Polyglot files: PHP hidden in media/text files * ---------------------------------------------------------------- */ $r[] = array( 'id' => 'media_php_polyglot', 'title' => 'PHP code hidden inside a non-PHP file', 'severity' => 'critical', 'confidence' => 90, 'verdict' => 'confirmed', 'remediation' => 'delete', 'types' => array( 'media' ), 'pattern' => '/<\?php\b|<\?=\s|<\?\s+(?:if|echo|eval|\$)/i', 'description' => 'Image, icon, text or CSS files must not contain PHP tags. This file is a disguised payload (loaded through include() or a handler trick).', ); $r[] = array( 'id' => 'text_php_polyglot', 'title' => 'PHP code inside a text/JS/CSS/JSON file', 'severity' => 'high', 'confidence' => 80, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'js', 'css', 'text', 'html' ), 'pattern' => '/<\?php\b/i', 'requires' => array( '/\b(?:eval|base64_decode|gzinflate|\$_(?:GET|POST|COOKIE|REQUEST)|system|exec|assert)\b/i' ), 'description' => 'PHP with dangerous functions is stored in a file that normally cannot execute; it is probably included by a loader elsewhere.', ); /* ------------------------------------------------------------------ * Context-weighted helpers (reported with lower confidence; escalate by location) * ---------------------------------------------------------------- */ $r[] = array( 'id' => 'php_system_calls', 'title' => 'Shell/system function usage', 'severity' => 'medium', 'confidence' => 35, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), // A method of the same name ($pdo->exec(), Prompt::system(), function system()) is not a shell call. 'pattern' => '/(?)(? array( 'upload' => 85, 'root' => 65, 'theme' => 60, 'dropin' => 60, 'mu' => 50, 'other' => 60 ), 'description' => 'System functions are rare in themes and never belong in uploads. In plugins they are sometimes legitimate (image tools, backups).', ); $r[] = array( 'id' => 'php_eval_usage', 'title' => 'eval() usage', 'severity' => 'medium', 'confidence' => 40, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/(?$])eval\s*\(/i', 'escalate' => array( 'upload' => 90, 'root' => 70, 'theme' => 70, 'dropin' => 65, 'mu' => 55, 'other' => 65 ), 'description' => 'eval() is almost never needed. Verify what is evaluated.', ); $r[] = array( 'id' => 'php_file_manager', 'title' => 'File-manager style capabilities with request input', 'severity' => 'medium', 'confidence' => 50, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\b(?:scandir|opendir|unlink|chmod|rename|rmdir|symlink|readfile|copy|mkdir|file_put_contents)\s*\(/i', 'min_count' => 6, 'requires' => array( '/\$_(?:GET|POST|REQUEST)\s*\[/' ), 'escalate' => array( 'upload' => 90, 'root' => 80, 'theme' => 75, 'other' => 80 ), 'description' => 'Many filesystem operations driven by request parameters: a file manager, which in the wrong place is a web shell.', ); $r[] = array( 'id' => 'php_phpinfo', 'title' => 'phpinfo() exposed', 'severity' => 'low', 'confidence' => 30, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\bphpinfo\s*\(\s*\)/i', 'escalate' => array( 'upload' => 70, 'root' => 55, 'other' => 55 ), 'description' => 'phpinfo() leaks server configuration; attackers drop info.php files to fingerprint hosts.', ); $r[] = array( 'id' => 'php_htaccess_writer', 'title' => 'Code writes .htaccess files', 'severity' => 'medium', 'confidence' => 40, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\b(?:file_put_contents|fwrite|fputs)\s*\([^;]{0,200}\.htaccess/i', 'escalate' => array( 'upload' => 85, 'root' => 70, 'theme' => 70, 'other' => 70 ), 'description' => 'Caching/security plugins legitimately manage .htaccess; anything else writing it deserves a look.', ); $r[] = array( 'id' => 'php_curl_to_eval', 'title' => 'Remote content fetched and evaluated', 'severity' => 'high', 'confidence' => 75, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/\b(?:curl_exec|file_get_contents|wp_remote_retrieve_body|fopen)\s*\(/i', 'requires' => array( '/\b(?:eval|assert|create_function)\s*\(\s*(?:@\s*)?(?:\$\w+|base64_decode|gzinflate|str_rot13)/i' ), 'description' => 'Content is downloaded and then executed with eval(): a remote payload loader.', ); $r[] = array( 'id' => 'php_uname_strings', 'title' => 'Reconnaissance strings (uname, /etc/passwd, whoami)', 'severity' => 'medium', 'confidence' => 45, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'types' => array( 'php' ), 'pattern' => '/(?:\buname\s+-a\b|\/etc\/passwd|\bwhoami\b|\/bin\/sh\b|\/bin\/bash\b|\bnetstat\s+-an|\bid\s*;\s*uname)/i', 'escalate' => array( 'upload' => 85, 'root' => 70, 'theme' => 70, 'other' => 70 ), 'description' => 'Strings used by web shells to fingerprint the server.', ); self::$rules = apply_filters( 'svabg_signature_rules', $r ); return self::$rules; } public static function rule( $id ) { foreach ( self::rules() as $rule ) { if ( $rule['id'] === $id ) { return $rule; } } return null; } /** * Maps a file extension to the signature "type" bucket. */ public static function type_for_ext( $ext, $basename = '' ) { $basename = strtolower( $basename ); if ( '.htaccess' === $basename || 'htaccess' === $ext ) { return 'htaccess'; } if ( in_array( $basename, array( '.user.ini', 'php.ini', '.htpasswd' ), true ) || 'ini' === $ext ) { return 'ini'; } if ( Softvolt_Ai_Bodyguard_Helpers::is_php_ext( $ext ) ) { return 'php'; } if ( in_array( $ext, array( 'js', 'mjs', 'cjs' ), true ) ) { return 'js'; } if ( in_array( $ext, array( 'html', 'htm', 'xhtml' ), true ) ) { return 'html'; } if ( 'css' === $ext ) { return 'css'; } if ( in_array( $ext, array( 'txt', 'json', 'xml', 'md', 'log', 'csv', 'sql', 'bak', 'old', 'orig', 'tmp', 'suspected', 'sh', 'cgi', 'pl', 'py', '' ), true ) ) { return 'text'; } if ( in_array( $ext, array( 'ico', 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp', 'svg', 'svgz', 'pdf', 'mp3', 'mp4', 'woff', 'woff2', 'ttf', 'eot', 'otf', 'zip', 'gz', 'tar', 'dat', 'avif', 'heic', 'mov', 'avi', 'webm', 'ogg', 'wav', 'psd', 'ai', 'eps', 'tiff', 'tif' ), true ) ) { return 'media'; } return 'text'; } /** * Scans content and returns rule hits. * * @param string $content file/database content * @param array $ctx ['type' => php|js|..., 'category' => core|plugin|theme|mu|dropin|upload|root|other|db] * @return array[] each: id,title,severity,confidence,verdict,remediation,description,line,count,excerpt */ public static function scan( $content, $ctx ) { $hits = array(); if ( '' === $content ) { return $hits; } $type = isset( $ctx['type'] ) ? $ctx['type'] : 'text'; $category = isset( $ctx['category'] ) ? $ctx['category'] : 'other'; $code = null; foreach ( self::rules() as $rule ) { $types = $rule['types']; if ( ! in_array( 'any', $types, true ) && ! in_array( $type, $types, true ) ) { continue; } // Rules about code that runs ignore comments (docs quote `$_POST` or name files in prose). $subject = $content; if ( ! empty( $rule['code_only'] ) && 'php' === $type ) { $code = null === $code ? self::without_comments( $content ) : $code; $subject = $code; } $count = @preg_match_all( $rule['pattern'], $subject, $m, PREG_OFFSET_CAPTURE ); if ( ! $count ) { continue; } if ( isset( $rule['min_count'] ) && $count < (int) $rule['min_count'] ) { continue; } if ( ! empty( $rule['requires'] ) ) { $ok = true; foreach ( $rule['requires'] as $req ) { if ( ! @preg_match( $req, $content ) ) { $ok = false; break; } } if ( ! $ok ) { continue; } } $offset = (int) $m[0][0][1]; $line = Softvolt_Ai_Bodyguard_Helpers::line_of_offset( $content, $offset ); $confidence = (int) $rule['confidence']; $severity = $rule['severity']; $verdict = $rule['verdict']; if ( ! empty( $rule['escalate'] ) && isset( $rule['escalate'][ $category ] ) ) { $confidence = max( $confidence, (int) $rule['escalate'][ $category ] ); if ( $confidence >= 80 ) { $severity = ! empty( $rule['escalate_severity'] ) ? $rule['escalate_severity'] : ( 'critical' === $severity ? 'critical' : 'high' ); if ( ! empty( $rule['escalate_verdict'] ) ) { $verdict = $rule['escalate_verdict']; } } elseif ( $confidence >= 60 && 'low' === $severity ) { $severity = 'medium'; } } $excerpt = Softvolt_Ai_Bodyguard_Helpers::excerpt( $content, $offset ); $hits[] = array( 'id' => $rule['id'], 'title' => $rule['title'], 'severity' => $severity, 'confidence' => $confidence, 'verdict' => $verdict, 'remediation' => $rule['remediation'], 'description' => $rule['description'], 'line' => $line, 'count' => (int) $count, 'excerpt' => $excerpt, ); } if ( 'php' === $type ) { $heuristic = self::obfuscation_heuristic( $content ); if ( $heuristic ) { $hits[] = $heuristic; } } usort( $hits, array( __CLASS__, 'sort_hits' ) ); return $hits; } public static function sort_hits( $a, $b ) { $ra = Softvolt_Ai_Bodyguard_Helpers::severity_rank( $a['severity'] ); $rb = Softvolt_Ai_Bodyguard_Helpers::severity_rank( $b['severity'] ); if ( $ra !== $rb ) { return $rb - $ra; } return $b['confidence'] - $a['confidence']; } /** * Structural heuristic: very long code lines, high symbol density, many decode calls. */ /** * PHP source with its comments blanked out, byte for byte (line breaks kept), so offsets and line * numbers stay those of the file. Unchanged when the tokenizer is missing. * * @since 1.5.1 * @param string $content PHP source. * @return string */ public static function without_comments( $content ) { if ( ! function_exists( 'token_get_all' ) ) { return $content; } $out = ''; foreach ( token_get_all( $content ) as $token ) { if ( is_array( $token ) && ( T_COMMENT === $token[0] || T_DOC_COMMENT === $token[0] ) ) { $out .= preg_replace( '/[^\n]/', ' ', $token[1] ); } else { $out .= is_array( $token ) ? $token[1] : $token; } } return strlen( $out ) === strlen( $content ) ? $out : $content; } private static function obfuscation_heuristic( $content ) { $len = strlen( $content ); if ( $len < 200 ) { return null; } $score = 0; $why = array(); $sample = substr( $content, 0, 200000 ); $decoders = preg_match_all( '/\b(?:base64_decode|gzinflate|gzuncompress|str_rot13|strrev|hex2bin|chr|ord|convert_uudecode)\s*\(/i', $sample ); if ( $decoders >= 8 ) { $score += 2; $why[] = sprintf( '%d decode/char calls', $decoders ); } $longest = 0; $line_no = 1; $i = 1; foreach ( preg_split( '/\r\n|\r|\n/', $sample ) as $line ) { $l = strlen( $line ); if ( $l > $longest ) { $longest = $l; $line_no = $i; } $i++; } if ( $longest > 5000 && preg_match( '/\$\w+\s*=|\(|;/', $sample ) ) { $score += 2; $why[] = sprintf( 'a %d-character line', $longest ); } $symbols = preg_match_all( '/[\$\{\}\[\]\(\)\.\^;]/', $sample ); $alnum = preg_match_all( '/[a-zA-Z0-9]/', $sample ); if ( $alnum > 0 && $symbols / $alnum > 0.9 ) { $score += 1; $why[] = 'very high symbol density'; } $weird_vars = preg_match_all( '/\$[a-zA-Z_]{1,3}\d{3,}\b|\$[a-zA-Z]{12,}\b/', $sample ); if ( $weird_vars > 25 ) { $score += 1; $why[] = 'machine-generated variable names'; } if ( preg_match( '/\b(?:eval|assert|create_function|call_user_func)\s*\(/i', $sample ) ) { $score += 1; $why[] = 'dynamic code execution present'; } if ( $score >= 4 ) { return array( 'id' => 'php_obfuscation_heuristic', 'title' => 'Obfuscated PHP (structural heuristic)', 'severity' => 'medium', 'confidence' => min( 75, 40 + $score * 7 ), 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => 'The file looks machine-obfuscated: ' . implode( ', ', $why ) . '. Minified libraries can trigger this; AI analysis will tell them apart.', 'line' => $line_no, 'count' => 1, 'excerpt' => '', ); } return null; } /** * Rules that apply to database content (posts, options, comments...). */ public static function scan_db_text( $text, $context = 'post' ) { $hits = self::scan( $text, array( 'type' => 'db', 'category' => 'db' ) ); // Extra DB-only checks. if ( preg_match_all( '/]*>(.*?)<\/script>/is', $text, $scripts, PREG_OFFSET_CAPTURE ) ) { foreach ( $scripts[1] as $idx => $body ) { $inner = $body[0]; $tag = $scripts[0][ $idx ][0]; $line = Softvolt_Ai_Bodyguard_Helpers::line_of_offset( $text, $scripts[0][ $idx ][1] ); if ( preg_match( '/\b(?:eval|atob|unescape|fromCharCode|document\.write|location\.(?:href|replace)|\.cookie|XMLHttpRequest|iframe)\b/i', $inner ) ) { $hits[] = array( 'id' => 'db_inline_script_dangerous', 'title' => 'Inline script with eval/redirect/cookie access', 'severity' => 'high', 'confidence' => 78, 'verdict' => 'suspicious', 'remediation' => 'remove_js', 'description' => 'Stored content contains an inline script using eval, decoding, redirects or cookie access.', 'line' => $line, 'count' => 1, 'excerpt' => Softvolt_Ai_Bodyguard_Helpers::trim_text( preg_replace( '/\s+/', ' ', $tag ), 220 ), ); } elseif ( preg_match( '/src\s*=\s*[\'"](?:https?:)?\/\/([^\'"\/]+)/i', $tag, $src ) ) { $host = strtolower( $src[1] ); // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent -- a list of host names the scanner recognises; nothing is loaded. if ( ! preg_match( '/' . self::EMBED_HOSTS . '$/i', $host ) && ! preg_match( '/(?:googletagmanager|google-analytics|googleapis|gstatic|cloudflare|jsdelivr|unpkg|jquery|bootstrapcdn|fontawesome|hotjar|clarity\.ms|facebook\.net|hs-scripts|typekit|stripe|paypal|recaptcha|wp\.com|gravatar|mailchimp|list-manage|cookiebot|onetrust|klaviyo|zdassets|intercom|crisp\.chat|tawk\.to|cdn\.|static\.)/i', $host ) && ! preg_match( '/' . preg_quote( (string) wp_parse_url( home_url(), PHP_URL_HOST ), '/' ) . '$/i', $host ) ) { $hits[] = array( 'id' => 'db_external_script', 'title' => 'External script from an unrecognised domain', 'severity' => 'medium', 'confidence' => 50, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => sprintf( 'Content loads a script from %s. Confirm you added it on purpose.', $host ), 'line' => $line, 'count' => 1, 'excerpt' => Softvolt_Ai_Bodyguard_Helpers::trim_text( $tag, 220 ), ); } } } } if ( preg_match_all( '/]*src\s*=\s*[\'"](?:https?:)?\/\/([^\'"\/]+)[^>]*>/i', $text, $frames, PREG_OFFSET_CAPTURE ) ) { foreach ( $frames[1] as $idx => $h ) { $host = strtolower( $h[0] ); if ( ! preg_match( '/' . self::EMBED_HOSTS . '$/i', $host ) && ! preg_match( '/' . preg_quote( (string) wp_parse_url( home_url(), PHP_URL_HOST ), '/' ) . '$/i', $host ) ) { $hits[] = array( 'id' => 'db_iframe_unknown', 'title' => 'Iframe from an unrecognised domain', 'severity' => 'medium', 'confidence' => 45, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => sprintf( 'Content embeds an iframe from %s.', $host ), 'line' => Softvolt_Ai_Bodyguard_Helpers::line_of_offset( $text, $frames[0][ $idx ][1] ), 'count' => 1, 'excerpt' => Softvolt_Ai_Bodyguard_Helpers::trim_text( $frames[0][ $idx ][0], 220 ), ); } } } if ( preg_match( '/PD9waHA|base64_decode\s*\(|<\?php/i', $text, $mm, PREG_OFFSET_CAPTURE ) ) { $hits[] = array( 'id' => 'db_php_payload', 'title' => 'PHP code or base64-encoded PHP stored in the database', 'severity' => 'high', 'confidence' => 72, 'verdict' => 'suspicious', 'remediation' => 'remove_db_record', 'description' => '"PD9waHA" is " Softvolt_Ai_Bodyguard_Helpers::line_of_offset( $text, $mm[0][1] ), 'count' => 1, 'excerpt' => Softvolt_Ai_Bodyguard_Helpers::excerpt( $text, $mm[0][1] ), ); } if ( 'post' === $context || 'comment' === $context ) { $links = preg_match_all( '/]*href\s*=\s*[\'"](?:https?:)?\/\/([^\'"\/]+)/i', $text, $lm ); if ( $links >= 8 ) { $spam_hosts = 0; foreach ( $lm[1] as $host ) { if ( preg_match( '/\.' . self::BAD_TLDS . '$/i', $host ) ) { $spam_hosts++; } } if ( $spam_hosts >= 3 ) { $hits[] = array( 'id' => 'db_link_farm', 'title' => 'Many outbound links to suspicious domains', 'severity' => 'high', 'confidence' => 70, 'verdict' => 'suspicious', 'remediation' => 'remove_spam', 'description' => sprintf( '%d outbound links, %d of them to risky top-level domains: looks like injected link spam.', $links, $spam_hosts ), 'line' => 0, 'count' => $links, 'excerpt' => '', ); } } } usort( $hits, array( __CLASS__, 'sort_hits' ) ); return $hits; } }