Skip to content

Use a WAF version of Nginx

By default, the image provided in the NGX Ops stack is based on a standard Nginx image.

For environments that need Web Application Firewall (WAF) features, two variants of the Nginx image are also available:

  • ModSecurity 3
  • Coraza

Both solutions integrate a WAF engine directly into Nginx and notably allow using the OWASP Core Rule Set (CRS) rules.

Warning: these two images are meant for specific needs. Using them is not required to use the security and analysis features of NGX Ops.

ModSecurity

ModSecurity is one of the historical WAF engines most used with Nginx.

The version provided by NGX Ops is based on ModSecurity 3 and notably allows using the OWASP Core Rule Set to detect different categories of attacks:

  • SQL Injection;
  • Cross-Site Scripting (XSS);
  • Local/Remote File Inclusion;
  • directory traversal attacks;
  • abnormal HTTP requests;
  • and more generally the behaviors covered by the CRS rules.

ModSecurity runs directly in the Nginx process and can therefore block or log requests before they reach the application.

In the configuration provided by NGX Ops, ModSecurity is initially set to DetectionOnly mode to prevent a poorly tuned WAF rule from immediately blocking an application.

Coraza

Coraza is a modern implementation of a WAF engine compatible with ModSecurity rules and the OWASP Core Rule Set.

Unlike ModSecurity, Coraza is developed in Go. It therefore allows using the same rule principle while relying on a different implementation.

The Nginx image provided by NGX Ops integrates Coraza directly into Nginx and notably allows using the OWASP CRS rules.

This integration remains experimental in the context of NGX Ops, however. Differences may exist between Coraza's behavior and ModSecurity's, notably when validating or loading rules.

Docker images

The sources of the Nginx images are available in the repository:

Nginx image sources

The published Docker images are available in the registry:

Nginx Reverse Proxy Docker images


Integration in NGX Ops

NGX Ops automatically detects when an Nginx image that includes ModSecurity or Coraza is used.

When one of these images is detected, a WAF entry is added to the interface menu.

This interface notably gives access to the logs generated by the WAF engine so they can be analyzed.

Important: to be used by NGX Ops, WAF logs must be generated in JSON format.

This approach keeps the analysis features of NGX Ops while leaving the WAF engine responsible for analyzing requests.


Enable the WAF in Nginx

By default, ModSecurity and Coraza are set to DetectionOnly mode.

The engine is also disabled at the VHost level so that no blocking is done unintentionally.

To enable the WAF on a VHost, add the corresponding configuration in the server { ... } block.

ModSecurity

modsecurity on;

modsecurity_rules_file /etc/nginx/modsec/main.conf;

modsecurity_rules '

    # Switch this VHost to detection-only mode
    # and do not block requests.
    SecRuleEngine DetectionOnly

    SecAuditLogParts ABIJDEFHZ

    # Enable the audit log for this VHost
    SecAuditEngine RelevantOnly

    # Log 5xx responses and 4xx responses
    # except 404.
    SecAuditLogRelevantStatus "^(?:5|(?:4(?!04)))"

    # WAF log location
    SecAuditLog /var/log/nginx/<vhost-name>.waf.log

    SecAuditLogType Serial
    SecAuditLogFormat JSON

';

To switch the WAF to blocking mode, you can replace:

SecRuleEngine DetectionOnly

with:

SecRuleEngine On

It is however recommended to start with DetectionOnly mode to identify possible false positives before enabling blocking.

Coraza

The Coraza configuration is similar:

coraza on;

coraza_rules_file /etc/nginx/coraza/coraza-main.conf;

coraza_rules '

    # Switch this VHost to detection-only mode
    # and do not block requests.
    SecRuleEngine DetectionOnly

    SecAuditLogParts ABIJDEFHZ

    # Enable the audit log for this VHost
    SecAuditEngine RelevantOnly

    # Log 5xx responses and 4xx responses
    # except 404.
    SecAuditLogRelevantStatus "^(?:5|(?:4(?!04)))"

    # WAF log location
    SecAuditLog /var/log/nginx/<vhost-name>.waf.log

    SecAuditLogType Serial
    SecAuditLogFormat JSON

';

Limitations and feedback

These integrations are functional, but have several limitations that are important to know before using them in production.

ModSecurity: memory consumption

The ModSecurity integration works correctly with Nginx and currently offers the most mature integration.

The main negative point observed, however, concerns memory consumption.

Two elements deserve particular attention:

  • the WAF configuration files are loaded independently for each VHost;
  • during Nginx reloads (nginx -s reload), old workers can stay in memory for some time.

On a configuration with several VHosts using the WAF, memory consumption can therefore increase gradually after several reloads.

In this case, a periodic restart of the Nginx container may be needed to fully free the memory used by the old workers.

Coraza: experimental integration

The Coraza integration is currently considered experimental.

Since Coraza is developed in Go and integrated into Nginx through an architecture different from ModSecurity's, some behaviors may differ.

A particularly important point concerns configuration validation.

It is possible that an:

nginx -t

is valid while a Coraza rule ultimately has an error when loaded or executed.

In some cases, this can cause unexpected Nginx behavior, or even prevent it from starting properly.

Recommendation: if you use Coraza in production, systematically test your rules in a pre-production environment before deploying them.


Managing exclusions with ModSecurity

Using the OWASP Core Rule Set generally requires setting up exclusions for some applications.

This is notably the case with complex applications such as Nextcloud, which use HTTP methods and parameters that can trigger some CRS rules.

Managing exclusions can quickly become complex.

