HttpOnly是Cookie的一个重要安全属性,可以防止XSS攻击获取Cookie。下面介绍如何使用CodeQL来分析代码中未设置HttpOnly标志的Cookie。
import java
from Constructor call, MethodAccess setCookieCall
where
call.getConstructedType().hasQualifiedName("javax.servlet.http", "Cookie") and
setCookieCall.getMethod().getName() = "addCookie" and
setCookieCall.getMethod().getDeclaringType().hasQualifiedName("javax.servlet.http", "HttpServletResponse") and
not exists(FieldRead fr |
fr.getField().hasQualifiedName("javax.servlet.http", "Cookie", "setHttpOnly") and
fr.getQualifier() = call
)
select setCookieCall, "Cookie created here is missing HttpOnly flag"
import csharp
from ObjectCreation cookieCreation, MethodAccess addCookieCall
where
cookieCreation.getType().hasQualifiedName("System.Web", "HttpCookie") and
addCookieCall.getMethod().getName() = "Add" and
addCookieCall.getMethod().getDeclaringType().hasQualifiedName("System.Web", "HttpResponse") and
not exists(PropertyAssignment pa |
pa.getProperty().getName() = "HttpOnly" and
pa.getTarget() = cookieCreation
)
select addCookieCall, "Cookie created here is missing HttpOnly flag"
import javascript
from CallExpr cookieCall
where
(cookieCall.getCalleeName() = "cookie" or cookieCall.getCalleeName() = "setCookie") and
not exists(Obj obj |
obj = cookieCall.getArgument(1) and
exists(PropWrite pw |
pw.getObject() = obj and
pw.getPropertyName() = "httpOnly" and
pw.getValue().asExpr().getValue().toBoolean() = true
)
)
select cookieCall, "Cookie set here may be missing HttpOnly flag"
import java
from Constructor call, MethodAccess setCookieCall, string name
where
call.getConstructedType().hasQualifiedName("javax.servlet.http", "Cookie") and
setCookieCall.getMethod().getName() = "addCookie" and
name = call.getArgument(0).(Literal).getValue() and
(name.matches("%session%") or name.matches("%token%")) and
not exists(FieldRead fr |
fr.getField().hasQualifiedName("javax.servlet.http", "Cookie", "setHttpOnly") and
fr.getQualifier() = call
)
select setCookieCall, "Sensitive cookie '"+name+"' is missing HttpOnly flag"
识别框架特定的Cookie设置方式:不同框架可能有不同的Cookie设置方式,需要针对性地分析
结合XSS漏洞分析:可以将HttpOnly缺失分析与XSS漏洞分析结合,找出最危险的组合
通过CodeQL的这些查询,您可以系统地识别代码中所有未设置HttpOnly标志的Cookie,从而有针对性地加强应用程序的安全性。