A condition makes a rule or a whitelist depend on the value of an NGINX variable; this allows Naxsi to take decisions based on data computed by NGINX or by other NGINX modules, like the client country (geoip2), the client network (geo), the request method (map), etc..
A condition is defined by the "if:$<variable>" (or "if:!$<variable>") parameter of the MainRule and BasicRule directives.
⚠️ Warning
The variables used in the conditions must have their value already available when Naxsi inspects the request.
Naxsi inspects the requests at the beginning of the NGINX
rewritephase, before theset,if,rewriteandreturndirectives defined in thelocationblock, and before any module that runs in later phases. Variables which are not ready yet are seen by Naxsi as empty (thus false) and the conditions are silently evaluated with that value.Variables that can be used:
- Variables computed on demand, like the ones defined by
map,geoandgeoip2(these are the recommended way to define conditions).- Request variables, like
$remote_addr,$request_method,$http_<header>,$arg_<name>,$cookie_<name>,$ssl_*, etc.. ($remote_addralready includes the changes made by therealipmodule).- Variables defined via
setin theserverblock (outside anylocation).Variables that cannot be used (they are always empty when Naxsi inspects the request):
- Variables defined via
set(orif) within alocationblock.- Variables defined by modules running in the
rewritephase after Naxsi or in later phases, likerewrite_by_lua*/access_by_lua*(OpenResty),auth_request_set(auth_requestmodule),js_setwhen its value depends on later phases, etc..- Variables related to the upstream or to the response, like
$upstream_*,$status,$sent_http_*,$request_time, etc..When unsure, test the condition with a rule in learning mode and check the logs.
"if:$variable": the condition holds when the variable is true."if:!$variable": the condition holds when the variable is false.A variable is true when it is defined, not empty and different from 0 (the same logic used by the NGINX if ($variable) directive); otherwise it is false.
ℹ️ Info
- Only one condition is allowed per rule or whitelist; to combine multiple conditions use a
mapon the concatenation of the variables, for examplemap "$a:$b" $a_and_b { "1:1" 1; default 0; }.- The comparison with a specific value is not supported by Naxsi; use a
mapto transform the value into a boolean (see the examples below).- The variable must be defined somewhere within the NGINX configuration (even after the rule), otherwise NGINX will fail to start with
unknown "<name>" variable.- The
negativekeyword cannot be used to negate a condition, because it negates the search pattern (str:orrx:); use"if:!$variable"instead.
Conditions can be used in three ways:
A rule with a condition but without a search pattern (str:, rx: or d:libinj_*) matches only on the condition, once per request; the rule score is then handled like for any other rule, thus the action is defined by the CheckRule directives and it is possible to use learning mode, IgnoreIP/IgnoreCIDR and whitelists.
These rules must not define any matchzone (mz:), otherwise NGINX fails to start; they are reported in the logs with the zone VAR, the variable name (without $) as var_name and, in the extended logs, the variable value as content.
ℹ️ Info
VARis only the zone reported in the logs, it is not a valid matchzone:"mz:VAR"is refused by Naxsi in any rule or whitelist.
📣 Important
These rules do not count as loaded rules for the internal rule
19: the location still requires the usual rules (for examplenaxsi_core.rules), otherwise all the requests are dropped.
Example: allow only the clients from United States and Uruguay, by using the geoip2 module:
http {
geoip2 /etc/nginx/dbip-city-lite.mmdb {
$geoip2_country_code source=$remote_addr country iso_code;
}
map $geoip2_country_code $blocked_country {
US 0;
UY 0;
default 1;
}
include /etc/nginx/naxsi_core.rules;
MainRule id:4000001 "s:$GEO:8" "if:$blocked_country" "msg:country not allowed";
server {
listen 80;
location / {
SecRulesEnabled;
DeniedUrl "/RequestDenied";
CheckRule "$GEO >= 8" BLOCK;
# ... other CheckRule ...
proxy_pass http://127.0.0.1:8080;
}
location /RequestDenied {
internal;
return 403;
}
}
}
A blocked request is logged as follows:
NAXSI_FMT: ip=1.2.3.4&server=example.com&uri=%2F&config=block&rid=4f9b0d3a6c8e2b1f7a5d9c0e3b6a8f21&cscore0=$GEO&score0=8&zone0=VAR&id0=4000001&var_name0=blocked_country
💡 Tip
Since the condition only increases a score, it can be combined with other rules using the same score; for example, to block the access to the login page only for the clients outside the allowed countries:
MainRule id:4000001 "s:$GEO_LOGIN:4" "if:$blocked_country" "msg:country not allowed"; MainRule id:4000002 "s:$GEO_LOGIN:4" "str:/wp-login.php" "mz:URL" "msg:login page"; # location block CheckRule "$GEO_LOGIN >= 8" BLOCK;
A rule with a condition and a search pattern is applied only when the condition holds; otherwise the rule is skipped.
Example: search for PHP code only in the body of the requests which can upload data:
map $request_method $is_write {
POST 1;
PUT 1;
PATCH 1;
default 0;
}
MainRule id:4000010 "s:$UPLOAD:8" "rx:<\?php" "mz:BODY" "if:$is_write" "msg:php code upload";
A whitelist with a condition is applied only when the condition holds; this works with any whitelist, with or without a matchzone.
Example: lift some rules only for the office network, by using the geo module:
http {
geo $office {
default 0;
10.20.0.0/16 1;
}
server {
location /admin/ {
SecRulesEnabled;
DeniedUrl "/RequestDenied";
# ... CheckRule ...
# the office can send HTML within the "content" argument
BasicRule wl:1302,1303 "mz:$ARGS_VAR:content" "if:$office";
# the office is not subject to the country restrictions
BasicRule wl:4000001 "if:$office";
}
}
}
ℹ️ Info
Unlike
IgnoreIPandIgnoreCIDR, which never block the client but still apply all the rules, a conditional whitelist removes only the specified rules (and only in the specified matchzone).
📣 Important
The rules without a search pattern (zone
VAR) are not bound to any matchzone, thus they can be whitelisted only by whitelists without matchzone (likeBasicRule wl:4000001;); use thelocationblocks (or a condition) to restrict where the whitelist applies.
The whitelist logs report the configuration file and line of the conditional whitelist that was applied.