Skip to content
Crypto Disclosed report Intermediate

[crypto-js] Insecure entropy source - Math.random()

The crypto-js library's WordArray.random function used Math.random as its only source of randomness, which is not cryptographically secure. An attacker who could observe enough outputs might recover the internal seed and predict future random values.

chalker Follow

Originally disclosed on HackerOne

Medium 4.8 View original
  • Fixed in 8 months
  • Disclosed after 9 months

Module

module name: crypto-js version: 3.1.9-1 npm page: https://www.npmjs.com/package/crypto-js

Module Description

JavaScript library of crypto standards.

Module Stats

Replace stats below with numbers from npm’s module page:

184959 downloads in the last day 912568 downloads in the last week 3945947 downloads in the last month

Vulnerability

Vulnerability Description

Math.random() is not a cryptographically secure source of randomness, but it is used as the sole entropy source in require('crypto-js').lib.WordArray.random, which creates a "random" array of 32-bit words:

Steps To Reproduce:

E.g. to confirm that that is predictable given the same initial seed:

$ node --random_seed=42 -e "console.log(require('crypto-js').lib.WordArray.random(16))"
{ words: [ -1477405629, 964516052, 1254255372, 1089500106 ],
  sigBytes: 16 }
$ node --random_seed=42 -e "console.log(require('crypto-js').lib.WordArray.random(16))"
{ words: [ -1477405629, 964516052, 1254255372, 1089500106 ],
  sigBytes: 16 }

It could in theory be possible to recover the internal XorShift128+ Math.random seed by gathering enough observations.

Even if this method attempts to "mask" Math.random somehow perhaps in order to make extracting the seed harder, that could never be enough. For example, Math.random seed could be also recovered by observations over some other channel, e.g. if something else presents Math.random results to the user (e.g. not crypto-related).

Patch

Use window.crypto.getRandomValues in browser and require('crypto').randomBytes in Node.js instead.

Wrap up

Select Y or N for the following statements:

  • I contacted the maintainer to let them know: N
  • I opened an issue in the related repository: N

Impact

Predict the values of require('crypto-js').lib.WordArray.random, which could be perceived as crypto-secure by users.

The original report as published on HackerOne, by chalker. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

Root cause

The library used Math.random, a non-cryptographic PRNG, as the only entropy source for generating random word arrays.

Impact

An attacker could predict the values produced by WordArray.random, which users might wrongly believe are cryptographically secure.

What the attacker needed

Ability to observe enough random outputs

The fix

The library should use window.crypto.getRandomValues in browsers and require('crypto').randomBytes in Node.js instead of Math.random.

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.

Random numbers used for security, like keys or tokens, must be unpredictable. This library used a normal JavaScript random function that is designed to be fast, not secret, so someone who sees enough outputs might work out the pattern and guess future values.

What is Crypto?

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

More by chalker

View all

36 more disclosed reports, best first.

More from Node.js third-party modules

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.