get_var("SHOW TABLES LIKE '{$wpdb->prefix}wpmm_waf_events'")) { $waf = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wpmm_waf_events ORDER BY id DESC LIMIT " . (int) $limit, ARRAY_A); foreach ($waf as $r) { $rows[] = [ 'time' => $r['created_at'], 'ip' => $r['ip_address'] ?? '', 'endpoint' => $r['route'] ?? '', 'attack_type' => $r['rule_name'] ?? $r['rule_id'] ?? 'WAF match', 'rule' => $r['rule_id'] ?? '', 'action' => $r['action'] ?? 'log', 'source' => 'WAF', ]; } } if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}wpmm_firewall_events'")) { $fw = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wpmm_firewall_events ORDER BY id DESC LIMIT " . (int) $limit, ARRAY_A); foreach ($fw as $r) { $rows[] = [ 'time' => $r['created_at'], 'ip' => $r['ip_address'] ?? '', 'endpoint' => $r['category'] ?? '', 'attack_type' => $r['reason'] ?? ucfirst($r['category'] ?? 'Firewall event'), 'rule' => $r['category'] ?? '', 'action' => 'block', 'source' => 'Firewall', ]; } } if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}wpmm_honeypot_hits'")) { $hp = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wpmm_honeypot_hits ORDER BY id DESC LIMIT " . (int) $limit, ARRAY_A); foreach ($hp as $r) { $rows[] = [ 'time' => $r['created_at'], 'ip' => $r['ip_address'] ?? '', 'endpoint' => $r['target'] ?? '', 'attack_type' => 'Honeypot: ' . ($r['honeypot_type'] ?? ''), 'rule' => 'honeypot', 'action' => 'block', 'source' => 'Honeypot', ]; } } usort($rows, function ($a, $b) { return strtotime($b['time']) <=> strtotime($a['time']); }); $rows = array_slice($rows, 0, $limit); foreach ($rows as &$r) { $geo = class_exists('WPMM_GeoIP') ? WPMM_GeoIP::lookup($r['ip']) : ['country' => '', 'country_code' => '']; $r['country'] = $geo['country'] ?? ''; $r['country_code'] = $geo['country_code'] ?? ''; } return $rows; } public static function get_top_threats($limit_per_category = 10, $sample_size = 500) { $attacks = self::get_recent_attacks($sample_size); $by_ip = []; $by_country = []; $by_type = []; $by_endpoint = []; foreach ($attacks as $a) { if (!empty($a['ip'])) $by_ip[$a['ip']] = ($by_ip[$a['ip']] ?? 0) + 1; if (!empty($a['country'])) $by_country[$a['country']] = ($by_country[$a['country']] ?? 0) + 1; if (!empty($a['attack_type'])) $by_type[$a['attack_type']] = ($by_type[$a['attack_type']] ?? 0) + 1; if (!empty($a['endpoint'])) $by_endpoint[$a['endpoint']] = ($by_endpoint[$a['endpoint']] ?? 0) + 1; } arsort($by_ip); arsort($by_country); arsort($by_type); arsort($by_endpoint); return [ 'top_ips' => array_slice($by_ip, 0, $limit_per_category, true), 'top_countries' => array_slice($by_country, 0, $limit_per_category, true), 'top_types' => array_slice($by_type, 0, $limit_per_category, true), 'top_endpoints' => array_slice($by_endpoint, 0, $limit_per_category, true), ]; } }