固定SessionID 漏洞 攻击(session fixation attacks)

来源:互联网 发布:linux查看端口nc 编辑:程序博客网 时间:2024/04/30 16:43

固定session 攻击:session fixation attacks
一个简单的登录控制
下面是一个最常用最简单的登录控制流程,通过表单提交用户名密码,servlet判断用户名密码,正确则写一个session,然后跳转到登录后的能够看到的页面
登录页面JSP

<body> <form action="SessionTestServlet" method="post"> 用户名:<input name="username" type="text" value=""/>  密码:<input name="password" type="password" value=""/> <input name="submit" type="submit" value="Submit"/>  </form>  </body>

SessionTestServlet

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {     HttpSession session = request.getSession();     String username=request.getParameter("username");     String password=request.getParameter("password");     if("admin".equals(username) && "pass".equals(password)){             session.setAttribute("login", true);  response.sendRedirect("hello.jsp"); }else{     response.sendRedirect("login.jsp");     } }

登录后的页面hello.jsp

<body> <% boolean isLogin = (Boolean)request.getSession().getAttribute("login"); if(isLogin!=null){ out.print("Welcome"); }else{ out.print("Sorry!"); } %> </body>

匿名SessionID
运行着个简单的demo后,打开login.jsp,使用firebug或chrome会发现,即使没有登录,我们也会有一个JSESSIONID,这是由服务器端在会话开始是通过set-cookie来设置的匿名SessionId
这里写图片描述

可以发现,登录前和登录后的JSESSIONID并没有改变,那么这就是一个固定SessionID的漏洞(详见《黑客攻防技术宝典-web实战》第七章)

简单的漏洞攻击
第一步,需要获取被攻击用户的JSESSIONID,可以通过给被攻击用户一个伪造的JSESSIONID,使其用该JESSIONID登录,获取用户登录后的JESSIONID。(这里作为示范,直接从浏览器中获取)
第二步,等被攻击用户登录,是JESSIONID成为已登录状态。
第三步,伪造请求,访问登录后的资源。
在用户登录使该JSESSIONID称为已登录的ID后,攻击者就可以利用这个ID伪造请求访问登录后的资源。下面是一个简单的python脚本

#!/bin/python import urllib, urllib2 request = urllib2.Request('http://localhost:9090/sec/hello.jsp') opener = urllib2.build_opener() //设置窃取的JSESSIONID request.add_header('Cookie','JSESSIONID=CF2D43B2C433F1A9FD78CE9D01E2E52D') hellodata=opener.open(request).read() print hellodata

执行该脚本,结果如下:
这里写图片描述
能够看到需要登录的页面

漏洞分析处理
出现该问题的主要原因是登录控制使用的固定的SessionID,登录前与登录后的SessionID是一样的。这样就使得攻击者可以简单的伪造一个SessionID诱使用户使用该SessionID登录,即可获取登录权限。如果配合XSS漏洞,则更加可以轻易获取登录权限。避免这一漏洞的方法主要有两种:
1.在登录后重置sessionID
在登录验证成功后,通过重置session,是之前的匿名sessionId失效,这样可以避免使用伪造的sessionId进行攻击。代码如下

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {     String username=request.getParameter("username");     String password=request.getParameter("password");     if("admin".equals(username) && "pass".equals(password)){     //使之前的匿名session失效     request.getSession().invalidate();      request.getSession().setAttribute("login", true);  response.sendRedirect("hello.jsp"); }    else{ response.sendRedirect("login.jsp"); } }

这样登录前与登录后的sessionID就不会相同
这里写图片描述
2.设置httpOnly属性
httponly是微软对cookie做的扩展,该值指定 Cookie 是否可通过客户端脚本访问, 解决用户的cookie可能被盗用的问题,减少跨站脚本攻击
主流的大多数浏览器已经支持此属性。httpOnly是cookie的扩展属性,并不包含在servlet2.x的规范里,因此一些javaee应用服务器并不支持httpOnly,针对tomcat,>6.0.19或者>5.5.28的版本才支持httpOnly属性,具体方法是在conf/context.xml添加httpOnly属性设置

<Context useHttpOnly="true"> ... </Context>

另一种设置httpOnly的方式是使用Tomcat的servlet扩展直接写header

response.setHeader( "Set-Cookie", "name=value; HttpOnly");
0 0
原创粉丝点击