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
// Copyright (c) 2026 Bountyy Oy. All rights reserved.
// This software is proprietary and confidential.

use crate::http_client::HttpClient;
use crate::types::{ScanConfig, Severity, Vulnerability};
use std::sync::Arc;
use tracing::{debug, info};

mod uuid {
    pub use uuid::Uuid;
}

/// Scanner for CORS (Cross-Origin Resource Sharing) misconfiguration vulnerabilities
pub struct CorsMisconfigurationScanner {
    http_client: Arc<HttpClient>,
    test_marker: String,
}

impl CorsMisconfigurationScanner {
    pub fn new(http_client: Arc<HttpClient>) -> Self {
        let test_marker = format!("cors-{}", uuid::Uuid::new_v4().to_string().replace("-", ""));
        Self {
            http_client,
            test_marker,
        }
    }

    /// Run CORS misconfiguration scan
    pub async fn scan(
        &self,
        url: &str,
        _config: &ScanConfig,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        info!("Starting CORS misconfiguration scan on {}", url);

        let mut all_vulnerabilities = Vec::new();
        let mut total_tests = 0;

        // Test arbitrary origin reflection
        let (vulns, tests) = self.test_arbitrary_origin(url).await?;
        all_vulnerabilities.extend(vulns);
        total_tests += tests;

        // Test null origin
        let (vulns, tests) = self.test_null_origin(url).await?;
        all_vulnerabilities.extend(vulns);
        total_tests += tests;

        // Test wildcard with credentials
        let (vulns, tests) = self.test_wildcard_credentials(url).await?;
        all_vulnerabilities.extend(vulns);
        total_tests += tests;

        // Test subdomain reflection
        let (vulns, tests) = self.test_subdomain_reflection(url).await?;
        all_vulnerabilities.extend(vulns);
        total_tests += tests;

        // Test insecure origins
        let (vulns, tests) = self.test_insecure_origins(url).await?;
        all_vulnerabilities.extend(vulns);
        total_tests += tests;

        info!(
            "CORS misconfiguration scan completed: {} tests run, {} vulnerabilities found",
            total_tests,
            all_vulnerabilities.len()
        );

        Ok((all_vulnerabilities, total_tests))
    }

