2015년 12월 6일 일요일

보안기능 - 사용자 하드디스크에 저장되는 쿠키를 통한 정보 노출

1)     사용자 하드디스크에 저장되는 쿠키를 통한 정보노출
대부분의 웹 응용프로그램에서 쿠키는 메모리에 상주하며, 브라우저 실행이 종료되면 사라진다. 프로그래머가 원하는 경우, 브라우저 세션에 관계없이 지속적으로 저장되도록 설정할 수 있으며, 이것은 디스크에 기록되고 다음 브라우저 세션이 시작되었을 때 메모리에 로드된다. 개인정보, 인증정보 등이 영속적인 쿠키(persistent Cookie)에 저장된다면, 공격자는 쿠키에 접근할 수 잇는 보다 많은 기회를 가지게 되며, 이는 시스템을 취약하게 만든다.

n  안전한 코딩 기법

     쿠키의 만료시간은 세션이 지속되는 시간과 관련하여 최소한으로 설정하고 영속적인 쿠키에는 사용자 권한 등급, 세션ID가 포함되지 않도록 한다.


  1. private final String LOGIN_ACT = "login";  
  2. private final String LOGOUT_ACT = "logout";  
  3. private final String OTHER_ACT = "other_action";  
  4. private final String USER_ID_PARM = "user_id";  
  5. private final String PASSWORD_PARM = "password";  
  6. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  7.     String action = request.getParameter("action");  
  8.     if(action == null || "".equals(action)) {  
  9.         RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index");  
  10.         requestDispatcher.forward(request, response);  
  11.     }  
  12.       
  13.     if(LOGIN_ACT.equals(action)) {  
  14.         String userId = request.getParameter(USER_ID_PARM);  
  15.         String password = request.getParameter(PASSWORD_PARM);  
  16.           
  17.         /* do something. For example, password encryption */  
  18.           
  19.         Cookie idCookie = new Cookie("id", userId);  
  20.         Cookie passwordCookie = new Cookie("password", password);  
  21.       
  22.         idCookie.setMaxAge(-60 * 2); // 브라우저 종료시, 삭제되도록 음수 값 설정. 계속 사용해야 하 는경우 cookie 갱신  
  23.         passwordCookie.setMaxAge(-60 * 2);  
  24.           
  25.         idCookie.setSecure(true);  
  26.         passwordCookie.setSecure(true);  
  27.           
  28.         response.addCookie(idCookie);  
  29.         response.addCookie(passwordCookie);  
  30.     } else if(LOGOUT_ACT.equals(action)) {  
  31.         /* do something. */  
  32.         Cookie[] cookies = request.getCookies();  
  33.         if(cookies != null && cookies.length > 0) {  
  34.             for(int i = 0; i < cookies.length; i++) {  
  35.                 Cookie cookie = new Cookie("id""");  
  36.                 if(cookies[i].getName().equals("id")) {  
  37.                     cookie = new Cookie("id""");  
  38.                 } else if(cookies[i].getName().equals("password")) {  
  39.                     cookie = new Cookie("password""");  
  40.                 }  
  41.                 cookie.setMaxAge(0);  
  42.                 response.addCookie(cookie);  
  43.             }  
  44.         }  
  45.         /* do something. */  
  46.     } else if(OTHER_ACT.equals(action)) {  
  47.         /* do something. */  
  48.         Cookie[] cookies = request.getCookies();  
  49.         if(cookies != null && cookies.length > 0) {  
  50.             for(int i = 0; i < cookies.length; i++) {  
  51.                 Cookie cookie = new Cookie("id", cookies[i].getValue());  
  52.                 if(cookies[i].getName().equals("id")) {  
  53.                     cookie = new Cookie("id""");  
  54.                 } else if(cookies[i].getName().equals("password")) {  
  55.                     cookie = new Cookie("password", cookies[i].getValue());  
  56.                 }  
  57.                 cookie.setMaxAge(-60 * 2);  
  58.                 response.addCookie(cookie);  
  59.             }  
  60.         }  
  61.         /* do something. */  
  62.     }  
  63. }  


댓글 없음:

댓글 쓰기