get_charset_collate(); $table = $wpdb->prefix . self::TABLE_HITS; $sql = "CREATE TABLE $table ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, function_name VARCHAR(50) NOT NULL, request_uri TEXT NULL, ip_address VARCHAR(45) NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta($sql); } protected static $previous_handler = null; public static function init() { self::$previous_handler = set_error_handler([__CLASS__, 'catch_disabled_function_warning'], E_WARNING); register_shutdown_function([__CLASS__, 'catch_disabled_function_fatal']); } public static function catch_disabled_function_fatal() { $error = error_get_last(); if (!$error || $error['type'] !== E_ERROR) return; if (preg_match('/^Uncaught Error: Call to undefined function ([a-zA-Z_][a-zA-Z0-9_]*)\(\)/', $error['message'], $m)) { $function_name = $m[1]; if (in_array($function_name, self::DISABLEABLE_FUNCTIONS, true)) { self::record_hit($function_name); } } } public static function catch_disabled_function_warning($errno, $errstr, $errfile = '', $errline = 0, $errcontext = []) { if (preg_match('/^([a-zA-Z_][a-zA-Z0-9_]*)\(\) has been disabled for security reasons/', $errstr, $m)) { self::record_hit($m[1]); } if (self::$previous_handler && is_callable(self::$previous_handler)) { return call_user_func(self::$previous_handler, $errno, $errstr, $errfile, $errline, $errcontext); } return false; } protected static function record_hit($function_name) { global $wpdb; $ip = class_exists('WPMM_Firewall') ? WPMM_Firewall::get_client_ip() : sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'] ?? '')); $uri = isset($_SERVER['REQUEST_URI']) ? substr(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])), 0, 500) : ''; $wpdb->insert($wpdb->prefix . self::TABLE_HITS, [ 'function_name' => $function_name, 'request_uri' => $uri, 'ip_address' => $ip, 'created_at' => current_time('mysql', true), ]); $message = "Confirmed exploitation attempt: something just tried to call $function_name(), which is disabled on this server - this can only happen if malicious/compromised code is actively running"; if (class_exists('WPMM_Activity_Log')) { WPMM_Activity_Log::log('disabled_function_call_attempt', $function_name, $message); } if (class_exists('WPMM_Notifications')) { WPMM_Notifications::notify('malware_detected', $message, 'critical'); } if ($ip && class_exists('WPMM_Firewall') && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { WPMM_Firewall::block_ip($ip, $message, 'auto', 'rasp'); } } public static function get_recent_hits($limit = 50) { global $wpdb; $table = $wpdb->prefix . self::TABLE_HITS; return $wpdb->get_results("SELECT * FROM $table ORDER BY id DESC LIMIT " . (int) $limit, ARRAY_A); } public static function get_recommended_ini_snippet() { $currently_disabled = array_filter(array_map('trim', explode(',', ini_get('disable_functions')))); $combined = array_unique(array_merge($currently_disabled, self::DISABLEABLE_FUNCTIONS)); return implode(',', $combined); } public static function is_function_disabled($function_name) { $disabled = array_filter(array_map('trim', explode(',', ini_get('disable_functions')))); return in_array($function_name, $disabled, true); } public static function detect_functions_in_content($content) { $found = []; foreach (self::DANGEROUS_FUNCTIONS as $func) { if (preg_match('/\b' . preg_quote($func, '/') . '\s*\(/i', $content)) { $found[] = $func; } } return $found; } protected static function get_baseline() { $baseline = get_option(self::OPTION_BASELINE, []); return is_array($baseline) ? $baseline : []; } public static function check_against_baseline($plugin_slug, $relative_path, $functions_found) { if (empty($functions_found)) return null; $baseline = self::get_baseline(); if (!isset($baseline[$plugin_slug])) { return null; } $known_functions = $baseline[$plugin_slug]['functions'] ?? []; $new_functions = array_diff($functions_found, $known_functions); if (!empty($new_functions)) { return sprintf( '%s now calls %s, which no file in this plugin has ever called before (established baseline on %s) - a legitimate update rarely introduces a brand-new dangerous function; this can indicate the plugin has been tampered with', $relative_path, implode(', ', array_map(function ($f) { return $f . '()'; }, $new_functions)), $baseline[$plugin_slug]['established_at'] ); } return null; } public static function update_baseline($plugin_slug, $all_functions_found) { $baseline = self::get_baseline(); if (!isset($baseline[$plugin_slug])) { $baseline[$plugin_slug] = [ 'functions' => array_values(array_unique($all_functions_found)), 'established_at' => current_time('mysql', true), ]; } else { $baseline[$plugin_slug]['functions'] = array_values(array_unique(array_merge( $baseline[$plugin_slug]['functions'], $all_functions_found ))); } update_option(self::OPTION_BASELINE, $baseline); } public static function get_baseline_summary() { $baseline = self::get_baseline(); $summary = []; foreach ($baseline as $slug => $data) { $summary[] = [ 'plugin' => $slug, 'functions' => $data['functions'], 'established_at' => $data['established_at'], ]; } return $summary; } }