# Monitoring pfSense with Wazuh: A Comprehensive Guide

> Configure Wazuh Agent in pfSense for security monitoring - agent installation, log forwarding, and integration with Yandex Cloud or VK Cloud

Source: https://opennix.org/en/docs/pfsense/pfsense-wazuh-integration/


There are several ways to integrate pfSense with Wazuh. The easiest method is syslog, but you can also use the Wazuh agent.
The pfSense images published in the Yandex Cloud Marketplace and the VK Cloud Marketplace ship with the Wazuh agent already installed as a native pfSense package. On those images you can skip the installation step and go straight to configuration.

## Configuration

Installing packages for pfSense from FreeBSD repositories is not recommended as this can greatly increase the risk of problems and malfunctions.

1. First, you need to enable agent autorun. Edit the `/etc/rc.conf.local` file and replace string `wazuh_agent_enable="NO"` with string `wazuh_agent_enable="YES"`. Alternatively, run the following command:

```shell
sysrc -f /etc/rc.conf.local wazuh_agent_enable="YES"
```

2. Next, configure the agent's connection to the Wazuh Cluster. Edit the `/var/ossec/etc/ossec.conf` file and replace the IP line with the IP address or FQDN of the Wazuh cluster.

```xml
  <client>
    <server>
      <address>IP</address>
    </server>
    <config-profile></config-profile>
    <crypto_method>aes</crypto_method>
  </client>

```
3. The next step is to launch the agent by executing the command:

```shell
service wazuh-agent start
```
It is recommended to configure a cron task to automatically clear agent logs to optimize disk space usage (for example, older than 30 days). For this run the command:

```shell
crontab -e

```
An example of the cleanup command:

```text
0 4 * * * find /var/ossec/logs/ossec/ -d 1 -mtime +30 -type d -exec rm -rf {} \; > /dev/null
```
The agent should register with Wazuh after starting it. Check it by connecting to the Wazuh master via SSH and executing the following command:

```shell
cd /var/ossec/bin/
./agent_control  -l 
```
If configured correctly, there is the state of pfSense as ‘Active’:

```shell
Wazuh agent_control. List of available agents:
   ID: 000, Name: wazuh (server), IP: 127.0.0.1, Active/Local
   ID: 001, Name: pfsense.ru-central1.internal, IP: any, Active

```
Also, the new agent will be in the Wazuh web interface (see Figure 1).

![Wazuh web interface with the new pfSense agent](/img/pfsense-wazuh.webp)
<p style="text-align: center;">Figure 1. The Wazuh web Interface With the new Agent</p>

The initial setup is complete, now you need to change Wazuh.

## Configuring Suricata in pfSense with sending logs to Wazuh
 
First, you need to install [Suricata](https://suricata.io/)

 If you are already using [Suricata](/en/docs/yc/pfsense/suricata-integration/) , you can skip this step.

The next two parameters should be set as you can see at Figure 2. 

![Suricata EVE output settings for Wazuh](/img/suricata-eve-wazuh.webp)
<p style="text-align: center;">Figure 2. EVE Output Settings </p>

- EVE Json Log - checked.
- EVE Output Type - “File.

The rest of the eve.json parameters can be set at the user's preference during configuration.

2. For Wazuh to start analyzing events from Suricata, the following lines must be added to the `/var/ossec/etc/ossec.conf` configuration file:

```xml
<localfile>
    <log_format>json</log_format>
    <location>/var/log/suricata/*/eve.json</location>
</localfile>
```
Then restart the agent:

```shell
service wazuh-agent restart
```
The initial setup is complete, now you need to change Wazuh.

## Configuring for firewall logs

Add the following lines to the `/var/ossec/etc/ossec.conf` configuration file:

```xml
<localfile>
	<log_format>syslog</log_format>
	<location>/var/log/filter.log</location>
</localfile>
```
Then restart the agent.

The default pfSense firewall discard event rule is not logged, as specified by the line `<options>no_log</options>` in the [rule](https://github.com/wazuh/wazuh/blob/master/ruleset/rules/0540-pfsense_rules.xml?ref=benheater.com#L22) declaration.

Enabling logging for this rule can significantly increase the log size!

If multiple requests are made from a single source by the pfSense firewall, the blocking event will be logged by default. If you want the blocking event from the pfSense firewall to be logged in Wazuh, you can change this. The following shows how to do this.

### Create a custom rules file

1. Open the Wazuh menu and and navigate `Management > Rules` (see Figure 3).

![Wazuh rules management panel](/img/wazuh-pf-rules.webp)
<p style="text-align: center;">Figure 3. Wazuh Control Panel</p>

2. Find rules for pfSense (see Figure 4).

![Default pfSense rules list in Wazuh](/img/wazuh-default-pf-rules.webp)
<p style="text-align: center;"> List Rules</p>

3. Click to open the rules in the `0540-pfsense_rules.xml` file and copy the contents 

```xml
<group name="pfsense,">
  <rule id="87700" level="0">
    <decoded_as>pf</decoded_as>
    <program_name>filterlog</program_name>
    <description>pfSense firewall rules grouped.</description>
  </rule>

  <!-- We don't log firewall events, because they go
    -  to their own log file.
    -->
  <rule id="87701" level="5">
    <if_sid>87700</if_sid>
    <action>block</action>
    <options>no_log</options>
    <description>pfSense firewall drop event.</description>
    <group>firewall_block,pci_dss_1.4,gpg13_4.12,hipaa_164.312.a.1,nist_800_53_SC.7,tsc_CC6.7,tsc_CC6.8,</group>
  </rule>

  <rule id="87702" level="10" frequency="18" timeframe="45" ignore="240">
    <if_matched_sid>87701</if_matched_sid>
    <same_source_ip />
    <description>Multiple pfSense firewall blocks events from same source.</description>
    <mitre>
      <id>T1110</id>
    </mitre>
    <group>multiple_blocks,pci_dss_1.4,pci_dss_10.6.1,gpg13_4.12,hipaa_164.312.a.1,hipaa_164.312.b,nist_800_53_SC.7,nist_800_53_AU.6,tsc_CC6.7,tsc_CC6.8,tsc_CC7.2,tsc_CC7.3,</group>
  </rule>
</group>

```
4. Navigate to the previous screen and select "Add new rules file". The file name can be anything you like, for example, "custom-pfSense-overrides.xml". The following is a sample of its contents:

```xml
<group name="pfsense,">
  <rule id="87700" level="0">
    <decoded_as>pf</decoded_as>
    <program_name>filterlog</program_name>
    <description>pfSense firewall rules grouped.</description>
  </rule>

  <!-- We don't log firewall events, because they go
    -  to their own log file.
    -->
  <rule id="87701" level="5" overwrite="yes">
    <if_sid>87700</if_sid>
    <action>block</action>
    <description>pfSense firewall drop event.</description>
    <group>firewall_block,pci_dss_1.4,gpg13_4.12,hipaa_164.312.a.1,nist_800_53_SC.7,tsc_CC6.7,tsc_CC6.8,</group>
  </rule>

  <rule id="87702" level="10" frequency="18" timeframe="45" ignore="240">
    <if_matched_sid>87701</if_matched_sid>
    <same_source_ip />
    <description>Multiple pfSense firewall blocks events from same source.</description>
    <mitre>
      <id>T1110</id>
    </mitre>
    <group>multiple_blocks,pci_dss_1.4,pci_dss_10.6.1,gpg13_4.12,hipaa_164.312.a.1,hipaa_164.312.b,nist_800_53_SC.7,nist_800_53_AU.6,tsc_CC6.7,tsc_CC6.8,tsc_CC7.2,tsc_CC7.3,</group>
  </rule>
</group>
```
In the example above, we removed `<option>no_log</option>` and added `overwrite` for the rule.

5. Then press Save and Restart (see Figure 5).

![Wazuh custom rule save and restart window](/img/wazuh-pf-custom-rule.webp)
<p style="text-align: center;">Figure 5. Save and Settings Window</p>

6. It is necessary to confirm the applied settings (see Figure 6).

![Wazuh settings confirmation dialog](/img/wazuh-pf-custom-rule-confirm.webp)
<p style="text-align: center;">Figure 6. Settings Applied Confirmation Window</p>

You will now receive firewall events for the Wazuh agent on pfSense (see Figure 7).

![Firewall security events for pfSense Wazuh agent](/img/pfsense-wazuh-result.webp)
<p style="text-align: center;">Figure 7. Security Events Window</p>

Configuration is complete.
