JWT jku&x5u = ❤
Attacking JSON WEB TOKENS…
Louis Nyffenegger
@PentesterLab
louis@pentesterlab.com
About me
PentesterLab.com / @PentesterLab
Security Engineer
PentesterLab:
Pentester/Code Reviewer/Security consultant/Security architect
Platform to learn web security/penetration testing
100% Hands-on
Available for individuals (free and PRO) and enterprises
Run a website to help people learn security
Who uses JWT?
PentesterLab.com / @PentesterLab
• A lot of people for OAuth2
• A lot of people for sessions
• A lot of people to manage trust
• A lot of people for password reset
• A lot of people who care about being stateless
and multi-datacenter architecture
Crypto 101
Signature vs Encryption
PentesterLab.com / @PentesterLab
Encryption gives you confidentiality
Signature gives you integrity
Multiple ways of signing
PentesterLab.com / @PentesterLab
• With a secret using HMAC
• With a private key using RSA/EC/… (asymmetric)
Signing with a secret
PentesterLab.com / @PentesterLab
Sign! Verify!
Secret
Signing: asymmetric
PentesterLab.com / @PentesterLab
Sign!
PrivatePublic
Verify!
THE JWT FORMAT
JavaScript Object Notation (JSON)
PentesterLab.com / @PentesterLab
Human readable format to store or transmit objects
The Compact JWS Format
PentesterLab.com / @PentesterLab
Header Payload Signature
3 parts in a JSON Web Token:
The Compact JWS Format
PentesterLab.com / @PentesterLab
Header Payload Signature
Separated by a dot
. .
The Compact JWS Format
PentesterLab.com / @PentesterLab
eyJ0eXAiOiJK
V1QiLCJhbGci
OiJIUzI1NiJ9
eyJsb2dpbi
I6ImFkb
WluIn0
FSfvCBAwypJ4abF6
jFLmR7JgZhkW674
Z8dIdAIRyt1E
Separated by a dot
. .
eyJ = Base64('{"')
The Compact JWS Format
PentesterLab.com / @PentesterLab
Base64({…}) Base64({…}) Base64(…)
Header and Payload are base64* encoded JSON
. .
* urlsafe base64 encoding without padding
The signature is also base64 encoded
The Compact JWS Format: Encoding
PentesterLab.com / @PentesterLab
Urlsafe base64 encoding without padding:
*https://tools.ietf.org/html/rfc7515#appendix-C
The JWT Format: header
PentesterLab.com / @PentesterLab
Base64({"alg": "HS256",
"typ": "JWS"})
The header contains an algorithm “alg” attribute:
In this example HMAC with SHA256 was used
To tell how the token was signed.
…
. . …
The JWT Format: Algorithms
PentesterLab.com / @PentesterLab
A lot of different algorithms are supported*:
None
* https://jwt.io/ covers most
HS256
HS384
HS512
RS256
RS384
RS512
ES256
ES384
ES512
PS256
PS384
PS512
The JWT Format: payload
PentesterLab.com / @PentesterLab
…
The payload may contain literally anything:
Base64({"user":"admin",
"roles": ["adm","users"]}). . …
The JWT Format: payload
PentesterLab.com / @PentesterLab
The payload may contain registered claims:
Base64({"user":"admin",
"exp":12…, "iat":1234.. }). .… …
The JWT Format: creating a token
PentesterLab.com / @PentesterLab
• Create the JSON header and base64 encode it
• Create the JSON payload and base64 encode it
• Concatenate with a dot the (encoded) header
and payload
• Sign the result (header+.+payload)
• Base64 encode the signature
• Append a dot then the signature
The JWT Format: verifying a token
PentesterLab.com / @PentesterLab
• Split the token in three parts based on the dots
• Base64 decode each part
• Parse the JSON for the header and payload
• Retrieve the algorithm from the header
• Verify the signature based on the algorithm
• Verify the claims
Classic JWT attacks
PentesterLab.com / @PentesterLab
• None algorithm
• Trivial secret
• Algorithm confusion
• Injection in the kid parameter
• CVE-2018-0114
•
jku & x5u
jku and x5u
PentesterLab.com / @PentesterLab
• If you read some of the JWS RFC, you probably
learnt about jku and x5u parameter for the headers
• People are starting to use jku (JWK URL)
The JWT Format: jku&x5u
PentesterLab.com / @PentesterLab
Base64({"jku": "https://...",
...})
…
. . …
Base64({"x5u": "https://...",
...})
…
. . …
The JWT Format: jwk
PentesterLab.com / @PentesterLab
{
"keys": [
{
"kty": "RSA",
"use": "sig",
"kid": "pentesterlab",
"n": "oTtAXRgdJ6Pu0jr3hK3opCF5uqKWKbm4Kkq...vTF0FGw",
"e": "AQAB",
"alg": "RS256"
}
]
}
The JWT Format: x5c
PentesterLab.com / @PentesterLab
{
"keys": [
{
"kty": "RSA",
"use": "sig",
"kid": "pentesterlab",
"x5c": "MIIDWDCCAkACCQCnE....fpye27SQbC2fBxebsek=",
"alg": "RS256"
}
]
}
jku and x5u
PentesterLab.com / @PentesterLab
Application
Trusted
Server
User
Application
Trusted
Server
User
HTTP Request with JWT1
jku and x5u
PentesterLab.com / @PentesterLab
Application
Trusted
Server
User
HTTP Request with JWT Parsing of the JWT to extract the “jku” header1 2
jku and x5u
PentesterLab.com / @PentesterLab
Application
Trusted
Server
User
HTTP Request with JWT Parsing of the JWT to extract the “jku” header1 2
3 Fetching of the JWK based on the “jku” header
jku and x5u
PentesterLab.com / @PentesterLab
Fetching of the JWK based on the “jku” header
Application
Trusted
Server
User
HTTP Request with JWT Parsing of the JWT to extract the “jku” header1 2
3
Parsing of the JWK4
jku and x5u
PentesterLab.com / @PentesterLab
Application
Trusted
Server
User
HTTP Request with JWT Parsing of the JWT to extract the “jku” header1 2
3
Parsing of the JWK4
Verifying the JWT signature using the JWK5
Fetching of the JWK based on the “jku” header
jku and x5u
PentesterLab.com / @PentesterLab
Application
Trusted
Server
User
HTTP Request with JWT Parsing of the JWT to extract the “jku” header
Response
1
6
2
3
Parsing of the JWK4
Verifying the JWT signature using the JWK5
Fetching of the JWK based on the “jku” header
jku and x5u
PentesterLab.com / @PentesterLab
jku and x5u
PentesterLab.com / @PentesterLab
Application
Malicious
Server
HTTP Request with malicious JWT Parsing of the JWT to extract the “jku” header
Response
1
6
2
3
Parsing of the JWK4
Verifying the JWT signature using the JWK5
Attacker
Fetching of the malicious JWK based on the “jku” header
jku and x5u
PentesterLab.com / @PentesterLab
Application
Malicious
Server
HTTP Request with malicious JWT Parsing of the JWT to extract the “jku” header1 2
3
Attacker
Fetching of the malicious JWK based on the “jku” header
jku and x5u
PentesterLab.com / @PentesterLab
Turns out filtering URLs is incredibly hard
jku and x5u : regular expression
PentesterLab.com / @PentesterLab
https://trusted.example.com => https://trustedzexample.com
jku and x5u : starts with
PentesterLab.com / @PentesterLab
https://trusted => https://trusted@pentesterlab.com
https://trusted/jwks/ => https://trusted/jwks/../file_uploaded
https://trusted/jwks/ => https://trusted/jwks/../open_redirect
https://trusted/jwks/ => https://trusted/jwks/../header_injection
3 Fetching of the JWK based on the “jku” header
Parsing of the JWT to extract the “jku” header2
Application
Trusted
Server
HTTP Request with malicious JWT1
Malicious
Server
Attacker
jku and Open Redirect
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Open Redirect
Trusted
Server
HTTP Request with malicious JWT1
Malicious
Server
3 Fetching of the JWK based on the “jku” header
Attacker
jku and Open Redirect
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Open Redirect
Trusted
Server
3 Fetching of the JWK based on the “jku” header
3a Redirect to malicious server
HTTP Request with malicious JWT1
Malicious
Server
Attacker
jku and Open Redirect
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Open Redirect
Trusted
Server
3 Fetching of the JWK based on the “jku” header
3a Redirect to malicious server
3b Fetching of the malicious JWK
after following the redirect
HTTP Request with malicious JWT1
Malicious
Server
Attacker
jku and Open Redirect
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Open Redirect
Trusted
Server
3
Parsing of the JWK4
Fetching of the JWK based on the “jku” header
3a Redirect to malicious server
3b Fetching of the malicious JWK
after following the redirect
HTTP Request with malicious JWT1
Malicious
Server
Attacker
jku and Open Redirect
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Open Redirect
Trusted
Server
3
Parsing of the JWK4
Verifying the JWT signature using the malicious JWK5
Fetching of the JWK based on the “jku” header
3a Redirect to malicious server
3b Fetching of the malicious JWK
after following the redirect
HTTP Request with malicious JWT1
Malicious
Server
Attacker
jku and Open Redirect
PentesterLab.com / @PentesterLab
3 Fetching of the JWK based on the “jku” header
Parsing of the JWT to extract the “jku” header2
Application
Trusted
Server
HTTP Request with malicious JWT1
Attacker
jku and Header Injection
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Header Injection
Trusted
Server
3 Fetching of the JWK based on the “jku” header
HTTP Request with malicious JWT1
Header Injection
Attacker
Parsing of the JWT to extract the “jku” header2
Application
Header Injection
Trusted
Server
3 Fetching of the JWK based on the “jku” header
HTTP Request with malicious JWT1
Header Injection
Attacker
jku and Header Injection
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Header Injection
Trusted
Server
3 Fetching of the JWK based on the “jku” header
3a The jku uses the header injection
to reflect the jwk in a response
HTTP Request with malicious JWT1
Header Injection
Attacker
jku and Header Injection
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Header Injection
Trusted
Server
3
Parsing of the JWK4
Fetching of the JWK based on the “jku” header
3a The jku uses the header injection
to reflect the jwk in a response
HTTP Request with malicious JWT1
Header Injection
Attacker
jku and Header Injection
PentesterLab.com / @PentesterLab
Parsing of the JWT to extract the “jku” header2
Application
Header Injection
Trusted
Server
3
Parsing of the JWK4
Verifying the JWT signature using the
JWK from the header injection
5
Fetching of the JWK based on the “jku” header
3a The jku uses the header injection
to reflect the jwk in a response
HTTP Request with malicious JWT1
Header Injection
Attacker
jku and Header Injection
PentesterLab.com / @PentesterLab
Libraries: jku header injection - Exploitation
PentesterLab.com / @PentesterLab
Exploitation:
• Find a Header Injection
• Use the Header Injection to return
your JWK
• Add the Header Injection as jku
• Sign the token with your RSA key
jku and x5u: downgrade
PentesterLab.com / @PentesterLab
• The RFC calls out enforcing TLS to avoid MITM
• Few implementations get it wrong:
Enforcing when you set the value
vs
Enforcing when you fetch the key
Conclusion
Recommendations
PentesterLab.com / @PentesterLab
✓ Use strong keys and secrets
✓ Don’t store them in your source code
✓ Make sure you have key rotation built-in
Recommendations
PentesterLab.com / @PentesterLab
✓ Review the libraries you pick (KISS library)
✓ Make sure you check the signature
✓ Make sure your tokens expire
✓ Enforce the algorithm
Recommendations
PentesterLab.com / @PentesterLab
✓ Test for x5u and jku
✓ Don't burn Open Redirect
✓ Read RFC
✓ Hack all the things!
Conclusion
PentesterLab.com / @PentesterLab
• JWT are complex and kind of insecure by design
(make sure you check https://github.com/paragonie/paseto)
• JWT libraries introduce very interesting bugs
• Make sure you test for those if you write code,
pentest or do bug bounties
Any questions?
FOR YOUR TIME !
THANKS
louis@pentesterlab.com / PentesterLab.com / @PentesterLab

More Related Content

PDF
OpenID Connect Explained
PPTX
User Management Life Cycle with Keycloak
PPTX
Ethereum (Blockchain Network)
PDF
Programming smart contracts in solidity
PPTX
Write smart contract with solidity on Ethereum
PDF
Secure Spring Boot Microservices with Keycloak
PPTX
OpenId Connect Protocol
PPT
Understanding OpenID
OpenID Connect Explained
User Management Life Cycle with Keycloak
Ethereum (Blockchain Network)
Programming smart contracts in solidity
Write smart contract with solidity on Ethereum
Secure Spring Boot Microservices with Keycloak
OpenId Connect Protocol
Understanding OpenID

What's hot (20)

PPTX
Ethereum Blockchain with Smart contract and ERC20
PDF
JSON Web Token
PDF
Jwt == insecurity?
PPTX
API Security : Patterns and Practices
PDF
Json web token
PPTX
Smart Contract & Ethereum
PPTX
Solidity
PPTX
Building secure applications with keycloak
PDF
Introduction to Self Sovereign Identity
PPTX
OpenID Connect: An Overview
PDF
Http methods
PPTX
Understanding JWT Exploitation
PDF
MobiliteaTime #7 : Blockchain
PPTX
Pentesting jwt
PPTX
Ethereum 2.0
PDF
Redecentralizing the Web: IPFS and Filecoin
PPTX
Hyperledger
PPTX
Bitcoin, Ethereum, Smart Contract & Blockchain
PPTX
Secure your app with keycloak
Ethereum Blockchain with Smart contract and ERC20
JSON Web Token
Jwt == insecurity?
API Security : Patterns and Practices
Json web token
Smart Contract & Ethereum
Solidity
Building secure applications with keycloak
Introduction to Self Sovereign Identity
OpenID Connect: An Overview
Http methods
Understanding JWT Exploitation
MobiliteaTime #7 : Blockchain
Pentesting jwt
Ethereum 2.0
Redecentralizing the Web: IPFS and Filecoin
Hyperledger
Bitcoin, Ethereum, Smart Contract & Blockchain
Secure your app with keycloak
Ad

Similar to JWT: jku x5u (20)

PPTX
Building Secure User Interfaces With JWTs
PDF
[OPD 2019] Attacking JWT tokens
PDF
Securing TodoMVC Using the Web Cryptography API
PDF
2018 JavaLand Deconstructing and Evolving REST Security
PDF
Securing Your Containerized Applications with NGINX
PPTX
OpenStack Toronto Meetup - Keystone 101
PDF
Как разработать DBFW с нуля
PDF
JavaEE & GlassFish UG - Digital JavaEE 7 New & Noteworthy by P.Pilgrim
PDF
Java EE & Glass Fish User Group: Digital JavaEE 7 - New and Noteworthy
PDF
STATE OF THE ART AUTHENTICATION MIT JAVA EE 8
PDF
OpenStack Swift的性能调优
PPTX
How to get along with HATEOAS without letting the bad guys steal your lunch?
PDF
REST API Pentester's perspective
PDF
Con fess 2013-sse-websockets-json-bhakti
PPTX
Top Ten Java Defense for Web Applications v2
PPS
Hacking Client Side Insecurities
PDF
Stateless Microservice Security via JWT and MicroProfile - ES
PDF
Stateless Microservice Security via JWT and MicroProfile - Guatemala
PDF
Stateless Microservice Security via JWT and MicroProfile - Mexico
PPTX
Invoke-CradleCrafter: Moar PowerShell obFUsk8tion & Detection (@('Tech','niqu...
Building Secure User Interfaces With JWTs
[OPD 2019] Attacking JWT tokens
Securing TodoMVC Using the Web Cryptography API
2018 JavaLand Deconstructing and Evolving REST Security
Securing Your Containerized Applications with NGINX
OpenStack Toronto Meetup - Keystone 101
Как разработать DBFW с нуля
JavaEE & GlassFish UG - Digital JavaEE 7 New & Noteworthy by P.Pilgrim
Java EE & Glass Fish User Group: Digital JavaEE 7 - New and Noteworthy
STATE OF THE ART AUTHENTICATION MIT JAVA EE 8
OpenStack Swift的性能调优
How to get along with HATEOAS without letting the bad guys steal your lunch?
REST API Pentester's perspective
Con fess 2013-sse-websockets-json-bhakti
Top Ten Java Defense for Web Applications v2
Hacking Client Side Insecurities
Stateless Microservice Security via JWT and MicroProfile - ES
Stateless Microservice Security via JWT and MicroProfile - Guatemala
Stateless Microservice Security via JWT and MicroProfile - Mexico
Invoke-CradleCrafter: Moar PowerShell obFUsk8tion & Detection (@('Tech','niqu...
Ad

More from snyff (10)

PDF
Code that gets you pwn(s|'d)
PDF
Entomology 101
PDF
Entrepreneurship for hackers
PDF
Finding Needles in Haystacks
PDF
Defcon CTF quals
PDF
Ruxmon cve 2012-2661
PDF
Ln monitoring repositories
PDF
Owasp tds
PDF
Ruxmon feb 2013 what happened to rails
PDF
Harder Faster Stronger
Code that gets you pwn(s|'d)
Entomology 101
Entrepreneurship for hackers
Finding Needles in Haystacks
Defcon CTF quals
Ruxmon cve 2012-2661
Ln monitoring repositories
Owasp tds
Ruxmon feb 2013 what happened to rails
Harder Faster Stronger

Recently uploaded (20)

PPT
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
PPTX
Mathew Digital SEO Checklist Guidlines 2025
PPTX
AI_Cyberattack_Solutions AI AI AI AI .pptx
PPT
Ethics in Information System - Management Information System
PDF
Top 8 Trusted Sources to Buy Verified Cash App Accounts.pdf
PDF
Alethe Consulting Corporate Profile and Solution Aproach
PPTX
artificialintelligenceai1-copy-210604123353.pptx
PDF
Smart Home Technology for Health Monitoring (www.kiu.ac.ug)
PDF
Lean-Manufacturing-Tools-Techniques-and-How-To-Use-Them.pdf
PDF
Session 1 (Week 1)fghjmgfdsfgthyjkhfdsadfghjkhgfdsa
PPTX
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
PPTX
t_and_OpenAI_Combined_two_pressentations
PPTX
KSS ON CYBERSECURITY INCIDENT RESPONSE AND PLANNING MANAGEMENT.pptx
PPTX
Top Website Bugs That Hurt User Experience – And How Expert Web Design Fixes
PDF
si manuel quezon at mga nagawa sa bansang pilipinas
PPTX
IPCNA VIRTUAL CLASSES INTERMEDIATE 6 PROJECT.pptx
PPTX
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
PDF
Containerization lab dddddddddddddddmanual.pdf
PDF
Uptota Investor Deck - Where Africa Meets Blockchain
PDF
The Evolution of Traditional to New Media .pdf
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
Mathew Digital SEO Checklist Guidlines 2025
AI_Cyberattack_Solutions AI AI AI AI .pptx
Ethics in Information System - Management Information System
Top 8 Trusted Sources to Buy Verified Cash App Accounts.pdf
Alethe Consulting Corporate Profile and Solution Aproach
artificialintelligenceai1-copy-210604123353.pptx
Smart Home Technology for Health Monitoring (www.kiu.ac.ug)
Lean-Manufacturing-Tools-Techniques-and-How-To-Use-Them.pdf
Session 1 (Week 1)fghjmgfdsfgthyjkhfdsadfghjkhgfdsa
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
t_and_OpenAI_Combined_two_pressentations
KSS ON CYBERSECURITY INCIDENT RESPONSE AND PLANNING MANAGEMENT.pptx
Top Website Bugs That Hurt User Experience – And How Expert Web Design Fixes
si manuel quezon at mga nagawa sa bansang pilipinas
IPCNA VIRTUAL CLASSES INTERMEDIATE 6 PROJECT.pptx
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
Containerization lab dddddddddddddddmanual.pdf
Uptota Investor Deck - Where Africa Meets Blockchain
The Evolution of Traditional to New Media .pdf

JWT: jku x5u

  • 1. JWT jku&x5u = ❤ Attacking JSON WEB TOKENS… Louis Nyffenegger @PentesterLab louis@pentesterlab.com
  • 2. About me PentesterLab.com / @PentesterLab Security Engineer PentesterLab: Pentester/Code Reviewer/Security consultant/Security architect Platform to learn web security/penetration testing 100% Hands-on Available for individuals (free and PRO) and enterprises Run a website to help people learn security
  • 3. Who uses JWT? PentesterLab.com / @PentesterLab • A lot of people for OAuth2 • A lot of people for sessions • A lot of people to manage trust • A lot of people for password reset • A lot of people who care about being stateless and multi-datacenter architecture
  • 5. Signature vs Encryption PentesterLab.com / @PentesterLab Encryption gives you confidentiality Signature gives you integrity
  • 6. Multiple ways of signing PentesterLab.com / @PentesterLab • With a secret using HMAC • With a private key using RSA/EC/… (asymmetric)
  • 7. Signing with a secret PentesterLab.com / @PentesterLab Sign! Verify! Secret
  • 8. Signing: asymmetric PentesterLab.com / @PentesterLab Sign! PrivatePublic Verify!
  • 10. JavaScript Object Notation (JSON) PentesterLab.com / @PentesterLab Human readable format to store or transmit objects
  • 11. The Compact JWS Format PentesterLab.com / @PentesterLab Header Payload Signature 3 parts in a JSON Web Token:
  • 12. The Compact JWS Format PentesterLab.com / @PentesterLab Header Payload Signature Separated by a dot . .
  • 13. The Compact JWS Format PentesterLab.com / @PentesterLab eyJ0eXAiOiJK V1QiLCJhbGci OiJIUzI1NiJ9 eyJsb2dpbi I6ImFkb WluIn0 FSfvCBAwypJ4abF6 jFLmR7JgZhkW674 Z8dIdAIRyt1E Separated by a dot . . eyJ = Base64('{"')
  • 14. The Compact JWS Format PentesterLab.com / @PentesterLab Base64({…}) Base64({…}) Base64(…) Header and Payload are base64* encoded JSON . . * urlsafe base64 encoding without padding The signature is also base64 encoded
  • 15. The Compact JWS Format: Encoding PentesterLab.com / @PentesterLab Urlsafe base64 encoding without padding: *https://tools.ietf.org/html/rfc7515#appendix-C
  • 16. The JWT Format: header PentesterLab.com / @PentesterLab Base64({"alg": "HS256", "typ": "JWS"}) The header contains an algorithm “alg” attribute: In this example HMAC with SHA256 was used To tell how the token was signed. … . . …
  • 17. The JWT Format: Algorithms PentesterLab.com / @PentesterLab A lot of different algorithms are supported*: None * https://jwt.io/ covers most HS256 HS384 HS512 RS256 RS384 RS512 ES256 ES384 ES512 PS256 PS384 PS512
  • 18. The JWT Format: payload PentesterLab.com / @PentesterLab … The payload may contain literally anything: Base64({"user":"admin", "roles": ["adm","users"]}). . …
  • 19. The JWT Format: payload PentesterLab.com / @PentesterLab The payload may contain registered claims: Base64({"user":"admin", "exp":12…, "iat":1234.. }). .… …
  • 20. The JWT Format: creating a token PentesterLab.com / @PentesterLab • Create the JSON header and base64 encode it • Create the JSON payload and base64 encode it • Concatenate with a dot the (encoded) header and payload • Sign the result (header+.+payload) • Base64 encode the signature • Append a dot then the signature
  • 21. The JWT Format: verifying a token PentesterLab.com / @PentesterLab • Split the token in three parts based on the dots • Base64 decode each part • Parse the JSON for the header and payload • Retrieve the algorithm from the header • Verify the signature based on the algorithm • Verify the claims
  • 22. Classic JWT attacks PentesterLab.com / @PentesterLab • None algorithm • Trivial secret • Algorithm confusion • Injection in the kid parameter • CVE-2018-0114 •
  • 24. jku and x5u PentesterLab.com / @PentesterLab • If you read some of the JWS RFC, you probably learnt about jku and x5u parameter for the headers • People are starting to use jku (JWK URL)
  • 25. The JWT Format: jku&x5u PentesterLab.com / @PentesterLab Base64({"jku": "https://...", ...}) … . . … Base64({"x5u": "https://...", ...}) … . . …
  • 26. The JWT Format: jwk PentesterLab.com / @PentesterLab { "keys": [ { "kty": "RSA", "use": "sig", "kid": "pentesterlab", "n": "oTtAXRgdJ6Pu0jr3hK3opCF5uqKWKbm4Kkq...vTF0FGw", "e": "AQAB", "alg": "RS256" } ] }
  • 27. The JWT Format: x5c PentesterLab.com / @PentesterLab { "keys": [ { "kty": "RSA", "use": "sig", "kid": "pentesterlab", "x5c": "MIIDWDCCAkACCQCnE....fpye27SQbC2fBxebsek=", "alg": "RS256" } ] }
  • 28. jku and x5u PentesterLab.com / @PentesterLab Application Trusted Server User
  • 29. Application Trusted Server User HTTP Request with JWT1 jku and x5u PentesterLab.com / @PentesterLab
  • 30. Application Trusted Server User HTTP Request with JWT Parsing of the JWT to extract the “jku” header1 2 jku and x5u PentesterLab.com / @PentesterLab
  • 31. Application Trusted Server User HTTP Request with JWT Parsing of the JWT to extract the “jku” header1 2 3 Fetching of the JWK based on the “jku” header jku and x5u PentesterLab.com / @PentesterLab
  • 32. Fetching of the JWK based on the “jku” header Application Trusted Server User HTTP Request with JWT Parsing of the JWT to extract the “jku” header1 2 3 Parsing of the JWK4 jku and x5u PentesterLab.com / @PentesterLab
  • 33. Application Trusted Server User HTTP Request with JWT Parsing of the JWT to extract the “jku” header1 2 3 Parsing of the JWK4 Verifying the JWT signature using the JWK5 Fetching of the JWK based on the “jku” header jku and x5u PentesterLab.com / @PentesterLab
  • 34. Application Trusted Server User HTTP Request with JWT Parsing of the JWT to extract the “jku” header Response 1 6 2 3 Parsing of the JWK4 Verifying the JWT signature using the JWK5 Fetching of the JWK based on the “jku” header jku and x5u PentesterLab.com / @PentesterLab
  • 35. jku and x5u PentesterLab.com / @PentesterLab Application Malicious Server HTTP Request with malicious JWT Parsing of the JWT to extract the “jku” header Response 1 6 2 3 Parsing of the JWK4 Verifying the JWT signature using the JWK5 Attacker Fetching of the malicious JWK based on the “jku” header
  • 36. jku and x5u PentesterLab.com / @PentesterLab Application Malicious Server HTTP Request with malicious JWT Parsing of the JWT to extract the “jku” header1 2 3 Attacker Fetching of the malicious JWK based on the “jku” header
  • 37. jku and x5u PentesterLab.com / @PentesterLab Turns out filtering URLs is incredibly hard
  • 38. jku and x5u : regular expression PentesterLab.com / @PentesterLab https://trusted.example.com => https://trustedzexample.com
  • 39. jku and x5u : starts with PentesterLab.com / @PentesterLab https://trusted => https://trusted@pentesterlab.com https://trusted/jwks/ => https://trusted/jwks/../file_uploaded https://trusted/jwks/ => https://trusted/jwks/../open_redirect https://trusted/jwks/ => https://trusted/jwks/../header_injection
  • 40. 3 Fetching of the JWK based on the “jku” header Parsing of the JWT to extract the “jku” header2 Application Trusted Server HTTP Request with malicious JWT1 Malicious Server Attacker jku and Open Redirect PentesterLab.com / @PentesterLab
  • 41. Parsing of the JWT to extract the “jku” header2 Application Open Redirect Trusted Server HTTP Request with malicious JWT1 Malicious Server 3 Fetching of the JWK based on the “jku” header Attacker jku and Open Redirect PentesterLab.com / @PentesterLab
  • 42. Parsing of the JWT to extract the “jku” header2 Application Open Redirect Trusted Server 3 Fetching of the JWK based on the “jku” header 3a Redirect to malicious server HTTP Request with malicious JWT1 Malicious Server Attacker jku and Open Redirect PentesterLab.com / @PentesterLab
  • 43. Parsing of the JWT to extract the “jku” header2 Application Open Redirect Trusted Server 3 Fetching of the JWK based on the “jku” header 3a Redirect to malicious server 3b Fetching of the malicious JWK after following the redirect HTTP Request with malicious JWT1 Malicious Server Attacker jku and Open Redirect PentesterLab.com / @PentesterLab
  • 44. Parsing of the JWT to extract the “jku” header2 Application Open Redirect Trusted Server 3 Parsing of the JWK4 Fetching of the JWK based on the “jku” header 3a Redirect to malicious server 3b Fetching of the malicious JWK after following the redirect HTTP Request with malicious JWT1 Malicious Server Attacker jku and Open Redirect PentesterLab.com / @PentesterLab
  • 45. Parsing of the JWT to extract the “jku” header2 Application Open Redirect Trusted Server 3 Parsing of the JWK4 Verifying the JWT signature using the malicious JWK5 Fetching of the JWK based on the “jku” header 3a Redirect to malicious server 3b Fetching of the malicious JWK after following the redirect HTTP Request with malicious JWT1 Malicious Server Attacker jku and Open Redirect PentesterLab.com / @PentesterLab
  • 46. 3 Fetching of the JWK based on the “jku” header Parsing of the JWT to extract the “jku” header2 Application Trusted Server HTTP Request with malicious JWT1 Attacker jku and Header Injection PentesterLab.com / @PentesterLab
  • 47. Parsing of the JWT to extract the “jku” header2 Application Header Injection Trusted Server 3 Fetching of the JWK based on the “jku” header HTTP Request with malicious JWT1 Header Injection Attacker Parsing of the JWT to extract the “jku” header2 Application Header Injection Trusted Server 3 Fetching of the JWK based on the “jku” header HTTP Request with malicious JWT1 Header Injection Attacker jku and Header Injection PentesterLab.com / @PentesterLab
  • 48. Parsing of the JWT to extract the “jku” header2 Application Header Injection Trusted Server 3 Fetching of the JWK based on the “jku” header 3a The jku uses the header injection to reflect the jwk in a response HTTP Request with malicious JWT1 Header Injection Attacker jku and Header Injection PentesterLab.com / @PentesterLab
  • 49. Parsing of the JWT to extract the “jku” header2 Application Header Injection Trusted Server 3 Parsing of the JWK4 Fetching of the JWK based on the “jku” header 3a The jku uses the header injection to reflect the jwk in a response HTTP Request with malicious JWT1 Header Injection Attacker jku and Header Injection PentesterLab.com / @PentesterLab
  • 50. Parsing of the JWT to extract the “jku” header2 Application Header Injection Trusted Server 3 Parsing of the JWK4 Verifying the JWT signature using the JWK from the header injection 5 Fetching of the JWK based on the “jku” header 3a The jku uses the header injection to reflect the jwk in a response HTTP Request with malicious JWT1 Header Injection Attacker jku and Header Injection PentesterLab.com / @PentesterLab
  • 51. Libraries: jku header injection - Exploitation PentesterLab.com / @PentesterLab Exploitation: • Find a Header Injection • Use the Header Injection to return your JWK • Add the Header Injection as jku • Sign the token with your RSA key
  • 52. jku and x5u: downgrade PentesterLab.com / @PentesterLab • The RFC calls out enforcing TLS to avoid MITM • Few implementations get it wrong: Enforcing when you set the value vs Enforcing when you fetch the key
  • 54. Recommendations PentesterLab.com / @PentesterLab ✓ Use strong keys and secrets ✓ Don’t store them in your source code ✓ Make sure you have key rotation built-in
  • 55. Recommendations PentesterLab.com / @PentesterLab ✓ Review the libraries you pick (KISS library) ✓ Make sure you check the signature ✓ Make sure your tokens expire ✓ Enforce the algorithm
  • 56. Recommendations PentesterLab.com / @PentesterLab ✓ Test for x5u and jku ✓ Don't burn Open Redirect ✓ Read RFC ✓ Hack all the things!
  • 57. Conclusion PentesterLab.com / @PentesterLab • JWT are complex and kind of insecure by design (make sure you check https://github.com/paragonie/paseto) • JWT libraries introduce very interesting bugs • Make sure you test for those if you write code, pentest or do bug bounties
  • 58. Any questions? FOR YOUR TIME ! THANKS louis@pentesterlab.com / PentesterLab.com / @PentesterLab