naxsi

Naxsi Conditions

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 rewrite phase, before the set, if, rewrite and return directives defined in the location block, 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 that cannot be used (they are always empty when Naxsi inspects the request):

When unsure, test the condition with a rule in learning mode and check the logs.

Syntax

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

Conditions can be used in three ways:

  1. Rules without a search pattern: the rule matches only on the condition.
  2. Rules with a search pattern: the rule is applied only when the condition holds.
  3. Whitelists: the whitelist is applied only when the condition holds.

Rules without a search pattern

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

VAR is 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 example naxsi_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;

Rules with a search pattern

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";

Whitelists

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 IgnoreIP and IgnoreCIDR, 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 (like BasicRule wl:4000001;); use the location blocks (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.

Go Back

Table of Contents.