返回列表 回复 发帖

JSESSIONID Regeneration in Struts 2

Background

Whenever a user crosses an authentication boundary, the user's session ID should be regenerated. This concept applies to a user logging into an application, logging out, or when a user reauthenticates due to a risk-based authentication process. The regeneration of session IDs is an important practice that helps eliminate session fixation vulnerabilities and may limit the impact of session theft vulnerabilities prior to authentication.


For more information on Session Fixation vulnerabilities and Session ID regeneration practices, please see the OWASP pages below:


http://www.owasp.org/index.php/Session_Fixation
http://www.owasp.org/index.php/S ... ation_of_Session_To

kens
Session ID Regeneration in Traditional Java Web Applications In a J2EE application, the user's JSESSIONID cookie should be regenerated and the previous session should be removed or deleted from the server. Example code below shows how this might be accomplished in a traditional Java web application.
  1. public class ExampleLoginServlet extends HttpServlet {

  2. public void doGet(HttpServletRequest request, HttpServletResponse response)


  3.      throws ServletException, IOException {


  4.      if( //authentication was successful ) {


  5.         request.getSession().invalidate();


  6.         HttpSession session = request.getSession(true);


  7.         session.setAttribute("AUTHENTICATED", new Boolean(true));


  8.         response.sendRedirect("PageRequiringAuthentication.jsp");


  9. //Additional Code Would Normally Follow


  10. Session ID Regeneration in Struts 2 Applications


  11. In Struts 2 applications, developers typically don't directly interact with
  12. the HttpServletRequest, HTTPServletResponse, or HttpSession objects. With
  13. consideration of these factors, the solution discussed above for a
  14. traditional Java web application may not be appropriate for Struts 2.


  15. I did a little research and through trial an error I came up with a Struts 2
  16. specific solution for regenerating JSESSIONIDs. This solution utilizes the
  17. SessionAware interface. Please excuse the unrealistic authentication code...


  18. package nickcoblentzblog.actions.sessions;


  19. import java.util.Map;


  20. import org.apache.struts2.interceptor.SessionAware;


  21. import com.opensymphony.xwork2.ActionContext;


  22. import com.opensymphony.xwork2.ActionSupport;


  23. import org.apache.struts2.dispatcher.SessionMap;


  24. public class Login extends ActionSupport implements SessionAware  {


  25. private String userid;


  26. private String password;


  27. private Map session;


  28. public String execute() {


  29.   if(userid.equals("admin") && password.equals("admin"))  {


  30.      /* Session ID Regeneration: Try #4 */


  31.      ((SessionMap)this.session).invalidate();


  32.      this.session = ActionContext.getContext().getSession();


  33.      /* End Try #4 */


  34.      session.put("AUTHENTICATED", new Boolean(true));


  35.      return SUCCESS;


  36.   }


  37.   else


  38.      return ERROR;



  39. }


  40. public String getUserid() {

  41.   return userid;



  42. }


  43. public void setUserid(String userid) {

  44.   this.userid = userid;



  45. }


  46. public String getPassword() {

  47.   return password;



  48. }


  49. public void setPassword(String password) {

  50.   this.password = password;



  51. }


  52. public void setSession(Map session) {

  53.   this.session = session;



  54. }
  55. }


  56. To test this code, I followed the following procedure.

  57. 1. Cleared all browser cookies
  58. 2. Visited the Login JSP page
  59. 3. Used the Web Developer Toolbar to view my initial JSESSIONID
  60. 4. Logged into the application successfully
  61. 5. Used the Web Developer Toolbar to view my final JSESSIONID


  62. The initial JSESSIONID value was:
  63. AA4996C5E24BB8221BB27B23EA599F34


  64. The final JSESSIONID value was:
  65. 325ED18851B93EBA542D2AE7926E7F8E


  66. Based on these tests this solution appears to work successfully.


  67. In case anyone is curious, here are a couple other ideas I toyed with:


  68. /* Try # 1:


  69. this.request.getSession().invalidate();


  70. this.request.getSession(true);


  71. */


  72. /* Try #2:


  73. HTTPUtilities esapiHTTPUtilities = ESAPI.httpUtilities();


  74. esapiHTTPUtilities.setCurrentHTTP(request, response);


  75. try {


  76. esapiHTTPUtilities.changeSessionIdentifier();



  77. }


  78. catch(Exception e) {

  79. e.printStackTrace();



  80. }


  81. */

  82. /* Try #3:


  83. ((SessionMap)ActionContext.getContext().getSession()).invalidate();


  84. */


  85. Posted by Nick Coblentz
复制代码
如果2...请深2...
返回列表