Skip to main content

OpenSearch modsec setup

We have introduced an openSearch dashboard which collects all modsec logs and has document level security enabled. This means users can only access the logs for the github team they are in see here for more details. With this feature in place users can self serve and access their own modsec logs. In the case of a rare error and logs aren’t flowing to OpenSearch, then you must use the instructions below to access modsec logs on behalf of the user.

Get an audit log from modsec (when fluent-bit is not pushing to OpenSearch)

On occasion users may need you to provide them with audit log information on an modsec event from our ingress-controllers. This information may be sensitive so it can’t be placed in our org-wide Elasticsearch cluster. You’ll need to fetch this information from the pod that generated the log.

How do I check the audit log

As mentioned above, the audit log cannot be placed into Elasticsearch so you’ll need the following:

  • A Kibana event from the user. A request will come into the ask-cloud-platform channel asking something like:
Good afternoon, could I ask for the detailed logs for this block from ModSecurity, please?
https://kibana.cloud-platform.service.justice.gov.uk/_plugin/kibana/app/kibana#/doc/fb2e6550-0186-11ec-a2cf-6b21[…]lue:0),time:(from:now-3h,to:now))
(I need to find out which rules triggered the block, it has 2 critical fails)

example: https://mojdt.slack.com/archives/C57UPMZLY/p1630936971082200
  • The Kibana event above should provide you with the following key information
 modsec pod name (optional): This will allow you to hone in on the correct pod.
 unique_id: This is a hash of the event in question, e.g. 16494071776.005464
  • Kubectl access to the live cluster and access to the ingress-controllers namespace.

Perform a search for the unique-id (obtained from the Kibana entry)

# assuming the event id is 16494071776.005464
$ event="16494071776.005464"
$ for k in `kubectl -n ingress-controllers get pods | grep modsec01-nx-controller | awk '{print $1}'` ; do echo $k ; kubectl -n ingress-controllers exec -ti $k -- bash -c "cat /var/log/modsec_audit.log ; grep -r $event *" && break ; done

Which should return the event.

If you want to copy the filtered events of a particular search pattern to a folder and download to the local machine to extract the data, use the commands in bash

 cd /var/log/
 cat modsec_audit.log | grep -r "String or hostname to search" > list.txt
 mkdir copy
 cp list.txt ./copy

This should copy the filtered folders and files into the copy folder

Exit the modsec pod and from your machine, enter the below command

 kubectl -n ingress-controllers cp <modsec-pod-name>:/var/log/copy .

If you want to send the log data to a user, make sure you filter the data specific to that application and encrypt the file. From mac, you can use gpgkeychain to encrypt the file using the receiver’s public key before sending it to the user.

This page was last reviewed on 24 May 2024. It needs to be reviewed again on 24 November 2024 by the page owner #cloud-platform .
This page was set to be reviewed before 24 November 2024 by the page owner #cloud-platform. This might mean the content is out of date.