1)
사용자 하드디스크에 저장되는 쿠키를 통한 정보노출
대부분의 웹 응용프로그램에서 쿠키는 메모리에 상주하며, 브라우저 실행이 종료되면 사라진다. 프로그래머가 원하는 경우, 브라우저 세션에 관계없이 지속적으로 저장되도록 설정할 수 있으며, 이것은
디스크에 기록되고 다음 브라우저 세션이 시작되었을 때 메모리에 로드된다. 개인정보, 인증정보 등이 영속적인 쿠키(persistent Cookie)에
저장된다면, 공격자는 쿠키에 접근할 수 잇는 보다 많은 기회를 가지게 되며, 이는 시스템을 취약하게 만든다.
n 안전한 코딩
기법
①
쿠키의 만료시간은 세션이 지속되는 시간과 관련하여 최소한으로 설정하고 영속적인 쿠키에는
사용자 권한 등급, 세션ID가 포함되지 않도록 한다.
- private final String LOGIN_ACT = "login";
- private final String LOGOUT_ACT = "logout";
- private final String OTHER_ACT = "other_action";
- private final String USER_ID_PARM = "user_id";
- private final String PASSWORD_PARM = "password";
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String action = request.getParameter("action");
- if(action == null || "".equals(action)) {
- RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index");
- requestDispatcher.forward(request, response);
- }
- if(LOGIN_ACT.equals(action)) {
- String userId = request.getParameter(USER_ID_PARM);
- String password = request.getParameter(PASSWORD_PARM);
- /* do something. For example, password encryption */
- Cookie idCookie = new Cookie("id", userId);
- Cookie passwordCookie = new Cookie("password", password);
- idCookie.setMaxAge(-60 * 2); // 브라우저 종료시, 삭제되도록 음수 값 설정. 계속 사용해야 하 는경우 cookie 갱신
- passwordCookie.setMaxAge(-60 * 2);
- idCookie.setSecure(true);
- passwordCookie.setSecure(true);
- response.addCookie(idCookie);
- response.addCookie(passwordCookie);
- } else if(LOGOUT_ACT.equals(action)) {
- /* do something. */
- Cookie[] cookies = request.getCookies();
- if(cookies != null && cookies.length > 0) {
- for(int i = 0; i < cookies.length; i++) {
- Cookie cookie = new Cookie("id", "");
- if(cookies[i].getName().equals("id")) {
- cookie = new Cookie("id", "");
- } else if(cookies[i].getName().equals("password")) {
- cookie = new Cookie("password", "");
- }
- cookie.setMaxAge(0);
- response.addCookie(cookie);
- }
- }
- /* do something. */
- } else if(OTHER_ACT.equals(action)) {
- /* do something. */
- Cookie[] cookies = request.getCookies();
- if(cookies != null && cookies.length > 0) {
- for(int i = 0; i < cookies.length; i++) {
- Cookie cookie = new Cookie("id", cookies[i].getValue());
- if(cookies[i].getName().equals("id")) {
- cookie = new Cookie("id", "");
- } else if(cookies[i].getName().equals("password")) {
- cookie = new Cookie("password", cookies[i].getValue());
- }
- cookie.setMaxAge(-60 * 2);
- response.addCookie(cookie);
- }
- }
- /* do something. */
- }
- }
댓글 없음:
댓글 쓰기