Website protection with OPNsense

with nginx plugin OPNsense become a strong full featured Web Application Firewall (WAF)

The OPNsense security platform can help you to protect your network and your webservers with the nginx plugin addition.

Installing OPNsense

In old days, install an open source firewall was a very trick task, but today it can be done with few clicks (or key strokes). In this article I'll not describe the detailed OPNsense installation process, but you can watch this video that was extracted from my OPNsense course available in Udemy. The video is in portuguese language, but with the translation CC Youtube feature you may be able to follow it without problems (if you don't are a portuguese speaker ofcourse) :-)

from my Udemy OPNsense course: check out here !

OPNsense installation video (portuguese)

Depend on the platform you are installing the OPNsense, the steps may vary, following the official installation guide:

Plugin installation

To install the plugin, follow this simple steps:

  1. Access your OPNsense firewall web gui: (https://<YOUR_IP>)

2. Go to the Menu: System: Firmware: Plugins

3.Find the os-nginx plugin and click the install option [+]

os-nginx plugin installtion

Initial configuration

Before the configuration, we need to understand some terms:

  • Upstream Server: The real webserver that will host the web page/application;
  • Upstream: The backend where will be configured the server(s);
  • Location: The URL pattern that should have an Upstream configured;
  • HTTP Server: The frontend that should have one or more Locations configured;

So, our configuration will start with the Upstream Server configuration:

To make the initial configuration, go to the Nginx configuration menu:

1. Upstream Server

click on add button
Upstream Server: Example configuration

The server params will depend on your webserver/application and network environment. To better protection, it's recommended that you set some limits in Maximum Connections/Failures and in the Fail Timeout.

2. Upstream

click on add button
Select the Upstream Server configured in the previous step

In the above example we are not using TLS in the backend.

The configured Upstream will look like this:

3. Download the NAXSI (WAF) rules

About NAXSI: (extracted from: https://github.com/nbs-system/naxsi)

NAXSI means Nginx Anti XSS & SQL Injection.

Technically, it is a third party nginx module, available as a package for many UNIX-like platforms. This module, by default, reads a small subset of simple (and readable) rules containing 99% of known patterns involved in website vulnerabilities. For example, <, | or drop are not supposed to be part of a URI.

Before proceed with the configuration, we will need to download the Naxsi rules, that will be enabled in our Location configuration later.

Go to the Submenu:

Click on Download button to fetch the NAXSI rules
You must accept the NAXSI repository licence to Download the rules
After download process, the rules will be shown

4. Location

Create a new Location:

Location Submenu
Click on add button
  • Description: Fill with something that makes sense to you;
  • URL pattern: To match with your webserver root fill with "/". To match with another path change it to something like "/anotherpath/";
  • Enable Security Rules: This option enable the WAF protection. Combined with the Custom Security Policy you will be able to protect your website from the specified policies (group of rules);
  • Learning mode: In this mode, the plugin will not block the requests that match with the selected policies, will only log the requests. It's a good idea to adjust your own whitelist rule before start to block requests.

5. HTTP Server

Finally, create your website frontend:

Click on the add button
  • Server Name: the FQDN name of your website;
  • Locations: the Location created in the previous step;
  • TLS Certificate / CA Certificate/ Enable Let's Encrypt Plugin: If you want (you should) enable HTTPS (TLS) in your website, these options need to be configured with the your TLS and CA certificate (if you already have a valid one) or you can install and configure the Let's Encrypt Plugin that will create those to you at no cost.
HTTP Server configured

6. OPNsense configuration adjusts

To enable our nginx service without port conflict problems, we'll need to adjust some webgui configurations.

!! Make sure that you have access to your OPNsense from your LAN interface or add the required rules before changing the following configurations !!

Go to the System: Setting: Administration menu

Change the Webgui HTTPS port and disable the HTTP Redirect rule (port 80)

Back to the menu Services: Nginx: Configuration and enable the service:

Check Enable and click on Apply button
After applied the configuration, the service will become up and running

The final step is to create the required Firewall Rules to allow the HTTP/HTTPS traffic on desired network interface (probabilly the WAN).

Conclusion

I tried to cover in this article the basic configuration of this great plugin to the awesome OPNsense security platform, to provide some website protection. It's possible to get an advanced protection with this plugin, but it will demand many others articles to explore all possibilities.

Thanks to the OPNsense team and community!

If you need some help with OPNsense, open source security or Cloud Managed Security Services:

Julio Cesar Camargo (JCC)

Written by

Cyber security professional / open source and aviation enthusiast. CTO@Cloudfence — https://cloudfence.com.br