security_rust/protocol/
websocket.rs1use regex::Regex;
4use std::sync::LazyLock;
5
6use crate::{AttackCategory, DetectionResult, Detector, Severity};
7
8static PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
9 vec![
10 Regex::new(r"(?i)Upgrade:\s*websocket").unwrap(),
11 Regex::new(r"(?i)Sec-WebSocket-Key:").unwrap(),
12 Regex::new(r"(?i)Origin:\s*null.*Upgrade").unwrap(),
13 Regex::new(r"(?i)ws://").unwrap(),
14 ]
15});
16
17pub struct WebSocketDetector;
18
19impl Detector for WebSocketDetector {
20 fn name(&self) -> &'static str {
21 "websocket"
22 }
23
24 fn detect(&self, input: &str) -> Option<DetectionResult> {
25 for re in PATTERNS.iter() {
26 if let Some(m) = re.find(input) {
27 return Some(DetectionResult {
28 attack_type: "websocket".into(),
29 category: AttackCategory::Protocol,
30 severity: Severity::High,
31 matched_pattern: m.as_str().to_string(),
32 offset: m.start(),
33 message: "WebSocket hijack attempt detected".into(),
34 });
35 }
36 }
37 None
38 }
39}