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
//! Evasion configuration — knobs for the strategy engine.
//!
//! One struct, one job: controls which evasion layers are enabled
//! and how aggressively the engine escalates.
use serde::{Deserialize, Serialize};
/// Evasion configuration.
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvasionConfig {
/// Enable payload encoding transformations.
pub encoding_enabled: bool,
/// Enable Content-Type switching (form → JSON, XML, multipart).
pub content_type_switching: bool,
/// Enable browser fingerprint rotation (User-Agent, Accept, etc.).
pub fingerprint_rotation: bool,
/// Enable header obfuscation (case mixing, tab separators, folding).
pub header_obfuscation: bool,
/// Enable grammar-aware payload mutation (SQL/XSS/CMD transforms).
pub grammar_mutations: bool,
/// Enable request smuggling metadata generation.
pub smuggling_enabled: bool,
/// Enable HTTP/2 evasion metadata generation.
pub h2_evasion_enabled: bool,
/// Maximum evasion retry attempts before giving up.
pub max_attempts: u32,
/// Disable TLS verification (`danger_accept_invalid_certs`).
pub insecure_tls: bool,
/// Proxies for round-robin IP rotation.
#[serde(default)]
pub proxies: Vec<String>,
/// Manual origin bypass mapping from Host to IP.
#[serde(default)]
pub origin_bypass: std::collections::HashMap<String, std::net::IpAddr>,
/// Body padding (bytes) — pre-pend N bytes of inert filler to
/// JSON / form / multipart bodies so the malicious payload sits
/// past cloud-WAF inspection windows (Cloudflare Pro 8 KB, AWS
/// WAF 16 KB). 0 disables padding entirely. Anything below
/// `wafrift_evolution::body_padding::MIN_USEFUL_PAD` is silently
/// skipped at strategy time.
#[serde(default)]
pub body_padding_bytes: usize,
/// Mutate URL/query-param payload bytes (off by default).
///
/// When true, the evade pipeline applies the same encoding +
/// grammar mutations to query-string parameter VALUES (not
/// names) and to the path's last segment. This is the canonical
/// attack surface for SQLi-in-`?id=` / XSS-in-`?q=` etc — most
/// production attacks live in URL parameters, not request bodies.
///
/// **Off by default** because mutating the URL changes upstream
/// routing semantics (e.g. cache keys, log entries, downstream
/// handler dispatch). Operators must opt in via
/// `wafrift-proxy --mutate-url` (or set this field via TOML).
#[serde(default)]
pub mutate_url: bool,
/// Allow upstream targets that fall in the bogon set
/// (loopback / RFC1918 / CGN / Teredo / IMDS / etc.).
///
/// Off by default — wafrift-transport's `EvasionClient` rejects
/// literal-IP requests to these ranges as a defence-in-depth
/// against accidental SSRF. Set true when targeting a lab
/// upstream on 127.0.0.1 / 192.168.x or when running tests
/// against a wiremock instance bound to loopback. Audit
/// (2026-05-10).
#[serde(default)]
pub allow_private_upstream: bool,
/// Weight for the ensemble sub-score dilution component in evolutionary
/// fitness scoring (`--dilution-weight`). Range `[0.0, 1.0]`.
///
/// When `> 0`, the evolution engine blends a dilution-plausibility
/// score (from `wafrift-wafmodel::ensemble_dilution`) into the oracle
/// fitness at submission time:
/// `final_fitness = oracle_fitness * (1 - w) + dilution_score * w`
///
/// Only active when the target WAF fingerprint shows a multi-rule-group
/// ensemble (Cloudflare Managed Rules, AWS Core Rule Set). On non-ensemble
/// WAFs this field has no effect regardless of value.
///
/// Default `0.0` (disabled) — callers opt in explicitly via CLI or TOML.
#[serde(default)]
pub dilution_weight: f64,
}
impl Default for EvasionConfig {
fn default() -> Self {
Self {
encoding_enabled: true,
content_type_switching: true,
fingerprint_rotation: true,
header_obfuscation: true,
grammar_mutations: true,
smuggling_enabled: true,
h2_evasion_enabled: true,
max_attempts: 5,
insecure_tls: false,
proxies: Vec::new(),
origin_bypass: std::collections::HashMap::new(),
body_padding_bytes: 0,
mutate_url: false,
allow_private_upstream: false,
dilution_weight: 0.0,
}
}
}
impl EvasionConfig {
/// Create a minimal config with only encoding enabled.
#[must_use]
pub fn encoding_only() -> Self {
Self {
encoding_enabled: true,
content_type_switching: false,
fingerprint_rotation: false,
header_obfuscation: false,
grammar_mutations: false,
smuggling_enabled: false,
h2_evasion_enabled: false,
max_attempts: 3,
insecure_tls: false,
proxies: Vec::new(),
origin_bypass: std::collections::HashMap::new(),
body_padding_bytes: 0,
mutate_url: false,
allow_private_upstream: false,
dilution_weight: 0.0,
}
}
/// Create a maximum-aggression config with everything enabled.
#[must_use]
pub fn maximum() -> Self {
Self {
encoding_enabled: true,
content_type_switching: true,
fingerprint_rotation: true,
header_obfuscation: true,
grammar_mutations: true,
smuggling_enabled: true,
h2_evasion_enabled: true,
max_attempts: 10,
insecure_tls: false,
proxies: Vec::new(),
origin_bypass: std::collections::HashMap::new(),
// Default to AWS-WAF-default-tier (16 KB) — covers
// Cloudflare Pro (8 KB), AWS WAF default, Akamai default.
body_padding_bytes: 16 * 1024,
// maximum() is opt-in aggression — flip URL mutation on
// too. Operators reaching for `maximum()` already accept
// the routing-semantics tradeoffs.
mutate_url: true,
// maximum() is for offensive testing on lab infra, where
// loopback / RFC1918 targets are normal. Stays false in
// production via the default; flips true here.
allow_private_upstream: true,
// maximum() enables dilution scoring at the recommended
// operational weight. Callers that don't want it use default().
dilution_weight: 0.3,
}
}
/// Maximum permitted `body_padding_bytes`. Requests padded beyond this
/// (256 MiB) would exhaust memory on the sending host before reaching
/// the WAF.
pub const MAX_BODY_PADDING_BYTES: usize = 256 * 1024 * 1024;
/// Maximum permitted `max_attempts`. Values above this would effectively
/// make the retry loop infinite for any non-trivial target.
pub const MAX_ATTEMPTS: u32 = 10_000;
/// Validate the configuration for conflicts or missing dependencies.
pub fn validate(&self) -> Result<(), String> {
if self.insecure_tls {
tracing::warn!(
"TLS certificate validation is disabled (--insecure-tls). Do not use in production."
);
}
if self.grammar_mutations && !self.encoding_enabled {
tracing::warn!(
"Grammar mutations are enabled but encoding is disabled. Mutations may require encoding to bypass effectively."
);
}
if self.max_attempts == 0 {
return Err("max_attempts must be greater than 0".to_string());
}
if self.max_attempts > Self::MAX_ATTEMPTS {
return Err(format!(
"max_attempts {} exceeds maximum allowed value {}",
self.max_attempts,
Self::MAX_ATTEMPTS,
));
}
if self.body_padding_bytes > Self::MAX_BODY_PADDING_BYTES {
return Err(format!(
"body_padding_bytes {} exceeds maximum allowed value {} (256 MiB)",
self.body_padding_bytes,
Self::MAX_BODY_PADDING_BYTES,
));
}
if !self.dilution_weight.is_finite() {
return Err(format!(
"dilution_weight must be finite, got {}",
self.dilution_weight
));
}
if self.dilution_weight < 0.0 || self.dilution_weight > 1.0 {
return Err(format!(
"dilution_weight {} is out of range [0.0, 1.0]",
self.dilution_weight
));
}
self.validate_proxies()?;
Ok(())
}
/// Validate the format of proxies.
fn validate_proxies(&self) -> Result<(), String> {
for proxy in &self.proxies {
if !proxy.starts_with("http://")
&& !proxy.starts_with("https://")
&& !proxy.starts_with("socks5://")
&& !proxy.starts_with("socks5h://")
{
return Err(format!(
"Invalid proxy URL '{proxy}': must start with http://, https://, socks5://, or socks5h://"
));
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_config_all_enabled() {
let config = EvasionConfig::default();
assert!(config.encoding_enabled);
assert!(config.content_type_switching);
assert!(config.fingerprint_rotation);
assert!(config.header_obfuscation);
assert!(config.grammar_mutations);
assert!(config.smuggling_enabled);
assert!(config.h2_evasion_enabled);
assert_eq!(config.max_attempts, 5);
assert!(!config.insecure_tls);
}
#[test]
fn encoding_only_config() {
let config = EvasionConfig::encoding_only();
assert!(config.encoding_enabled);
assert!(!config.content_type_switching);
assert!(!config.grammar_mutations);
}
#[test]
fn maximum_config() {
let config = EvasionConfig::maximum();
assert!(config.grammar_mutations);
assert_eq!(config.max_attempts, 10);
}
#[test]
fn config_serde_roundtrip() {
let config = EvasionConfig::default();
let json = serde_json::to_string(&config).expect("serialize");
let deserialized: EvasionConfig = serde_json::from_str(&json).expect("deserialize");
assert_eq!(deserialized.max_attempts, config.max_attempts);
}
// ── validate() bounds checks ─────────────────────────────────
#[test]
fn validate_rejects_max_attempts_zero() {
let cfg = EvasionConfig {
max_attempts: 0,
..Default::default()
};
assert!(cfg.validate().is_err(), "max_attempts=0 must be rejected");
}
#[test]
fn validate_rejects_max_attempts_above_ceiling() {
let cfg = EvasionConfig {
max_attempts: EvasionConfig::MAX_ATTEMPTS + 1,
..Default::default()
};
let err = cfg.validate().unwrap_err();
assert!(
err.contains("max_attempts"),
"error must mention field, got: {err}"
);
}
#[test]
fn validate_accepts_max_attempts_at_ceiling() {
let cfg = EvasionConfig {
max_attempts: EvasionConfig::MAX_ATTEMPTS,
..Default::default()
};
assert!(cfg.validate().is_ok());
}
#[test]
fn validate_rejects_body_padding_above_ceiling() {
let cfg = EvasionConfig {
body_padding_bytes: EvasionConfig::MAX_BODY_PADDING_BYTES + 1,
..Default::default()
};
let err = cfg.validate().unwrap_err();
assert!(
err.contains("body_padding_bytes"),
"error must mention field, got: {err}"
);
}
#[test]
fn validate_accepts_body_padding_at_ceiling() {
let cfg = EvasionConfig {
body_padding_bytes: EvasionConfig::MAX_BODY_PADDING_BYTES,
..Default::default()
};
assert!(cfg.validate().is_ok());
}
#[test]
fn validate_rejects_dilution_weight_nan() {
let cfg = EvasionConfig {
dilution_weight: f64::NAN,
..Default::default()
};
let err = cfg.validate().unwrap_err();
assert!(
err.contains("dilution_weight"),
"error must mention field, got: {err}"
);
}
#[test]
fn validate_rejects_dilution_weight_inf() {
let mut cfg = EvasionConfig {
dilution_weight: f64::INFINITY,
..Default::default()
};
let err = cfg.validate().unwrap_err();
assert!(err.contains("dilution_weight"));
cfg.dilution_weight = f64::NEG_INFINITY;
let err2 = cfg.validate().unwrap_err();
assert!(err2.contains("dilution_weight"));
}
#[test]
fn validate_rejects_dilution_weight_out_of_range() {
let mut cfg = EvasionConfig {
dilution_weight: 1.1,
..Default::default()
};
let err = cfg.validate().unwrap_err();
assert!(err.contains("dilution_weight"), "got: {err}");
cfg.dilution_weight = -0.1;
let err2 = cfg.validate().unwrap_err();
assert!(err2.contains("dilution_weight"), "got: {err2}");
}
#[test]
fn validate_accepts_dilution_weight_boundary_values() {
let mut cfg = EvasionConfig {
dilution_weight: 0.0,
..Default::default()
};
assert!(cfg.validate().is_ok());
cfg.dilution_weight = 1.0;
assert!(cfg.validate().is_ok());
cfg.dilution_weight = 0.5;
assert!(cfg.validate().is_ok());
}
#[test]
fn validate_rejects_invalid_proxy_scheme() {
let cfg = EvasionConfig {
proxies: vec!["ftp://bad.proxy:21".to_string()],
..Default::default()
};
assert!(cfg.validate().is_err());
}
#[test]
fn validate_accepts_valid_proxy_schemes() {
let mut cfg = EvasionConfig::default();
for scheme in &[
"http://proxy:8080",
"https://proxy:8443",
"socks5://proxy:1080",
"socks5h://proxy:1080",
] {
cfg.proxies = vec![(*scheme).to_string()];
assert!(cfg.validate().is_ok(), "scheme {scheme} should be accepted");
}
}
#[test]
fn maximum_config_passes_validate() {
assert!(EvasionConfig::maximum().validate().is_ok());
}
#[test]
fn encoding_only_config_passes_validate() {
assert!(EvasionConfig::encoding_only().validate().is_ok());
}
}