security_rust/file/
path_traversal.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"\.\./").unwrap(),
11 Regex::new(r"\.\.\\").unwrap(),
12 Regex::new(r"(?i)\.\.%2[Ff]").unwrap(),
13 Regex::new(r"(?i)%2[Ee]%2[Ee]").unwrap(),
14 Regex::new(r"(?i)php://filter").unwrap(),
15 Regex::new(r"(?i)php://input").unwrap(),
16 Regex::new(r"(?i)data://").unwrap(),
17 Regex::new(r"(?i)expect://").unwrap(),
18 Regex::new(r"(?i)phar://").unwrap(),
19 Regex::new(r"(?i)zip://").unwrap(),
20 Regex::new(r"(?i)glob://").unwrap(),
21 Regex::new(r"%00").unwrap(),
22 Regex::new(r"\x00").unwrap(),
23 ]
24});
25
26pub struct PathTraversalDetector;
27
28impl Detector for PathTraversalDetector {
29 fn name(&self) -> &'static str {
30 "path_traversal"
31 }
32
33 fn detect(&self, input: &str) -> Option<DetectionResult> {
34 for re in PATTERNS.iter() {
35 if let Some(m) = re.find(input) {
36 return Some(DetectionResult {
37 attack_type: "path_traversal".into(),
38 category: AttackCategory::File,
39 severity: Severity::Critical,
40 matched_pattern: m.as_str().to_string(),
41 offset: m.start(),
42 message: "Path traversal attack detected".into(),
43 });
44 }
45 }
46 None
47 }
48}