轉自:http://www.frostsky.com/2011/10/xss-hack/
對於的用戶輸入中出現XSS漏洞的問題,主要是由於開發人員對XSS了解不足,安全的意識不夠造成的。現在讓我們來普及一下XSS的一些常識,以后在開發的時候,每當有用戶輸入的內容時,都要加倍小心。請記住兩條原則:過濾輸入和轉義輸出。
一、什么是XSS
XSS又叫CSS (Cross Site Script) ,跨站腳本攻擊。它指的是惡意攻擊者往Web頁面里插入惡意html代碼,當用戶瀏覽該頁之時,嵌入其中Web里面的html代碼會被執行,從而達到惡意的特殊目的。XSS屬於被動式的攻擊,因為其被動且不好利用,所以許多人常呼略其危害性。
在WEB2.0時代,強調的是互動,使得用戶輸入信息的機會大增,在這個情況下,我們作為開發者,在開發的時候,要提高警惕。
二、XSS攻擊的主要途徑
XSS攻擊方法只是利用HTML的屬性,作各種的嘗試,找出注入的方法。現在對三種主要方式進行分析。
第一種:對普通的用戶輸入,頁面原樣內容輸出。
打開http://go.ent.163.com/goproducttest/test.jsp(限公司IP),輸 入:<script>alert(‘xss’)</script>, JS腳本順利執行。當攻擊者找到這種方法后,就可以傳播這種鏈接格式的鏈接 (http://go.ent.163.com/goproducttest/test.jsp?key=JSCODE)如:http: //go.ent.163.com/goproducttest/test.jsp?key=<script>alert(‘xss’)& lt;/script>,並對JSCODE做適當偽裝,如:
http://go.ent.163.com/goproducttest/test.jsp?key=%3c%73%63%72%69%70 %74%3e%61%6c%65%72%74%28%27%78%73%73%27%29%3c%2f%73%63%72%69%70%74%3e,當其 它用戶當點此鏈接的時候,JS就運行了,造成的后果會很嚴重,如跳去一個有木馬的頁面、取得登陸用戶的COOKIE等。
第二種:在代碼區里有用戶輸入的內容
原則就是,代碼區中,絕對不應含有用戶輸入的東西。
第三種:允許用戶輸入HTML標簽的頁面。
用戶可以提交一些自定義的HTML代碼,這種情況是最危險的。因為,IE瀏覽器默認采用的是UNICODE編碼,HTML編碼可以用&#ASCII方式來寫,又可以使用”/”連接16進制字符串來寫,使得過濾變得異常復雜,如下面的四個例子,都可以在IE中運行。
1,直接使用JS腳本。
<img src=”javascript:alert(‘xss’)” />
2,對JS腳本進行轉碼。
<img src=”javascript:alert(‘xss’)” />
3,利用標簽的觸發條件插入代碼並進行轉碼。
<img onerror=”alert(‘xss’)” />
4,使用16進制來寫(可以在傲游中運行)
三、XSS攻擊解決辦法
請記住兩條原則:過濾輸入和轉義輸出。
具體執行的方式有以下幾點:
第一、在輸入方面對所有用戶提交內容進行可靠的輸入驗證,提交內容包括URL、查詢關鍵字、http頭、post數據等
第二、在輸出方面,在用戶輸內容中使用<XMP>標簽。標簽內的內容不會解釋,直接顯示。
第三、嚴格執行字符輸入字數控制。
四、在腳本執行區中,應絕無用戶輸入。
----------------------------------------分割線----------------------------------------
如何讓HTML標簽不被解析
轉自:http://segmentfault.com/q/1010000002516795
要符合“內部的html標簽不被解析”,我們根據HTML5的標准,分元素類別討論吧:
- Void Elements,如
br
等。
他們不允許有內部文本。 - Foreign Elements,如
svg
和mathml
的相關標簽
跟xml語法一致,他們內部文本若不想被當作標簽解析,只有用<![CDATA[
和]]>
包裹 - RCDATA elements:即
textarea
和title
。
他們不能嵌套自身,內部的實體會被轉義,內部的<
不會被當作tag open解析。
因此,他們內部的其他標簽自然不會被解析。 - Raw text elements:即
script
和style
。
他們不能嵌套自身,內部的實體不會被轉義,內部的<
不會被當作tag open解析。
因此,他們內部的其他標簽自然不會被解析。 - Normal elements,普通的元素,基本上上列沒有提及的都屬於這一列,包括
pre
和code
。
他們的特點是,內部的實體會被轉義,內部的<
可能根據上下文,被當作tag open解析。
他們內部的文本若想展示標簽文本而不解析,必須先轉義<
為<
,>
為>
如果目標是讓HTML標簽文本內容正常顯示而不被解析,最簡單的方案是嵌入到<script type="text/html">
或<script type="text/template">
內部,並加上display: block
即可。
HTML4舊有的有xmp
、listing
和plaintext
類似於HTML5的Raw text elements,可以包含標簽而不解析,內部實體不被轉義,但是已經在HTML5中廢棄。
例子:
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="Keywords" content=""> <meta name="Description" content=""> <title><script type="text/javascript">alert(111);</script></title> </head> <body> <textarea><script type="text/javascript">alert("textarea");</script></textarea> <script type="text/html" style="display:block"> <script type="text/javascript">alert("script type=text/html");</script> </script> <script type="text/template" style="display:block"> <script type="text/javascript">alert("script type=text/template");</script> </script> <style style="display:block"> <script type="text/javascript">alert("style style=display:block");</script> </style> <xmp> <script type="text/javascript">alert("xmp");</script> </xmp> <xmp> <script type="text/javascript">alert("xmp");</script> </xmp> fghfgh <plaintext> <script type="text/javascript">alert("plaintext");</script> </plaintext> <plaintext> <script type="text/javascript">alert("plaintext");</script> </plaintext> </body> </html>
顯示效果:
看到並沒有執行javascript腳本。
----------------------------------------分割線----------------------------------------
轉自:http://tuhaitao.iteye.com/blog/1126592
xss漏洞之進制轉換
SQL注入的事件已經是上個世紀最令人頭疼的攻擊方法,21世紀又出現了HTML注入漏洞,隨着web飛速的發展,XSS漏洞已經不容忽視,簡單介紹一下XSS漏洞, 只要有用戶輸入的地方,就會出現XSS漏洞,例如在發表一篇帖子的時候,在其中加入腳本。
1.HTML標簽注入:
- <script>alert('Hello World!')</script>
很多網站為了避免XSS的攻擊,對用戶的輸入都采取了過濾,最常見的就是對<>轉換成<以及>,經過轉換 以后<>雖然可在正確顯示在頁面上,但是已經不能構成代碼語句了。這個貌似很徹底,因為一旦<>被轉換掉,什 么<script src=1.js></script>就會轉換成“<script src=1.js></script>”,不能執行,因此,很多人認為只要用戶的輸入沒有構成<& gt;,就不能閉合前后的標簽,其語句當然也不會有害,但是,萬事總有可能,只要有一定的條件,我們就可以構造經過編碼后的語句來進行XSS,稍候我會提 到16進制、8進制轉換,以及混合轉換。
2. HTML屬性注入
於是程序員想辦法封堵這個漏洞,過濾了<script></script> 標簽,那么在頁面上就不會執行這段js代碼,
於是乎,黑客想了一個不用<script>標簽的辦法,注入html, 怎么回事呢?
是這樣:
<img src='http://dl.iteye.com/upload/picture/pic/94494/0a949350-1644-3d50-9b13-b028f4891981.png'>
正常情況下,img的src標簽是指向一個web服務器的圖片URL,但是也可以替換為:
<img src='javascript:alert("Hello world!")'>
這樣黑客通過繞道的形式,繞開了程序員的過濾,順利執行了XSS攻擊
程序員見況,同理有過濾了用戶輸入的src屬性,過濾掉里邊的javascript開頭的關鍵字,暫時封堵了XSS。
3.ASCII 10進制轉換繼續XSS
javascript:alert("Hello world!")可以用HTML 10進制ASCII編碼代替:
格式:&#(ASCII10進制編碼);
javascript:alert("Hello world!")
img標簽變為:
<img src='javascript:alert("Hello world!")' />
ASCII 10進制轉換,同樣適合於HTML標簽注入:
例如:
把<script>alert("Hello world");</script>標簽轉換成10進制 ASCII轉義字符:
<script>alert("Hello world");</script>";
接下來,使用URL編碼得到:
%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%22%48%65%6C%6C%6F%20%77%6F%72%6C%64%22%29%3B%3C%2F%73%63%72%69%70%74%3E
然后放入到參數中:
http://www.test.com/a=%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%22%48%65%6C%6C%6F%20%77%6F%72%6C%64%22%29%3B%3C%2F%73%63%72%69%70%74%3E
接着程序員用JSP得到參數
<% string str_a = rrequest.getParameter("a");%>
var a= <%=str_a%>
document.write(a);
同樣會引發XSS漏洞
4. ASCII 16進制轉換
格式: &#x(ASCII16進制編碼);
<img src="javascript:alert('b')">
與10進制ASCII轉義一樣,只不過換了一種進制規則表示
5. ASCII 8進制轉換
其實16進制還有一種表現形式,與8進制類似
格式:\x(ASCII 16進制編碼)
格式:\(ASCII 8進制編碼)
例如:
<script>alert("Hello world!");</script>
轉換為16進制是:
\x3C\x73\x63\x72\x69\x70\x74\x3E\x61\x6C\x65\x72\x74\x28\x22\x48\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x21\x22\x29\x3B\x3C\x2F\x73\x63\x72\x69\x70\x74\x3E
八進制是去掉\后的x,數值轉換為8進制數值即可,我就懶的自己轉了,有興趣大家可以試試
同樣以構造URL參數,或者HTML屬性的形式注入到HTML當中,即可產生XSS漏洞
6. 8進制、10進制、16進制混合轉換
道理一樣,只不過一段代碼中的進制數混合,自由構造,組合比較多
7. 加入混淆字符
這 樣做的目的還是為了繞開程序員代碼的過濾, 其中加入一些混淆轉義字符,在系統控制字符中,除了頭部的�(null)和尾部的(del)外,其他31個字符均可作為混淆字符,比如、 等字符都可插入到javascript或vbscript的頭部,其中Tab符 、換行符、回車符還可以插入到代碼中任意地方, 當然還包括字母的大小寫混合;
這里我摘抄了網上的一些例子:
例1:<img src="javascript:alert(/a/)"> '/插入到代碼頭部,其中可寫成,效果一樣
例2:<img src="java scr ipt:alert(/a/)"> '/插入到代碼中任意位置,其中 可寫成
例3:<IMG SRC="jav ascript:alert('XSS')"> '/ 是回車符的16進制形式
例4:<IMG SRC="jav ascript:alert('XSS')"> '/ 是換行符的16進制形式
這些是比較常用的例子,組合很多,變化多端, 有興趣大家可以自己研究一下:)
----------------------------------------分割線----------------------------------------
轉自:https://www.exploit-db.com/papers/15446/
Bypass XSS filters (Paper)
############################################# #Title : XSS, how to bypass filters # #Author : k3nz0 # #Contact : o9p@hotmail.fr # #Category : Papers # #Website : k3nz0.com # ############################################# #################Tunisian#################### ##################Hacker##################### ############################################# This lessons is devided into 3 parts : [1] Introduction [2] Types of filters [3] Conclusion [1] Introduction : Nowadays, most of "securised" websites, make filters to don't allow cross site scripting "injections", however, we can bypass these filters by using the methods shown below :) [2] Types of filters : We will learn, how to bypass these xss filters : [+]Bypass magic_quotes_gpc (if it's on ) [+]Bypass with cryption in full html [+]Bypass with Obfuscation [+]Bypass with trying around method ############################################ [+]Bypass magic_quotes_gpc When magic_quotes_gpc is on, it means that the server doesn't allow, ", / and ' (it depends) to bypass it we use : String.fromCharCode() We write our code, in the () crypted in ASCII exemple : String.fromCharCode(107, 51, 110, 122, 48) (Here I crypted k3nz0 in ascii : 107, 51, 110, 122, 48 And we use it : <script>String.fromCharCode(107, 51, 110, 122, 48)</script> We will see : k3nz0 We bypassed magic_quotes_gpc :) ############################################# [+] Bypass with cryption in full html : Very simple, we have to encode our code in full HTTP! Our code : <script>alert('i am here')</script> And in full HTTP : %3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%27%69%20%61%6D%20%68%65%72%65%27%29%3C%2F%73%63%72%69%70%74%3E Now, you can inject it :) ! Notice that you can use the tool "Coder" to do encode it in full HTTP We bypassed filter. ############################################# [+] Bypass with Obfuscation : Very simple too, this filter, don't allows for exemple these words : -script -alert To bypass it, you change "script" with for exemple "sCriPt", and "alert" with "ALerT" ! For exemple : <ScriPt>ALeRt("i am here")</scriPt> We bypassed the filter. ############################################## [+] Bypass with trying around method : Generally, it is in the searchs scripts, we just add "> at the begining to close current fields : exemple : http://target.com/search.php?search="><script>alert("hello")</script> We bypassed the filter. ############################################### [3] Conclusion : It was, how we can bypass xss filters, and how to inject our code :) This lesson is explained by k3nz0 Thank you for reading GREETZ : ALLAH ! Aymanos, v1r, kannibal615 , born to kill, & more.. ################################################