lonkero 3.7.0

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

//! Reflection-based XSS Scanner (No Chrome Required)
//!
//! XSS scanner that works without headless Chrome by detecting
//! reflected payloads in HTTP responses. Detects:
//! - Reflected XSS in HTML context
//! - Reflected XSS in JavaScript context
//! - Reflected XSS in attribute context
//!
//! Uses comprehensive payload library (12,450+ payloads) with intensity-based
//! filtering for efficient scanning.

use crate::http_client::HttpClient;
use crate::payloads;
use crate::scanners::parameter_filter::{ParameterFilter, ScannerType};
use crate::scanners::registry::PayloadIntensity;
use crate::types::{Confidence, ScanConfig, Severity, Vulnerability};
use anyhow::Result;
use regex::Regex;
use std::sync::Arc;
use tracing::{debug, info};

pub struct ReflectionXssScanner {
    http_client: Arc<HttpClient>,
    /// Payload intensity - controls how many payloads to test
    intensity: PayloadIntensity,
}

impl ReflectionXssScanner {
    pub fn new(http_client: Arc<HttpClient>) -> Self {
        Self {
            http_client,
            intensity: PayloadIntensity::Standard,
        }
    }

    /// Create scanner with specific payload intensity
    pub fn with_intensity(http_client: Arc<HttpClient>, intensity: PayloadIntensity) -> Self {
        Self {
            http_client,
            intensity,
        }
    }

    /// Set payload intensity
    pub fn set_intensity(&mut self, intensity: PayloadIntensity) {
        self.intensity = intensity;
    }

    /// Scan URL for reflected XSS vulnerabilities
    pub async fn scan(
        &self,
        url: &str,
        _config: &ScanConfig,
    ) -> Result<(Vec<Vulnerability>, usize)> {
        self.scan_with_intensity(url, _config, self.intensity).await
    }

    /// Scan URL for reflected XSS with specific payload intensity
    pub async fn scan_with_intensity(
        &self,
        url: &str,
        _config: &ScanConfig,
        intensity: PayloadIntensity,
    ) -> Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let mut tests_run = 0;

        info!(
            "[Reflection-XSS] Starting scan for: {} (intensity: {:?})",
            url, intensity
        );

        // Extract parameters from URL
        let params = self.extract_parameters(url);
        if params.is_empty() {
            debug!("[Reflection-XSS] No parameters found in URL");
            return Ok((vulnerabilities, tests_run));
        }

        // Get payloads based on intensity
        let comprehensive_payloads = payloads::get_xss_payloads_by_intensity(intensity);
        info!(
            "[Reflection-XSS] Testing with {} payloads",
            comprehensive_payloads.len()
        );

        // Test each parameter
        for (param_name, _original_value) in &params {
            // Skip non-injectable parameters
            if ParameterFilter::should_skip_parameter(param_name, ScannerType::XSS) {
                continue;
            }

            // First, test priority payloads (fast path)
            let mut found_vuln = false;
            for (payload, context, description) in self.get_priority_payloads() {
                tests_run += 1;

                let test_url = self.build_test_url(url, param_name, &payload);

                match self.http_client.get(&test_url).await {
                    Ok(response) => {
                        if let Some(vuln) = self.analyze_reflection(
                            &response.body,
                            &payload,
                            param_name,
                            url,
                            context,
                            description,
                        ) {
                            info!("[Reflection-XSS] Found XSS in parameter: {}", param_name);
                            vulnerabilities.push(vuln);
                            found_vuln = true;
                            break; // One vuln per parameter is enough
                        }
                    }
                    Err(e) => {
                        debug!("[Reflection-XSS] Request failed: {}", e);
                    }
                }
            }

            // If no vuln found with priority payloads, try comprehensive payloads
            if !found_vuln && intensity != PayloadIntensity::Minimal {
                for payload in &comprehensive_payloads {
                    tests_run += 1;

                    let test_url = self.build_test_url(url, param_name, payload);
                    let (context, description) = self.detect_payload_context(payload);

                    match self.http_client.get(&test_url).await {
                        Ok(response) => {
                            if let Some(vuln) = self.analyze_reflection(
                                &response.body,
                                payload,
                                param_name,
                                url,
                                context,
                                description,
                            ) {
                                info!(
                                    "[Reflection-XSS] Found XSS with comprehensive payload in: {}",
                                    param_name
                                );
                                vulnerabilities.push(vuln);
                                break; // One vuln per parameter is enough
                            }
                        }
                        Err(e) => {
                            debug!("[Reflection-XSS] Request failed: {}", e);
                        }
                    }
                }
            }
        }

        info!(
            "[Reflection-XSS] Scan complete: {} vulnerabilities, {} tests",
            vulnerabilities.len(),
            tests_run
        );

