ModSecurity 2
Rule Language
Processing Phases
<ð
ModSecurity splits processing into 5 processing
phases:
1. Request Headers
2. Request Body
3. Response Headers
4. Response Body
5. Logging
<ð
This many phases allow you to decide what you
want to happen at key points of transaction
processing.
ModSecurity 2 Rule Language 2 / 30
Rule Syntax
<ð
The most used directive is SecRule:
SecRule VARIABLES OPERATOR [ACTIONS]
<ð
This directive will:
1. Expand collection variables from the VARIABLES
section.
2. Apply the operator as specified in the OPERATOR
section to the expanded variables.
3. One rule will trigger once for a match in every
variable.
4. A match will either execute the per-rule actions, or
perform the default actions.
ModSecurity 2 Rule Language 3 / 30
Simple Rule
<ð
In the simplest case:
SecRule REQUEST_URI aaa
<ð
The above will look for the pattern aaa in the
variable REQUEST_URI.
<ð
The pattern is a regular expression.
<ð
A similar pattern can be written as:
SecRule REQUEST_URI b{3}
<ð
ModSecurity uses PCRE (http://www.pcre.org)
ModSecurity 2 Rule Language 4 / 30
Multiple Variables As Targets
<ð
There can be any number of variables in the
VARIABLES section (separated by pipes):
SecRule "REQUEST_URI|QUERY_STRING" \
ccc
<ð
Configuration directives can be split over several
lines (that s an Apache feature) by terminating
the line with a backslash.
<ð
The whitespace at the beginning of next line will
become part of the directive.
<ð
If you need to have a whitespace use double
quotes to delimit parameter.
ModSecurity 2 Rule Language 5 / 30
Variable Collections
<ð
Some variables expand at runtime:
SecRule ARGS ddd
<ð
The above will expand into variables
representing individual request parameters, but
only if there are parameters present.
<ð
Only the content is examined.
<ð
Another variable is used for the names:
SecRule ARGS_NAMES eee
<ð
There is a variable for every bit of transaction.
ModSecurity 2 Rule Language 6 / 30
Targeting Individual Parameters
<ð
You can target individual parameters with the
help of the selection operator:
SecRule ARGS:p fff
<ð
Or you can target all parameters except the
ones you specify:
SecRule ARGS|!ARGS:q ggg
<ð
You can even use a regular expression to select
the parameters (* does the opposite in beta-3):
SecRule ARGS:/^z/ hhh
ModSecurity 2 Rule Language 7 / 30
Counting Variables In a Collection
<ð
You can count how many variables there are in
a collection (e.g. parameters, request headers,
response headers, etc):
SecRule &ARGS !^0$
<ð
The above triggers if there are any parameters
supplied in the request.
<ð
You might have noticed the exclamation mark; it
negates the regular expression.
ModSecurity 2 Rule Language 8 / 30
Variable Names (1)
<ð
ARGS, ARGS_COMBINED_SIZE, ARGS_NAMES
<ð
REQBODY_PROCESSOR,
REQBODY_PROCESSOR_ERROR,
REQBODY_PROCESSOR_ERROR_MSG
<ð
XML
<ð
WEBSERVER_ERROR_LOG
<ð
FILES, FILES_TMPNAMES, FILES_NAMES,
FILE_SIZES, FILES_COMBINED_SIZE
<ð
TX
<ð
ENV
ModSecurity 2 Rule Language 9 / 30
Variable Names (2)
<ð
REMOTE_HOST, REMOTE_ADDR,
REMOTE_PORT, REMOTE_USER
<ð
PATH_INFO, QUERY_STRING
<ð
AUTH_TYPE
<ð
SERVER_NAME, SERVER_PORT, SERVER_ADDR
<ð
REQUEST_LINE, REQUEST_URI,
REQUEST_METHOD, REQUEST_PROTOCOL
<ð
REQUEST_FILENAME, REQUEST_BASENAME
<ð
SCRIPT_FILENAME, SCRIPT_BASENAME
ModSecurity 2 Rule Language 10 / 30
Variable Names (3)
<ð
TIME, TIME_EPOCH
<ð
TIME_YEAR, TIME_MON, TIME_DAY,
TIME_HOUR, TIME_MIN, TIME_SEC,
TIME_WDAY
<ð
SCRIPT_UID, SCRIPT_GID
<ð
SCRIPT_USERNAME, SCRIPT_GROUPNAME
<ð
SCRIPT_MODE
<ð
REQUEST_HEADERS,
REQUEST_HEADERS_NAMES
ModSecurity 2 Rule Language 11 / 30
Variable Names (4)
<ð
REQUEST_COOKIES,
REQUEST_COOKIES_NAMES
<ð
REQUEST_BODY
<ð
RESPONSE_LINE, RESPONSE_STATUS
<ð
RESPONSE_PROTOCOL
<ð
RESPONSE_HEADERS,
RESPONSE_HEADERS_NAMES
<ð
RESPONSE_BODY
<ð
WEBAPPID, SESSIONID
ModSecurity 2 Rule Language 12 / 30
Explicit Operators In Rules
<ð
Regular expression matcher is the default
operator.
<ð
In a general case you can choose exactly which
operator you want to use:
SecRule REQUEST_URI "@rx iii"
<ð
You can still use the exclamation mark in front
of the @ character (and the meaning is the
same).
ModSecurity 2 Rule Language 13 / 30
Supported Operators
<ð
The following operators are supported in 2.0.0-beta-3:
eq
rx
ge
validateByteRange
gt
validateDTD
inspectFile
validateSchema
le
validateUrlEncoding
lt
validateUtf8Encoding
rbl
ModSecurity 2 Rule Language 14 / 30
Operator Usage Examples
<ð
Validate files that are uploaded:
SecRule FILES_TMPNAMES "@inspectFile \
/opt/apache/bin/inspect_script.pl"
<ð
Check only certain bytes are used in parameters:
SecRule ARGS "@validateByteRange \
10,13,32-126"
<ð
Validate UTF-8 encoding:
SecRule ARGS "@validateUtf8Encoding"
<ð
Real-time Block List lookup:
SecRule REMOTE_ADDR "@rbl sc.surbl.org"
ModSecurity 2 Rule Language 15 / 30
Actions
<ð
There are five types of action:
1. Disruptive actions interrupt current transaction.
2. Non-disruptive actions change state.
3. Flow actions change rule flow.
4. Meta-data actions contain rule metadata.
5. Data actions mere placeholders for other
actions.
<ð
Usage example:
SecRule ARGS ddd log,deny,status:500
SecAction nolog,pass,exec:/bin/this/that.pl
ModSecurity 2 Rule Language 16 / 30
Disruptive Actions
<ð
Interrupt or disrupt transaction:
4ð
deny stops transaction.
4ð
drop drops connection
4ð
redirect respond with a redirection.
4ð
proxy forward request to another server.
4ð
pause slow down execution.
ModSecurity 2 Rule Language 17 / 30
Meta-data Actions
<ð
Meta-data actions describe the rule:
4ð
id unique rule ID.
4ð
rev rule revision.
4ð
msg custom message.
4ð
severity as syslog (0-7).
4ð
phase the phase where the rule is supposed to
run.
4ð
log, nolog whether or not to log the match.
4ð
auditlog, noauditlog whether or not to count the
match toward audit logging.
ModSecurity 2 Rule Language 18 / 30
Flow Actions
<ð
Flow actions affect how rules are processed:
4ð
allow stop processing rules.
4ð
chain combine the rule with the next one.
4ð
pass ignore match in the current rule.
4ð
skip skip over one or more rules.
ModSecurity 2 Rule Language 19 / 30
Data Actions
<ð
Data actions are helpers for other parts of the
rule:
4ð
capture used in combination with @rx to capture
subexpressions.
4ð
status which status code to use for deny, redirect.
4ð
t defines which transformation functions need to be
run against the variables.
4ð
xmlns defines namespace for XPath expressions.
ModSecurity 2 Rule Language 20 / 30
Audit Log Sanitisation Actions
<ð
There are four actions:
4ð
sanitiseArg
4ð
sanitiseMatched
4ð
sanitiseRequestHeader
4ð
sanitiseResponseHeader
<ð
Examples:
SecAction nolog,pass,sanitiseArg:p
SecAction \
nolog,pass,sanitiseRequestHeader:Authorization
SecRule ARGS secret \
nolog,pass,sanitiseMatched
ModSecurity 2 Rule Language 21 / 30
Variable Actions
<ð
Working with environment variables:
setenv:name=value
setenv:!name
<ð
Working with variables:
setvar:tx.score=10
setvar:tx.score=+5
setvar:!tx.score
deprecatevar:session.score=60/3600
expirevar:session.blocked=3600
ModSecurity 2 Rule Language 22 / 30
Collection Actions
<ð
initcol create a persistent collection:
initcol:ip=%{REMOTE_ADDR}
<ð
setsid initialise session storage:
SecRule REQUEST_COOKIES:PHPSESSID !^$ chain,nolog,pass
SecAction setsid:%{REQUEST_COOKIES.PHPSESSID}
<ð
This action will initialise variable SESSIONID.
<ð
Use SecWebAppId directive to create session
storage namespace for each application.
ModSecurity 2 Rule Language 23 / 30
Built-in Collection Variables
<ð
Some variables are automatically generated:
4ð
CREATE_TIME
4ð
KEY
4ð
LAST_UPDATE_TIME
4ð
TIMEOUT
4ð
UPDATE_COUNTER
4ð
UPDATE_RATE
<ð
Some variable names have pre-defined purpose:
4ð
BLOCKED
4ð
SCORE
ModSecurity 2 Rule Language 24 / 30
Other Actions
<ð
Execute external script:
exec:/bin/script.pl
<ð
Update transaction settings dynamically:
4ð
ctl
żð
auditEngine
żð
auditLogParts
żð
debugLogLevel
żð
requestBodyAccess
żð
requestBodyLimit
żð
requestBodyProcessor
żð
responseBodyAccess
żð
responseBodyLimit
4ð
For example:
żð
ctl:auditEngine=off
ModSecurity 2 Rule Language 25 / 30
Transformation Functions (1)
<ð
Transformation functions will automatically
convert data before matching:
hexDecode
lowercase
hexEncode
replaceNulls
htmlEntityDecode
compressWhitespace
escapeSeqDecode
replaceComments
normalisePath
urlDecode
normalisePathWin
urlDecodeUni
md5
base64Encode
sha1
base64Decode
ModSecurity 2 Rule Language 26 / 30
Transformation Functions (2)
<ð
The following is performed by default (and in
this order):
4ð
lowercase
4ð
replaceNulls
4ð
compressWhitespace
<ð
But you can change the default setting for all
subsequent rules:
SecDefaultAction log,deny,status:500,\
t:replaceNulls,t:compressWhitespace
<ð
Or, just for one rule:
SecRule ARG:base64 ABC t:base64decode
ModSecurity 2 Rule Language 27 / 30
Complete XML Example (1)
<ð
Detect XML and instruct ModSecurity to parse it:
# Phase 1
SecDefaultAction phase:1
# Detect XML requests and process them as XML
SecRule REQUEST_HEADERS:Content-Type ^text/xml$ \
nolog,pass,ctl:requestBodyProcessor=XML
ModSecurity 2 Rule Language 28 / 30
Complete XML Example (2)
# Phase 2
SecDefaultAction phase:2
# Stop on request body processing errors
# (e.g. XML is not well formed)
SecRule REQBODY_PROCESSOR_ERROR "@eq 1"
# Validate XML against a DTD
SecRule REQBODY_PROCESSOR "^XML$ chain
SecRule XML "@validateDTD /opt/apache-frontend/conf/xml.dtd"
# Look into only one part of the XML
SecRule XML:/person/name/firstname/text() Ivan
ModSecurity 2 Rule Language 29 / 30
THE END!
Questions?
ModSecurity 2 Rule Language 30 / 30
Wyszukiwarka
Podobne podstrony:
language expressionsLanguage and Skills Test Units 1 2language3? EXAM LANGUAGE ELEMENTSfor studentslanguagelanguage ooplanguage tests 10alanguageLanguage Courselanguage operators comparisonlanguage operators incrementlanguage templatewięcej podobnych podstron