Giter VIP home page Giter VIP logo

lua-resty-waf's Introduction

Name

lua-resty-waf - High-performance WAF built on the OpenResty stack

Table of Contents

Status

Build Status Codewake CII Best Practices

NOTE: lua-resty-waf is essentially abandoned. This project had a use in a time when ModSecurity for Nginx was not a viable option; this is no longer the case. There was an attempt to revitalize the project in 2020, but I do not have the resources to complete this; this work is partially complete in the redux branch.

Description

lua-resty-waf is a reverse proxy WAF built using the OpenResty stack. It uses the Nginx Lua API to analyze HTTP request information and process against a flexible rule structure. lua-resty-waf is distributed with a ruleset that mimics the ModSecurity CRS, as well as a few custom rules built during initial development and testing, and a small virtual patchset for emerging threats. Additionally, lua-resty-waf is distributed with tooling to automatically translate existing ModSecurity rules, allowing users to extend lua-resty-waf implementation without the need to learn a new rule syntax.

lua-resty-waf was initially developed by Robert Paprocki for his Master's thesis at Western Governor's University.

Requirements

lua-resty-waf requires several third-party resty lua modules, though these are all packaged with lua-resty-waf, and thus do not need to be installed separately. It is recommended to install lua-resty-waf on a system running the OpenResty software bundle; lua-resty-waf has not been tested on platforms built using separate Nginx source and Nginx Lua module packages.

For optimal regex compilation performance, it is recommended to build Nginx/OpenResty with a version of PCRE that supports JIT compilation. If your OS does not provide this, you can build JIT-capable PCRE directly into your Nginx/OpenResty build. To do this, reference the path to the PCRE source in the --with-pcre configure flag. For example:

# ./configure --with-pcre=/path/to/pcre/source --with-pcre-jit

You can download the PCRE source from the PCRE website. See also this blog post for a step-by-step walkthrough on building OpenResty with a JIT-enabled PCRE library.

Performance

lua-resty-waf was designed with efficiency and scalability in mind. It leverages Nginx's asynchronous processing model and an efficient design to process each transaction as quickly as possible. Load testing has show that deployments implementing all provided rulesets, which are designed to mimic the logic behind the ModSecurity CRS, process transactions in roughly 300-500 microseconds per request; this equals the performance advertised by Cloudflare's WAF. Tests were run on a reasonable hardware stack (E3-1230 CPU, 32 GB RAM, 2 x 840 EVO in RAID 0), maxing at roughly 15,000 requests per second. See this blog post for more information.

lua-resty-waf workload is almost exclusively CPU bound. Memory footprint in the Lua VM (excluding persistent storage backed by lua-shared-dict) is roughly 2MB.

Installation

A simple Makefile is provided:

# make && sudo make install

Alternatively, install via Luarocks:

# luarocks install lua-resty-waf

lua-resty-waf makes use of the OPM package manager, available in modern OpenResty distributions. The client OPM tools requires that the resty command line tool is available in your system's PATH environmental variable.

Note that by default lua-resty-waf runs in SIMULATE mode, to prevent immediately affecting an application; users who wish to enable rule actions must explicitly set the operational mode to ACTIVE.

Synopsis

http {
    init_by_lua_block {
        -- use resty.core for performance improvement, see the status note above
        require "resty.core"

        -- require the base module
        local lua_resty_waf = require "resty.waf"

        -- perform some preloading and optimization
        lua_resty_waf.init()
    }

    server {
        location / {
            access_by_lua_block {
                local lua_resty_waf = require "resty.waf"

                local waf = lua_resty_waf:new()

                -- define options that will be inherited across all scopes
                waf:set_option("debug", true)
                waf:set_option("mode", "ACTIVE")

                -- this may be desirable for low-traffic or testing sites
                -- by default, event logs are not written until the buffer is full
                -- for testing, flush the log buffer every 5 seconds
                --
                -- this is only necessary when configuring a remote TCP/UDP
                -- socket server for event logs. otherwise, this is ignored
                waf:set_option("event_log_periodic_flush", 5)

                -- run the firewall
                waf:exec()
            }

            header_filter_by_lua_block {
                local lua_resty_waf = require "resty.waf"

                -- note that options set in previous handlers (in the same scope)
                -- do not need to be set again
                local waf = lua_resty_waf:new()

                waf:exec()
            }

            body_filter_by_lua_block {
                local lua_resty_waf = require "resty.waf"

                local waf = lua_resty_waf:new()

                waf:exec()
            }

            log_by_lua_block {
                local lua_resty_waf = require "resty.waf"

                local waf = lua_resty_waf:new()

                waf:exec()
            }
        }
    }
}

Public Functions

lua-resty-waf.load_secrules()

Translate and initialize a ModSecurity SecRules file from disk. Note that this still requires the ruleset to be added via add_ruleset (the basename of the file must be given as the key).

Example:

http {
    init_by_lua_block {
        local lua_resty_waf = require "resty.waf"

        -- this translates and calculates a ruleset called 'ruleset_name'
        local ok, errs = pcall(function()
            lua_resty_waf.load_secrules("/path/to/secrules/ruleset_name")
        end)

        -- errs is an array-like table
        if errs then
            for i = 1, #errs do
                ngx.log(ngx.ERR, errs[i])
            end
        end
    }

    server {
        location / {
            access_by_lua_block {
                local lua_resty_waf = require "resty.waf"

                local waf = lua_resty_waf:new()

                -- in order to use the loaded ruleset, it must be added via
                -- the 'add_ruleset' option
                waf:set_option("add_ruleset", "ruleset_name")
            }
        }
    }
}

Additionally, load_secrules can take an optional second argument as a table of options to pass to various translation functions. The following options are recognized:

  • path: Define a filesystem path to search for data files for operators such as @pmFromFile. If no such key is defined, the current working directory (.) is used
  • force: Do not error and abort when failing to translate a rule variable
  • loose: Do not error and abort when failing to translate a rule action
  • quiet: Do not error or warn when failing to translate a rule action

This function can also take a third option as a table to catch translation errors, for later processing. If this option is not present or a not a table, translation errors will instead be logged to the error log.

lua-resty-waf.init()

Perform some pre-computation of rules and rulesets, based on what's been made available via the default distributed rulesets. It's recommended, but not required, to call this function (not doing so will result in a small performance penalty). This function should never be called outside this scope.

Example:

http {
    init_by_lua_block {
        local lua_resty_waf = require "resty.waf"

        lua_resty_waf.init()
    }
}

Public Methods

lua-resty-waf:new()

Instantiate a new instance of lua-resty-waf. You must call this in every request handler phase you wish to run lua-resty-waf, and use the return result to call further object methods.

Example:

location / {
    access_by_lua_block {
        local lua_resty_waf = require "resty.waf"

        local waf = lua_resty_waf:new()
    }
}

lua-resty-waf:set_option()

Configure an option on a per-scope basis.

Example:

location / {
    access_by_lua_block {
        local lua_resty_waf = require "resty.waf"

        local waf = lua_resty_waf:new()

        -- enable debug logging only for this scope
        waf:set_option("debug", true)
    }
}

lua-resty-waf:set_var()

Define a transaction variable (stored in the TX variable collection) before executing the WAF. This can be used to define variables used by complex rulesets such as the OWASP CRS.

Example:

location / {
    access_by_lua_block {
        local lua_resty_waf = require "resty.waf"

        local waf = lua_resty_waf:new()

        waf:set_var("FOO", "bar")
    }
}

Note that as with any other ModSecurity rule, the existence of a variable bears no functional change to WAF processing; it is the responsibility of the rule author to understand and use TX variables.

lua-resty-waf:sieve_rule()

Define a collection exclusion for a given rule.

Example:

location / {
    access_by_lua_block {
        local lua_resty_waf = require "resty.waf"

        local waf = lua_resty_waf:new()

        local sieves = {
            {
                type   = "ARGS",
                elts   = "foo",
                action = "ignore",
            }
        }

        waf:sieve_rule("12345", sieves)
    }
}

See the rule sieves wiki page for details and advanced usage examples.

lua-resty-waf:exec()

Run the rule engine. By default, the engine is executed according to the currently running phase. An optional table may be passed, allowing users to "mock" execution of a different phase.

Example:

location / {
    access_by_lua_block {
        local lua_resty_waf = require "resty.waf"

        local waf = lua_resty_waf:new()

        -- execute according to access phase collections and rules
        waf:exec()
    }

    content_by_lua_block {
        local lua_resty_waf = require "waf"

        local waf = lua_resty_waf:new()

        -- execute header_filter rules, passing in a table of additional collections
        -- this assumes the 'request_headers' and 'status' Lua variables were
        -- declared and initialized elsewhere
        local opts = {
            phase = 'header_filter',
            collections = {
                REQUEST_HEADERS = request_headers,
                STATUS = status,
            }
        }

        waf:exec(opts)
    }
}

lua-resty-waf:write_log_events()

Write any audit log entries that were generated from the transaction. This is only optional when exec is called in a log_by_lua handler.

Example:

location / {
    log_by_lua_block {
        local lua_resty_waf = require "resty.waf"

        local waf = lua_resty_waf:new()

        -- write out any event log entries to the
        -- configured target, if applicable
        waf:write_log_events()
    }
}

Options

add_ruleset

Default: none

Adds an additional ruleset to be used during processing. This allows users to implement custom rulesets without stomping over the included rules directory. Additional rulesets must reside within a folder called "rules" that lives within the lua_package_path.

Example:

http {
    -- the rule file 50000.json must live at
    -- /path/to/extra/rulesets/rules/50000.json
    lua_package_path '/path/to/extra/rulesets/?.lua;;';

    server {
        location / {
            access_by_lua_block {
                waf:set_option("add_ruleset", "50000_extra_rules")
            }
        }
    }
}

