Skip to content

Instantly share code, notes, and snippets.

@Alanaktion
Last active June 12, 2018 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alanaktion/08bdd57c61cd9decfcda526f363579b3 to your computer and use it in GitHub Desktop.
Save Alanaktion/08bdd57c61cd9decfcda526f363579b3 to your computer and use it in GitHub Desktop.
Silly timing attack POC in PHP
<?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