Firefox WebCrypto Private Key Recovery Attack on DH small subgroups
Firefox's WebCrypto implementation of Diffie-Hellman allows an attacker to supply a malicious prime with a smooth p-1, enabling recovery of the private key via the Lim-Lee attack. An attacker with XSS can exfiltrate the private key object and then recover the full key using the Chinese Remainder Theorem.
Originally disclosed on Mozilla
- Fixed in 18 months
- Paid in 19 months
- Disclosed after 18 months
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0
Build ID: 20180621125625
Steps to reproduce:
Firefox is the only browser that supports DH (not ECDH).
Other browsers did not implement due the fact is not anymore part of the spec.
There is a WebCrypto Private Key Recovery Attack on DH small subgroups.
You can find an instant demo in https://asanso.github.io/firefox/victim.html
Actual results:
One of the security property of the WebCrypto API is that even with an XSS an attacker can surely use the vulnerability to encrypt/decrypt the data but the access to the raw key material that is private, secret, or explicitly typed as non-extractable should not be accessible to Javascript.
This happens to be true for every primitive but not for Diffie Hellman.
Is always possible to create a malicious prime (having p-1 being smooth) and apply the famous Lim-Lee key recovery attack (http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.44.5296) to recover the private key.
For my example in https://asanso.github.io/firefox/victim.html I have used a malicious prime 171470411456254147604591110776164450321 that hase a p-1 = 2^4 * 5 * 23 * 2082757 * 744748579247 * 60079053324863537.
My example calculate privateKey mod 5 but you we can add the other small subgroups and applying the Chinese Remainder Theorem (CRT) to recover the entire private key.
It is enough an XSS to exfiltrate the privateKey object to an attacker controlled domain via postMessage and then the attacker can apply the Lim-Lee attack.
Expected results:
I would recommend to either deprecate the DH API in Firefox (as the other browsers do) or to at least making privateKey.algorithm.generator and privateKey.algorithm.prime not writable
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Impact
An attacker with XSS can recover the private key of a Diffie-Hellman key pair, breaking the security property that private keys are non-extractable.
What the attacker needed
XSS on a page using WebCrypto DH; Firefox browser
The fix
The report recommends deprecating the DH API in Firefox or making privateKey.algorithm.generator and privateKey.algorithm.prime not writable.
How it was found
- Create a malicious prime with a smooth p-1.
- Use the WebCrypto API to generate a Diffie-Hellman key pair with that prime.
- Exfiltrate the private key object to an attacker-controlled domain via postMessage.
- Apply the Lim-Lee attack to recover the private key modulo small factors.
- Use the Chinese Remainder Theorem to recover the entire private key.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
WebCrypto is a browser API for cryptography. Normally, private keys cannot be read by JavaScript. But in Firefox, the Diffie-Hellman key exchange lets an attacker choose a special number (the prime) that makes it possible to work out the private key. An attacker who can run JavaScript on the page (via XSS) can steal the key.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.