I know that some of PHP's random functions are insecure due to them not being completely random and are considered a bad practice. My question is how an attacker go about finding that the developer used an insecure function to create a token, such as a forgotten password token or CSRF token?

2 Answers 2


You can't actually determine how random a number is, because it's the nature of randomness. However, if you had a series of values, you could perform statistical randomness tests on the values and possibly find patterns/weaknesses in the random number generator. Certain patterns may give away what random generator was used. But even supposing the numbers were random in a statistical sense, the problem that remains afterwards is security. Something that passes randomness tests may not actually be secure!

In short:

  • true randomness = security
  • statistical randomness ≠ true randomness
  • Thank you. I appreciate the reply. If something is statistically random, are there any easy ways to detect it? Or would it simply be analyzing them on your own and looking for patterns? Commented Apr 18, 2014 at 14:20
  • 1
    A naïve way to detect randomness is to create 256 counters corresponding to the 256 possible values of a byte and then count the bytes (the more, the better). At the end of the count, you should have similar values for all of the counters with a little bit of deviation from total_bytes_counted / 256. en.wikipedia.org/wiki/Diehard_tests has more interesting ones, some which are easy to make a quick and dirty implementation of in PHP for testing.
    – Kevin Li
    Commented Apr 18, 2014 at 17:47
  • Thank you. I have another question and I don't know if I should bring it to chat or not (but I didn't see a chat dedicated to security), but what would be a good proof of concept for a company to prove that their tokens have a low degree of randomness? Being able to actually predict the outcome would be nice, but I don't see a way, or an easy to way to do that. Commented Apr 19, 2014 at 3:42
  • 1
    I had a bit of fun slacking at work today. I made a dice with paper, scotch tape, and a sharpie, rolled it 100 times, and then compared it with the numbers that random.org/dice gave me. Random.org's numbers are really random, so it's something good to test against. I put the data in here: php-developer.org/ttest.php. Generate some values and play around with it. There are a lot more tests you can do in Microsoft Excel or LibreOffice Calc: depts.alverno.edu/nsmt/stats.htm Cheers!
    – Kevin Li
    Commented Apr 19, 2014 at 3:57
  • stats.stackexchange.com/questions/30/… seems to approve of the Die Hard tests for you very scenario as well.
    – Kevin Li
    Commented Apr 19, 2014 at 4:30

Further to Kevin Li's answer, testing your session IDs for randomness is covered here: How to test session identifier strength with OWASP WebScarab

You need to request loads of session tokens and perform a statistical analysis on them to determine if they appear to exhibit the desired level of randomness. 10^4 session IDs would be a good minimal starting point IMHO but it would be best to analyse more if you can.

There is also an automated tool in PortSwigger's Burp proxy to do the same thing which I use but burp is quite expensive.

(would comment on the other answer but I don't have the rep)

  • Appreciate it. I actually have Burp and will try that Commented Apr 18, 2014 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .