Nyffenegger JWAT PDF
Nyffenegger JWAT PDF
Nyffenegger JWAT PDF
Louis Nyffenegger
@PentesterLab
[email protected]
About me
Security Engineer
PentesterLab:
Platform to learn web security/penetration testing
100% Hands-on
PentesterLab.com / @PentesterLab
Who uses JWT?
PentesterLab.com / @PentesterLab
Acronyms
• JOSE:
• Javascript Object Signing and Encryption
• Also the name of the working group
• JWT: JSON Web Token == “jot” Token
• JWE: JSON Web Encryption
• JWS: JSON Web Signature
• JWK: JSON Web Key
• JWA: JSON Web Algorithm
PentesterLab.com / @PentesterLab
Crypto 101
Signature vs Encryption
PentesterLab.com / @PentesterLab
Multiple ways of signing
PentesterLab.com / @PentesterLab
Signing with a secret
Sign! Verify!
Secret
PentesterLab.com / @PentesterLab
Signing: asymmetric
Sign! Verify!
Public Private
PentesterLab.com / @PentesterLab
THE JWT FORMAT
JavaScript Object Notation (JSON)
PentesterLab.com / @PentesterLab
The Compact JWS Format
PentesterLab.com / @PentesterLab
The Compact JWS Format
Separated by a dot
Header
. Payload
. Signature
PentesterLab.com / @PentesterLab
The Compact JWS Format
Separated by a dot
eyJ = Base64('{"')
PentesterLab.com / @PentesterLab
The Compact JWS Format
Base64({…})
. Base64({…})
. Base64(…)
PentesterLab.com / @PentesterLab
The Compact JWS Format: Encoding
* https://tools.ietf.org/html/rfc7515#appendix-C
PentesterLab.com / @PentesterLab
The JWT Format: header
Base64({"alg": "HS256",
"typ": "JWS"}) . …
. …
PentesterLab.com / @PentesterLab
The JWT Format: Algorithms
PentesterLab.com / @PentesterLab
The JWT Format: Algorithms
PentesterLab.com / @PentesterLab
The JWT Format: Algorithms
HS384
HS512
Secret
PentesterLab.com / @PentesterLab
The JWT Format: Algorithms
HS384
HS512
Secret
Compromised secret
PentesterLab.com / @PentesterLab
The JWT Format: Algorithms
HS384
HS512
Compromised secret
PentesterLab.com / @PentesterLab
The JWT Format: Asymmetric
RS256
Asymmetric: sharing the key
RS384
RS512
ES256
ES384
ES512
PS256
Private PS384
Public PS512
PentesterLab.com / @PentesterLab
The JWT Format: Asymmetric
RS256
Asymmetric: Only trusted services get the
RS384
private key RS512
ES256
ES384
ES512
PS256
Private PS384
Public PS512
PentesterLab.com / @PentesterLab
The JWT Format: Asymmetric
RS256
Asymmetric: If one service gets compromised…
RS384
RS512
ES256
ES384
ES512
PS256
Private PS384
Public PS512
PentesterLab.com / @PentesterLab
The JWT Format: Asymmetric
RS256
Asymmetric: Even in the browser!
RS384
RS512
ES256
ES384
ES512
PS256
Private PS384
Public PS512
PentesterLab.com / @PentesterLab
The JWT Format: payload
…
. Base64({"user":"admin",
"roles": ["adm","users"]}) . …
PentesterLab.com / @PentesterLab
The JWT Format: payload
…
. Base64({"user":"admin",
"exp":12…, "iat":1234.. }) . …
PentesterLab.com / @PentesterLab
The JWT Format: payload
PentesterLab.com / @PentesterLab
The JWT Format: creating a token
PentesterLab.com / @PentesterLab
Keep in mind
PentesterLab.com / @PentesterLab
Attacking JWT
PentesterLab.com / @PentesterLab
Not checking the signature
Not checking the signature
PentesterLab.com / @PentesterLab
Not checking the signature
Exploitation:
• Get a token
• Decode and tamper with the payload
• Profit
PentesterLab.com / @PentesterLab
None algorithm
The None algorithm
PentesterLab.com / @PentesterLab
The None algorithm
Exploitation:
• Get a token
• Decode the header and change the algorithm to
“None” (or “none”)
• Decode and tamper with the payload
• Keep or remove the signature
• Profit
PentesterLab.com / @PentesterLab
Trivial Secret
Trivial secret
PentesterLab.com / @PentesterLab
Trivial secret
https://github.com/aichbauer/express-rest-api-boilerplate/blob/master/api/services/auth.service.js
PentesterLab.com / @PentesterLab
Trivial secret
Exploitation:
• Get a token
• Brute force the secret until you get the same
signature
• Tamper with the payload
• Re-sign the token using the secret
PentesterLab.com / @PentesterLab
Algorithm confusion
Algorithm confusion
The sender controls the algorithm used
You can tell the receiver that the token has been
signed using HMAC instead of RSA for example
With RSA, you sign with the private key and verify
with the public key
With HMAC, you sign and verify with the same key
If you tell the receiver it’s an HMAC and it verifies it
with the public key (thinking it’s RSA?)
PentesterLab.com / @PentesterLab
Algorithm confusion
With RSA, you sign with the private key and verify
with the public key
With HMAC, you sign and verify with the same key
You tell the receiver it’s an HMAC (instead of RSA)
and it verifies the signature using HMAC with the
public key as the secret (thinking it’s RSA):
You can sign the token with the public key
PentesterLab.com / @PentesterLab
Algorithm confusion
PentesterLab.com / @PentesterLab
Algorithm confusion
Exploitation:
• Get a token signed with RSA (you only have
access to the public key)
• Decode the header and change the algorithm
from RSA “RS256” to HMAC “HS256”
• Tamper with the payload
• Sign the token with the public RSA key
PentesterLab.com / @PentesterLab
kid injection
Kid parameter
The header can contain a kid parameter:
• Key id (https://tools.ietf.org/html/
rfc7515#section-4.1.4)
• Often used to retrieve a key from:
✴ The filesystem
✴ A Database
This is done prior to the verification of the signature
If the parameter is injectable, you can bypass the
signature
PentesterLab.com / @PentesterLab
Kid Injection
Exploitation:
• Get a signed token containing a kid parameter
• Decode the header and change the kid with a
SQL injection payload
• Tamper with the payload
• Sign the token using the return value
from the SQL injection
PentesterLab.com / @PentesterLab
CVE-2018-0114
Libraries: CVE-2018-0114
PentesterLab.com / @PentesterLab
Libraries: CVE-2018-0114
Integrity bypass!
PentesterLab.com / @PentesterLab
Libraries: CVE-2018-0114 - Exploitation
Exploitation:
• Get a token
• Decode and tamper with the payload
• Generate a RSA key
• Add “n" & “e” to the header and use
RS256
• Sign the token with your RSA key
PentesterLab.com / @PentesterLab
Conclusion
Recommendations
PentesterLab.com / @PentesterLab
Recommendations
PentesterLab.com / @PentesterLab
Back to the future
PentesterLab.com / @PentesterLab
Back to the future
1 HTTP Request with JWT 2 Parsing of the JWT to extract the “jku” header
Trusted
Server
PentesterLab.com / @PentesterLab
Back to the future
PentesterLab.com / @PentesterLab
THANKS
FOR YOUR TIME !
Any questions?