Multiple rulesets may be added by passing a table of values to set_option. Note that ruleset names are sorted before processing. Rulesets are processed in a low-to-high sorted order.

add_ruleset_string

Default: none

Adds an additional ruleset to be used during processing. This allows users to implement custom rulesets without stomping over the included rules directory. Rulesets are defined inline as a Lua string, in the form of a translated ruleset JSON structure.

Example:

location / {
    access_by_lua_block {
        waf:set_option("add_ruleset_string", "70000_extra_rules", [=[{"access":[{"action":"DENY","id":73,"operator":"REGEX","opts":{},"pattern":"foo","vars":[{"parse":{"values":1},"type":"REQUEST_ARGS"}]}],"body_filter":[],"header_filter":[]}]=])
    }
}

Note that ruleset names are sorted before processing, and must be given as strings. Rulesets are processed in a low-to-high sorted order.

allow_unknown_content_types

Default: false

Instructs lua-resty-waf to continue processing the request when a Content-Type header has been sent that is not in the allowed_content_types table. Such requests will not have their request body processed by lua-resty-waf (the REQUEST_BODY collection will be nil). In this manner, users do not need to explicitly whitelist all possible Content-Type headers they may encounter.

Example:

location / {
    access_by_lua_block {
        waf:set_option("allow_unknown_content_types", true)
    }
}

allowed_content_types

Default: none

Defines one or more Content-Type headers that will be allowed, in addition to the default Content-Types application/x-www-form-urlencoded and multipart/form-data. A request whose content type matches one of allowed_content_types will set the REQUEST_BODY collection to a single string containing (rather than a table); a request whose content type does not match one of these values, or application/x-www-form-urlencoded or multipart/form-data, will be rejected.

Example:

location / {
    access_by_lua_block {
        -- define a single allowed Content-Type value
        waf:set_option("allowed_content_types", "text/xml")

        -- defines multiple allowed Content-Type values
        waf:set_option("allowed_content_types", { "text/html", "text/json", "application/json" })
    }
}

Note that mutiple set_option calls with a parameter of allowed_content_types will simply override the existing options table, so if you want to define multiple allowed content types, you must define them as a Lua table as shown above.

debug

Default: false

Disables/enables debug logging. Debug log statements are printed to the error_log. Note that debug logging is very expensive and should not be used in production environments.

Example:

location / {
    access_by_lua_block {
        waf:set_option("debug", true)
    }
}

debug_log_level

Default: ngx.INFO

Sets the nginx log level constant used for debug logging.

Example:

location / {
    access_by_lua_block {
        waf:set_option("debug_log_level", ngx.DEBUG)
    }
}

deny_status

Default: ngx.HTTP_FORBIDDEN

Sets the status to use when denying requests.

Example:

location / {
    access_by_lua_block {
        waf:set_option("deny_status", ngx.HTTP_NOT_FOUND)
    }
}

disable_pcre_optimization

Default: false

Removes the oj flags from all ngx.re.match, ngx.re.find, and ngx.re.sub calls. This may be useful in some cases where older PCRE libraries are used, but will cause severe performance degradation, so its use is strongly discouraged; users are instead encouraged to build OpenResty with a modern, JIT-capable PCRE library.

Example:

location / {
    access_by_lua_block {
        waf:set_option("disable_pcre_optimization", true)
    }
}

Note: This behavior is deprecated and will be removed in future versions.

event_log_altered_only

Default: true

Determines whether to write log entries for rule matches in a transaction that was not altered by lua-resty-waf. "Altered" is defined as lua-resty-waf acting on a rule whose action is ACCEPT or DENY. When this option is unset, lua-resty-waf will log rule matches even if the transaction was not altered. By default, lua-resty-waf will only write log entries for matches if the transaction was altered.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_altered_only", false)
    }
}

Note that mode will not have an effect on determing whether a transaction is considered altered. That is, if a rule with a DENY action is matched, but lua-resty-waf is running in SIMULATE mode, the transaction will still be considered altered, and rule matches will be logged.

event_log_buffer_size

Default: 4096

Defines the threshold size, in bytes, of the buffer to be used to hold event logs. The buffer will be flushed when this threshold is met.

Example:

location / {
    access_by_lua_block {
        -- 8 KB event log message buffer
        waf:set_option("event_log_buffer_size", 8192)
    }
}

event_log_level

Default: ngx.INFO

Sets the nginx log level constant used for event logging.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_level", ngx.WARN)
    }
}

event_log_ngx_vars

Default: empty

Defines what extra variables from ngx.var are put to the log event. This is a generic way to extend the alert with extra context. The variable name will be the key of the entry under an ngx key in the log entry. If the variable is not present as an nginx variable, no item is added to the event.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_ngx_vars", "host")
        waf:set_option("event_log_ngx_vars", "request_id")
    }
}

The resulting event has these extra items:

{
"ngx": {
    "host": "example.com",
    "request_id": "373bcce584e3c18a"
}
}

event_log_periodic_flush

Default: none

Defines an interval, in seconds, at which the event log buffer will periodically flush. If no value is configured, the buffer will not flush periodically, and will only flush when the event_log_buffer_size threshold is reached. Configure this option for very low traffic sites that may not receive any event log data in a long period of time, to prevent stale data from sitting in the buffer.

Example:

location / {
    access_by_lua_block {
        -- flush the event log buffer every 30 seconds
        waf:set_option("event_log_periodic_flush", 30)
    }
}

event_log_request_arguments

Default: false

When set to true, the log entries contain the request arguments under the uri_args key.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_request_arguments", true)
    }
}

event_log_request_body

Default: false

When set to true, the log entries contain the request body under the request_body key.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_request_body", true)
    }
}

event_log_request_headers

Default: false

The headers of the HTTP request is copied to the log event, under the request_headers key.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_request_headers", true)
    }
}

The resulting event has these extra items:

{
"request_headers": {
    "accept": "*/*",
    "user-agent": "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3"
}
}

event_log_ssl

Default: false

Enable SSL connections when logging via TCP/UDP.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_ssl", true)
    }
}

event_log_ssl_sni_host

Default: none

Set the SNI host for lua-resty-logger-socket connections.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_ssl_sni_host", "loghost.example.com")
    }
}

event_log_ssl_verify

Default: false

Enable certification verification for SSL connections when logging via TCP/UDP.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_ssl_verify", true)
    }
}

event_log_socket_proto

Default: udp

Defines which IP protocol to use (TCP or UDP) when shipping event logs via a remote socket. The same buffering and recurring flush logic will be used regardless of protocol.

Example:

location / {
    access_by_lua_block {
        -- send logs via TCP
        waf:set_option("event_log_socket_proto", "tcp")
    }
}

event_log_target

Default: error

Defines the destination for event logs. lua-resty-waf currently supports logging to the error log, a separate file on the local file system, or a remote TCP or UDP server. In the latter two cases, event logs are buffered and flushed when a defined threshold is reached (see below for further options regarding event logging options).

Example:

location / {
    access_by_lua_block {
        -- send event logs to the server's error_log location (default)
        waf:set_option("event_log_target", "error")

        -- send event logs to a local file on disk
        waf:set_option("event_log_target", "file")

        -- send event logs to a remote server
        waf:set_option("event_log_target", "socket")
    }
}

Note that, due to a limition in the logging library used, only a single target socket can be defined. This is to say, you may only configure one socket target with a specific host/port combination; if you configure a second host/port combination, data will not be properly logged.

event_log_target_host

Default: none

Defines the target server for event logs that target a remote server.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_target_host", "10.10.10.10")
    }
}

event_log_target_path

Default: none

Defines the target path for event logs that target a local file system location.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_target_path", "/var/log/lua-resty-waf/event.log")
    }
}

This path must be in a location writeable by the nginx user. Note that, by nature, on-disk logging can cause significant performance degredation in high-concurrency environments.

event_log_target_port

Default: none

Defines the target port for event logs that target a remote server.

Example:

location / {
    access_by_lua_block {
        waf:set_option("event_log_target_port", 9001)
    }
}

hook_action

Default: none

Override the functionality of actions taken when a rule is matched. See the example for more details

Example:

    location / {
        access_by_lua_block {
            local deny_override = function(waf, ctx)
                ngx.log(ngx.INFO, "Overriding DENY action")
                ngx.status = 404
            end

            -- override the DENY action with the function defined above
            waf:set_option("hook_action", "DENY", deny_override)
        }
    }

ignore_rule

Default: none

Instructs the module to ignore a specified rule ID. Note that ignoring a rule in a chain will result in the entire chain being ignored, and processing will continue to the next rule following the chain.

Example:

location / {
    access_by_lua_block {
        waf:set_option("ignore_rule", 40294)
        waf:set_option("ignore_rule", {40002, 41036})
    }
}

Multiple rules can be ignored by passing a table of rule IDs to set_option.

ignore_ruleset

Default: none

Instructs the module to ignore an entire ruleset. This can be useful when some rulesets (such as the SQLi or XSS CRS rulesets) are too prone to false positives, or aren't applicable to your application.

Example:

location / {
    access_by_lua_block {
        waf:set_option("ignore_ruleset", "41000_sqli")
    }
}

mode

Default: SIMULATE

Sets the operational mode of the module. Options are ACTIVE, INACTIVE, and SIMULATE. In ACTIVE mode, rule matches are logged and actions are run. In SIMULATE mode, lua-resty-waf loops through each enabled rule and logs rule matches, but does not complete the action specified in a given run. INACTIVE mode prevents the module from running.

By default, SIMULATE is selected if a mode is not explicitly set; this requires new users to actively implement blocking by setting the mode to ACTIVE.

Example:

location / {
    access_by_lua_block {
        waf:set_option("mode", "ACTIVE")
    }
}

nameservers

Default: none

Sets the DNS resolver(s) to be used for RBL lookups. Currently only UDP/53 traffic is supported. This option must be defined as a numeric address, not a hostname. If this option is not defined, all RBL lookup rules will return false.

Example:

location / {
    access_by_lua_block {
        waf:set_option("nameservers", "10.10.10.10")
    }
}

process_multipart_body

Default true

Enable processing of multipart/form-data request bodies (when present), using the lua-resty-upload module. In the future, lua-resty-waf may use this processing to perform stricter checking of upload bodies; for now this module performs only minimal sanity checks on the request body, and will not log an event if the request body is invalid. Disable this option if you do not need this checking, or if bugs in the upstream module are causing problems with HTTP uploads.

Example:

location / {
    access_by_lua_block {
        -- disable processing of multipart/form-data requests
        -- note that the request body will still be sent to the upstream
        waf:set_option("process_multipart_body", false)
    }
}

req_tid_header

Default: false

Set an HTTP header X-Lua-Resty-WAF-ID in the upstream request, with the value as the transaction ID. This ID will correlate with the transaction ID present in the debug logs (if set). This can be useful for request tracking or debug purposes.

Example:

location / {
    access_by_lua_block {
        waf:set_option("req_tid_header", true)
    }
}

res_body_max_size

Default: 1048576 (1 MB)

Defines the content length threshold beyond which response bodies will not be processed. This size of the response body is determined by the Content-Length response header. If this header does not exist in the response, the response body will never be processed.

Example:

location / {
    access_by_lua_block {
        -- increase the max response size to 2 MB
        waf:set_option("res_body_max_size", 1024 * 1024 * 2)
    }
}

Note that by nature, it is required to buffer the entire response body in order to properly use the response as a collection, so increasing this number significantly is not recommended without justification (and ample server resources).

res_body_mime_types

Default: "text/plain", "text/html"

Defines the MIME types with which lua-resty-waf will process the response body. This value is determined by the Content-Type header. If this header does not exist, or the response type is not in this list, the response body will not be processed. Setting this option will add the given MIME type to the existing defaults of text/plain and text/html.

Example:

location / {
    access_by_lua_block {
        -- mime types that will be processed are now text/plain, text/html, and text/json
        waf:set_option("res_body_mime_types", "text/json")
    }
}

Multiple MIME types can be added by passing a table of types to set_option.

res_tid_header

Default: false

Set an HTTP header X-Lua-Resty-WAF-ID in the downstream response, with the value as the transaction ID. This ID will correlate with the transaction ID present in the debug logs (if set). This can be useful for request tracking or debug purposes.

Example:

location / {
    access_by_lua_block {
        waf:set_option("res_tid_header", true)
    }
}

score_threshold

Default: 5

Sets the threshold for anomaly scoring. When the threshold is reached, lua-resty-waf will deny the request.

Example:

location / {
    access_by_lua_block {
        waf:set_option("score_threshold", 10)
    }
}

storage_backend

Default: dict

Define an engine to use for persistent variable storage. Current available options are dict (ngx_lua shared memory zone), memcached, amd redis.

Example:

location / {
    acccess_by_lua_block {
        waf:set_option("storage_backend", "memcached")
    }
}

storage_keepalive

Default: true

Enable or disable TCP keepalive for connections to remote persistent storage hosts.

Example:

location / {
    acccess_by_lua_block {
        waf:set_option("storage_keepalive", false)
    }
}

storage_keepalive_timeout

Default: 10000

Configure (in milliseconds) the timeout for the cosocket keepalive pool for remote persistent storage hosts.

Example:

location / {
    acccess_by_lua_block {
        waf:set_option("storage_keepalive_timeout", 30000)
    }
}

storage_keepalive_pool_size

Default: 100

Configure the pool size for the cosocket keepalive pool for remote persistent storage hosts.

Example:

location / {
    acccess_by_lua_block {
        waf:set_option("storage_keepalive_pool_size", 50)
    }
}

storage_memcached_host

Default: 127.0.0.1

Define a host to use when using memcached as a persistent variable storage engine.

Example:

location / {
    acccess_by_lua_block {
        waf:set_option("storage_memcached_host", "10.10.10.10")
    }
}

storage_memcached_port

Default: 11211

Define a port to use when using memcached as a persistent variable storage engine.

Example:

location / {
    acccess_by_lua_block {
        waf:set_option("storage_memcached_port", 11221)
    }
}

storage_redis_host

Default: 127.0.0.1

Define a host to use when using redis as a persistent variable storage engine.

Example:

location / {
    acccess_by_lua_block {
        waf:set_option("storage_redis_host", "10.10.10.10")
    }
}

storage_redis_port

Default: 6379

Define a port to use when using redis as a persistent variable storage engine.

Example:

location / {
    acccess_by_lua_block {
        waf:set_option("storage_redis_port", 6397)
    }
}

storage_zone

Default: none

Defines the lua_shared_dict that will be used to hold persistent storage data. This zone must be defined in the http{} block of the configuration.

Example:_

http {
    -- define a 64M shared memory zone to hold persistent storage data
    lua_shared_dict persistent_storage 64m;
}

location / {
    access_by_lua_block {
        waf:set_option("storage_zone", "persistent_storage")
    }
}

Multiple shared zones can be defined and used, though only one zone can be defined per configuration location. If a zone becomes full and the shared dictionary interface cannot add additional keys, the following will be entered into the error log:

Error adding key to persistent storage, increase the size of the lua_shared_dict

Phase Handling

lua-resty-waf is designed to run in multiple phases of the request lifecycle. Rules can be processed in the following phases:

  • access: Request information, such as URI, request headers, URI args, and request body are available in this phase.
  • header_filter: Response headers and HTTP status are available in this phase.
  • body_filter: Response body is available in this phase.
  • log: Event logs are automatically written at the completion of this phase.

These phases correspond to their appropriate Nginx lua handlers (access_by_lua, header_filter_by_lua, body_filter_by_lua, and log_by_lua, respectively). Note that running lua-resty-waf in a lua phase handler not in this list will lead to broken behavior. All data available in an earlier phase is available in a later phase. That is, data available in the access phase is also available in the header_filter and body_filter phases, but not vice versa.

Included Rulesets

lua-resty-waf is distributed with a number of rulesets that are designed to mimic the functionality of the ModSecurity CRS. For reference, these rulesets are listed here:

  • 11000_whitelist: Local policy whitelisting
  • 20000_http_violation: HTTP protocol violation
  • 21000_http_anomaly: HTTP protocol anomalies
  • 35000_user_agent: Malicious/suspect user agents
  • 40000_generic_attack: Generic attacks
  • 41000_sqli: SQLi
  • 42000_xss: XSS
  • 90000_custom: Custom rules/virtual patching
  • 99000_scoring: Anomaly score handling

Rule Definitions

lua-resty-waf parses rules definitions from JSON blobs stored on-disk. Rules are grouped based on purpose and severity, defined as a ruleset. The included rulesets were created to mimic some functionality of the ModSecurity CRS, particularly the base_rules definitions. Additionally, the included modsec2lua-resty-waf.pl script can be used to translate additional or custom rulesets to a lua-resty-waf-compatible JSON blob.

Note that there are several limitations in the translation script, with respect to unsupported actions, collections, and operators. Please see this wiki page for an up-to-date list of known incompatibilities.

Notes

Community

There is a Freenode IRC channel #lua-resty-waf. Travis CI sends notifications here; feel free to ask questions/leave comments in this channel as well.

Additionally, Q/A is available on CodeWake:

Codewake

Pull Requests

Please target all pull requests towards the development branch, or a feature branch if the PR is a significant change. Commits to master should only come in the form of documentation updates or other changes that have no impact of the module itself (and can be cleanly merged into development).

Roadmap

  • Expanded virtual patch ruleset: Increase coverage of emerging threats.
  • Expanded integration/acceptance testing: Increase coverage of common threats and usage scenarios.
  • Expanded ModSecurity syntax translations: Support more operators, variables, and actions.
  • Common application profiles: Tuned rulesets for common CMS/applications.
  • Support multiple socket/file logger targets: Likely requires forking the lua-resty-logger-socket project.

Limitations

lua-resty-waf is undergoing continual development and improvement, and as such, may be limited in its functionality and performance. Currently known limitations can be found within the GitHub issue tracker for this repo.

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/

Bugs

Please report bugs by creating a ticket with the GitHub issue tracker.

See Also

lua-resty-waf's People

Contributors

ian-kelling avatar kwaping avatar lwh avatar mickygough avatar nqkdev avatar orthographic-pedant avatar p0pr0ck5 avatar tperalta82 avatar y1z2g3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lua-resty-waf's Issues

Limit number of possible timers

Per the ngx.timer.at docs:

Because timer callbacks run in the background and their running time will not add to any client request's response time, they can easily accumulate in the server and exhaust system resources due to either Lua programming mistakes or just too much client traffic. To prevent extreme consequences like crashing the Nginx server, there are built-in limitations on both the number of "pending timers" and the number of "running timers" in an Nginx worker process.

Since expire time granularity is tracked in seconds, add in logic to only create one new timer per second

about 42000.lua

why 42000.lua 's regex expression use "" in pattern, but others use [=[ ]=].

mp4 is not in "whitelisting extensions - media"

Hi Robert,

mp4 is not in "whitelisting extensions - media", rule id: 10007

/test.mp3?a=a' union select --> pass
/test.flv?a=a' union select --> pass
/test.mp4?a=a' union select --> forbidden

Phase handling

Right now everything runs in the access phase. If we want to pursue behavioral analysis, we'd need to look at capturing more data like response status, times, and header/body content. A few problems with this:

  • Need to figure out an efficient way to instruct exec() (or build a new model) to only handle rules of a single phase- having every phase handler run through -every- rule (and skip over phase mismatches) seems like a big waste, but need to profile that if it proves to be an easy option
  • Users would need to configure multiple phases manually. This seems like it might be too much responsibility to put on the user
  • How would we handle chaining?
  • Do we use ngx.ctx? A shm? Re-run access phase rules in filter phases (eww)?

about performance of freeWAF

1 Is the spend time you test FreeWAF based on Logging mode or not ? that is to say , whether you turn on the debug logging switch?

2 I find that if I turn on the debug logging mode, in my machine, it takes 9-10milliseconds, but 900-1000 microseconds when it turn off.

3 On one request, you have make some cache between rules to reduce exec time. Can we make some cache between requests to reduce response time?

more operator

modsecurity has some rules like "prefix", although this operator can change to PM to satisfied your operator, but I think it is not necessary, I think freeway should offer new operator to support "prefix"

_regex_match 's opts param

local function _regex_match(self, subject, pattern, opts)
local opts = self._pcre_flags

since opts will be override by self._pcre_flags, why should pass opts param in function

performance questions?

process transactions in roughly 300-500 microseconds per request; this equals the performance advertised by Cloudflare's WAF.

But as far as I know, CF's transaction time is 300-500us, less than 1 ms, why equals ?

Nginx segfault when FreeWAF installed

Hi Robert,

Sometimes I get segfault messages from Nginx, if FreeWAF is enabled. Related lines from dmesg:

[ 2983.164425] nginx[4298]: segfault at 414cb62a ip 00007ff720df7a5a sp 00007fff7cb96820 error 4
[ 3043.214933] nginx[4126]: segfault at 41a61e22 ip 00007ff720e07a5a sp 00007fff7cb96890 error 4
[ 3103.362729] nginx[4301]: segfault at 424f33da ip 00007ff720e07a5a sp 00007fff7cb96820 error 4

This is a Debian Jessie box with a default kernel from repo:
3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt4-3 (2015-02-03) x86_64

nginx-extras and libluajit-5.1-2 packages from Debian repo:
nginx -V
nginx version: nginx/1.6.2
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt=-Wl,-z,relro --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_mp4_module --with-http_perl_module --with-http_random_index_module --with-http_secure_link_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/headers-more-nginx-module --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-auth-pam --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-cache-purge --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-dav-ext-module --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-development-kit --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-echo --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/ngx-fancyindex --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-http-push --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-lua --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-upload-progress --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-upstream-fair --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/ngx_http_substitutions_filter_module

Backtrace of the core dump:
http://www.emrah.com/files/freewafbt1.txt
http://www.emrah.com/files/freewafbt2.txt
http://www.emrah.com/files/freewafbt3.txt

I have Nginx's debug log too but it's really big and I don't know how to find the related lines.

regards
emrah

Chains & Execution

It might be possible to get a greater execution speed if the control flow was pre-calculated.

What this would likely involve is precalculating a jump-offset for all rules, for non chained rules this would be one. In normal cases the pointer moves forward by one, either in a chain or when matching in a chain. If however the rule did not match the pointer moves forward by the rule's precalculated jump. That is it does to the end of the chain.

Saves a fair few executions of _process_rule

libac.so

can you tell me what repository you use to generate libac.so, because we use Mac OS

Profile new changes

See how more more (if any) new delay has been introduced by changes like ignore_ruleset (which is inefficient and now runs with every request)

Interesting

Hey,

This project looks quite interesting. Its obvious you have put quite a lot of hard work in already ๐Ÿ‘
It is also pretty remarkable the features available given the size of the code. Working on understanding it all currently.

  1. Is there any documentation on which CRS rules are implemented? It might be a good idea to insert a comment above converted rules with the original rule to make it easy to see gaps. I am guessing you might be missing some of the features needed to do the complete ruleset (that and honestly some of the rules would be quite a performance drain!)
  2. Re the benchmarks conducted: was OpenResty compiled with lua or luajit?
  3. Have you considered using a nginx pre-processor for debug logs? Its fairly common for large systems (e.g nginx) to be compiled with debug logging for greater performance. The pre-processing could leave the debug log call in lua comments so that it runs in production mode without processing.
  4. If you happen to be against to this, perhaps consider making debug logging a global property to the worker, then if it is disabled - overwrite the _log function with an empty function. It would be better than nothing. Still, personally I dislike that complexity and would prefer point 3.
  5. I would do the same with disabling pcre optimization, its both a user support risk and a mostly pointless option outside of debugging.
  6. In LUA string concatenation is doubly expensive as the hashing is done at this point too, when concatenating a few or more strings `table.concat`` is often faster
  7. Is there any lists of tests you have performed? I am interested in doing testing as to the performance of various scenarios - to make sure there are no resource exhaustion / DoS attacks lying in wait.

I will continue reading, but I am interested in getting this up and running - and of course contributing back. Feel free to ignore any questions that seem a bit ignorant at all, I haven't got it running yet so I might be mistaken on a point or two.

At a glance this does most of what I need it to do already. And hopefully it will be able to meet our performance requirements (looks to be very very close already).

Thanks in advance for reading all this :)

Level of logging

When debugging I have noticed an extreme amount of debug output. I propose reducing some of the mostly superfluous output. I propose starting with utility methods. This has an added benifit of simplifying the signatures for these outling methods.

Some possible targets include the operators.*:

_log(self, "Needle is a table, so recursing!")

and

_log(self, "Comparing (greater) " .. tostring(a) .. " and " .. tostring(b))

Some information from this could be included in the log output of process rule instead, possibly grouped with another message.

This has the added benefit of making the utility functions more generic and suitable for use from module context (not just wf context) where logging is not available.

Some work in this area is in my fork.

about chain rule

rule1 -> rule2 -> rule3

when rule1 match rule, ctx.chained = true, rule2 not match rule, but I think you don't set ctx.chained = false again, so when rule3 match again, it also take the rule3' action.

Implement Persistent Storage

This should be relatively easy to implement:

  • Store in a shm (needs to be user-defined in http{} block)
  • deprecatevar/expirevar implemented via timer

This will be useful to provide an abstraction layer to implement more features like bf protection, tarpitting, etc

FreeWAF stops to log

I upgraded to the current development branch and FreeWAF stops to log.

Server1:
Common FreeWAF config in /etc/nginx/conf.d/freewaf.conf:
access_by_lua '
FreeWAF = require "FreeWAF.fw"
local fw = FreeWAF:new()
fw:set_option("mode", "ACTIVE")
fw:set_option("disable_pcre_optimization", true)
fw:set_option("score_threshold", 10)

    fw:set_option("event_log_target", "file")
    fw:set_option("event_log_target_path", "/var/log/nginx/freewaf.log")
    fw:set_option("event_log_buffer_size", 8192)
    fw:set_option("event_log_verbosity", 3)

    -- fw:set_option("whitelist", "127.0.0.1")
    -- fw:set_option("ignore_rule", 41014)
    -- fw:set_option("ignore_rule", 41015)

    fw:exec()

';

The log file permission is OK. But there is no log.

Server2:
There are two domains and each one has their own config in server { } block. "access_by_lua" blocks are same except the filename in "event_log_target_path".

log is OK for one domain but it doesn't work for other. But if I comment the log options for the first domain then the second starts to log.

how to handle many keys

request_headers.host => opts = { key = "specific", value = "host"}

how to handle request_headers.host and request_headers.user-agent ?

to be opts = {key = "specific", value = {"host", "user-agent"}} ?

Handling of large request bodies / file uploads is poor

Currently FreeWAF will skip over request body data that is larger than client_buffer_body_size. A workaround to this is to set client_buffer_body_size equal to client_max_body_size, but this could lead to severe performance degradation. A better alternative might be to integrate a file scanner, and provide an option to enable/disable file upload checking.

Transform functions

e.g. base64 decode, etc. should be baked in to _parse_collection and memoized

Clean up set_option

Lookup table is getting a little clumsy, set the default behavior to just set the instance value to the option value, and make other behaviors do the needful.

resty.upload dependency

Hi Robert,

I installed the current master branch to Debian Jessie box with Nginx 1.6.2 from Debian repo.
I get "module 'resty.upload' not found" error message.

Is it possible to put this module under FreeWAF's directory tree like "inc.resty.cookie"?

regards

whitelist/blacklist

Can I ask the reason for an explicit whitelist / blacklist? From my understanding it would function the same as using allow/deny from the nginx access module which are always executed before LUA.

The main application I have brainstormed is when integrating into a larger application (like we are looking to do). Such an application is likely rare, and in most cases such a system would want to do this outside of the WAF layer for additional control.

Thanks

Per-server configuration

Right now config options are set globally in the init phase. Need to find a way to deliver config options (whitelist, rulesets, etc) on a per-server basis.

consult for owasp's core rule

Do you know "t:none,t:urlDecodeUni" mean the variables do operator twice? one for none transform function, another for urlDecodeUni transform function?

and I get to know you are doing the work that translate owasp's core rule to your own rule set, what's your schedule ๏ผŸ

Behavioral analysis ruleset

Now that we have persistent storage, we can start tracking bad behavior of clients

  • Number of 400 < x < 500 status responses (vs host avg)
  • Number of requests resulting in long upstream response time (vs host avg)
  • Repeated requests to the same resource
  • Request rate (vs host avg)

Will need header filter phase handling for this

Incorporate tarpit

Look into how we can integrate lua-resty-tarpit's state tracking, and investigate creating a TARPIT action

Add new event logging options

Need flexible logging destinations beyond error.log.

  • resty socket logger
  • plaintext file logger
  • logger settings can use set_option

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.