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
// 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 - CRLF Injection Scanner
* Tests for HTTP response splitting and CRLF injection vulnerabilities
*
* Detects:
* - HTTP header injection via CRLF (\r\n)
* - HTTP response splitting
* - Cookie injection via CRLF
* - Location header injection
* - Arbitrary header injection
* - XSS via injected headers
*
* @copyright 2026 Bountyy Oy
* @license Proprietary
*/
use tracing::{debug, info};
pub struct CrlfInjectionScanner {
http_client: Arc<HttpClient>,
}
impl CrlfInjectionScanner {
pub fn new(http_client: Arc<HttpClient>) -> Self {
Self { http_client }
}
/// Scan a parameter for CRLF 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!(
"[CRLF] Skipping framework/internal parameter: {}",
param_name
);
return Ok((Vec::new(), 0));
}
let mut vulnerabilities = Vec::new();
let mut tests_run = 0;
info!(
"[CRLF] Testing CRLF injection on parameter: {} (priority: {})",
param_name,
ParameterFilter::get_parameter_priority(param_name)
);
// Test various CRLF injection payloads
let payloads = vec![
// Cookie injection
(
"%0d%0aSet-Cookie:%20admin=true",
"Cookie injection (URL encoded)",
),
(
"\r\nSet-Cookie: admin=true",
"Cookie injection (literal CRLF)",
),
(
"%0d%0aSet-Cookie:%20session=hijacked",
"Session cookie injection",
),
(
"\r\n\r\nSet-Cookie: token=stolen",
"Double CRLF cookie injection",
),
// Location header injection
(
"%0d%0aLocation:%20https://evil.com",
"Location header injection",
),
(
"\r\nLocation: https://evil.com",
"Location redirect injection",
),
// Arbitrary header injection
(
"%0d%0aX-Custom-Header:%20injected",
"Custom header injection",
),
("\r\nX-Injected: true", "Header injection (literal)"),
// HTTP response splitting
(
"%0d%0a%0d%0aHTTP/1.1%20200%20OK%0d%0a",
"HTTP response splitting (encoded)",
),
(
"\r\n\r\nHTTP/1.1 200 OK\r\n",
"HTTP response splitting (literal)",
),
(
"%0d%0a%0d%0a<script>alert(1)</script>",
"Response splitting with XSS",
),
// Content-Type injection
(
"%0d%0aContent-Type:%20text/html",
"Content-Type header injection",
),
(
"\r\nContent-Type: text/html\r\n\r\n<html><script>alert(1)</script></html>",
"Content-Type with XSS",
),
// Multiple header injection
(
"%0d%0aX-Header1:%20value1%0d%0aX-Header2:%20value2",
"Multiple headers",
),
// Unicode/alternative encodings
("%E5%98%8A%E5%98%8DSet-Cookie:%20admin=true", "Unicode CRLF"),
(
"%E5%98%8D%E5%98%8ASet-Cookie:%20admin=true",
"Alternative Unicode",
),
// Null byte variants
("%00%0d%0aSet-Cookie:%20admin=true", "Null byte + CRLF"),
];
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) => {
let headers_vec: Vec<(String, String)> = response
.headers
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
if let Some(vuln) = self.analyze_response(
&headers_vec,
&response.body,
payload,
description,
&test_url,
param_name,
) {
info!("CRLF 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 CRLF 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 CRLF injection indicators
/// Returns Some(Vulnerability) only if we can VERIFY that our payload affected the response headers
fn analyze_response(
&self,
headers: &[(String, String)],
body: &str,
payload: &str,
_description: &str,
url: &str,
param_name: &str,
) -> Option<Vulnerability> {
// IMPORTANT: We must verify that our injected values actually appear in the response headers.
// Simply sending a payload is not enough - the server must actually be vulnerable.
// Many modern frameworks/servers block CRLF injection, so we need concrete proof.
// Check for injected Set-Cookie header with our specific injected values
if payload.contains("Set-Cookie") {
for (key, value) in headers {
if key.to_lowercase() == "set-cookie" {
// Only flag if our EXACT injected cookie value is present
if value.contains("admin=true")
|| value.contains("session=hijacked")
|| value.contains("token=stolen")
{
return Some(self.create_vulnerability(
url,
param_name,
payload,
"CRLF injection - Cookie injection",
&format!("Injected cookie detected in response headers: {}", value),
Confidence::High,
));
}
}
}
// If payload contains Set-Cookie but we didn't find our injected value, NOT vulnerable
return None;
}
// Check for injected Location header with our specific injected URL
if payload.contains("Location") {
for (key, value) in headers {
if key.to_lowercase() == "location" {
// Only flag if our EXACT injected location is present
if value.contains("evil.com") || value.contains("attacker") {
return Some(self.create_vulnerability(
url,
param_name,
payload,
"CRLF injection - Location header injection",
&format!("Injected Location header in response: {}", value),
Confidence::High,
));
}
}
}
// If payload contains Location but we didn't find our injected value, NOT vulnerable
return None;
}
// Check for injected custom headers - these should ONLY exist if we successfully injected them
let injected_header_names = vec!["x-custom-header", "x-injected", "x-header1", "x-header2"];
if payload.contains("X-Custom-Header")
|| payload.contains("X-Injected")
|| payload.contains("X-Header1")
|| payload.contains("X-Header2")
{
for (key, value) in headers {
let key_lower = key.to_lowercase();
for injected_name in &injected_header_names {
if key_lower == *injected_name {
// Verify the value matches what we tried to inject
if value == "injected"
|| value == "true"
|| value == "value1"
|| value == "value2"
{
return Some(self.create_vulnerability(
url,
param_name,
payload,
"CRLF injection - Arbitrary header injection",
&format!("Injected header found in response: {}: {}", key, value),
Confidence::High,
));
}
}
}
}
// If payload contains custom headers but we didn't find them in response, NOT vulnerable
return None;
}
// Check for HTTP response splitting - body must contain our injected HTTP response
if payload.contains("HTTP/1.1") {
// The body should contain literal "HTTP/1.1 200 OK" at start of a line (response splitting)
// AND this should be in addition to normal body content (indicating two responses)
if body
.lines()
.any(|line| line.trim().starts_with("HTTP/1.1 200 OK"))
{
return Some(self.create_vulnerability(
url,
param_name,
payload,
"CRLF injection - HTTP response splitting",
"Multiple HTTP responses detected - injected HTTP response found in body",
Confidence::Medium,
));
}
// If payload contains HTTP/1.1 but no response splitting detected, NOT vulnerable
return None;
}
// Check for XSS via CRLF injection - body must contain our EXACT injected script
if payload.contains("<script>alert(1)</script>") {
// Only vulnerable if our exact script appears (not just any script tag)
if body.contains("<script>alert(1)</script>") {
return Some(self.create_vulnerability(
url,
param_name,
payload,
"CRLF injection with XSS",
"Injected script tag found in response body via CRLF injection",
Confidence::High,
));
}
// If we tried to inject script but it's not in response, NOT vulnerable
return None;
}
// Check for Content-Type injection
if payload.contains("Content-Type") {
for (key, value) in headers {
// Look for unusual Content-Type that matches our injection attempt
if key.to_lowercase() == "content-type" {
// If we see multiple Content-Type values or our injected value
if value.matches("text/html").count() > 1 {
return Some(self.create_vulnerability(
url,
param_name,
payload,
"CRLF injection - Content-Type header injection",
&format!("Injected Content-Type detected: {}", value),
Confidence::Medium,
));
}
}
}
// Content-Type injection attempt but no evidence, NOT vulnerable
return None;
}
// Default: no vulnerability detected
// We do NOT report based on just sending a payload - we must see evidence in the response
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!("crlf_injection_{}", uuid::Uuid::new_v4()),
vuln_type: "CRLF Injection".to_string(),
severity: Severity::High,
confidence,
category: "Injection".to_string(),
url: url.to_string(),
parameter: Some(param_name.to_string()),
payload: payload.to_string(),
description: format!(
"CRLF injection vulnerability in parameter '{}': {}",
param_name, description
),
evidence: Some(evidence.to_string()),
cwe: "CWE-93".to_string(),
cvss: 7.5,
verified,
false_positive: false,
remediation: "1. Sanitize CRLF characters (\\r\\n, %0d%0a, %0a, %0d) from user input\n\
2. Validate and encode all user input before including in HTTP headers\n\
3. Use framework-provided header setting functions\n\
4. Implement proper input validation and output encoding\n\
5. Reject requests containing CRLF sequences\n\
6. Use allowlists for redirect URLs\n\
7. Set proper Content-Type and X-Content-Type-Options headers"
.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() -> CrlfInjectionScanner {
let http_client = Arc::new(HttpClient::new(30, 3).unwrap());
CrlfInjectionScanner::new(http_client)
}
#[test]
fn test_analyze_cookie_injection() {
let scanner = create_test_scanner();
let headers = vec![("Set-Cookie".to_string(), "admin=true".to_string())];
let body = "";
let result = scanner.analyze_response(
&headers,
body,
"%0d%0aSet-Cookie:%20admin=true",
"Cookie injection",
"http://example.com",
"redirect",
);
assert!(result.is_some());
let vuln = result.unwrap();
assert_eq!(vuln.vuln_type, "CRLF Injection");
assert_eq!(vuln.severity, Severity::High);
assert_eq!(vuln.cwe, "CWE-93");
assert!(vuln.description.contains("Cookie injection"));
}
#[test]
fn test_analyze_location_injection() {
let scanner = create_test_scanner();
let headers = vec![("Location".to_string(), "https://evil.com".to_string())];
let body = "";
let result = scanner.analyze_response(
&headers,
body,
"%0d%0aLocation:%20https://evil.com",
"Location injection",
"http://example.com",
"next",
);
assert!(result.is_some());
let vuln = result.unwrap();
assert!(vuln.description.contains("Location header injection"));
}
#[test]
fn test_analyze_custom_header_injection() {
let scanner = create_test_scanner();
let headers = vec![("X-Custom-Header".to_string(), "injected".to_string())];
let body = "";
let result = scanner.analyze_response(
&headers,
body,
"%0d%0aX-Custom-Header:%20injected",
"Header injection",
"http://example.com",
"url",
);
assert!(result.is_some());
let vuln = result.unwrap();
assert!(vuln.description.contains("Arbitrary header injection"));
}
#[test]
fn test_analyze_response_splitting() {
let scanner = create_test_scanner();
let headers = vec![];
let body = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html>Injected</html>";
let result = scanner.analyze_response(
&headers,
body,
"%0d%0a%0d%0aHTTP/1.1%20200%20OK",
"Response splitting",
"http://example.com",
"page",
);
assert!(result.is_some());
let vuln = result.unwrap();
assert!(vuln.description.contains("HTTP response splitting"));
}
#[test]
fn test_analyze_crlf_with_xss() {
let scanner = create_test_scanner();
let headers = vec![];
let body = "<script>alert(1)</script>";
let result = scanner.analyze_response(
&headers,
body,
"%0d%0a%0d%0a<script>alert(1)</script>",
"CRLF with XSS",
"http://example.com",
"dest",
);
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 headers = vec![("Content-Type".to_string(), "text/html".to_string())];
let body = "<html><body>Normal page</body></html>";
let result = scanner.analyze_response(
&headers,
body,
"normal_value",
"Normal request",
"http://example.com",
"param",
);
assert!(result.is_none());
}
#[test]
fn test_create_vulnerability() {
let scanner = create_test_scanner();
let vuln = scanner.create_vulnerability(
"http://example.com/redirect",
"url",
"%0d%0aSet-Cookie:%20admin=true",
"CRLF injection - Cookie injection",
"Injected cookie: admin=true",
Confidence::High,
);
assert_eq!(vuln.vuln_type, "CRLF Injection");
assert_eq!(vuln.severity, Severity::High);
assert_eq!(vuln.parameter, Some("url".to_string()));
assert_eq!(vuln.cwe, "CWE-93");
assert_eq!(vuln.cvss, 7.5);
assert!(vuln.verified);
assert!(vuln.remediation.contains("CRLF characters"));
}
}