axiejundong 2019-07-01
例如:利用图片加载错误,执行脚本;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>xss存储型攻击</title> </head> <body> <div id='test'></div> <script> var text = "<img src='404.html' onerror='alert(1)'>" // 从后台读取的数据 var $test = document.querySelector('#test'); $test.innerHTML = text; </script> </body> </html>
防范: 对html经行编码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>防范xss攻击(html-encode)</title> </head> <body> <div id='test'></div> <script> // 1. 富文本;白名单 // 2. 纯文本;html encode , js encode var htmlEscape = function (str) { // 在展示的时候转义 return String(str) .replace(/&/g,'&') .replace(/"/g,'"') .replace(/'/g,''') .replace(/</g,'<') .replace(/>/g,'>') } var htmlUnescape = function (str){ return String(str) .replace(/&/g,'&') .replace(/"/g,'"') .replace(/'/g,"'") .replace(/</g,'<') .replace(/>/g,'>') } var text = "<img src='404.html' onerror='alert(1)'>" // 从后台读取的数据 var $test = document.querySelector('#test'); $test.innerHTML = htmlEscape(text); </script> </body> </html>
例如:通过地址栏传输数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>xss反射性攻击</title> </head> <body> <div id='test'></div> <script> // <img src='404.html' onerror='alert(1)'> var $test = document.querySelector('#test'); $test.innerHTML = decodeURIComponent(window.location.hash); </script> </body> </html>
例如:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>页面劫持</title> </head> <body> <div style="position: absolute;left: 16px;top:16px;opacity: 0;background: red"> 账号:<input id='haha' oninput="changValue()" type="text"><br/> 密码:<input type="text"><br/> </div> <iframe id="iframe" src="./hikjack.html" frameborder="0"></iframe> <script> function changValue(a) { console.log(a); } window.onload = function(){ let oiframe = document.querySelector('#iframe').contentWindow; let ohaha = document.querySelector('#haha'); let _ipt = oiframe.document.querySelector('input'); window.changValue = function(){ _ipt.value = ohaha.value; } } </script> </body> </html>
防范: 可以通过检测是否被嵌套来预防
if(window.parent != window){ alert('hikjack') }
例如: 发布诱导链接,通过iframe提交表单;(注意:发送时会带上你请求域名下的cookie)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>csrf攻击</title> </head> <body> <iframe src="" name="csrf-from"></iframe> <form target="csrf-from" action="http://127.0.0.1:3001/csrf" method="post"> <input type="text" name="name" value="111"> <input type="submit" value="提交"> </form> </body> </html>
防范:1. 尽量使用POST,限制GET;get的Referer Check维护成本高;
2. 加验证码; 3. 通过token验证;
例如:
防范手段:
例如:扫码登陆