Skip to main content

security_rust/file/
upload.rs

1// Copyright (c) 2026 erik <erik@erik.xyz> — https://erik.xyz
2
3use 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)<\?php").unwrap(),
11        Regex::new(r"(?i)<\?=").unwrap(),
12        Regex::new(r"(?i)<%\s*@").unwrap(),
13        Regex::new(r"(?i)<%\s*=").unwrap(),
14        Regex::new(r#"(?i)<script\s+language\s*=\s*["']?(?:php|vbscript|jscript)["']?"#).unwrap(),
15        Regex::new(r"(?i)eval\s*\(\s*\$").unwrap(),
16        Regex::new(r"(?i)system\s*\(\s*\$").unwrap(),
17        Regex::new(r"(?i)exec\s*\(\s*\$").unwrap(),
18        Regex::new(r"(?i)passthru\s*\(\s*\$").unwrap(),
19        Regex::new(r"(?i)shell_exec\s*\(\s*\$").unwrap(),
20        Regex::new(r"(?i)\$_GET\[").unwrap(),
21        Regex::new(r"(?i)\$_POST\[").unwrap(),
22        Regex::new(r"(?i)\$_REQUEST\[").unwrap(),
23        Regex::new(r"(?i)\$_SERVER\[").unwrap(),
24        Regex::new(r"(?i)base64_decode\s*\(").unwrap(),
25    ]
26});
27
28pub struct UploadDetector;
29
30impl Detector for UploadDetector {
31    fn name(&self) -> &'static str {
32        "upload"
33    }
34
35    fn detect(&self, input: &str) -> Option<DetectionResult> {
36        for re in PATTERNS.iter() {
37            if let Some(m) = re.find(input) {
38                return Some(DetectionResult {
39                    attack_type: "upload".into(),
40                    category: AttackCategory::File,
41                    severity: Severity::Critical,
42                    matched_pattern: m.as_str().to_string(),
43                    offset: m.start(),
44                    message: "Malicious file upload detected".into(),
45                });
46            }
47        }
48        None
49    }
50}