lonkero 3.6.2

Web scanner built for actual pentests. Fast, modular, Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// Copyright (c) 2026 Bountyy Oy. All rights reserved.
// This software is proprietary and confidential.

use crate::http_client::HttpClient;
use crate::scanners::parameter_filter::{ParameterFilter, ScannerType};
use crate::types::{Confidence, ScanConfig, Severity, Vulnerability};
use std::sync::Arc;
/**
 * Bountyy Oy - LDAP Injection Scanner
 * Tests for LDAP injection vulnerabilities in directory services
 *
 * Detects:
 * - Authentication bypass via LDAP filter manipulation
 * - Search filter injection
 * - DN (Distinguished Name) injection
 * - Blind LDAP injection
 * - LDAP attribute enumeration
 * - Active Directory specific attacks
 *
 * @copyright 2026 Bountyy Oy
 * @license Proprietary
 */
use tracing::{debug, info};

pub struct LdapInjectionScanner {
    http_client: Arc<HttpClient>,
}

impl LdapInjectionScanner {
    pub fn new(http_client: Arc<HttpClient>) -> Self {
        Self { http_client }
    }

    /// Scan a parameter for LDAP injection vulnerabilities
    pub async fn scan_parameter(
        &self,
        url: &str,
        param_name: &str,
        _config: &ScanConfig,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        // Smart parameter filtering - skip framework internals
        if ParameterFilter::should_skip_parameter(param_name, ScannerType::Other) {
            debug!(
                "[LDAP] Skipping framework/internal parameter: {}",
                param_name
            );
            return Ok((Vec::new(), 0));
        }

        let mut vulnerabilities = Vec::new();
        let mut tests_run = 0;

        info!(
            "[LDAP] Testing LDAP injection on parameter: {} (priority: {})",
            param_name,
            ParameterFilter::get_parameter_priority(param_name)
        );

        // Test various LDAP injection payloads
        let payload_tests = vec![
            // Authentication bypass
            ("*", "Wildcard authentication bypass", "auth_bypass"),
            (
                "*)(uid=*",
                "LDAP filter injection - uid wildcard",
                "filter_injection",
            ),
            ("*)(|(uid=*", "LDAP OR filter bypass", "filter_bypass"),
            (
                "*)(&",
                "LDAP AND filter manipulation",
                "filter_manipulation",
            ),
            (
                "*)|(objectclass=*",
                "Objectclass filter bypass",
                "objectclass_bypass",
            ),
            // Error-based detection
            (
                "(cn=admin))",
                "Unbalanced parentheses - right",
                "error_based",
            ),
            (
                "((cn=admin)",
                "Unbalanced parentheses - left",
                "error_based",
            ),
            ("(cn=admin", "Missing closing parenthesis", "error_based"),
            // Filter manipulation
            ("(cn=*)", "CN wildcard search", "search_filter"),
            ("(uid=*)", "UID wildcard search", "search_filter"),
            ("(objectclass=*)", "Objectclass wildcard", "search_filter"),
            (
                "(|(cn=*)(mail=*))",
                "OR condition injection",
                "complex_filter",
            ),
            (
                "(&(cn=*)(objectclass=*))",
                "AND condition injection",
                "complex_filter",
            ),
            // DN injection
            ("cn=admin,dc=example,dc=com", "DN injection", "dn_injection"),
            (
                "cn=*,dc=example,dc=com",
                "DN wildcard injection",
                "dn_injection",
            ),
            // Active Directory specific
            (
                "(adminCount=1)",
                "AD privileged account enumeration",
                "ad_specific",
            ),
            (
                "(userAccountControl:1.2.840.113556.1.4.803:=512)",
                "AD user account control",
                "ad_specific",
            ),
        ];

        for (payload, description, attack_type) in payload_tests {
            tests_run += 1;

            let test_url = if url.contains('?') {
                format!("{}&{}={}", url, param_name, urlencoding::encode(payload))
            } else {
                format!("{}?{}={}", url, param_name, urlencoding::encode(payload))
            };

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    if let Some(vuln) = self.analyze_response(
                        &response.body,
                        response.status_code,
                        payload,
                        description,
                        attack_type,
                        &test_url,
                        param_name,
                    ) {
                        info!("LDAP injection vulnerability detected: {}", description);
                        vulnerabilities.push(vuln);
                        break; // Found vulnerability, move to next parameter
                    }
                }
                Err(e) => {
                    debug!("Request failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Scan endpoint for LDAP injection (general scan)
    pub async fn scan(
        &self,
        url: &str,
        config: &ScanConfig,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        // Only test parameters discovered from actual forms/URLs - no spray-and-pray
        // The main scanner will call scan_parameter() with discovered params
        Ok((Vec::new(), 0))
    }

    /// Analyze response for LDAP injection indicators
    fn analyze_response(
        &self,
        body: &str,
        status: u16,
        payload: &str,
        description: &str,
        attack_type: &str,
        url: &str,
        param_name: &str,
    ) -> Option<Vulnerability> {
        // LDAP error patterns
        let ldap_error_patterns = vec![
            "LDAP.*error",
            "javax\\.naming\\.directory",
            "LDAPException",
            "com\\.sun\\.jndi\\.ldap",
            "Invalid DN syntax",
            "Bad search filter",
            "Protocol error.*LDAP",
            "Unprocessed Continuation Reference",
            "Operations Error.*LDAP",
            "ldap_search",
            "ldap_bind",
            "LDAP injection",
            "naming exception",
            "directory context",
        ];

        // Check for LDAP error indicators
        for pattern in &ldap_error_patterns {
            if let Ok(regex) = regex::Regex::new(&format!("(?i){}", pattern)) {
                if regex.is_match(body) {
                    return Some(self.create_vulnerability(
                        url,
                        param_name,
                        payload,
                        description,
                        "LDAP error message detected in response",
                        Confidence::High,
                        attack_type,
                    ));
                }
            }
        }

        // Check for successful LDAP filter bypass (unusually large response)
        if payload == "*" && body.len() > 10000 {
            return Some(self.create_vulnerability(
                url,
                param_name,
                payload,
                "Wildcard query returned unusually large response",
                "Possible LDAP filter bypass - wildcard query returned excessive data",
                Confidence::Medium,
                attack_type,
            ));
        }

        // Check for authentication bypass patterns
        if payload.contains("admin") && status == 200 {
            let auth_success_patterns = vec![
                "welcome",
                "dashboard",
                "logged in",
                "authentication successful",
                "profile",
                "account",
            ];

            for pattern in &auth_success_patterns {
                if body.to_lowercase().contains(pattern) {
                    return Some(self.create_vulnerability(
                        url,
                        param_name,
                        payload,
                        "Potential authentication bypass via LDAP injection",
                        &format!(
                            "Successful authentication with LDAP payload - found '{}'",
                            pattern
                        ),
                        Confidence::Medium,
                        attack_type,
                    ));
                }
            }
        }

        // Check for LDAP attribute exposure
        let ldap_attributes = vec![
            "objectClass",
            "distinguishedName",
            "cn=",
            "ou=",
            "dc=",
            "uid=",
            "memberOf",
            "userPassword",
            "sAMAccountName",
        ];

        let mut attribute_count = 0;
        for attr in &ldap_attributes {
            if body.contains(attr) {
                attribute_count += 1;
            }
        }

        if attribute_count >= 3 {
            return Some(self.create_vulnerability(
                url,
                param_name,
                payload,
                "LDAP directory information disclosure",
                &format!(
                    "Response contains {} LDAP attributes - possible directory enumeration",
                    attribute_count
                ),
                Confidence::Medium,
                attack_type,
            ));
        }

        None
    }

    /// Create a vulnerability record
    fn create_vulnerability(
        &self,
        url: &str,
        param_name: &str,
        payload: &str,
        description: &str,
        evidence: &str,
        confidence: Confidence,
        attack_type: &str,
    ) -> Vulnerability {
        let severity = match attack_type {
            "auth_bypass" | "filter_bypass" => Severity::Critical,
            "filter_injection" | "dn_injection" | "ad_specific" => Severity::High,
            _ => Severity::Medium,
        };

        let cvss = match severity {
            Severity::Critical => 9.0,
            Severity::High => 7.5,
            Severity::Medium => 5.3,
            _ => 3.1,
        };

        let verified = matches!(confidence, Confidence::High);

        Vulnerability {
            id: format!("ldap_injection_{}", uuid::Uuid::new_v4()),
            vuln_type: "LDAP Injection".to_string(),
            severity,
            confidence,
            category: "Injection".to_string(),
            url: url.to_string(),
            parameter: Some(param_name.to_string()),
            payload: payload.to_string(),
            description: format!(
                "LDAP injection vulnerability in parameter '{}': {}",
                param_name, description
            ),
            evidence: Some(evidence.to_string()),
            cwe: "CWE-90".to_string(),
            cvss,
            verified,
            false_positive: false,
            remediation: self.get_remediation(attack_type),
            discovered_at: chrono::Utc::now().to_rfc3339(),
            ml_data: None,
        }
    }

    /// Get remediation advice based on attack type
    fn get_remediation(&self, attack_type: &str) -> String {
        match attack_type {
            "auth_bypass" | "filter_bypass" => {
                "1. Use parameterized LDAP queries with proper escaping\n\
                 2. Validate and sanitize all user input before LDAP operations\n\
                 3. Implement proper authentication mechanisms\n\
                 4. Use LDAP libraries that support prepared statements\n\
                 5. Apply principle of least privilege to LDAP service accounts\n\
                 6. Implement input validation using allowlists\n\
                 7. Escape special LDAP characters: * ( ) \\ NUL"
                    .to_string()
            }
            "filter_injection" | "complex_filter" => {
                "1. Escape special LDAP filter characters: * ( ) \\ NUL\n\
                 2. Use LDAP filter encoding functions\n\
                 3. Validate filter syntax before execution\n\
                 4. Implement strict input validation\n\
                 5. Use allowlists for permitted characters\n\
                 6. Avoid string concatenation for LDAP filters"
                    .to_string()
            }
            "dn_injection" => "1. Validate DN format and structure\n\
                 2. Escape DN special characters: , + \" \\ < > ; = NUL\n\
                 3. Use DN parsing and validation libraries\n\
                 4. Implement proper input sanitization\n\
                 5. Verify DN components against allowed values"
                .to_string(),
            "ad_specific" => "1. Restrict access to Active Directory attributes\n\
                 2. Implement proper authorization checks\n\
                 3. Use secure LDAP (LDAPS) for all connections\n\
                 4. Monitor for unusual LDAP queries\n\
                 5. Apply security patches to AD infrastructure\n\
                 6. Limit exposure of sensitive AD attributes"
                .to_string(),
            _ => "1. Use parameterized LDAP queries\n\
                 2. Sanitize all user input\n\
                 3. Implement proper input validation\n\
                 4. Use prepared statements where available\n\
                 5. Apply principle of least privilege\n\
                 6. Enable LDAP query logging and monitoring"
                .to_string(),
        }
    }
}

// UUID generation helper
mod uuid {
    use rand::Rng;

    pub struct Uuid;

    impl Uuid {
        pub fn new_v4() -> String {
            let mut rng = rand::rng();
            format!(
                "{:08x}-{:04x}-{:04x}-{:04x}-{:012x}",
                rng.random::<u32>(),
                rng.random::<u16>(),
                rng.random::<u16>(),
                rng.random::<u16>(),
                rng.random::<u64>() & 0xffffffffffff
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::http_client::HttpResponse;
    use std::sync::Arc;

    fn create_test_scanner() -> LdapInjectionScanner {
        let http_client = Arc::new(HttpClient::new(30, 3).unwrap());
        LdapInjectionScanner::new(http_client)
    }

    #[test]
    fn test_analyze_ldap_error_response() {
        let scanner = create_test_scanner();

        let body = "Error: LDAPException - Invalid DN syntax";
        let result = scanner.analyze_response(
            body,
            500,
            "*)(uid=*",
            "LDAP filter injection",
            "filter_injection",
            "http://example.com/api/users",
            "username",
        );

        assert!(result.is_some());
        let vuln = result.unwrap();
        assert_eq!(vuln.vuln_type, "LDAP Injection");
        assert_eq!(vuln.severity, Severity::High);
        assert_eq!(vuln.cwe, "CWE-90");
    }

    #[test]
    fn test_analyze_javax_naming_error() {
        let scanner = create_test_scanner();

        let body = "javax.naming.directory.InvalidSearchFilterException: Bad search filter";
        let result = scanner.analyze_response(
            body,
            500,
            "*)(&",
            "LDAP AND filter manipulation",
            "filter_manipulation",
            "http://example.com/search",
            "query",
        );

        assert!(result.is_some());
        let vuln = result.unwrap();
        assert_eq!(vuln.confidence, Confidence::High);
        assert!(vuln.description.contains("LDAP injection"));
    }

    #[test]
    fn test_analyze_wildcard_bypass() {
        let scanner = create_test_scanner();

        // Simulate large response from wildcard query
        let body = "A".repeat(15000);
        let result = scanner.analyze_response(
            &body,
            200,
            "*",
            "Wildcard authentication bypass",
            "auth_bypass",
            "http://example.com/auth",
            "username",
        );

        assert!(result.is_some());
        let vuln = result.unwrap();
        assert_eq!(vuln.severity, Severity::Critical);
        assert!(vuln.evidence.unwrap().contains("wildcard query"));
    }

    #[test]
    fn test_analyze_auth_bypass() {
        let scanner = create_test_scanner();

        let body = "Welcome to your dashboard, admin!";
        let result = scanner.analyze_response(
            body,
            200,
            "*)(uid=admin",
            "Authentication bypass",
            "auth_bypass",
            "http://example.com/login",
            "username",
        );

        assert!(result.is_some());
        let vuln = result.unwrap();
        assert_eq!(vuln.severity, Severity::Critical);
        assert!(vuln.description.contains("authentication bypass"));
    }

    #[test]
    fn test_analyze_ldap_attribute_disclosure() {
        let scanner = create_test_scanner();

        let body = r#"
            {
                "distinguishedName": "cn=admin,dc=example,dc=com",
                "cn": "admin",
                "objectClass": ["person", "user"],
                "memberOf": ["cn=Domain Admins,dc=example,dc=com"],
                "sAMAccountName": "admin"
            }
        "#;

        let result = scanner.analyze_response(
            body,
            200,
            "(cn=*)",
            "CN wildcard search",
            "search_filter",
            "http://example.com/ldap/search",
            "filter",
        );

        assert!(result.is_some());
        let vuln = result.unwrap();
        assert!(vuln
            .description
            .contains("directory information disclosure"));
    }

    #[test]
    fn test_analyze_safe_response() {
        let scanner = create_test_scanner();

        let body = "User not found";
        let result = scanner.analyze_response(
            body,
            404,
            "testuser",
            "Normal query",
            "normal",
            "http://example.com/users",
            "username",
        );

        assert!(result.is_none());
    }

    #[test]
    fn test_get_remediation_auth_bypass() {
        let scanner = create_test_scanner();
        let remediation = scanner.get_remediation("auth_bypass");

        assert!(remediation.contains("parameterized LDAP queries"));
        assert!(remediation.contains("special LDAP characters"));
    }

    #[test]
    fn test_get_remediation_dn_injection() {
        let scanner = create_test_scanner();
        let remediation = scanner.get_remediation("dn_injection");

        assert!(remediation.contains("DN format"));
        assert!(remediation.contains("DN special characters"));
    }

    #[test]
    fn test_create_vulnerability() {
        let scanner = create_test_scanner();

        let vuln = scanner.create_vulnerability(
            "http://example.com/api",
            "user",
            "*)(uid=*",
            "LDAP filter bypass",
            "LDAP error detected",
            Confidence::High,
            "filter_bypass",
        );

        assert_eq!(vuln.vuln_type, "LDAP Injection");
        assert_eq!(vuln.severity, Severity::Critical);
        assert_eq!(vuln.parameter, Some("user".to_string()));
        assert_eq!(vuln.cwe, "CWE-90");
        assert_eq!(vuln.cvss, 9.0);
        assert!(vuln.verified);
    }
}