    /// Test arbitrary origin reflection
    async fn test_arbitrary_origin(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 2;

        debug!("Testing CORS arbitrary origin reflection");

        // Test arbitrary origins
        let test_origins = vec![
            format!("https://evil-{}.com", self.test_marker),
            "https://attacker.com".to_string(),
        ];

        for origin in test_origins {
            let headers = vec![("Origin".to_string(), origin.clone())];

            match self.http_client.get_with_headers(url, headers).await {
                Ok(response) => {
                    if self.detect_arbitrary_origin_reflected(&response.headers, &origin) {
                        vulnerabilities.push(self.create_vulnerability(
                            "CORS Arbitrary Origin Reflection",
                            url,
                            &format!("Server reflects arbitrary origin '{}' in Access-Control-Allow-Origin header with credentials", origin),
                            Severity::High,
                            "CWE-346",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    info!("Arbitrary origin test failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test null origin acceptance
    async fn test_null_origin(&self, url: &str) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 1;

        debug!("Testing CORS null origin acceptance");

        let headers = vec![("Origin".to_string(), "null".to_string())];

        match self.http_client.get_with_headers(url, headers).await {
            Ok(response) => {
                if self.detect_null_origin_allowed(&response.headers) {
                    vulnerabilities.push(self.create_vulnerability(
                        "CORS Null Origin Allowed",
                        url,
                        "Server allows 'null' origin with credentials, enabling CORS bypass via sandboxed iframe",
                        Severity::High,
                        "CWE-346",
                    ));
                }
            }
            Err(e) => {
                info!("Null origin test failed: {}", e);
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test wildcard origin with credentials
    async fn test_wildcard_credentials(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 1;

        debug!("Testing CORS wildcard with credentials");

        let headers = vec![("Origin".to_string(), "https://example.com".to_string())];

        match self.http_client.get_with_headers(url, headers).await {
            Ok(response) => {
                if self.detect_wildcard_with_credentials(&response.headers) {
                    vulnerabilities.push(self.create_vulnerability(
                        "CORS Wildcard with Credentials",
                        url,
                        "Server uses wildcard '*' in Access-Control-Allow-Origin with Access-Control-Allow-Credentials: true",
                        Severity::High,
                        "CWE-346",
                    ));
                }
            }
            Err(e) => {
                info!("Wildcard credentials test failed: {}", e);
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test subdomain reflection vulnerability
    async fn test_subdomain_reflection(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 2;

        debug!("Testing CORS subdomain reflection");

        // Extract domain from URL
        if let Ok(parsed_url) = url::Url::parse(url) {
            if let Some(host) = parsed_url.host_str() {
                // Test subdomain variations
                let test_origins = vec![
                    format!("https://evil.{}", host),
                    format!("https://{}.evil.com", host),
                ];

                for origin in test_origins {
                    let headers = vec![("Origin".to_string(), origin.clone())];

                    match self.http_client.get_with_headers(url, headers).await {
                        Ok(response) => {
                            if self.detect_arbitrary_origin_reflected(&response.headers, &origin) {
                                vulnerabilities.push(self.create_vulnerability(
                                    "CORS Subdomain Reflection",
                                    url,
                                    &format!("Server reflects subdomain origin '{}' without proper validation", origin),
                                    Severity::Medium,
                                    "CWE-346",
                                ));
                                break;
                            }
                        }
                        Err(e) => {
                            info!("Subdomain reflection test failed: {}", e);
                        }
                    }
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test insecure HTTP origins
    async fn test_insecure_origins(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 1;

        debug!("Testing CORS insecure HTTP origins");

        // Test HTTP origin (insecure)
        let insecure_origin = "http://attacker.com".to_string();
        let headers = vec![("Origin".to_string(), insecure_origin.clone())];

        match self.http_client.get_with_headers(url, headers).await {
            Ok(response) => {
                if self.detect_arbitrary_origin_reflected(&response.headers, &insecure_origin) {
                    vulnerabilities.push(self.create_vulnerability(
                        "CORS Insecure HTTP Origin Allowed",
                        url,
                        "Server allows insecure HTTP origins, enabling man-in-the-middle attacks",
                        Severity::Medium,
                        "CWE-346",
                    ));
                }
            }
            Err(e) => {
                info!("Insecure origin test failed: {}", e);
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Detect arbitrary origin reflection
    fn detect_arbitrary_origin_reflected(
        &self,
        headers: &std::collections::HashMap<String, String>,
        origin: &str,
    ) -> bool {
        for (key, value) in headers {
            let key_lower = key.to_lowercase();

            // Check if Access-Control-Allow-Origin reflects our origin
            if key_lower == "access-control-allow-origin" {
                if value == origin || value == "*" {
                    // Check if credentials are also allowed (critical)
                    for (cred_key, cred_value) in headers {
                        if cred_key.to_lowercase() == "access-control-allow-credentials"
                            && cred_value.to_lowercase() == "true"
                        {
                            return true;
                        }
                    }
                    // Even without credentials, arbitrary origin is a concern
                    if value == origin {
                        return true;
                    }
                }
            }
        }

        false
    }

    /// Detect null origin allowed
    fn detect_null_origin_allowed(
        &self,
        headers: &std::collections::HashMap<String, String>,
    ) -> bool {
        let mut null_origin_allowed = false;
        let mut credentials_allowed = false;

        for (key, value) in headers {
            let key_lower = key.to_lowercase();

            if key_lower == "access-control-allow-origin" && value == "null" {
                null_origin_allowed = true;
            }

            if key_lower == "access-control-allow-credentials" && value.to_lowercase() == "true" {
                credentials_allowed = true;
            }
        }

        null_origin_allowed && credentials_allowed
    }

    /// Detect wildcard with credentials (actually invalid but some servers try)
    fn detect_wildcard_with_credentials(
        &self,
        headers: &std::collections::HashMap<String, String>,
    ) -> bool {
        let mut wildcard_origin = false;
        let mut credentials_allowed = false;

        for (key, value) in headers {
            let key_lower = key.to_lowercase();

            if key_lower == "access-control-allow-origin" && value == "*" {
                wildcard_origin = true;
            }

            if key_lower == "access-control-allow-credentials" && value.to_lowercase() == "true" {
                credentials_allowed = true;
            }
        }

        wildcard_origin && credentials_allowed
    }

    /// Create a vulnerability record
    fn create_vulnerability(
        &self,
        vuln_type: &str,
        url: &str,
        evidence: &str,
        severity: Severity,
        cwe: &str,
    ) -> Vulnerability {
        let cvss = match severity {
            Severity::Critical => 9.1,
            Severity::High => 8.1,
            Severity::Medium => 5.3,
            Severity::Low => 3.7,
            Severity::Info => 2.0,
        };

        Vulnerability {
            id: format!("cors_{}", uuid::Uuid::new_v4().to_string()),
            vuln_type: vuln_type.to_string(),
            severity,
            confidence: crate::types::Confidence::High,
            category: "Configuration".to_string(),
            url: url.to_string(),
            parameter: None,
            payload: "".to_string(),
            description: format!("{}: {}", vuln_type, evidence),
            evidence: Some(evidence.to_string()),
            cwe: cwe.to_string(),
            cvss: cvss as f32,
            verified: true,
            false_positive: false,
            remediation: self.get_remediation(vuln_type),
            discovered_at: chrono::Utc::now().to_rfc3339(),
            ml_data: None,
        }
    }

    /// Get remediation advice based on vulnerability type
    fn get_remediation(&self, vuln_type: &str) -> String {
        match vuln_type {
            "CORS Arbitrary Origin Reflection" => {
                "Don't reflect arbitrary origins in Access-Control-Allow-Origin. Use a strict allow-list of trusted origins. Validate origins against the allow-list server-side. Never use Access-Control-Allow-Credentials: true with dynamic origins without proper validation.".to_string()
            }
            "CORS Null Origin Allowed" => {
                "Never allow 'null' origin in Access-Control-Allow-Origin, especially with credentials. The null origin can be triggered by sandboxed iframes and enables CORS bypass attacks. Use a strict allow-list of HTTPS origins.".to_string()
            }
            "CORS Wildcard with Credentials" => {
                "Never use Access-Control-Allow-Origin: * with Access-Control-Allow-Credentials: true. This combination is actually invalid per spec but some browsers may honor it. Use specific trusted origins instead of wildcard when credentials are needed.".to_string()
            }
            "CORS Subdomain Reflection" => {
                "Validate origins against a strict allow-list. Don't use regex patterns that can match attacker-controlled subdomains. Each allowed origin should be explicitly listed. Be careful with subdomain wildcards.".to_string()
            }
            "CORS Insecure HTTP Origin Allowed" => {
                "Only allow HTTPS origins in production. HTTP origins are vulnerable to man-in-the-middle attacks. Implement strict HTTPS-only policy for CORS. Use HSTS to prevent protocol downgrade attacks.".to_string()
            }
            _ => {
                "Implement secure CORS policy: use strict allow-list of trusted HTTPS origins, never allow null origin with credentials, validate origins server-side, don't use wildcard with credentials, and implement proper authentication/authorization.".to_string()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::ScanConfig;
    use std::collections::HashMap;

    fn create_test_scanner() -> CorsMisconfigurationScanner {
        let client = Arc::new(HttpClient::new(10000, 3).unwrap());
        CorsMisconfigurationScanner::new(client)
    }

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

        let mut headers = HashMap::new();
        headers.insert(
            "Access-Control-Allow-Origin".to_string(),
            "https://evil.com".to_string(),
        );
        headers.insert(
            "Access-Control-Allow-Credentials".to_string(),
            "true".to_string(),
        );

        assert!(scanner.detect_arbitrary_origin_reflected(&headers, "https://evil.com"));

        // Without credentials
        let mut headers2 = HashMap::new();
        headers2.insert(
            "Access-Control-Allow-Origin".to_string(),
            "https://evil.com".to_string(),
        );

        assert!(scanner.detect_arbitrary_origin_reflected(&headers2, "https://evil.com"));
    }

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

        let mut headers = HashMap::new();
        headers.insert(
            "Access-Control-Allow-Origin".to_string(),
            "null".to_string(),
        );
        headers.insert(
            "Access-Control-Allow-Credentials".to_string(),
            "true".to_string(),
        );

        assert!(scanner.detect_null_origin_allowed(&headers));

        // Without credentials should not trigger
        let mut headers2 = HashMap::new();
        headers2.insert(
            "Access-Control-Allow-Origin".to_string(),
            "null".to_string(),
        );

        assert!(!scanner.detect_null_origin_allowed(&headers2));
    }

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

        let mut headers = HashMap::new();
        headers.insert("Access-Control-Allow-Origin".to_string(), "*".to_string());
        headers.insert(
            "Access-Control-Allow-Credentials".to_string(),
            "true".to_string(),
        );

        assert!(scanner.detect_wildcard_with_credentials(&headers));

        // Without credentials should not trigger
        let mut headers2 = HashMap::new();
        headers2.insert("Access-Control-Allow-Origin".to_string(), "*".to_string());

        assert!(!scanner.detect_wildcard_with_credentials(&headers2));
    }

    #[test]
    fn test_test_marker_uniqueness() {
        let scanner1 = create_test_scanner();
        let scanner2 = create_test_scanner();

        assert_ne!(scanner1.test_marker, scanner2.test_marker);
        assert!(scanner1.test_marker.starts_with("cors-"));
    }
}