php密码格式验证函数,[置顶] 基于长密码的PHP函数验证漏洞学习(Long Password Denial of Service (DoS))...

本文详细介绍了使用PHP实现的PasswordHash类,演示了如何通过增加密码长度来测试其在面对长密码时的性能,揭示了可能存在的时间攻击风险。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

啥都不说,干货直接上源码。

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

//demo 0x001

//require_once 'phpass.php';

set_time_limit(0);

ignore_user_abort(true);

class PasswordHash

{

var $itoa64;

var $iteration_count_log2;

var $portable_hashes;

var $random_state;

function PasswordHash($iteration_count_log2, $portable_hashes)

{

$this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)

$iteration_count_log2 = 8;

$this->iteration_count_log2 = $iteration_count_log2;

$this->portable_hashes = $portable_hashes;

$this->random_state = microtime();

if (function_exists('getmypid'))

$this->random_state .= getmypid();

}

function get_random_bytes($count)

{

$output = '';

if (is_readable('/dev/urandom') &&

($fh = @fopen('/dev/urandom', 'rb')))

{

$output = fread($fh, $count);

fclose($fh);

}

if (strlen($output) < $count)

{

$output = '';

for ($i = 0; $i < $count; $i += 16)

{

$this->random_state = md5(microtime() . $this->random_state);

$output .=

pack('H*', md5($this->random_state));

}

$output = substr($output, 0, $count);

}

return $output;

}

function encode64($input, $count)

{

$output = '';

$i = 0;

do

{

$value = ord($input[$i++]);

$output .= $this->itoa64[$value & 0x3f];

if ($i < $count)

$value |= ord($input[$i]) << 8;

$output .= $this->itoa64[($value >> 6) & 0x3f];

if ($i++ >= $count)

break;

if ($i < $count)

$value |= ord($input[$i]) << 16;

$output .= $this->itoa64[($value >> 12) & 0x3f];

if ($i++ >= $count)

break;

$output .= $this->itoa64[($value >> 18) & 0x3f];

} while ($i < $count);

return $output;

}

function gensalt_private($input)

{

$output = '$P$';

$output .= $this->itoa64[min($this->iteration_count_log2 +

((PHP_VERSION >= '5') ? 5 : 3), 30)];

$output .= $this->encode64($input, 6);

return $output;

}

function crypt_private($password, $setting)

{

$output = '*0';

if (substr($setting, 0, 2) == $output)

$output = '*1';

$id = substr($setting, 0, 3);

# We use "$P$", phpBB3 uses "$H$" for the same thing

if ($id != '$P$' && $id != '$H$')

return $output;

$count_log2 = strpos($this->itoa64, $setting[3]);

if ($count_log2 < 7 || $count_log2 > 30)

return $output;

$count = 1 << $count_log2;

$salt = substr($setting, 4, 8);

if (strlen($salt) != 8)

return $output;

# We're kind of forced to use MD5 here since it's the only

# cryptographic primitive available in all versions of PHP

# currently in use. To implement our own low-level crypto

# in PHP would result in much worse performance and

# consequently in lower iteration counts and hashes that are

# quicker to crack (by non-PHP code).

if (PHP_VERSION >= '5')

{

$hash = md5($salt . $password, TRUE);

do

{

$hash = md5($hash . $password, TRUE);

} while (--$count);

} else

{

$hash = pack('H*', md5($salt . $password));

do

{

$hash = pack('H*', md5($hash . $password));

} while (--$count);

}

$output = substr($setting, 0, 12);

$output .= $this->encode64($hash, 16);

return $output;

}

function gensalt_extended($input)

{

$count_log2 = min($this->iteration_count_log2 + 8, 24);

# This should be odd to not reveal weak DES keys, and the

# maximum valid value is (2**24 - 1) which is odd anyway.

$count = (1 << $count_log2) - 1;

$output = '_';

$output .= $this->itoa64[$count & 0x3f];

$output .= $this->itoa64[($count >> 6) & 0x3f];

$output .= $this->itoa64[($count >> 12) & 0x3f];

$output .= $this->itoa64[($count >> 18) & 0x3f];

$output .= $this->encode64($input, 3);

return $output;

}

function gensalt_blowfish($input)

{

# This one needs to use a different order of characters and a

# different encoding scheme from the one in encode64() above.

# We care because the last character in our encoded string will

# only represent 2 bits. While two known implementations of

# bcrypt will happily accept and correct a salt string which

# has the 4 unused bits set to non-zero, we do not want to take

# chances and we also do not want to waste an additional byte

# of entropy.

$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

$output = '$2a$';

$output .= chr(ord('0') + $this->iteration_count_log2 / 10);

$output .= chr(ord('0') + $this->iteration_count_log2 % 10);

$output .= '$';

$i = 0;

do

{

$c1 = ord($input[$i++]);

$output .= $itoa64[$c1 >> 2];

$c1 = ($c1 & 0x03) << 4;

if ($i >= 16)

{

$output .= $itoa64[$c1];

break;

}

$c2 = ord($input[$i++]);

$c1 |= $c2 >> 4;

$output .= $itoa64[$c1];

$c1 = ($c2 & 0x0f) << 2;

$c2 = ord($input[$i++]);

$c1 |= $c2 >> 6;

$output .= $itoa64[$c1];

$output .= $itoa64[$c2 & 0x3f];

} while (1);

return $output;

}

