Read the full PHPWN paper here (presented at Black Hat & DEF CON 2010)
phpwn: Attack on PHP sessions and random numbers
Studying PHP's (5.3.1 and below) LCG (linear congruential generator, a pseudorandom number generator), I discovered that there are weaknesses that reduce the complexity of determining the sequence of pseudorandom numbers. What this means is that PHP is severely deficient in producing random session IDs or random numbers, leading to the possibility of stealing sessions or other sensitive information.
The initial seed can be reduced from 64-bits to 35-bits, and with PHP code execution, can be reduced further down to just under 20-bits, which takes only seconds to recreate the initial seed. You can test with sources available below.
Mad hax0r pr0pz to Arshan "DHS-most-wanted" Dabirsiaghi (bless you) and Amit "smartypants" Klein for pointing me in the right direction with the LCG. Other tools to work out the LCG in forward and reverse, as well as determine session IDs, found below.
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/samy/phpwn/index.php:21) in /var/www/samy/phpwn/index.php on line 25
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /var/www/samy/phpwn/index.php:21) in /var/www/samy/phpwn/index.php on line 25
Hi 188.8.131.52! The time is 1492974475
To test breaking the seed, run the following (after compiling s1s2.c)
time ./s1s2 2424 0.060412997163896
Can you guess my next lcg_value based off the above? (hint: it's 0.94310893360653).
Test by running:
time ./lcg-state-forward [s1] [s2] 100
Your session_id is e4672b594c5a115ba5bfb543c6f72c98 (or just look at your cookie)
Source for this page:
session_start(); echo "Hi $_SERVER[REMOTE_ADDR]! The time is " . time() . "<p>"; echo "To test breaking the seed, run the following (after compiling <a href='s1s2.c'>s1s2.c</a>)<br>"; echo "<code>time <a href='s1s2.c'>./s1s2</a> " . getmypid() . " " . lcg_value() . "</code><p>"; echo "Can you guess my next lcg_value based off the above? (hint: it's " . lcg_value() . ").<br>"; echo "Test by running: <code>time <a href='lcg-state-forward.c'>./lcg-state-forward</a> [s1] [s2] 100</code><p>"; echo "Your <a href='http://www.test.com/search?q=" . session_id() . "'>session_id</a> is " . session_id() . " (or just look at your cookie)";