Java世界

          學(xué)習(xí)筆記

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          天籟村

          新華網(wǎng)

          雅虎

          最新評(píng)論

          防止 XSS 攻擊 解決方案(轉(zhuǎn)載)

          XSS又叫CSS英文縮寫為Cross Site Script
          中文意思為跨站腳本攻擊
          具體內(nèi)容指的是惡意攻擊者往Web頁面里插入惡意html代碼,當(dāng)用戶瀏覽該頁之時(shí),
          嵌入其中Web里面的html代碼會(huì)被執(zhí)行,從而達(dá)到惡意用戶的特殊目的.

           

          解決方案

           

          第一。過濾

           

          過濾 標(biāo)簽 等字符 ,但是這樣 對(duì)用戶是不公平的。

          第二。用asii 碼替換

           

          如 <  >  ! 等 

           

          第三 。 用 element.innerText 顯示用戶數(shù)據(jù)

           

           這樣要寫大量的 js

          第四。 使用 <xmp>

          <xmp>標(biāo)簽不解析內(nèi)部的html元素,而且不執(zhí)行內(nèi)部的javascript腳本代碼

          但是要防止攻擊代碼在數(shù)據(jù)中間插入</xmp>從而繞過保護(hù)

           

          第五。通過 frame 來防止 數(shù)據(jù)頁面攻擊 主頁面

           使用 document.domain 屬性 來控制

           

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" >
          <head>
              <title>測試iframe的單向訪問</title>
              <style>
                  body
                  {
                   font-size:12px;
                  }
              </style>
          </head>
          <script language="javascript">

          //frame1的document
          var oDocument=null;

          //響應(yīng)frame1的onload事件
          function frame1_onload()
          {    
              //舊的domain
              var sDomainOld=document.domain;
              
              //降級(jí)domain
              document.domain="pimshell.com";
              
              //保存frame1的document
              oDocument=frame1.document;
              
              //恢復(fù)domain
              document.domain=sDomainOld;
              
              //準(zhǔn)備就緒
              button1.disabled=false;
          }

          //改變IFrame的背景顏色
          function changebackcolor()
          {
              if(oDocument.body.style.backgroundColor=="")
                  oDocument.body.style.backgroundColor="blue";
              else
                  oDocument.body.style.backgroundColor="";
          }

          </script>
          <body>
          為了避免XSS跨站攻擊,在大多數(shù)html編輯器的設(shè)計(jì)中,都是要將用戶輸入的HTML內(nèi)容進(jìn)行過濾。過濾代碼繁瑣暫且不說,關(guān)鍵是不能保證考慮到所有已知和未知的攻擊類型。
          <br />
          如果我們能設(shè)計(jì)一個(gè)iframe的單向訪問模型,就可以讓用戶輸入的HTML內(nèi)容在受控的環(huán)境中執(zhí)行,也就不需要再進(jìn)行過濾了。
          <br />
          舉例來說,我們html編輯器所在的主頁面為a,嵌入一個(gè)iframe為b。a可以訪問b的內(nèi)容,而b卻不能訪問a的內(nèi)容。這樣的話,b中的HTML內(nèi)容即使包含攻擊代碼,也無法發(fā)揮作用了。
          <br />
          <br />
          <table>
          <tr>
          <td width="300px;" valign="top">
              <iframe src="http://bb.pimshell.com/pimshell/test/b.htm" id="frame1" onload="frame1_onload();"></iframe>
              <br />
              <br />
              <button id="button1" onclick="changebackcolor();" disabled>--&gt; 改變IFrame的背景顏色</button>
          </td>
          <td valign="top">
              iframe中的代碼:
              <br />
              <pre style="background-color:whitesmoke; border:dashed 1px black; padding:4px;">
                  &lt;script language="javascript"&gt;

                  //domain
                  document.domain="pimshell.com";

                  //訪問主頁面試試看
                  function geta()
                  {
                      alert(parent.document.body.innerHTML);
                  }

                  &lt;/script&gt;    
              </pre>
              <br />
              主頁面中的代碼:
              <br />
              <pre style="background-color:whitesmoke; border:dashed 1px black; padding:4px;">
                  &lt;script language="javascript"&gt;

                      //frame1的document
                      var oDocument=null;

                      //響應(yīng)frame1的onload事件
                      function frame1_onload()
                      {    
                          //舊的domain
                          var sDomainOld=document.domain;
                          
                          //降級(jí)domain
                          document.domain="pimshell.com";
                          
                          //保存frame1的document
                          oDocument=frame1.document;
                          
                          //恢復(fù)domain
                          document.domain=sDomainOld;
                          
                          //準(zhǔn)備就緒
                          button1.disabled=false;
                      }

                      //改變IFrame的背景顏色
                      function changebackcolor()
                      {
                          if(oDocument.body.style.backgroundColor=="")
                              oDocument.body.style.backgroundColor="blue";
                          else
                              oDocument.body.style.backgroundColor="";
                      }

                  &lt;/script&gt;
              </pre>
          </td>
          </tr></table>
          </table>



          </div>

          </body>
          </html>

          posted on 2012-08-13 14:08 Rabbit 閱讀(6516) 評(píng)論(3)  編輯  收藏

          評(píng)論

          # re: 防止 XSS 攻擊 解決方案(轉(zhuǎn)載) 2013-09-16 11:06 whh

          <img onerror=”alert(123)”src=http:// www.2cto.com>  回復(fù)  更多評(píng)論   

          # re: 防止 XSS 攻擊 解決方案(轉(zhuǎn)載) 2013-12-26 17:22 <a href=# onclick=\"document.location=\'ht

          楳土ffwerwerwer  回復(fù)  更多評(píng)論   

          # re: 防止 XSS 攻擊 解決方案(轉(zhuǎn)載)"/><script>alert(document.cookie)</script><!-- 2014-05-07 14:14 會(huì)盡快

          幾何變換  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 景东| 吉首市| 巴楚县| 咸阳市| 洛川县| 北海市| 武邑县| 铅山县| 西充县| 德阳市| 会宁县| 渭源县| 调兵山市| 广宁县| 贺兰县| 南华县| 洛川县| 北碚区| 竹溪县| 诏安县| 化德县| 永德县| 砀山县| 娱乐| 土默特右旗| 内黄县| 涟水县| 南郑县| 肇源县| 六盘水市| 新乡市| 安康市| 宁安市| 炎陵县| 容城县| 方山县| 德清县| 华蓥市| 芦山县| 香格里拉县| 西乌珠穆沁旗|