NShiftKey-Rule-Guide logo NShiftKey-Rule-Guide

1. Vulnerability Description

2. How to check vulnerability

<img src=‘CRSF attack code />
<script src=…. />
<iframe src=…… />
<frame src=…… />
<meta ….. />
<form name="frm" method="POST" … > …</form>      <script>frm.submit();</script>
<script> var req = new ActiveXObject("Microsoft.XMLHTTP");     req.open("POST",  …);     req.send(null); </script>

3. Vulnerability Countermeasure

3.1. CSRF Countermeasures

3.2. CSRF solutions and its problems

Countermeasure Purpose Vulnerability / Problem Note
Using POST Method Defend against an attack code by GET query <form id=‘frm’ method=‘POST’ action=‘CSRF attack code’ ></form> - Unable to defend attacks using form.submit()
- Unable to defend AJAX queries where requests occur
Referrer Verification POST Method Vulnerability Alternative Most attack codes can be defended only by referrer check - Problems exist in browsers that do not deliver referrer values in Web plugin such as flash.
- It can be occured the web browser dependencies and Mobile program compatibility problem.
Using Security Token Referrer Problem Alternative Requires review of vulnerabilities resulting from the creation/issuance/verification/disposal of Security Token - Creating/issuing/verifying/disabling security Token is key
- Using security Token on the Web
- Using security Token in web plugin such as flash
- Using security Token in mobile(independent) programs
Secondary Verification (CAPCHA, Password Authentication) Inserts a secondary validation page for user awareness

3.3. Countermeasures for each use environment

4. Sample Code

4.1. Creating a CSRF token

// Any type of request may have a basically CSRF vulnerability.
 
// generate a CSRF token when logging in or requesting a work screen and save it to the session.
session.setAttribute("CSRF_TOKEN",UUID.randomUUID().toString());
 
// When requesting work screen, attach CSRF token to the screen and send
<input type="hidden" id="csrf" name="csrf" value="${CSRF_TOKEN}" />

4.2. Check CSRF token value

// check the CSRF token value using intercept for POST method request.
if ( ! request.getMethod().equalsIgnoreCase("post")) {
    return true;
}
else {
    if( request instanceof MultipartHttpServletRequest) {
                Enumeration<String> names = request.getParameterNames();
                while( names.hasMoreElements()) {
                    String paramName=names.nextElement();
                    if( paramName.equals("csrf")) {
                            String paramValue=request.getParameter(paramName);
                            // If the CSRF value passed to the parameter and the CSRF_TOKEN value stored in the session are the same,
                            if ( paramValue.equals( request.getSession().getAttribute("CSRF_TOKEN"))) {
                                    // forward the request to the Controller.
                                    return true;
                            }
                            // If the CSRF_TOKEN value does not match, it is recognized as a CSRF attack and processed, or moved to the page used for the original request.
                            response.sendRedirect("write.do");
                            return false;
                    }
                }
        }
}