        Ok((vulnerabilities, tests_run))
    }

    /// Extract parameters from URL
    fn extract_parameters(&self, url: &str) -> Vec<(String, String)> {
        let mut params = Vec::new();

        if let Ok(parsed) = url::Url::parse(url) {
            for (key, value) in parsed.query_pairs() {
                params.push((key.to_string(), value.to_string()));
            }
        }

        params
    }

    /// Build test URL with payload
    fn build_test_url(&self, base_url: &str, param_name: &str, payload: &str) -> String {
        if let Ok(mut parsed) = url::Url::parse(base_url) {
            let pairs: Vec<(String, String)> = parsed
                .query_pairs()
                .map(|(k, v)| {
                    if k == param_name {
                        (k.to_string(), payload.to_string())
                    } else {
                        (k.to_string(), v.to_string())
                    }
                })
                .collect();

            parsed.set_query(None);
            for (k, v) in pairs {
                parsed.query_pairs_mut().append_pair(&k, &v);
            }

            parsed.to_string()
        } else {
            base_url.to_string()
        }
    }

    /// Get XSS payloads with context information
    /// Uses comprehensive payload library (12,450+ payloads) limited by intensity
    fn get_xss_payloads(&self) -> Vec<(String, &'static str, &'static str)> {
        // Get comprehensive payloads based on intensity
        let comprehensive_payloads = payloads::get_xss_payloads_by_intensity(self.intensity);

        info!(
            "[Reflection-XSS] Using {} payloads (intensity: {:?})",
            comprehensive_payloads.len(),
            self.intensity
        );

        // Convert to tuple format with context detection
        comprehensive_payloads
            .into_iter()
            .map(|payload| {
                let (context, description) = self.detect_payload_context(&payload);
                (payload, context, description)
            })
            .collect()
    }

    /// Detect the context and description for a payload
    fn detect_payload_context(&self, payload: &str) -> (&'static str, &'static str) {
        let p = payload.to_lowercase();

        if p.contains("<script") {
            ("html", "Script tag injection")
        } else if p.contains("onerror=") || p.contains("onload=") || p.contains("onfocus=") {
            if p.contains("<img") || p.contains("<svg") || p.contains("<body") || p.contains("<input") {
                ("html", "Event handler injection")
            } else {
                ("attribute", "Attribute event handler")
            }
        } else if p.contains("onmouseover=") || p.contains("onclick=") || p.contains("onmouseenter=") {
            ("attribute", "Attribute event handler")
        } else if p.contains("javascript:") {
            ("html", "JavaScript URL")
        } else if p.starts_with("\"") || p.starts_with("'") {
            if p.contains("alert") || p.contains("confirm") || p.contains("prompt") {
                ("javascript", "JavaScript string breakout")
            } else {
                ("attribute", "Attribute breakout")
            }
        } else if p.contains("{{") || p.contains("${") || p.contains("<%") {
            ("template", "Template injection")
        } else if p.contains("<") && p.contains(">") {
            ("html", "HTML tag injection")
        } else {
            ("unknown", "XSS payload")
        }
    }

    /// Get hardcoded high-priority payloads for quick initial testing
    /// These are tested first before comprehensive payloads
    fn get_priority_payloads(&self) -> Vec<(String, &'static str, &'static str)> {
        vec![
            // HTML context - basic script injection
            (
                "<script>alert('XSS')</script>".to_string(),
                "html",
                "Script tag injection",
            ),
            // HTML context - img tag with onerror
            (
                "<img src=x onerror=alert('XSS')>".to_string(),
                "html",
                "IMG tag onerror handler",
            ),
            // HTML context - svg tag
            (
                "<svg onload=alert('XSS')>".to_string(),
                "html",
                "SVG onload handler",
            ),
            // Attribute context - breaking out of quotes
            (
                "\" onmouseover=\"alert('XSS')".to_string(),
                "attribute",
                "Attribute breakout (double quote)",
            ),
            (
                "' onmouseover='alert('XSS')".to_string(),
                "attribute",
                "Attribute breakout (single quote)",
            ),
            // JavaScript context
            (
                "';alert('XSS');//".to_string(),
                "javascript",
                "JavaScript string breakout",
            ),
            (
                "\";alert('XSS');//".to_string(),
                "javascript",
                "JavaScript string breakout (double quote)",
            ),
            // Event handler injection
            (
                "<body onload=alert('XSS')>".to_string(),
                "html",
                "Body onload handler",
            ),
            // Input with autofocus
            (
                "<input autofocus onfocus=alert('XSS')>".to_string(),
                "html",
                "Input autofocus onfocus",
            ),
            // Anchor tag with javascript
            (
                "<a href=\"javascript:alert('XSS')\">click</a>".to_string(),
                "html",
                "JavaScript URL in anchor",
            ),
            // Template injection that could lead to XSS
            (
                "{{constructor.constructor('alert(1)')()}}".to_string(),
                "template",
                "Template expression injection",
            ),
        ]
    }

    /// Analyze response for reflected XSS
    fn analyze_reflection(
        &self,
        body: &str,
        payload: &str,
        param_name: &str,
        url: &str,
        context: &str,
        description: &str,
    ) -> Option<Vulnerability> {
        let body_lower = body.to_lowercase();
        let payload_lower = payload.to_lowercase();

        // Check if payload is reflected
        if !body_lower.contains(&payload_lower) {
            return None;
        }

        // Check if payload is in an executable context
        let (is_executable, confidence) = self.check_executable_context(body, payload, context);

        if !is_executable {
            return None;
        }

        Some(Vulnerability {
            id: uuid::Uuid::new_v4().to_string(),
            vuln_type: format!("Reflected XSS in '{}' parameter", param_name),
            category: "XSS".to_string(),
            description: format!(
                "The parameter '{}' reflects user input without proper encoding. \
                 Detected {} in {} context.",
                param_name, description, context
            ),
            severity: Severity::High,
            confidence,
            url: url.to_string(),
            parameter: Some(param_name.to_string()),
            payload: payload.to_string(),
            evidence: Some(self.extract_evidence(body, payload)),
            remediation: "Implement proper output encoding based on context:\n\
                - HTML context: HTML entity encode (<, >, &, \", ')\n\
                - JavaScript context: JavaScript escape or use JSON.stringify()\n\
                - Attribute context: HTML attribute encode\n\
                - URL context: URL encode\n\n\
                Use Content-Security-Policy header to prevent inline script execution.\n\
                Consider using a templating engine with auto-escaping.".to_string(),
            cwe: "CWE-79".to_string(),
            cvss: 6.1,
            verified: false,
            false_positive: false,
            discovered_at: chrono::Utc::now().to_rfc3339(),
                ml_confidence: None,
                ml_data: None,
        })
    }

    /// Check if reflected payload is in an executable context
    fn check_executable_context(&self, body: &str, payload: &str, expected_context: &str) -> (bool, Confidence) {
        // Look for the payload in different contexts

        // Check if it's inside a <script> tag
        let script_re = Regex::new(r"<script[^>]*>[\s\S]*?</script>").ok();
        if let Some(re) = script_re {
            for cap in re.find_iter(body) {
                if cap.as_str().to_lowercase().contains(&payload.to_lowercase()) {
                    return (true, Confidence::High);
                }
            }
        }

        // Check if we injected our own script tag
        if payload.contains("<script") && body.to_lowercase().contains(&payload.to_lowercase()) {
            // Verify it's not HTML-encoded
            if !body.contains("&lt;script") {
                return (true, Confidence::High);
            }
        }

        // Check for event handler injection (onload, onerror, onclick, etc.)
        let event_re = Regex::new(r#"on\w+\s*=\s*['"]*[^'">\s]*alert"#).ok();
        if let Some(re) = event_re {
            if re.is_match(&body.to_lowercase()) {
                return (true, Confidence::High);
            }
        }

        // Check for javascript: URL
        if payload.contains("javascript:") && body.to_lowercase().contains("javascript:") {
            if !body.contains("&quot;javascript:") && !body.contains("&#") {
                return (true, Confidence::Medium);
            }
        }

        // Check if payload is reflected but might be encoded
        if body.to_lowercase().contains(&payload.to_lowercase()) {
            // Lower confidence if we can't confirm execution context
            return (true, Confidence::Low);
        }

        (false, Confidence::Low)
    }

    /// Extract evidence snippet around the payload
    fn extract_evidence(&self, body: &str, payload: &str) -> String {
        let payload_lower = payload.to_lowercase();
        let body_lower = body.to_lowercase();

        if let Some(pos) = body_lower.find(&payload_lower) {
            let start = pos.saturating_sub(50);
            let end = (pos + payload.len() + 50).min(body.len());

            let snippet = &body[start..end];
            format!("...{}...", snippet.replace('\n', " ").replace('\r', ""))
        } else {
            "Payload reflected in response".to_string()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_parameters() {
        let scanner = ReflectionXssScanner::new(Arc::new(
            HttpClient::new(Default::default()).unwrap(),
        ));

        let params = scanner.extract_parameters("https://example.com?foo=bar&baz=qux");
        assert_eq!(params.len(), 2);
        assert!(params.iter().any(|(k, v)| k == "foo" && v == "bar"));
        assert!(params.iter().any(|(k, v)| k == "baz" && v == "qux"));
    }

    #[test]
    fn test_build_test_url() {
        let scanner = ReflectionXssScanner::new(Arc::new(
            HttpClient::new(Default::default()).unwrap(),
        ));

        let result = scanner.build_test_url(
            "https://example.com?name=test&id=1",
            "name",
            "<script>alert(1)</script>",
        );

        assert!(result.contains("<script>alert(1)</script>") || result.contains("%3Cscript"));
        assert!(result.contains("id=1"));
    }
}