Skip to main content

encode_layered

Function encode_layered 

Source
pub fn encode_layered(
    payload: impl AsRef<[u8]>,
    strategies: &[Strategy],
) -> Result<String, EncodeError>
Expand description

Apply multiple encoding strategies in sequence (layered encoding).

ยงErrors

Returns EncodeError::PayloadTooLarge if the input exceeds super::strategy::MAX_PAYLOAD_SIZE. Returns EncodeError::LayeredOutputTooLarge if any intermediate output exceeds MAX_LAYERED_OUTPUT_SIZE.

Examples found in repository?
examples/layered.rs (lines 16-19)
7fn main() {
8    let payload = "SELECT * FROM users WHERE id=1";
9
10    println!("Original payload:");
11    println!("  {}", payload);
12    println!();
13
14    // Layered encoding: apply multiple strategies in sequence
15    // This bypasses WAFs that decode once or twice before matching
16    let layered = encode_layered(
17        payload,
18        &[Strategy::SqlCommentInsertion, Strategy::UrlEncode],
19    )
20    .unwrap();
21
22    println!("Layered (SQL comments + URL encoding):");
23    println!("  {}", layered);
24    println!();
25
26    // More aggressive example: triple-layer for paranoid WAFs
27    let aggressive = encode_layered(
28        payload,
29        &[
30            Strategy::CaseAlternation,
31            Strategy::WhitespaceInsertion,
32            Strategy::DoubleUrlEncode,
33        ],
34    )
35    .unwrap();
36
37    println!("Aggressive 3-layer (case + whitespace + double URL):");
38    println!(
39        "  {}",
40        aggressive[..aggressive.len().min(80)].to_string() + "..."
41    );
42    println!();
43
44    // Show pre-defined useful combinations
45    println!("Pre-defined useful combinations:");
46    for (i, combo) in layered_combinations(2).iter().enumerate() {
47        println!("  {}. {:?}", i + 1, combo);
48    }
49    println!();
50
51    // Demo: escalation ladder
52    println!("Escalation ladder (least to most aggressive):");
53    let strategies = all_strategies();
54    for (i, strategy) in strategies.iter().take(5).enumerate() {
55        let score = aggressiveness(*strategy);
56        let result = encode(payload, *strategy).unwrap();
57        println!(
58            "  {}. {:?} (score: {:.1}): {}...",
59            i + 1,
60            strategy,
61            score,
62            &result[..result.len().min(40)]
63        );
64    }
65    println!("     ... ({} more strategies)", strategies.len() - 5);
66}