Last active
June 12, 2018 23:00
-
-
Save Alanaktion/08bdd57c61cd9decfcda526f363579b3 to your computer and use it in GitHub Desktop.
Silly timing attack POC in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$iterations = 1e4; | |
$valid = 'password'; | |
$stored_hash = md5($valid); | |
$test = $arvg[1] ?? 'notvalid'; | |
echo "$iterations iterations\n"; | |
// Run tests | |
$v_start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
if ($stored_hash == md5($valid)) { | |
// Do nothing... | |
} | |
} | |
$v_time = microtime(true) - $v_start; | |
echo "{$v_time} seconds for idential hashes\n"; | |
$t_start = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
if ($stored_hash == md5($test)) { | |
// Do nothing... | |
} | |
} | |
$t_time = microtime(true) - $t_start; | |
echo "{$t_time} seconds for different hashes\n"; | |
// Analyze results | |
if (abs($v_time - $t_time) < .001) { | |
echo "Timing is close, test could match stored hash.\n"; | |
} elseif ($v_time > $t_time) { | |
echo "Correct hash took less time than test hash, so test is not valid.\n"; | |
} else { | |
echo "Stored hash took *longer* somehow. This happens sometimes.\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment