October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

ModSecurity SecRule: How Do Variables, Operators, and Actions Work?

Parse SecRule targets, operators, and actions; avoid phase, regex, selector, default-action, and chain mistakes when writing rules for ModSecurity or Coraza.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A SecRule evaluates selected variables against an operator, then applies actions if the condition matches. Write the phase and operator explicitly, give every rule a unique ID, and check how your engine handles selectors, defaults, and chains before deploying a rule that can block traffic.

What does each part of a SecRule do?

Coraza documents this canonical form:

SecRule VARIABLES "@OPERATOR OPERATOR_ARGUMENTS" "ACTIONS"

A practical layout is:

SecRule TARGETS "@OPERATOR ARGUMENTS" "id:10001,phase:1,pass,log,msg:'Explain the match'"
  • Targets identify the variables or collections to inspect.
  • Operator defines how their values are tested, such as equality, substring matching, or a regular expression.
  • Actions determine what happens on a match, including logging, metadata updates, or disruption.

Give each rule a unique id. State phase deliberately: Coraza documents phase 2 as the default when it is omitted, so a rule without a phase can inspect request-body data later than intended. The examples here specify both phase and operator rather than relying on defaults. See the Coraza syntax reference for its documented grammar and behavior.

As an Amazon Associate I earn from qualifying purchases.

The quotes and escaping around targets, operator arguments, and actions are interpreted in the configuration context. A value that works in one parser or connector may need different escaping in another; validate the complete rule in the environment where it will run.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do variable selectors change what a rule inspects?

Selectors can narrow a rule to one mapped key, combine targets, exclude a target, or count collection values. These Coraza examples illustrate different scopes:

#1 Best Overall
Fortinet Web Application Firewall - Virtual Appliance for All Supported Platforms. Supports up to 2 x vCPU core FWB-VM02
  • Fortinet Web Application Firewall - virtual appliance for all supported platforms. Supports up to 2 x vCPU core
  • Fortinet HW FWB-VM02
  • Manufacturer Part: FWB-VM02
SecRule REQUEST_HEADERS:User-Agent "@contains example" "id:10002,phase:1,pass,log	a"
SecRule &REQUEST_HEADERS:host "@eq 0" "id:10003,phase:1,deny,status:403"
SecRule REQUEST_HEADERS|!REQUEST_HEADERS:User-Agent "@detectSQLi" "id:10004,phase:1,pass,log"

In the first rule, : selects the User-Agent key from REQUEST_HEADERS. In the second, the leading & counts the selected collection values and tests whether the count equals zero. In the third, | combines targets and ! excludes the named header from the broader selection. These operators affect target selection; they do not change what the match operator means.

Do not assume regex selection of mapped-variable names is portable. Coraza’s syntax reference describes its PCRE-compatible selector as v2-only and says v3 supports RE2. Check the syntax for the exact Coraza release you deploy before using such selectors.

Which operator should you use, and why can a rule match unexpectedly?

Coraza documents @rx as the default operator. Consequently, leaving the operator out does not make the argument a literal string: it is interpreted as a regular expression. Use an explicit operator so the intended semantics are visible in review.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Fortinet Web Application Firewall - Virtual Appliance for All Supported Platforms. Supports up to 4 x vCPU core FWB-VM04
  • Fortinet Web Application Firewall - virtual appliance for all supported platforms. Supports up to 4 x vCPU core
  • Fortinet HW FWB-VM04
  • Manufacturer Part: FWB-VM04
Intent Coraza operator Behavior documented by Coraza
Exact equality @streq Case-sensitive string equality
Substring @strmatch Case-sensitive substring matching
Regular expression @rx RE2-based matching

For case-insensitive substring matching, Coraza’s operator reference recommends applying t:lowercase. Do not assume operators normalize input automatically. The Coraza operator reference describes these behaviors and available operators.

Check regex syntax and dot behavior

Coraza’s @rx implementation uses RE2 syntax, supports up to nine capture groups for use by actions, and enables dotall mode by default. With dotall, . can match a newline. A pattern that relies on PCRE-specific constructs may therefore fail or behave differently when moved to Coraza. Confirm the supported syntax and test representative inputs on the actual engine.

What do SecRule actions do, and what can defaults change?

Actions are comma-separated. Coraza groups them by purpose:

Rank #3
Fortinet Web Application Firewall - Virtual Appliance for All Supported Platforms. Supports up to 8 x vCPU core FWB-VM08
  • Fortinet Web Application Firewall - virtual appliance for all supported platforms. Supports up to 8 x vCPU core
  • Fortinet HW FWB-VM08
  • Manufacturer Part: FWB-VM08
  • Disruptive: includes deny, drop, redirect, allow, block, and pass.
  • Non-disruptive: includes logging, metadata, and setvar actions.
  • Flow: includes chain, skip, and skipAfter.
  • Metadata: includes id, rev, and severity.
  • Data: includes status.

Coraza says only one disruptive action applies per rule; if several are specified, the last takes precedence. A disruptive action does not execute when SecRuleEngine is set to DetectionOnly. The Coraza actions reference documents action categories, pass, and this DetectionOnly behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

pass means continue processing; it is not an allowlist decision. A rule with pass can still match and perform non-disruptive actions such as logging or variable updates.

