Write rule for Modsecurity to filter ipv4 addresses by regular expression and limit value limit to 15
Assistant
To create a ModSecurity rule that filters IPv4 addresses by regular expression and limits each octet value to a maximum of 15, you need to craft a regex that matches IPv4 patterns where each octet is between 0 and 15.
Regular IPv4 octets range from 0 to 255. Here, the requirement is to ensure each octet is ≤15.
Step 1: Describe possible values per octet
Numbers from 0 to 9: [0-9]
Numbers from 10 to 15: 1[0-5]
So, each octet is match by: (1[0-5]|[0-9])
Step 2: Full IPv4 pattern with octets limited to ≤15:
I think you wrong in something because your expression means there's always ip address starts with 1
Assistant
Thanks for pointing that out!
The regex I gave (1[0-5]|[0-9]) indeed gives priority to matching '1' followed by 0-5 first, but it doesn't require the octet to start with '1'. It matches either:
a two-digit number from 10 to 15: 1[0-5]
or a single digit from 0 to 9: [0-9]
Because the alternation (1[0-5]|[0-9]) checks for either option, a value starting with any digit 0-9 will match.
Example:
We want to inspect the POST body parameter ip and block requests if ip does NOT match this pattern (i.e., to allow only IPs with octets ≤15).
Example rule:
SecRule ARGS:ip "!^(1[0-5]|[0-9])\.(1[0-5]|[0-9])\.(1[0-5]|[0-9])\.(1[0-5]|[0-9])$" \
"id:12345,\
phase:2,\
deny,\
status:403,\
msg:'Invalid IP address - octet value exceeds 15',\
log"
Explanation:
SecRule ARGS:ip — check the ip parameter.
!^...$ — negates the regex match (if it does NOT match pattern, trigger).
id:12345 — unique rule ID.
phase:2 — inspect request body parameters (POST data).
deny — block the request.
status:403 — HTTP status code for forbidden.
msg — log message for auditing.
log — log this event.
If you want the rule to only allow valid IPs with octets ≤15 and Log or Deny those that don't match, this is the approach.
Note: this allows IPs such as 0.0.0.0, 15.15.15.15, 10.5.3.0. IPs with octets >15 like 16.0.0.1 will be blocked.
"9" matches [0-9]
"12" matches 1[0-5]
"0" matches [0-9]
"15" matches 1[0-5]
So, the first digit is not always 1. The regex is correct in this sense.
However, there is an issue if we consider input such as "05" — this is a two-digit number starting with zero, usually not valid in IP octets.
If you want to prevent leading zeros (except '0' alone), you may want to disallow octets like "05".
To improve the regex and accept only:
single digit Octet: 0-9
or two digit Octet: 10-15 only
We can write so:
Octet pattern: (1[0-5]|[0-9])
This matches '0', '1', ..., '9', or '10' to '15'.
If you want to avoid '05', that's additional.
Final Confirmed Regex and ModSecurity rule:
SecRule ARGS:ip "!^(1[0-5]|[0-9])\.(1[0-5]|[0-9])\.(1[0-5]|[0-9])\.(1[0-5]|[0-9])$" \
"id:12345,\
phase:2,\
deny,\
status:403,\
msg:'Invalid IP address - octet value exceeds 15 or format not allowed',\
log"
If you want the regex broken down:
For each octet:
1[0-5] = matches 10 to 15
[0-9] = matches 0 to 9
Alternate between these with the pipe |
Additional note: This restricts the IP octets tightly to a small range (0-15) which is not a normal IP range but probably intended to limit input for your use case.
If you still feel something is wrong, please share example input that causes trouble.