Trim Kadriu Trim Kadriu - 4 years ago 141
PHP Question

Predict PHP rand() by having many outputs of it

I was wondering if it possible to predict the next pseudo random number that will be generated out of PHP rand() function, by using the previous outputs of it.

An example would be that I have n outputs of rand(1,10) like 8, 5, 10, 3, 3, 2 ... 9, 8 ... and so on.

So the question is, is there any way that I can use these already generated numbers to predict the next one?


Since the question was marked as too broad, I will try to be more specific. Can I predict the next RGN or recover the seed, on PHP running on Linux, when using the rand() function, by only observing some outputs given from it.

A concrete example is the following output:





Generated by the following code:

echo rand(1, 68703);

Answer Source

Not sure what is under the hood, but it is likely covered by one of these links:

If it is not covered, then provide me the source code and I will break it.

EDIT: As requested in comments below, I update my answer.

One trivial way to break it is brute force search of the srand seed. Normally this is a 32-bit int (which will take a few minutes to brute force), but on some platforms it may be 64-bit (takes too long to break via brute force). So you just loop through each candidate seed, do srand( candidate seed) and then generate your rand(1, 68703) to see if it matches the outputs. If so, you presumably have the right seed and can generate future outputs. Otherwise continue loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download