Rules can also inherit actions from SecDefaultAction. Coraza documents that default actions combine with a rule’s actions and that rule actions override applicable defaults. When reviewing a short rule, inspect the effective configuration—including its applicable defaults—rather than treating the inline action list as the whole policy. See the Coraza directives reference.

Rank #4
Cisco Meraki MX100 Security Appliance, Firewall, GigE, 1U, Rack-Mountable
  • Meraki MX100: A building block for SASE in a rack-mountable form factor. Medium- to large-branch security and SD-WAN appliance for up to 500 users.
  • WAN: 1 x GbE RJ45, 1 x USB (cellular failover), Dual-purpose: 1 x GbE RJ45 +++ LAN: 8 x GbE RJ45, 2 x GbE SFP
  • Stateful firewall throughput: 750 Mbps +++ 500 Mbps site-to-site VPN throughput
  • Unified management for security, SD-WAN, Wi-Fi, switching, MDM, and IoT +++ Centralized management via web-based dashboard or API
  • True zero-touch provisioning +++ Smartphone-like firmware updates

How do chained SecRules work?

A chain combines conditions: the chained condition succeeds only when the required linked conditions match. Do not read each line as an independent rule with separate blocking behavior. The starter rule defines the chain’s entry point, and the disruptive action takes effect only if the chain succeeds.

Action-placement details depend on engine and version. The OWASP ModSecurity 2.x manual places disruptive, phase, metadata, and flow actions on the chain starter; non-disruptive actions may appear on members. That is a version-scoped ModSecurity 2.x rule, not proof that every Coraza release or connector follows identical rules. Before porting a chain, check the documentation for the target engine and version. The OWASP ModSecurity 2.x manual documents the v2 placement behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How should macros and dynamic values be handled?

Coraza documents macro expansion in the form %{VARIABLE.KEY}, including use in action values such as logdata and setvar. For example, a macro in an action value supplies dynamic data to that action; it does not by itself change the operator’s match input.

Best Value
UDPTCP Firewall, Intelligent Soft Routing Micro Appliance/Fanless Mini PC • Celeron N2840, 2 x RJ45(1000M), USB 3.0,HDMI,VGA,NO RAM NO mSATA SSD (8GB RAM 256GB SSD)
  • ◆Powerful Celeron N2840 Processor: N2840 Processor, 2 Cores 2 Threads, 1M Cache, Max Turbo Frequency 2.58 GHz, TDP 7.5 W. Whether you need a robust home server, a versatile tool for school education, seamless web browsing, or even efficient business office or industrial tasks, providing efficient performance for everyday tasks.
  • ◆Dual 1000M LAN: Mini Router PC with 2*Realtek RTL8111H network card chip full UDE 1000M with filter connector.Soft Router can monitor network data, improve network security, powerful and widely used.
  • ◆DDR3L Memory & Large Storage Capacity: Firewall box computer with 1 x DDR3L SO-DIMM memory 1333/1600MHz, 1xMSATA3.0 SSD.
  • ◆UHD Graphics & 4K Dual Screen Display: N2840 processor integrated UHD Graphics, HD and VGA dual display interfaces support 4K@60Hz. 
  • ◆Versatile Connections ports: 2 x1000M Realtek RTL8111H-LAN,2 xUSB3.0, 4 xUSB2.0, HDMI,VGA,AUDIO supports data storage and system boot.Mini desktop computer with WIFI dual antenna, which providing high-speed transmission and reliable connectivity. Support Dual Band Wifi, Internet, streaming media and audio can be used perfectly without interrupting the connection. Enjoy faster file transfers and smoother online experiences.

Quote and escape punctuation carefully in the configuration context. The documented macro form does not establish one universal escaping rule for every connector, so test the complete rule using the parser and connector that will load it.

What should you check before enabling a rule?

  1. Confirm the engine and version. Separate Coraza v2, Coraza v3, and ModSecurity 2.x behavior where their documentation differs; verify the connector too.
  2. Make the rule’s intent explicit. Use a unique id, an intentional phase, and an explicit operator.
  3. Review target scope. Check whether the rule inspects one key, a combined collection, or a broad target set with exclusions.
  4. Check inherited actions. Read the applicable SecDefaultAction and effective engine mode, not only the local action string.
  5. Validate regex portability. Check RE2 compatibility, dotall behavior, and any capture groups used by actions.
  6. Review chain placement. Confirm which conditions are linked and whether actions are placed where the target engine permits them.
  7. Test before blocking. Verify expected matches and non-matches with the exact engine and connector. A disruptive action will not execute in Coraza DetectionOnly mode.

If a rule produces false positives, consider whether a narrow target update or exclusion can address the problem before disabling a broader ruleset. Coraza’s directive reference documents target-update directives; their precise effect depends on the installed ruleset and engine.

Quick Recap

Bestseller No. 4
Cisco Meraki MX100 Security Appliance, Firewall, GigE, 1U, Rack-Mountable
Cisco Meraki MX100 Security Appliance, Firewall, GigE, 1U, Rack-Mountable
Stateful firewall throughput: 750 Mbps +++ 500 Mbps site-to-site VPN throughput; True zero-touch provisioning +++ Smartphone-like firmware updates
$344.00

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.