function HashPassword($password)

{

$random = '';

if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes)

{

$random = $this->get_random_bytes(16);

$hash = crypt($password, $this->gensalt_blowfish($random));

if (strlen($hash) == 60)

return $hash;

}

if (CRYPT_EXT_DES == 1 && !$this->portable_hashes)

{

if (strlen($random) < 3)

$random = $this->get_random_bytes(3);

$hash = crypt($password, $this->gensalt_extended($random));

if (strlen($hash) == 20)

return $hash;

}

if (strlen($random) < 6)

$random = $this->get_random_bytes(6);

$hash = $this->crypt_private($password, $this->gensalt_private($random));

if (strlen($hash) == 34)

return $hash;

# Returning '*' on error is safe here, but would _not_ be safe

# in a crypt(3)-like function used _both_ for generating new

# hashes and for validating passwords against existing hashes.

return '*';

}

function CheckPassword($password, $stored_hash)

{

$hash = $this->crypt_private($password, $stored_hash);

if ($hash[0] == '*')

$hash = crypt($password, $stored_hash);

return $hash == $stored_hash;

}

}

//Long Password Denial of Service (DoS)

//denial-of-service-responsible-disclosure

class hasher

{

function __construct($pass)

{

$pass_hash = new PasswordHash(8, TRUE);

$hashed_pass = $pass_hash->HashPassword($pass);

}

}

$length = array("1", "10", "50", "100", "500", "1000", "5000", "10000", "50000", "100000", "200000",

"300000", "400000", "500000", "600000", "700000", "800000", "900000", "1000000",

"2000000", "3000000","4000000", "5000000", "6000000", "7000000", "8000000", "9000000");

foreach ($length as $value)

{

$valor = str_repeat("A", $value);

$time_start = microtime(true);

$hashgen = new hasher($valor);//in this case the PHP script not control the input length:

echo 'Size in character(s): ' . $value;

echo ' Total execution time in seconds: ' . (microtime(true) - $time_start);

print "\n";

}

//关键区域注意hasher定义类这块代码

$hashed_pass = $pass_hash->HashPassword($pass);//注意此处函数,由于没有限制密码长度,导致系统运行耗时。

08e69c50dcc325406e7bb455e6b155bc.png

输出结果测试:

"php.exe" "TimingAttack.php"

Size in character(s): 1 Total execution time in seconds: 0.015599966049194

Size in character(s): 10 Total execution time in seconds: 0

Size in character(s): 50 Total execution time in seconds: 0.015599966049194

Size in character(s): 100 Total execution time in seconds: 0

Size in character(s): 500 Total execution time in seconds: 0.015599966049194

Size in character(s): 1000 Total execution time in seconds: 0.031200170516968

Size in character(s): 5000 Total execution time in seconds: 0.12480092048645

Size in character(s): 10000 Total execution time in seconds: 0.23399996757507

Size in character(s): 50000 Total execution time in seconds: 1.1700019836426

Size in character(s): 100000 Total execution time in seconds: 2.2776041030884

Size in character(s): 200000 Total execution time in seconds: 4.1808068752289

Size in character(s): 300000 Total execution time in seconds: 9.656417131424

Size in character(s): 400000 Total execution time in seconds: 14.149224996567

Size in character(s): 500000 Total execution time in seconds: 17.425230979919

Size in character(s): 600000 Total execution time in seconds: 19.094433069229

Size in character(s): 700000 Total execution time in seconds: 23.010040998459

Size in character(s): 800000 Total execution time in seconds: 24.070841789246

Size in character(s): 900000 Total execution time in seconds: 28.298449993134

Size in character(s): 1000000 Total execution time in seconds: 33.259258031845

Size in character(s): 2000000 Total execution time in seconds: 67.298518180847

Size in character(s): 3000000 Total execution time in seconds: 101.29097795486

Size in character(s): 4000000 Total execution time in seconds: 113.45899987221

Size in character(s): 5000000 Total execution time in seconds: 135.59543800354

Size in character(s): 6000000 Total execution time in seconds: 154.45587110519

Size in character(s): 7000000 Total execution time in seconds: 203.9391579628

Size in character(s): 8000000 Total execution time in seconds: 234.54641199112

Size in character(s): 9000000 Total execution time in seconds: 265.59046697617

Done.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值