feat: set correct hostname in log produced by Nginx #353
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reason
There was a discussion about this feature: #ModSecurity/3200
Current behavior: if ModSecurity catches an attack then it produces log entries. The problem is that the
[hostname]
field contains the server's IP address - which carries no information at all:At the end of the line, Nginx (and not ModSecurity) puts other fields, like
server
andhost
, but unfortunately those can be truncated if the other parts of line are too long, eg.[data]
field by ModSecurity, orrequest
field by Nginx, because Nginx truncates the log line after 2048 bytes.The other advantage of this patch that now the fields will be the same as in case of mod_security2, so parsing the lines (hopefully) will be easier.
Note, that previously I sent a PR to ModSecurity, but it was rejected. Now the result is the same but the solution is completely different, because the library gets the value from the HTTP server.
How does it work
In ngx_http_modsecurity_rewrite.c there is a new block (with a compilation condition - it needs libmodsecurity3 3.0.13 at least, because msc_set_request_hostname was implemented in that version). In this block the module try to get the
Host
header from the request which processed by Nginx. If there is noHost
header (eg. in case of HTTP/1.0) or the length is 0, then it gets theserver_name
value from the used virtualhost context - note, that it can be "_
" in default config. Also if there is noHost
header, Nginx will apply the default vhost, so the line will looks like[hostname "_"]
.In other cases (there is a
Host
header) that value will be in[hostname]
field.