Exclusions must be placed in the file:

REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf

For example:

# ==================================================================
# EXCLUSION 1: Nextcloud WebDAV
# ==================================================================

SecAction "id:1000000,phase:1,nolog,pass,setvar:'tx.allowed_methods=GET HEAD POST OPTIONS PROPFIND PROPPATCH MKCOL COPY MOVE LOCK UNLOCK REPORT DELETE PUT'"

# ==================================================================
# EXCLUSION 2
# ==================================================================

# Fix SQLi 942550 on JSON search

SecRuleUpdateTargetById 942550 "!ARGS:json.query.Url"

SecRuleUpdateTargetById 942550 "!ARGS:json.searchQuery.Url"

SecRuleUpdateTargetById 942550 "!REQUEST_COOKIES:/^ErmesSearch_/"

# Fix Content-Type 920420 for the /pro/ back office

SecRule REQUEST_URI "@beginsWith /my-folder/" \
    "id:1000002,\
    phase:1,\
    pass,\
    nolog,\
    ctl:ruleRemoveById=920420"

You can also make exclusions conditional on the server_name.

For example:

# ==================================================================
# 1. Nextcloud EXCLUSIONS
# ==================================================================

SecRule REQUEST_HEADERS:Host "@streq nextcloud.domain.tld" \
    "id:1000001,\
    phase:1,\
    pass,\
    nolog,\
    setvar:'tx.allowed_methods=GET HEAD POST OPTIONS PROPFIND PROPPATCH MKCOL COPY MOVE LOCK UNLOCK REPORT DELETE PUT'"

# ==================================================================
# 2. subdom.domaine.tld EXCLUSIONS
# ==================================================================

# Fix SQLi 942550

SecRule REQUEST_HEADERS:Host "@streq subdom.domaine.tld" \
    "id:1000002,\
    phase:1,\
    pass,\
    nolog,\
    ctl:ruleRemoveTargetById=942550;ARGS:json.query.Url,\
    ctl:ruleRemoveTargetById=942550;ARGS:json.searchQuery.Url,\
    ctl:ruleRemoveTargetById=942550;REQUEST_COOKIES:/^ErmesSearch_/"

# Fix Content-Type 920420

SecRule REQUEST_HEADERS:Host "@streq subdom.domaine.tld" \
    "id:1000003,\
    phase:1,\
    chain,\
    pass,\
    nolog"

    SecRule REQUEST_URI "@beginsWith /my-folder/" \
        "ctl:ruleRemoveById=920420"

The file must then be mounted in the Nginx container.

For example with Docker Compose:

services:
  nginx:
    ...

    volumes:
      - ./ms-rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf:/etc/nginx/modsec/coreruleset/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf

A container restart is then needed to make sure the configuration is taken into account.

Tip: before switching the WAF to blocking mode, it is strongly recommended to analyze the generated alerts and set up the necessary exclusions.


Integration with NGX Ops Intelligence

Alerts generated directly by ModSecurity or Coraza are currently not sent to NGX Ops Intelligence.

The native WAF integration therefore mainly lets you view and analyze logs from the NGX Ops interface.

If you want centralized correlation of WAF events with NGX Ops Intelligence, another approach is available.


Alternative: the NGX Ops emulated WAF

NGX Ops also offers a different approach with an emulated WAF.

This solution does not rely on a WAF module running directly in Nginx.

It uses a specific agent, deployed directly from NGX Ops, which analyzes the HTTP logs generated by Nginx.

The agent, developed in Go, embeds Coraza as well as the OWASP Core Rule Set rules.

It therefore works differently:

Client
   |
   v
Nginx
   |
   +----> Application
   |
   +----> HTTP logs
             |
             v
       NGX Ops Agent
             |
             v
          Coraza
             |
             v
       WAF analysis
             |
             +----> Blocklist
             |
             +----> HTTP challenge
             |
             +----> NGX Ops Intelligence

Configuration and remediation actions are then managed directly from NGX Ops.

This approach has several advantages:

  • no change to the Nginx binary;
  • no WAF rule loaded directly by Nginx;
  • no Nginx configuration change needed when adding a rule;
  • quick pick-up of changes;
  • ability to use NGX Ops remediation mechanisms;
  • alerts sent to NGX Ops Intelligence;
  • ability to correlate events with the other security mechanisms.

Emulated WAF limitations

This approach has an important limitation, however: the agent works from Nginx logs.

It therefore does not necessarily have the whole HTTP request.

Some rules therefore cannot be analyzed properly, notably those that require access to the full content of a POST request.

Another important difference concerns when blocking happens.

Since the analysis occurs after Nginx has processed the request, the first request from an unknown IP address can reach the target.

If the analysis then triggers remediation, subsequent requests can be blocked or submitted to a challenge.

The emulated WAF must therefore be considered a behavioral detection and remediation mechanism, not a strict replacement for a WAF running at the HTTP request level.


Which solution to choose?

Solution Analysis level Immediate blocking Consumption NGX Ops Intelligence Maturity
ModSecurity HTTP request Yes High No High
Coraza Nginx HTTP request Yes To assess No Experimental
NGX Ops emulated WAF Nginx logs After detection Low Yes Built into NGX Ops

For WAF protection directly integrated into Nginx, ModSecurity remains the most mature choice today.

For an approach more oriented toward detection, correlation and remediation, the NGX Ops emulated WAF is generally more interesting, notably thanks to its integration with NGX Ops Intelligence, the blocklists and the HTTP challenge mechanisms.