Repository navigation
Description
I am using @rbl xbl.spamhaus.org. rule for protecting my Fediverse server and I face the issue that with latest version of either ModSecurity or the docker.io/owasp/modsecurity-crs:nginx container the collection handling for the IP collection seems to not longer work properly.
In my pi-hole I see a huge number of queries to .xbl.spamhaus.org for all the ip addresses checked and with queries to the same ip based query many times where I would expect that this is not the case due to how I set up the ModSecurity rules.
- The issue is happening with the latest version of the docker.io/owasp/modsecurity-crs:nginx container.
- It is far less a occuring with docker.io/owasp/modsecurity-crs:4.10-nginx-202501050801 even when mounting ruleset 4.14 to this container.
- I don't believe it is a problem with the CRS.
Based on what I see I would see it as if
- the expirevar for the IP collection is not working properly
- maybe the persistent store is causing the problem (as I don't see any problems with the TX collection which is also a collection but not persistet). This might be an docker image problem.
According to modsecurity.conf the SecTmpDir is set to /tmp/modsecurity/tmp (as data and upload directory are also located in /tmp/modsecurity).
This directory is empty. Wouldn't this be the directory for persistent storage and shouldn't there be a file for the IP collection database?
This is the rules I use:
# xbl.spamhaus.org to block malicious/infected ips
SecAction \
"phase:1,id:1100,\
t:none,pass,nolog,\
tag:'COLLECTIONS',\
initcol:ip=%{remote_addr},\
setvar:'tx.real_ip=%{remote_addr}'"
SecRule IP:SPAMMER "@eq 1" \
"phase:1,id:1101,\
t:none,deny,log,auditlog,\
msg:'Request from Known SPAM Source (Previous RBL Match)',\
tag:'AUTOMATION/MALICIOUS',\
severity:'CRITICAL',\
setvar:'tx.msg=%{rule.msg}'"
SecRule IP:PREVIOUS_RBL_CHECK "@eq 1" "phase:1,id:1102,t:none,pass,nolog,skipAfter:END_RBL_LOOKUP"
SecAction "phase:1,id:1103,\
t:none,pass,nolog,\
setvar:ip.previous_rbl_check=1,\
expirevar:ip.previous_rbl_check=3600"
SecRule REMOTE_ADDR "@rbl xbl.spamhaus.org." \
"phase:1,id:1104,\
t:none,deny,log,auditlog,\
msg:'RBL Match for SPAM Source',\
tag:'AUTOMATION/MALICIOUS',\
severity:'CRITICAL',\
setvar:'tx.msg=%{rule.msg}',\
setvar:ip.spammer=1,\
expirevar:ip.spammer=3600"
SecMarker END_RBL_LOOKUP
This is part of my setup.conf before including the CRS rules. As said, it worked much better (but also not perfect) with the older docker image mentioned above.
Activity
[-]expirevar does not seem to work properly with latest ModSecurity[/-][+]ip collection does not seem to work properly with latest ModSecurity[/+]ne20002 commentedon May 29, 2025
I have tried to capture some log output and this is what I found:
Occurence of 205.166.94.38:
So setting the ip address to the collection seems to have no effect?
airween commentedon May 30, 2025
HI @ne20002,
thanks for reporting this issue, and for the detailed explanation.
Let me check this soon and give a feedback.
ne20002 commentedon May 31, 2025
I have done a logging session with debug level 9. It creates a rather big log file. From what I see there is no other log statement related to collection. I would have expected at least something according to storing the collection. But .. nothing like that.
@airween, if should do some special tests or can help in any other way ...
airween commentedon Jun 12, 2025
@ne20002
sorry for the late reply, finally I was able to take a look at this issue.
I tried your setup and got the same result - especially this line in debug.log:
I think this line is generated by this part of
@rbloperator's code.The used
hostvariable is set up by mapIpToAddress function, this produces the expected format by Spamhaus - in your example above, from the IP205.166.94.38this function creates the host name38.94.166.205.xbl.spamhaus.organd does a DNS query. But because that IP is not on the list, therefore thegetaddrinfo()returns with a non-zero value.I suggest you to first try to use the IP collection with some other way, eg. use a static list with @ipMatch operator.
ne20002 commentedon Jun 13, 2025
Thank you @airween for the answer. Actually the lookup of
38.94.166.205.xbl.spamhaus.orgis not the problem.The inverse number order is how this is handled by the rbl systems (they all use it) and a query to
38.94.166.205.xbl.spamhaus.orgresults in NXDOMAIN as expected as205.166.94.38is not listed as a spam source. So returning false here is correct and205.94.166.205should not be added to the ip:spammers collection.But the
205.166.94.38seems also not to be added toip:previous_rbl_checkand this is a problem. Theip:previous_rbl_checkin this setup is used to bypass further checks for requests from205.166.94.38.This is what I wanted to achieve:
If the ip is not known as spammer...
ip:previous_rbl_check). If the ip is known as already checked then skip the following test.If the ip has not been tested before...
Rule 1103: mark the ip as tested in the ip collection with
ip.previous_rbl_checkset to 1Rule 1104: now test the ip with the rbl check. If the ip is a spammer, add it to ip.collection setting
ip:spammer=1So I expect for the second and following requests from an ip address (within the 3600s timeframe) that it is
either known as spammer and the request is denied or
the rbl check is skipped as it has already been done.
The last part is the problem. From what I see is, that the checks on the ip collection (for
ip:spammerorip:previous_rbl_check) always return 0. And this then causes a rbl check for each request as I see it on my pi-hole.The interessting part is, that it has worked before. So I believe the problem is not the rbl check but the collection.
airween commentedon Jun 13, 2025
hi @ne20002,
ah, thanks for the clarification, now it's clear. I see the problem.
Do you remember in which version was that this worked as well?
ne20002 commentedon Jun 13, 2025
I'm using the
docker.io/owasp/modsecurity-crs:nginxcontainer, but with my own configuration which includes the setup as described above. I don't use the container's startup scripts, just the nginx with the modsecurity module.With a previous version (
docker.io/owasp/modsecurity-crs:4.10-nginx-202501050801) this worked not fully, but mostly well.I also read the documentation, and based on that:
So I don't think it is the collection code except for (based on what I see happening here):
This is also why I looked for the persistant storage file. And I haven't found one (so this e.g. might be a container problem).
I don't have the ability to setup a plain nginx with modsecurity so that is why I'm limited to the container.
So I'm limited in options to dig deeper but still I'm willing to try or test whatever you need.
airween commentedon Jun 13, 2025
CRS's Docker uses LMDB (based on this configuration part), which means you have to have a file inside the container with name
modsec-shared-collections(may be with some extension).Here you can find a Gist, with that you can read the content of the LMDB file (which is a "database": keys and values).
Could you try to read that file (and share the relevant content here)?
ne20002 commentedon Jun 14, 2025
I think I don't have that file.
ne20002 commentedon Jun 14, 2025
@airween
Can you help me with defining (a few) rules that should result in writing a lmdb file? Maybe by setting dummy values?
I will then check if it works and also do a debug log on debug level 9.
This way we may get info if writing a lmdb is working at all or not with the container.
airween commentedon Jun 14, 2025
@ne20002,
Today I investigated this issue and found a few problems. I will write a summary later.
airween commentedon Jun 14, 2025
Here is what I found:
TX) stored in a file/(root) directory; engine needs to create, open and write files/modsec-shared-collectionsand/modsec-shared-collections-lockin name of running user (www-data? or I don't know what's that in Docker...)elsebranch (rc == 0means the file was opened successfully)I changed your rules and set the keys to upper case format, then I get this:
As you can see, in the second request the rule
1102matched.Other sad fact, that this does not work in case of in-memory backend. In that case the collection handling does not work to me at all - this needs more investigations.
Workaround
Even though I changed the code and put the database to under
/tmp, I tried these steps:After the engine started and received the first request, the files were filled with data, so this could work in your case too.
I don't know how to solve it permanently in Docker. @theseion do you have some idea?
Note that if the engine runs and uses those files, you can open and look its content with this version of
lmdbread, this can open the file for read in a different process.ne20002 commentedon Jun 15, 2025
Thank you very much @airween
I updated my config to fix the lower-/uppercase issue (all lowercase now).
According to the documentation the storage path for the persistant collections should be
SecDataDir(but is not supported in v3). This is by default set to/tmp/modsecurity/data/in the docker image. This path is created and it is owned by usernginxbut is empty. So I assume you're right with the/being the path used.I tried to create the files as u did with touch by mounting a shell script into the
docker-entrypoint.ddirectory (mounting the script file worked).I could touch the files in
/tmp/modsecurity/data/but it is not used (still always 0 bytes size). So again,SecDataDirseems not be used as path.I wasn't able to create the files in the root directory or in
/var/www. In both cases I got a 'Permission denied'. Seems as if the scripts indocker-entrypoint.dare runned with the nginx user.I have now created those two files on my host and mounted them rw to ogw to the root directory inside the container. The files seems to be used as the size increased. According to the debug log it now works. :)
I believe the best option to solve the issue would be to fix the creation of the persistent storage to use SecDataDir as according to the documentation. If so, no change to the docker image would be required.
And also an error log message should be written if the attempt to create or write to the persistent store fails.
airween commentedon Jun 15, 2025
I think that has no effect, because as you wrote you don't have the database files...
Vainly, because - as your wrote too -
SecDataDiris not supported in v3. Unfortunately, in default modsecurity.conf there is no any warning... :(May be @theseion can help with this.
I think Nginx shouldn't start if a necessary file does not exists...
Anyway, I think this is another important part of the engine that we have to care about and fix the issues.
Moreover, if we deal with it, we should do some other missing things. Eg. it always bothered that a user have to choose in build time which backend want they use for collections (in-memory or LMDB). There is no option to build LMDB and choose in-memory if it's better for the requirements. I was thinking to build two separated packages from the library (for Debian and Ubuntu systems): once without LMDB and another one with it. Debian developers helped me, ahd suggested to solve this issue inside the library, and don't split the package into more parts. There we can add some code which controls the database path.
ne20002 commentedon Jun 15, 2025
I agree with the idea to fix a few more things then.
For me, with mounting the files into the docker container in its root directory, it is working now. Thank you very much for the investigation, I wouldn't have expected this as the reason for the issue.
By documentation SecDataDir should be the place for the persitent storage. So this should be implemented anyway. An option to choose for memory or lmdb would still be a good idea (but this is also not available in v2, am I right?)
For the docker image it would be an option to create/provide the two files writeable in the root directory by default. So persistent collection would work and other user won't run into this problem. But it would break the read-only feature in case someone uses the collections. If I remember correctly, collections are used in PL2 so this can currently be considered broken for the docker image.
airween commentedon Jun 15, 2025
you're welcome!
I have to investigate this in case of v2, but I have some concerns regarding to use this as the path. For eg. what if a user has several vhost contexts, and set this value as a different locations...? Now I can't tell yo what could we expect.
V2 does not support in-memory collections (only the
TX, but there is no other option). This "feature" was introduced in v3.Yes, but as I wrote, if a user chooses in-memory collection then it does not work at all... we have to inspect that too.
theseion commentedon Jun 16, 2025
That's on purpose :) We could probably create the files as symlinks to a writable location. I'm not sure it's worth the effort though. @ne20002 you're the first with this issue that I've seen and we now have a workaround. I'd rather wait for a proper working solution in ModSecurity than having to document how this is broken and how the workaround works for the container image.
ne20002 commentedon Jun 16, 2025
I agree.
But I would again mention that, AFAIK, the CRS in PL2 make use of the collections. Isn't it, that this can't work properly with the current image (except with using the found workaround)?
ne20002 commentedon Jun 16, 2025
I sometimes do funny things and like to stretch 'em. ;)
theseion commentedon Jun 17, 2025
That would surprise me. Do you have an example? I couldn't find anything.
ne20002 commentedon Jun 17, 2025
Sorry, my mistake. Initializing collections is disabled by default. It's just mentioning that some plugins may need them.
fzipi commentedon Feb 8, 2026
IS this working now? Can we close or we need something else?
ne20002 commentedon Feb 8, 2026
Hi @airween
It is working if I mount the files into the root directory of the container.
airween commentedon Feb 8, 2026
@ne20002,
Felipe asked that not me 😃.
@fzipi: please do not close this yet. The problem is not just in Docker, but generally in the library. I have an idea how can we fix this and working on it.
fzipi commentedon Oct 12, 2026
Revisiting this one. Open PR #3597 ("Honour SecDataDir for LMDB, fix literal setvar writes") is the fix @airween referred to for the library side, and #3639 (merged September 2026) removed the LMDB
MDB_WRITEMAPcrash. Reviewing #3597 is the next step; the Docker mount question is the symptom of the LMDB file landing in the working directory (#2354).