Skip to main content

security_rust/injection/
ldap_injection.rs

1// Copyright (c) 2026 erik <erik@erik.xyz> — https://erik.xyz
2
3use crate::{AttackCategory, DetectionResult, Detector, Severity};
4use regex::Regex;
5use std::sync::LazyLock;
6
7static PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
8    vec![
9        Regex::new(r"\(\s*&").unwrap(),
10        Regex::new(r"\(\s*\|").unwrap(),
11        Regex::new(r"\(\s*!").unwrap(),
12        Regex::new(r"\*\(cn=").unwrap(),
13        Regex::new(r"\(\s*objectClass\s*=").unwrap(),
14        Regex::new(r"\(\s*uid\s*=").unwrap(),
15        Regex::new(r"\)\(\s*").unwrap(),
16        Regex::new(r"\(\s*cn\s*=").unwrap(),
17    ]
18});
19
20pub struct LdapInjectionDetector;
21
22impl Detector for LdapInjectionDetector {
23    fn name(&self) -> &'static str {
24        "ldap_injection"
25    }
26
27    fn detect(&self, input: &str) -> Option<DetectionResult> {
28        for re in PATTERNS.iter() {
29            if let Some(m) = re.find(input) {
30                return Some(DetectionResult {
31                    attack_type: "ldap_injection".into(),
32                    category: AttackCategory::Injection,
33                    severity: Severity::High,
34                    matched_pattern: m.as_str().to_string(),
35                    offset: m.start(),
36                    message: "LDAP injection detected".into(),
37                });
38            }
39        }
40        None
41    }
42}