OWASP 企业安全 API https://www.owasp.org/index.php/Category%3aOWASP_Enterprise_Security_API有一个非常好的选择,可以提供针对 CSRF 的可靠保护。 CSRF其实很容易解决。 OWASP ESAPI 提供了实现 CSRF 保护的规范,如下所示。
1. 生成新的 CSRF 令牌并在登录时将其添加到用户并将用户存储在 http 会话中。
这是在默认的 ESAPI 实现中完成的,并且它被存储为User
存储在的对象session
.
/this code is in the DefaultUser implementation of ESAPI
/** This user's CSRF token. */
private String csrfToken = resetCSRFToken();
...
public String resetCSRFToken() {
csrfToken = ESAPI.randomizer().getRandomString(8, DefaultEncoder.CHAR_ALPHANUMERICS);
return csrfToken;
}
2. 在任何应受保护的表单或 URL 上,将令牌添加为参数/隐藏字段。
The addCSRFToken
对于任何将要呈现且需要 CSRF 保护的 url,都应该调用下面的方法。或者,如果您正在创建表单,或者有另一种呈现 URL 的技术(例如c:url
),然后一定要添加一个名为“的参数或隐藏字段ctoken
” 和值DefaultHTTPUtilities.getCSRFToken()
.
//from HTTPUtilitiles interface
final static String CSRF_TOKEN_NAME = "ctoken";
//this code is from the DefaultHTTPUtilities implementation in ESAPI
public String addCSRFToken(String href) {
User user = ESAPI.authenticator().getCurrentUser();
if (user.isAnonymous()) {
return href;
}
// if there are already parameters append with &, otherwise append with ?
String token = CSRF_TOKEN_NAME + "=" + user.getCSRFToken();
return href.indexOf( '?') != -1 ? href + "&" + token : href + "?" + token;
}
...
public String getCSRFToken() {
User user = ESAPI.authenticator().getCurrentUser();
if (user == null) return null;
return user.getCSRFToken();
}
3. 在服务器端,对于这些受保护的操作,检查提交的令牌是否与会话中用户对象的令牌匹配。
确保您从您的servlet
or spring
行动或jsf
控制器,或者您用来处理请求的任何服务器端机制。任何需要验证 CSRF 保护的请求都应该调用此方法。请注意,当令牌不匹配时,将被视为可能是伪造的请求。
//this code is from the DefaultHTTPUtilities implementation in ESAPI
public void verifyCSRFToken(HttpServletRequest request) throws IntrusionException {
User user = ESAPI.authenticator().getCurrentUser();
// check if user authenticated with this request - no CSRF protection required
if( request.getAttribute(user.getCSRFToken()) != null ) {
return;
}
String token = request.getParameter(CSRF_TOKEN_NAME);
if ( !user.getCSRFToken().equals( token ) ) {
throw new IntrusionException("Authentication failed", "Possibly forged HTTP request without proper CSRF token detected");
}
}
4. 注销和会话超时时,将从会话中删除用户对象并销毁会话。
这一步调用了logout。发生这种情况时,请注意,会话将失效,当前用户对象将重置为匿名用户,从而删除对当前用户的引用以及相应的 csrf 令牌。
//this code is in the DefaultUser implementation of ESAPI
public void logout() {
ESAPI.httpUtilities().killCookie( ESAPI.currentRequest(), ESAPI.currentResponse(), HTTPUtilities.REMEMBER_TOKEN_COOKIE_NAME );
HttpSession session = ESAPI.currentRequest().getSession(false);
if (session != null) {
removeSession(session);
session.invalidate();
}
ESAPI.httpUtilities().killCookie(ESAPI.currentRequest(), ESAPI.currentResponse(), "JSESSIONID");
loggedIn = false;
logger.info(Logger.SECURITY_SUCCESS, "Logout successful" );
ESAPI.authenticator().setCurrentUser(User.ANONYMOUS);
}
Source: http://www.jtmelton.com/2010/05/16/the-owasp-top-ten-and-esapi-part-6-cross-site-request-forgery-csrf/ http://www.jtmelton.com/2010/05/16/the-owasp-top-ten-and-esapi-part-6-cross-site-request-forgery-csrf/
希望这可以帮助你。
Shishir