Skip to main content

encode

Function encode 

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

Encode a payload using the selected strategy.

ยงErrors

Returns EncodeError::PayloadTooLarge if the input exceeds MAX_PAYLOAD_SIZE. Returns EncodeError::InvalidUtf8 for text-oriented strategies when the input contains invalid UTF-8.

Examples found in repository?
examples/encoding_basic.rs (line 14)
5fn main() {
6    // A classic SQL injection payload that most WAFs will catch
7    let payload = "' OR 1=1--";
8
9    println!("Original payload:");
10    println!("  {}", payload);
11    println!();
12
13    // Single URL encoding: converts special characters to %XX hex escapes
14    let encoded = encode(payload, Strategy::UrlEncode).unwrap();
15
16    println!("URL-encoded (bypasses keyword filters):");
17    println!("  {}", encoded);
18    println!();
19
20    // Show what the server decodes it back to
21    println!("Server decodes this back to:");
22    println!("  {}", payload);
23    println!();
24
25    // Try a few more strategies
26    println!("Other encodings for comparison:");
27
28    let double = encode(payload, Strategy::DoubleUrlEncode).unwrap();
29    println!("  Double URL:    {}", double);
30
31    let case_alt = encode(payload, Strategy::CaseAlternation).unwrap();
32    println!("  Case alt:      {}", case_alt);
33
34    let unicode = encode(payload, Strategy::UnicodeEncode).unwrap();
35    println!("  Unicode:       {}", unicode);
36}
More examples
Hide additional examples
examples/layered.rs (line 56)
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}