ESAPI之会话安全

ESAPI是开源组织owasp,开放的安全开发框架,但百度、google搜索相关的技术文章不多,今天小尝试了一下,分享一下心得。html

会话攻击,简单理解就是盗用窃取用户的cookie,假装成用户,向服务器端发送请求,窃取用户私密信息。java

具体如何防止会话攻击,很简单,参照《Web应用安全威胁与防治--基于OWASP TOP 10 与ESAPI》书中介绍的方法,一旦用户登陆成功后,立刻validate用户的会话,具体步骤以下:api

  1. 用户输入用户名和密码
  2. 系统对用户进行验证经过
  3. 已有的会话信息若是仍然须要,则转移到一个临时变量中去
  4. invalidate当前会话
  5. 建立一个新会话
  6. 把临时变量中保存的会话信息恢复到新建立的会话中去
  7. 用户使用这个新的会话登陆到系统中并进行操做

 

贴出实例安全

构造一个简单登陆页面服务器

 

[html]  view plain copy print ?
 
  1. <body>  
  2.     <form action="loginServlet" method="post">  
  3.         用户名:<input type="text" name="username" /><br/>  
  4.         密码:<input type="password" name="password"/><br/>  
  5.         <input type="submit" value="登陆"/>  
  6.     </form>  
  7.   </body>  


验证成功的页面cookie

 

 

[html]  view plain copy print ?
 
  1. <body>  
  2.    欢迎${sessionScope.username }登陆  
  3.  </body>  


而后是一个LoginServlet,其中DefaultHTTPUtilities是ESAPI中org.owasp.esapi.reference.DefaultHTTPUtilities类,该类的changeSessionIdentifier(request),就是实现上述功能。session

 

 

[java]  view plain copy print ?
 
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  2.             throws ServletException, IOException {  
  3.         String username = request.getParameter("username");  
  4.         String password = request.getParameter("password");  
  5.         DefaultHTTPUtilities dhUtil = new DefaultHTTPUtilities();  
  6.         try {  
  7.             HttpSession session = dhUtil.changeSessionIdentifier(request);  
  8.             session.setAttribute("username", username);  
  9.             session.setAttribute("password", password);  
  10.             request.getRequestDispatcher("/index.jsp").forward(request, response);  
  11.         } catch (AuthenticationException e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.     }  


贴出changeSessionIdentifier(request)方法源码框架

 

 

 

[java]  view plain copy print ?
 
    1. public HttpSession changeSessionIdentifier(HttpServletRequest request) throws AuthenticationException {  
    2.   
    3.         // get the current session  
    4.         HttpSession oldSession = request.getSession();  
    5.   
    6.         // make a copy of the session content  
    7.         Map<String,Object> temp = new ConcurrentHashMap<String,Object>();  
    8.         Enumeration e = oldSession.getAttributeNames();  
    9.         while (e != null && e.hasMoreElements()) {  
    10.             String name = (String) e.nextElement();  
    11.             Object value = oldSession.getAttribute(name);  
    12.             temp.put(name, value);  
    13.         }  
    14.   
    15.         // kill the old session and create a new one  
    16.         oldSession.invalidate();  
    17.         HttpSession newSession = request.getSession();  
    18.         User user = ESAPI.authenticator().getCurrentUser();  
    19.         user.addSession( newSession );  
    20.         user.removeSession( oldSession );  
    21.   
    22.         // copy back the session content  
    23.       for (Map.Entry<String, Object> stringObjectEntry : temp.entrySet())  
    24.       {  
    25.          newSession.setAttribute(stringObjectEntry.getKey(), stringObjectEntry.getValue());  
    26.         }  
    27.         return newSession;  
    28.     }  
相关文章
相关标签/搜索