ModSecurity Breach

ModSecurity Blog

« Helping Protect Cookies with HTTPOnly Flag | Main | Why Did Our Web Application Crash? Leveraging WAF Logging Data »

Fixing Both Missing HTTPOnly and Secure Cookie Flags

In a previous post I showed how you can use both ModSecurity and Apache together to identify/modify SessionIDs that are missing the HTTPOnly flag.  I received some feedback where people were asking how to accomplish the same thing but for the "Secure" cookie flag which instructs the browser to *only* send the SessionID back over an SSL connection. 

If you are only interested in addressing the missing "Secure" cookie flag, then you can simply take the example from the previous post and edit it slightly to swap out "httponly" with "secure".  If, however, you want to try and address both of these issues together, then you will need to change the rule set approach a bit so that it works correctly.  This is because there are now three different scenarios you have to account for -

  • Missing HTTPOnly flag
  • Missing Secure flag (if the SessionID is being sent over an SSL connection)
  • Missing both HTTPOnly and Secure flags

With this in mind, here is an updated rule set that will handle both missing HTTPOnly and Secure cooking flags.

#

# First we want to capture Set-Cookie SessionID data for later inspection

SecRule RESPONSE_HEADERS:/Set-Cookie2?/ "(?i:(j?sessionid|(php)?sessid|(asp|jserv|jw)?session[-_]?(id)?|cf(id|token)|sid))" "phase:3,t:none,pass,nolog,setvar:tx.sessionid=%{matched_var}"

 

#

# We now check the saved SessionID data for the HTTPOnly flag and set an Apache

# ENV variable if it is missing.

SecRule TX:SESSIONID "!(?i:\;? ?httponly;?)" "phase:3,t:none,setenv:httponly_cookie=%{matched_var},pass,log,auditlog,msg:'AppDefect: Missing HttpOnly Cookie Flag.'"

 

#

# Next we check the saved SessionID data for the Secure flag (if this is an SSL session)

# and set an Apache ENV variable if it is missing.

SecRule SERVER_PORT "@streq 443" "chain,phase:3,t:none,pass,log,auditlog,msg:'AppDefect: Missing Secure Cookie Flag.'"

 SecRule TX:SESSIONID "!(?i:\;? ?secure;?)" "t:none,setenv:secure_cookie=%{matched_var}"

 

#

# The final check is to see if BOTH of the HTTPOnly and Secure cookie flags are missing

# and set an Apache ENV variable if they are missing.

SecRule TX:SESSIONID "!(?i:\;? ?httponly;?)" "chain,phase:3,t:none,pass,log,auditlog,msg:'AppDefect: Missing HttpOnly and Secure Cookie Flag.'"

 SecRule SERVER_PORT "@streq 443" "chain,t:none"

  SecRule TX:SESSIONID "!(?i:\;? ?secure;?)" "t:none,setenv:secure_httponly_cookie=%{matched_var}"

 

#

# This last section executes the Apache Header command to

# add the appropriate Cookie flags 

Header set Set-Cookie "%{httponly_cookie}e; HTTPOnly" env=httponly_cookie

Header set Set-Cookie "%{secure_cookie}e; Secure" env=secure_cookie

Header set Set-Cookie "%{secure_httponly_cookie}e; Secure; HTTPOnly" env=secure_httponly_cookie

These rules will both alert and fix these cookie issues.  You may want to switch the actions to "nolog" so that you are not flooded with alerts.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e5512c9d3a88330105368a0e47970b

Listed below are links to weblogs that reference Fixing Both Missing HTTPOnly and Secure Cookie Flags:

Thanks for sharing. Definitely some movement with known attributes that we've been preaching over and over to include, either in browsers or apps.

The comments to this entry are closed.

Calendar

November 2010
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Feeds

Atom Feed

Search

Categories

Recent Entries

Archives