JWT: RS256 -> HS256 confusion
RS256 — asymmetric signature: the server signs the token with a private RSA key, and the client verifies it with a public key. There is a flaw in some libraries: if an attacker changes alg in the header from RS256 to HS256, the server verifies the token with HMAC-SHA256 and uses the publicly known public key as the HMAC key. As a result, the attacker can create a valid signature using the public key — this is an algorithm confusion attack.
The attached pubkey.pem — is the server's public key. token.txt contains the original RS256 token and the encrypted flag. Simulated server rule:
javob = flag XOR SHA-256(qabul_qilingan_token)
The SHA-256 key is 32 bytes, and the flag is longer than that — the key is used repeatedly (cyclically): ct[i] ^ kalit[i % 32].
Construct the following forged HS256 token (compact JSON, base64url, without padding):
- header JSON:
{"alg":"HS256","typ":"JWT"} - payload JSON:
{"login":"talaba","admin":1} - signing_input =
base64url(header) + "." + base64url(payload) - signature =
base64url(HMAC_SHA256(kalit=pubkey.pem baytlari, signing_input)) - token =
signing_input + "." + imzo
Important: read the pubkey.pem file exactly as bytes to use as the HMAC key (open('pubkey.pem','rb').read()) — formatting must be fully preserved.