Practical OPNsense 3rd 2021
Practical OPNsense 3rd 2021
Practical OPNsense
Markus Stubbig
Practical OPNsense
Building Enterprise Firewalls with Open Source
Third version, May 2021
All rights reserved. This book or any portion thereof may not be
reproduced or used in any manner whatsoever without the written
permission of the publisher except for the use of brief quotations in a book
review.
Trademark notice: Product or corporate names may be trademarks or
registered trademarks, and are used only for identification and explanation
without intent to infringe.
Contents
Preface xii
Introduction 17
I For Beginners 21
1 Lab Network 23
Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Virtualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Firewall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Lab Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Utilization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2 Platform 31
Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
VMware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
VirtualBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3 Installation 45
Operating system . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Post-installation tasks . . . . . . . . . . . . . . . . . . . . . . . . 48
v
4 Initial Setup 51
Initial setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Secondary setup . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Final testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5 IP Version 6 61
Crash course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Lab setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Addresses and routes . . . . . . . . . . . . . . . . . . . . . . . . . 63
Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
II For Intermediates 67
6 Firewall 69
OPNsense as a firewall . . . . . . . . . . . . . . . . . . . . . . . . 70
Lab setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Firewall rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Throughput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Best practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Additional filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Technical background . . . . . . . . . . . . . . . . . . . . . . . . 81
Order of processing . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
7 Transparent Firewall 85
Pros and cons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Lab setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Filter operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Ruleset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
vi
Uncover transparent firewall . . . . . . . . . . . . . . . . . . . . . 91
Technical background . . . . . . . . . . . . . . . . . . . . . . . . 91
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
11 OpenVPN 137
Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Differences to IPsec . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Lab setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
vii
Site-to-Site tunnel . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Client-server tunnel . . . . . . . . . . . . . . . . . . . . . . . . . 146
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Technical background . . . . . . . . . . . . . . . . . . . . . . . . 153
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
13 NetFlow 173
The content of a flow . . . . . . . . . . . . . . . . . . . . . . . . . 173
Lab setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Collector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Insight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Technical background . . . . . . . . . . . . . . . . . . . . . . . . 178
IPv6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
viii
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
16 Multi-WAN 227
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Load distribution in the WAN . . . . . . . . . . . . . . . . . . . . 229
Lab environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
IPv6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Technical background . . . . . . . . . . . . . . . . . . . . . . . . 239
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
ix
18 Intrusion Detection 255
IPS and IDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Network integration . . . . . . . . . . . . . . . . . . . . . . . . . 256
Lab setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Attack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Activate IDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Activate IPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Transparent IDS . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Technical background . . . . . . . . . . . . . . . . . . . . . . . . 264
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
x
Contents
22 Configuration 305
Dropbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Google Drive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Bibliography 335
Index 339
xi
Preface
Preface
OPNsense is 6 years old and has gradually grown up. There’s been no
quarrels with the code donor pfSense, instead OPNsense invests in better
security and code quality. Additionally, the web interface is now a linguist
and can handle ten languages.
OPNsense’s popularity is growing steadily: respectable computer magazines
favorably review the firewall and large IT companies offer services around
OPNsense. In Google Trends, OPNsense is getting closer and closer to its
predecessor pfSense.
Have fun reading and trying things out – and be ready for wonderful
surprises.
After all the effort, did OPNsense actually make the cut and find friends?
If so, who are they? As it turns out, well-structured and documented
source code, as seen in OPNsense, is apparently a significant attribute for
an open-source firewall! And several celebrities from the security world
have complimented OPNsense, first and foremost of these being the chief
developer of monowall.
xii
Overview
This book will show you how to operate OPNsense and the many fea-
tures which are all possible with this open-source firewall.
Enjoy reading and trying things out – and be ready for wonderful sur-
prises (and even a bit of cursing).
Overview
Part 1, For Beginners, sets up the network environment with physical de-
vices or on a virtual platform. All machines get an operating system and
a quick configuration, followed by essential functions, like routing and IPv6.
In part 2, For Intermediates, the firewalls fulfill some pressing tasks, which
must be present in every network. As a packet filter and address translator,
they will connect and isolate the attached subnets.
Part 3, For Experts, dives into enterprise-grade topics and establishes site-to-
site VPN tunnels and firewall clusters for high-availability. An in-depth look
inside the data flow provides good-old NetFlow. And the included proxy
server can even sniff inside TLS connections.
Outside the closed lab environment OPNsense acts in part 4, For Hack-
ers, as DSL router, load-balancer for multiple Internet links and even as
Sheriff for data trespassers.
Part 5, For Admins, provides many small hints that make daily work with
the firewall more fluent and straightforward. After that, OPNsense uploads
its configuration file to the cloud and stores it revision-safe on a DropBox
or Google Drive. Finally, let’s check out the programming interface of
OPNsense.
xiii
Resources
Resources
https://opnsense.org
The homepage of OPNsense offers a good start into the topic and links to
the official documentation, to the forum, and the download area.
https://github.com/opnsense
The source code is hosted at GitHub, where anybody can review the code
and its development process. It also offers the build tools and tutorials on
how to compile the code yourself.
https://docs.opnsense.org/
OPNsense for reading: manuals for user and developer, how-to documents
with many screenshots and step-by-step tutorials. Almost as comprehensive
as a full book.
https://forum.opnsense.org/
The forum is the first place to find small tutorials, discussions, and support
from the community. The language is not limited to English, and many
posts are in German.
Conventions
Constant Width regular shows the output of a command.
Typewriter font is used for configuration and keywords, and must be typed
exactly as shown.
Constant Width bold shows commands that expect some kind of output.
xiv
Legal
Legal
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. The author cannot attest to
the accuracy of this information. Use of a term in this book should not be
regarded as affecting the validity of any trademark or service mark.
While every precaution has been taken in the preparation of this book,
the publisher and author assume no responsibility for errors or omissions,
or for damages resulting from the use of the information contained herein.
xv
Introduction
16
Introduction
OPNsense is:
17
Introduction
On the contrary, security experts can review open-source products and have
a good chance of finding malicious code. Furthermore, it is challenging
for vendors to install a backdoor in the source code if it is available for
everybody to read and analyze.
Try before Buy. You can (and should) evaluate OPNsense thoroughly
before spending money on infrastructure. That’s the same idea as with a
shareware application. Who is happy about limited functionality or a demo
license that expires after 30 days?
In this context, try means evaluating with sample scenarios and buy stands
for deployment in the local environment.
18
Introduction
Best Of. Although OPNsense doesn’t reinvent the wheel, it does imple-
ment many services from the UNIX and Linux domain. The software has
attained rock-solid stability after years of development. The web proxy
comes from Squid, the SSH server is a subsidiary of OpenSSH and the
firewall ruleset is the packet filtering engine pf from BSD.
Is use of OPNsense, an open-source software, theft of intellectual property?
Not at all! It simply proves that open source works. When license terms
are met, it is perfectly legal to integrate 3rd party software. It is highly
recommended, especially in the security world, that application developers
do not invent another crypto algorithm; but rather use stable free libraries.
History
The history of OPNsense is coupled with monowall and pfSense. At the
beginning of 2003, monowall started as a firewall which used FreeBSD
as its operating system. One year later, pfSense forked monowall with
the goal of being better. This approach worked well and in 2006 pfSense
outperformed its predecessor regarding functionality and popularity.
The concept behind pfSense and its development was successful. In the
following years, they released one version after the other.
19
Introduction
own code. Currently (2021), both firewall distributions hardly have any
code lines in common.
OPNsense publishes new releases on a precise semiannual basis. This
regular interval makes update schedules easy, and the community likes
this strategy. Indeed, critical security patches have been released in the
meantime, when necessary, so there is no need to wait for the next major
release.
OPNsense and pfSense compete with similar goals to win trust of its users.
Which distribution will win has not been decided yet: OPNsense with a
fresh start and clear goals or pfSense with long-time confidence, stability,
and a reputable name.
20
Part I
For Beginners
21
Chapter 1
Lab Network
All topics within this book have a practical background. Each chapter
begins with the basics to establish an understanding or to refresh dusty
knowledge. The examples and exercises are meant to be played with and
rebuilt.
The chapters are based on the exact same network diagram, which rep-
resents a small corporate network with two remote sites and redundant
wide area network (WAN) connections. Depending on the complexity of
the topic, it might be enough to use only a small part of the lab network.
Some chapters might have a different setup or an additional device. In that
case, this will be explained in detail at the beginning of the section.
Resources
Using the exact same setup for the lab network removes the need to modify
the infrastructure between chapters, i.e. there is no need to re-cable or
modify the virtual environment. This saves time and prevents error. And
after a few chapters, the lab network will become more and more familiar,
since the names of firewalls, clients, network adapters, and IP addresses
23
Chapter 1. Lab Network
Despite the ability to run a lab on minimal requirements, more CPU cores,
memory space, and disk space are always encouraged. Table 1.1 gives
the specifications for different configurations. The numbers are based on
official documentation [2].
Some chapters use isolated networks, others need Internet access. The
path to the Internet is always through the firewall RT-core. The firewall
expects Internet access behind its network adapter em0. This is done with
a NAT adapter in a virtual environment. In a physical setup, connect the
network adapter to the DSL router. Any scenario that leads to the Internet
is welcome.
24
Resources
CL-1
Debian 10
10.1.1.0/24 eth0 10.5.1.15
fd00:1::/64 eth1 00:15:16:11:00:15
Site-1 vnet1
10.1.1.25 00:15:16:11:01:25
DMZ 10.4.1.0/24
fd00:4::/64
vnet4
10.1.1.1 10.4.1.1 00:15:16:07:04:07 10.4.1.2 10.1.1.2
00:15:16:01:01:01 00:15:16:01:04:01 10.4.1.7 00:15:16:02:04:02 00:15:16:02:01:02
198.51.100.6 WAN-1
00:15:16:06:07:06 198.51.100.0/24
em1 2001:db8:1::/64
WAN-2 vnet7
192.0.2.0/24 RT-core
2001:db8:2::/64 OPNsense 21.1
vnet6 em0 10.5.1.6
em2 00:15:16:06:00:06
192.0.2.6
00:15:16:06:06:06
192.0.2.3
00:15:16:03:06:03
em2
RT-3 CL-2
OPNsense 21.1
Windows 10
em0 10.5.1.3
10.5.1.25
00:15:16:03:00:03 00:15:16:12:00:25
em1 lan0
10.2.1.3 10.2.1.25
00:15:16:03:02:03 00:15:16:12:02:25
25
Chapter 1. Lab Network
Virtualization
It is possible to fully virtualize all devices used in the lab network. Each
firewall becomes a separate virtual machine (VM) with virtual network
cables connecting neighboring VMs. The interconnecting networks between
the VMs are VMnetX (at VMware) and vboxnetX (at VirtualBox). A physical
network adapter in the host system is only required when mixing the lab
with real gear.
Table 1.2 lists which interface belongs to which virtual network. Though not
technically required, the network adapters of the VMs can use a predefined
MAC address to help interpret the output. This technique makes it easy to
26
Hardware
Hardware
OPNsense performs well on all devices with x86_64-type architecture. The
brand of the network adapter is not that important since the lab setup
is intended to demonstrate features and not to provide the best possible
performance. Check the FreeBSD hardware compatibility list [3] to see
compatible gear.
Networks
The network architecture between the firewalls is based on Ethernet. Every
subnet is its own broadcast domain. It is therefore important not to mix ca-
bles of different segments. Two methods are available to correctly separate
the subnets: by switches or by VLANs.
Separate by switches
Each network segment uses a separate network switch or hub. The switches
are not interconnected.
A smaller 5-port device is acceptable since the subnets are rather small. Any
brand and model should be sufficient for the job.
Separate by VLANs
All cables are connected to a single switch. Cables and switch ports that
belong to the same network segment are members in a common virtual LAN
27
Chapter 1. Lab Network
(VLAN). For example, all switch ports leading to the WAN-2 core network
will be a member of VLAN 6.
The switch device must have enough ports to provide connectivity for all
the firewalls. The switch is not required to provide routing between VLANs.
A regular VLAN capable layer 2 switch is adequate.
It is even possible to mix both modes. For instance, both WAN segments
could connect to one switch and the site subnets could connect to another
switch. The requirements correspond to the method Separate by VLANs.
Firewall
The OPNsense firewall uses the current stable version 21.1 as a 64-bit
image. If different versions or additional devices are included, then the lab
is modified by replacing the device in question.
Each firewall has one additional network adapter for management access.
That way, an SSH client will still reach the firewall even if some configura-
tion change has failed and the regular interfaces become inaccessible. If
the hardware does not provide an extra interface for device management,
it is acceptable to skip this option.
The lab firewalls are serially numbered. The device number is echoed in
the IPv4, IPv6, and MAC addresses. This allows easy device identification
in a command output listing.
The name of each network adapter is printed next to the device icon. The
full IPv4 address is added below. Information about IPv4 subnet and IPv6
prefix is presented at the network line icon.
Addressing
The subnetworks of the imaginary remote sites use private IPv4 addresses
and unique local IPv6 addresses. Each site has a client computer, which is
used only to validate a feature or generate traffic. The required command
set is limited to ping, traceroute, netstat and a web browser. Even the
choice of the operating system is irrelevant – the demo lab picks Debian,
CentOS and Windows due to their popularity.
28
Lab Server
The area between the sites is the core network. Devices in this network use
the address ranges 192.0.2.0/24 and 198.51.100.0/24, which are reserved
for documentation (RFC 5737).
The addressing of IPv6 also uses two unequal prefixes to visually sim-
plify the differences: prefix fd00::/16 is used in site networks and prefix
2001:db8::/32 is used in the core network.
The address ranges are intended exactly for this purpose and do not collide
with public prefixes. Furthermore, the addressing is kept straightforward.
All ranges are structured uniformly and have only “regular” netmasks of
/24 (IPv4) or a prefix of /64 (IPv6).
Table 1.3 summarizes the IPv4 and IPv6 ranges attached to the VMnet net-
works. Additional addresses (e.g. for PPPoE, tunnel, CARP) are derived
from the same ranges.
Lab Server
The lab server provides infrastructure services. It can run on physical
hardware or as a virtual machine. If the OPNsense firewall is evaluated for
a client/server protocol, the lab server will be the counterpart. It can accept
requests from the firewalls on NTP, DNS, Syslog, FTP/TFTP, NetFlow and
HTTP. The deployed lab server runs on CentOS 8.
29
Chapter 1. Lab Network
Utilization
Each chapter uses a subset of the full lab network. Lesser devices provide
better control, simpler examples and briefer command output. This limi-
tation leads to a better overview. Feel free to insert additional firewalls to
dive deeper into features.
30
Chapter 2
Platform
The next step is all about setting up the lab components. It begins with the
creation or purchase of the equipment, followed by installation and finally
networking.
As mentioned in Chapter 1, the lab can run on physical hardware or find
its home entirely in a virtual environment. This makes a big difference in
the structure, but is irrelevant for the example scenarios in the following
chapters.
The installation procedure is the same for all methods: it first begins by
creating the virtual networks, which are separated either by a virtual switch
or a port group. The next step is to set up the virtual machines (VMs) and
finally the new VMs put their network adapters into the local VM networks.
The choice of virtualization software depends on your personal preferences.
The following explanations apply to VMware ESXi, Workstation and Player,
and VirtualBox.
31
Chapter 2. Platform
Preparation
The firewall installation starts with a live image in ISO format. The website
of OPNsense [4] always offers the latest release for download.
The lab in this book uses version 21.1 and the ISO image
OPNsense-21.1-OpenSSL-dvd-amd64.iso
First, in a hardware lab, burn the ISO file to a DVD and start the server
or laptop from this medium. Devices with a 32-bit CPU are no longer
supported – the latest version for 32-bit devices is 20.1.
VMware
VMware has a wide range of products, but the main ones for the lab are
ESXi, Player, and Workstation. The lab begins with the setup of the virtual
networks.
Workstation Pro
VMware Workstation Pro is a software application for Windows and Linux
that supports virtual machines.
The configuration takes place in the Virtual Network Editor. If not already
present, create the virtual networks VMnet1 to VMnet7 there. Choose type
Host-Only and do not use DHCP. The subnet IP is insignificant since it is
not addressed in the lab.
Next, create the virtual machines. The procedure is always the same:
1. Start VMware Workstation
2. File → New Virtual Machine. . .
3. Type of configuration? Custom (advanced)
4. Hardware compatibility: pick the latest (e.g. Workstation 15.x)
5. Installer disk image file: select the ISO file from the previous section
Preparation
6. Virtual machine name: RT-1
Location: no preference
32
VMware
7. Number of processors: 1
Number of cores per processor: 2
8. Memory for this virtual machine: 2 GB (or more)
9. Network connection: use host-only networking (the wizard allows
only one single network card, the others will follow later)
10. SCSI Controller: LSI Logic
11. Virtual disk type: SCSI
12. Disk: Create a new virtual disk
13. Maximum disk size (GB): 6
Store virtual disk as a single file
14. Disk File: no preference
The newly created machine will need a cleanup: a firewall does not need
a floppy drive, sound card, USB controller, and printer. However, it does
need more network adapters.
VM → Settings provides an insight into the soul of the virtual machine.
That’s the right place to add and delete objects until the settings fit. New
network adapters are always of type Custom with an assignment to the cor-
responding VMnet. The fixed MAC address is hidden behind the Advanced
button at Network Adapter.
Workstation Player
The software VMware Workstation Player is a reduced feature version of
VMware Workstation Pro. It is free of charge for non-commercial use.
Dialogues and procedures are similar, so the settings in the previous section
also apply here.
The properties of the virtual networks cannot be changed, but the default
setting is acceptable.
The creation of a VM begins with the button Create a New Virtual Machine.
Then some questions will follow concerning the installer image, the name,
and location of the VM, and finally, the size of the hard disk.
33
Chapter 2. Platform
All other details are adjusted outside the wizard. The same parameters
apply as for VMware Workstation.
The Linux version of the VMware Player is not suitable for the demo lab
because the dialog window does not show the selection of VMnet networks.
All host-only network adapters of the OPNsense firewall are in the same
network. Manual adjustments of VMnet networks and manipulation inside
the .vmx file of a VM do not yield any improvement.
The best alternative under Linux is VirtualBox (see section VirtualBox on
page 37).
ESXi
VMware ESXi is a Type 1 hypervisor. It does not run as an application on an
operating system, but works directly on the physical hardware. A graphical
web client creates and manages the virtual networks and machines. Inter-
nally, the virtual firewalls communicate with each other via virtual switches,
as illustrated in Figure 2.1 [5].
First, create a switch within the ESXi world. This switch later carries the
virtual networks with segmentation by VLANs. The principle corresponds to
the physical environment from section Hardware on page 41 in virtual form.
The virtual network environment starts in the navigator of the web client
under Networking in the register Virtual switches. If the lab is isolated
inside the ESXi, no physical network adapter is required. For everything
else, the following configuration requires the unused network card eth1,
which is listed as vmnic1 in ESXi.
34
VMware
vFirewall
Virtual
Machine RT-2
em1 em2 em3 em0
Virtual
Network
adapter
vNIC1 vNIC2 vNIC3 vNIC0
Port group Port group Port group Port group
Port groups VMnet1 VMnet4 VMnet7 VM Network
Virtual
switches vSwitch1 vSwitch0
vSwitch0
vmnic1 vmnic0
Physical NICs
eth1 eth0
Figure 2.1: The interaction of the virtual components
After the new switch vSwitch1 is created, it will not yet have any VLANs
or ports. VLANs correspond to a port group at ESXi and are created and
assigned at the register Port groups. Click the button Add port group to
set up the first group. The VLAN number is important if the VMs are to
communicate with a physical network. VLANs 1401 to 1407 are used for
the demo lab.
• Name: VMnet1
• VLAN ID: 1401
• Virtual Switch: vSwitch1
This step is identical for the other networks VMnet2 to VMnet7. The virtual
network environment is then complete and should resemble the list in
Figure 2.2 on the following page. The scenario for creating virtual machines
starts in the navigator under Virtual Machines. The button Create / Register
VM triggers the wizard. It will ask several questions; answer them as
follows.
35
Chapter 2. Platform
• Name: RT-1
• Compatibility: latest
• Guest OS family: Other
• Guest OS version: FreeBSD (64-bit)
• CPU: 1
• Cores per Socket: 2
• Memory: 2 GB
• Hard disk 1: 6 GB
• SCSI Controller 1: LSI Logic Parallel
36
VirtualBox
The newly created VM is still missing some network cards. Use the proper-
ties of the VM to add the network adapters and place them in the correct
VMnet. Table 1.2 on page 26 lists the affiliation of firewall interface to the
virtual network.
VirtualBox
VirtualBox is an application for Windows, Linux, and macOS that creates
and hosts virtual machines.
VirtualBox has a clear portfolio of products. It does not offer more than
one virtualization application, but it does provide several configuration
methods. The Oracle VM VirtualBox Manager is included in the normal
installation. It is easy to use but requires an X11 interface under Linux.
Alternatively (or complementary) phpVirtualBox supports the setup [6].
It is a web-based manager that provides the look and feel of the Oracle
manager inside the browser.
VirtualBox also offers a command line, so that the lab setup can be com-
pletely scripted.
vboxnet
The journey begins with the creation of the virtual networks vboxnet1 to
vboxnet7.
37
Chapter 2. Platform
phpVirtualBox
The advantage of phpVirtualBox is that it provides a look and feel identical
to the operation of the VirtualBox Manager GUI. Therefore, the virtual
network set up is the same as the Oracle Manager setup described in the
previous section.
CLI
The command line expects a lot of typing work for one-time use. The
procedure differs between Linux and Windows:
Linux
Windows
38
VirtualBox
Note
VirtualBox on Windows assigns the new network adapters a name of
“VirtualBox Host-Only Ethernet Adapter”, followed by a number.
The accuracy of the virtual networks’ setup can only be checked later when
the network is live and in use.
Virtual machines
The next step is to create the virtual machines. The process is similar for all
VMs, so the examples only show the steps from the first device.
1. Machine → New. . .
2. After creating the VM, adjustments are still important so that the
network adapters play along in the right networks (Figure 2.3).
• Mount the DVD with the image from section Preparation (see
page 32)
• Network
– Declare adapter as Host-only Adapter
– Attach to vboxnetX (Linux) or “VirtualBox Host-Only Ether-
net Adapter X” (Windows)
– Under Advanced change the type to Intel PRO/1000 MT
Server. This is one of the most powerful network adapters
(see Chapter 20), but the others also work well.
• Adjust MAC address if desired (not mandatory)
39
Chapter 2. Platform
• Four NICs can be set up via the GUI. All others (RT-1 is the
only VM with a fifth NIC) via the command line, see section CLI
below.
CLI The command line setup expects commands that correspond to the
GUI mouse clicks.
1. The journey via the command line path begins with the creation of a
virtual machine using the example of RT-1.
40
Hardware
2. Attach a DVD drive with inserted DVD image from section Preparation.
VBoxManage storagectl RT-1 --name "IDE Controller" --add ide
VBoxManage storageattach RT-1 --medium \
OPNsense-21.1-OpenSSL-dvd-amd64.iso \
--storagectl "IDE Controller" \
--port 0 --device 1 --type dvddrive
4. Setup and attach network card nic5 in host RT-1. This NIC must be
created via the CLI because the GUI is limited to four adapters.
1 VBoxManage modifyvm RT-1 --nic5 hostonly
2 VBoxManage modifyvm RT-1 --hostonlyadapter5 "vboxnet6"
3 VBoxManage modifyvm RT-1 --hostonlyadapter5 \
4 "VirtualBox Host-Only Ethernet Adapter #6"
5 VBoxManage modifyvm RT-1 --nictype5 82540EM
6 VBoxManage modifyvm RT-1 --macaddress5 001516010601
The instruction in line 2 is for VirtualBox on Linux and line 4 uses the
naming convention on Windows.
The first virtual firewall is now created and ready for installation. The
procedure for the remaining devices is identical, except for the network
adapters. To determine if everything is adequately connected, see Chapter 4
when the firewalls are assigned their IP addresses.
Hardware
The networks between the firewalls need a strict separation because many
protocols look for the best way on their own. And this runs – especially
with IPv6 – through unwanted paths if network separation is unclean.
41
Chapter 2. Platform
For example, the network segment with IPv4 range 192.0.2.0/24 contains
several firewalls and its network adapters:
• RT-1:em4
• RT-3:em2
• RT-core:em2
The adapters are connected to a 5-port switch. The switch has no other
connections or uplinks.
When using VLAN tagging, the cables of the firewall devices are connected
to a managed switch, for example on the first three switch ports. Listing 2.1
provides the configuration for a Cisco Catalyst switch. The OPNsense
firewalls do not notice this VLAN mapping. The configuration and setup of
vlan 1406
name WAN-2_192.0.2.0
!
interface range GigabitEthernet1/0/1 - 3
switchport mode access
switchport access vlan 1406
the cable can only be validated later when the interfaces of the firewall are
equipped with IP addresses as outlined in Chapter 4, i.e. when the ping
command can detect errors.
Embedded systems
OPNsense is based on BSD but is only offered for AMD64 architectures.
Images for other platforms, such as ARM or MIPS, are not available.
The boot process on a regular PC or server with a keyboard, screen, and
DVD drive is simple: insert DVD and boot. The installation starts in Chap-
ter 3.
42
Hardware
A practical example is the APU 1D4 -Board [7] of the Swiss manufacturer
PC-Engines. It is equipped with three Gigabit network adapters, SD card,
serial console, and two USB slots.
The OPNsense repositories do provide a precompiled flash image, but the
recommended installation method is via a USB stick. The APU board boots
from this medium and starts the installation of the operating system. The
installation image for devices with serial console includes the keyword
serial in its file name.
1. First retrieve the image file from the OPNsense download server.
wget https://pkg.opnsense.org/releases/21.1/ \
OPNsense-21.1-OpenSSL-serial-amd64.img.bz2
43
Chapter 2. Platform
physdiskwrite.exe -u -d 1 \
OPNsense-21.1-OpenSSL-serial-amd64.img
Warning
physdiskwrite writes the image file to the specified drive with
the -d option without further prompt. A wrong specification can
lead to data loss.
OPNsense is now running, but not yet installed. The installation on the SD
card starts in Chapter 3.
44
Chapter 3
Installation
The virtual networks have been set up and the machines are ready to go.
However, just like any regular computer, an operating system has to be
installed at the beginning. OPNsense does this the classic way: booting
from a DVD and starting the setup wizard.
This chapter deals with the installation of OPNsense on the (virtual) ma-
chine, as well as a minimal configuration for network connectivity.
Operating system
The system boots from the DVD or a USB stick and finally displays login: to
report the completed startup process of the live system. So far, the live DVD
has not installed anything. It has only started an OPNsense system from
the DVD. The installation begins after login with the user installer and the
password “opnsense”.
The installer asks the usual questions: 1) Where to install the operating
system? 2) How big should the partition be? and 3) Should there be a
stronger password for the login account?
Answer these questions with the default values. If the virtual machine
matches the recommendations in Chapter 2, the OPNsense installer will
find a hard drive that appears as da0 in the system. The last step initiates a
reboot and starts the fresh OPNsense installation from the disk.
The following section describes the installation steps that may differ from
the standard installation.
45
Chapter 3. Installation
If two different media are involved, only a mirrored drive will improve the
failover protection. For virtual machines, the provided hard disks should
not be located in the same datastore.
Note
The implementation of the following tips requires access to the web
GUI or the command line of the OPNsense firewall, which will be
presented in section Initial Setup of Chapter 4.
46
Storage
Storage
If the OPNsense firewall runs on regular hard disks, no further adjustments
are necessary. A closer look is recommended if the storage device is a USB
stick, CompactFlash, SD card, or any kind of flash-based memory.
Flash memory allows a limited number of write cycles. Even with this
limitation, the flash memory and the firewall will have a longer life if fewer
cycles are written. If any type of flash-based memory is used as a storage
medium for OPNsense, there are optimization solutions for operating the
firewall as long as possible:
• Memory file system. Variable and temporary files are also located in
the local file system under /var and /tmp. These directories could be
implemented as RAM disks. Thus their contents are no longer on the
storage medium but in memory.
Go to System → Settings → Miscellaneous, find the area Disk /
Memory Settings and enable the memory file system.
Warning
Since the files are in volatile memory, their contents will disap-
pear after a reboot (or power failure).
• Logging. A firewall with many clients writes many log messages that
perform write cycles to the storage medium. To protect the medium,
log entries can remain in the memory or get discarded immediately.
The procedure is the same as for the Memory file system. If the log
entries are of no interest, switch off logging completely. The verbatim
option is called Disable writing log files to the local disk and is hidden
in System → Settings → Logging.
47
Chapter 3. Installation
If the log files are required for verification or proof, this option should
not be used. OPNsense also allows you to send log entries to a remote
syslog server.
Post-installation tasks
VMware tools
Keyboard layout
The keyboard uses the US layout by default. Other countries have a dif-
ferent layout which might have an adverse effect on many commands. It
also becomes difficult to type IPv6 addresses with colons and prefixes with
slashes when the used keyboard has these keys at a different position.
If your keyboard differs from the US layout, you can switch to a different
one. The command shell behind the menu item 8) Shell can be used to
define the keyboard mapping inside the file /etc/rc.conf. For example,
a German keyboard requires a change to the variable keymap with a text
editor (see Appendix A) or via the single command:
48
Post-installation tasks
System sounds
OPNsense emits squeaky tones after system startup and before shutdown.
If this startup tone is unwanted or does not meet your personal musical
taste, it can be switched off in the System Sounds area of System →
Settings → Miscellaneous. Select and save with the option Disable the
startup/shutdown beep.
A system restart is not necessary but shows the quiet reboot of OPNsense.
49
Chapter 3. Installation
50
Chapter 4
Initial Setup
The OPNsense firewalls are now installed and require initial configuration.
This chapter starts with an introduction to the menu structure of the com-
mand line and basic setup for the web interface. After completing the initial
configuration, the firewalls are equipped with IP addresses to contact each
other.
This chapter uses a high-level approach to complete the basic configuration.
For a detailed step-by-step guide with many illustrations, please refer to the
OPNsense website.
51
Chapter 4. Initial Setup
Initial setup
The initial setup provides enough information to allow remote login. The
firewall can then be easily accessed with a web browser to begin the detailed
configuration work. The assignment of network adapters and IP addresses
is a question-and-answer process.
Defaults
Without further request, OPNsense uses the first network adapter as its
LAN interface, which connects to a trusted network. The second adapter
immediately becomes the WAN interface, with contact to the Internet or
another hostile network. Table 4.1 summarizes the default settings.
OPNsense permits access to the web interface only via the LAN interface.
For this reason, the first network adapter of any firewall in the laboratory
network becomes the management interface.
52
Initial setup
53
Chapter 4. Initial Setup
Assigning IP addresses
Now the network adapters are ready to use and require an IP address.
The first IP address of the firewall must be assigned via the console menu.
For all other network adapters, the simpler web interface method will be
available later. The setup procedure is the same for all interfaces.
It starts in the console menu with 2) Set interface IP address. The subse-
quent series of questions refer to interface number 1 (LAN) and expects the
management address to be specified, as listed in Table 1.2 on page 26. All
questions about DHCP can be confidently answered with No. All settings
for IPv6 remain empty for the time being because this will be the main
topic in Chapter 5.
Secondary setup
The secondary setup requires no further steps on the command line because
the new OPNsense machine should be accessible via the management
address. If the machine is accessible from a local computer, point a web
browser to the IPv4 address 10.5.1.1 of firewall RT-1 and wait for the usual
login page to appear. The initial login credentials of username root and
password opnsense will provide access to the web interface for the next
configuration tasks.
54
Secondary setup
Security
The first step is to choose a better password. Navigate to Lobby → Pass-
word and enter a strong password that can withstand a dictionary attack.
OPNsense versions 17.7 and later include this step during installation of
the operating system.
Next, review the software version; a firewall is a security-critical net-
work element and always requires the most recent version. At System
→ Firmware → Updates OPNsense shows which versions are available
and may offer an update. Armed with a strong password and up-to-date
software, you can now start the routine work.
Miscellaneous
At System → Settings → General OPNsense requires the usual information
about the hostname, domain, DNS server, and time zone. Thoroughly
examine the other areas below System → Settings; it will provide tools
needed for logging, notification, hardware customization, and accessing
the firewall via the web, SSH, and console.
The system setup is pretty standard in the market and does not differ signifi-
cantly from comparable firewalls of other manufacturers.
For orientation, Table 4.3 on the following page shows where the common
parameters are located in the OPNsense web interface.
Network card
Initially, OPNsense uses only the first two network adapters that appear in
the web menu at Interfaces as LAN and WAN. All other network cards must
be created manually at Interfaces → Assignments.
Firewall RT-1 has three additional adapters available, which are activated
next to the string New interface in Figure 4.2 on the next page by clicking
on the orange plus button. The new interface has the tedious name OPT1
and is available for its configuration. The adapter is now listed by name
under Interfaces. In this area, all network adapters receive their static IPv4
address and subnet mask.
This section is also the right place to manipulate Maximum Transmission
Unit (MTU), MAC address, speed, and duplex.
55
Chapter 4. Initial Setup
56
Routing
Routing
A firewall has a routing table that contains the known networks. The rout-
ing table is automatically supplied with the IP addresses of the installed
network adapters. The firewall learns all other IP networks by manual
entry.
At first, the IPv4 routing table of firewall RT-1 looks quite empty. The
section System → Routes → Status displays all entries of the routing table.
Using the example of RT-1, OPNsense will learn the path to the remote
network 10.2.1.0/24 via firewall RT-3. That makes RT-3 the gateway with
the IPv4 address 192.0.2.3. Both firewalls communicate over the shared
network segment of interface WAN2. Figure 4.3 on the following page
shows the route to the desired destination network.
The firewall will learn all other networks in the same manual way. The new
static routes are added to the local routing table. Now RT-1 knows all IP
networks of the demo network.
All other networks are reachable by a default route. A device uses its default
route when it has absolutely no idea how to reach that particular target
network. OPNsense creates a default route if the Default Gateway option is
active for a gateway.
57
Chapter 4. Initial Setup
Final testing
Now all components are ready for an accessibility test. The best tool for
this purpose is the ping command. Unfortunately, a firewall blocks all
connection requests resulting in a definite ping failure.
Thus, for a simple connection test in the lab network, the firewall needs
to temporarily open its gates. There is an excellent option for this task,
which is called Disable all packet filtering. Enable this feature at Firewall
→ Settings → Advanced to disable the filtering function of the firewall.
Warning
This option does exactly what its name says: it deactivates all filters
completely and also disables the address translation. This action turns
a strict firewall into a permissive router. The method is not suitable for
tests in productive environments.
The ping command is available at the command line and in the web GUI at
Interfaces → Diagnostics → Ping. Specify the target IP address, pick the
correct source address, and hit the ping button to start the test.
58
Summary
Note
The ping of the command line distinguishes between IPv4 and IPv6
addresses. For an IPv6 address, the command changes to ping6.
Warning
Do not forget to enable packet filtering after the connection testing is
complete.
Summary
This chapter has given a rough outline of the first setup of the OPNsense
firewall without going into detail. After completing the system configuration
and setting up the network adapters with the intended IP addresses, all
laboratory devices have connectivity with each other.
59
Chapter 4. Initial Setup
60
Chapter 5
IP Version 6
Crash course
This book is not a manual for migrating to IP version 6, so let’s go through
the basics and differences briefly.
The addresses have grown larger in size. An IPv4 address is 32 bits in
length and the IPv6 address is up to 128 bits. To make the address readable,
it is split into eight 16-bit blocks and written as a hexadecimal number. The
blocks are separated by a colon, which is distinguishable from the period
used with IPv4 addresses. A conversion from IPv4 to IPv6 is not intended.
IPv6 addresses take on a new meaning, which is roughly explained in
Table 5.1 on page 63.
A complete IPv6 address might look like this:
2001:0db8:0002:0000:0000:0000:0000:0001
61
Chapter 5. IP Version 6
The leading zeros may be omitted. This follows the same logic as IPv4
addresses and in a mathematical equation; the above address is reduced as
follows:
2001:db8:2:0:0:0:0:1
The next reduction concerns multiple blocks, which contain only zeros.
These must be replaced by two colons so that the address is further reduced
as follows:
2001:db8:2::1
The subnet mask from IPv4 appears as a prefix length in IPv6. The meaning
is the same: it separates the routing prefix, subnet, and interface identifier.
The final 64 bit of the address are used as Interface ID and represent the
network adapter. One of the many global registries provides the first 64 bits.
They offer address ranges to Internet Service Providers (ISP). The ISP use
these ranges to supply their customers with address prefixes. The end
customer receives a prefix between /48 (generous ISP, see Figure 5.1) and
/56 (economic ISP).
Registry /12
ISP /32
Customer /48
Site /64
62
Lab setup
as with IPv4. The devices in the lab network use reserved addresses for
documentation and Unique Local addresses, which are similar to the private
addresses of IPv4. The choice of addressing prevents lab equipment from
colliding with productive routers, even if the networks are accidentally
connected.
This concludes a brief explanation of IPv6 and its architecture. Due to this
new concept, developers must adapt all protocols or develop new ones.
This includes DNS, DHCP, all routing protocols, ICMP or ARP, which gets
additional tasks with IPv6 as Neighbor Discovery Protocol .
Lab setup
The structure of this chapter is adventurous because it skips IPv4 altogether
(except for the management interface). The lab focuses on IPv6 addresses
and IPv6 routes.
The result of this lab is the basis for all further chapters that include IPv6,
for instance, the firewall (Chapter 6) or NetFlow (Chapter 13).
In this lab scenario, all firewalls and clients participate in a basic configura-
tion for IPv6.
63
Chapter 5. IP Version 6
Each interface receives its static IPv6 address under Interfaces and the
respective network adapter name. The prefix length is always /64 in order
to avoid unnecessary complexity. The full list of addresses is printed in the
lab description of Chapter 1 in Table 1.2 on page 26.
In addition, the setup of the routes in IPv6 networks does not differ from
the procedure of IPv4 in section Routing of Chapter 4. Figure 5.2 illustrates
the two IP protocols on the same network adapter em0 of firewall RT-1.
Figure 5.3 shows the setup of a static IPv6 route.
64
Clients
Clients
All favorite client operating systems provide good support for IPv6. The
configuration of IPv6 addresses and routes is usually convenient using a
graphical method. Even dusty Windows XP has limited support for IPv6,
albeit outside the GUI. A modern Windows computer can add the IPv6
address via the well-known Network and Sharing Center. Figure 5.4 shows
a network connection with both IPv4 and IPv6 addresses.
Now CL-1 is ready for IPv6. Name resolution is best configured using the
distribution-specific method.
65
Chapter 5. IP Version 6
Connections
Clients can reach each other as soon as all firewalls are configured with
their IPv6 addresses and routes. Client CL-1 checks the connection to the
other computers and firewalls using the ping6 command. traceroute6
even shows which way the IPv6 packet takes to get to its destination:
root@CL-1:~# traceroute6 -In fd00:2::25
traceroute to fd00:2::25 (fd00:2::25), [...], 80 byte packets
1 fd00:1::1 0.315 ms 0.305 ms 0.316 ms
2 2001:db8:2::3 1.040 ms 1.089 ms 1.066 ms
3 fd00:2::25 1.697 ms 1.965 ms 1.944 ms
The traceroute command uses ICMP packets to visualize the path. These
must be allowed in the intermediate firewalls. Alternatively, the IP filter
takes a short nap; this method may still be known from the initial setup in
section Final testing on page 58 of Chapter 4.
Summary
IP Version 6 is no longer some strange phenomenon in modern networks.
Today, routers and firewalls must also understand the language of IPv6 to
protect end devices. OPNsense is well positioned for this and knows several
methods for assigning IPv6 addresses, of which the lab network only uses
static addresses.
Thus, all participants are at eye level with IPv6 and ready for any upcoming
challenges.
66
Part II
For Intermediates
67
Chapter 6
Firewall
69
Chapter 6. Firewall
OPNsense as a firewall
A packet filter has a set of rules that classify IP packets. Each rule has one
or more conditions that the package must meet for further processing. As
soon as a packet matches a rule, it is treated according to the rule action.
That is, it is either forwarded or dismissed.
This description mainly applies to all packet filters. Most firewall vendors
differ in the way the ruleset is configured and how detailed the rules can be.
OPNsense uses the first match principle to process the ruleset. It checks
the IP packet against each rule from top to bottom until a rule matches. If
there is no matching rule, the packet filter will discard the packet.
Note
The basic filter logic of OPNsense looks like this: a rule always belongs
to an interface and filters packets in the inbound direction.
The first match principle makes the order of the rules important. A blocking
rule at the beginning of the ruleset makes the subsequent rules ineffective.
It is best to sort the rules according to the following scheme:
2. Special rules. The upper part of the ruleset should contain rules with
detailed information about the source IP, target IP, and ports.
3. General rules. The lower part of the ruleset should host the general
rules. These rules filter by source networks, destination networks
with or without port numbers.
70
Lab setup
4. Cleanup rules. Next, put the rule which silently drops everything that
should not appear in the log file.
Lab setup
A firewall ruleset can grow long and become complicated. For that reason,
a small number of devices is sufficient to investigate the possibilities of the
OPNsense firewall.
The host RT-1 acts as a firewall and filters between its local network at
adapter em1, the DMZ network at em2, and the Internet behind em3
(Figure 6.1 on the following page).
The host RT-core gets degraded to a regular client, which tries to reach
firewall RT-1 via the WAN network. The target is the host labsrv located in
the DMZ and protected by RT-1.
Firewall rules
The main objective of OPNsense is to filter packets. The web UI locates
the filtering policy at Firewall → Rules. Each network adapter has its own
ruleset. All rulesets are independent of each other. The LAN interfaces
have two default rules, which allow all IPv4/IPv6 traffic to pass. The other
interfaces have no rules preinstalled and subsequently block all connection
attempts.
71
Chapter 6. Firewall
DMZ 10.4.1.0/24
fd00:4::/64
em3
198.51.100.1
WAN-1
198.51.100.6 198.51.100.0/24
2001:db8:1::/64
em1
RT-core
OPNsense
em0 – 10.5.1.6
Note
OPNsense does not issue a warning if a rule is configured correctly but
lies in the ruleset of the wrong interface. Troubleshooting will show
that no traffic is matching this specific rule.
72
Logging
• Action: Pass
• Interface: WAN1
• Direction: in
• TCP/IP Version: IPv4
• Protocol: TCP
• Destination: 10.4.1.7
• Destination port range: HTTPS
• Description: public https access to labsrv
The position of this new rule belongs to the upper section of the ruleset
because its content is specific for access to the lab server.
That way, the ruleset of all network adapters grows over time. Finally, they
include all required settings to enforce the security policy of the surrounding
network.
Block or Reject?
OPNsense can either block or reject connection attempts. Both actions
deny the request, block silently discards the packet. Reject also dis-
cards the packet, but it will inform the sender, by an ICMP packet, that
the connection was denied.
In principle, block is used for hostile networks (e.g. Internet), so that
no response packet is generated that could potentially disclose informa-
tion or amplify to a DDoS attack. For friendly networks (LAN, Wi-Fi)
reject is best used, so that the clients know immediately about the
denied connection attempt. That way, the user does not have to spend
time waiting.
Logging
A firewall rule can create a record in the log file, if it allows, rejects, or
denies a new connection attempt. The option Log packets that are handled
73
Chapter 6. Firewall
by this rule applies per rule. When the firewall has many rules with a
logging function or deals with lots of connections, the log area can become
overgrown.
Logging only makes sense if something meaningful is done with the log
messages. Otherwise, they are data garbage or a flash killer (see Chapter 3).
The usual cases are:
Even the help text of the log function advises against excessive logging:
“Hint: the firewall has limited local log space. Do not turn on
logging for everything. For those with heavy logging, consider
using a remote syslog server.”
Throughput
As the number of rules increases, the throughput rate of the firewall de-
creases. That is because OPNsense has to consult the ruleset or the connec-
tion table for each incoming packet and act accordingly. Also, some packets
require an address translation or an entry in the logbook.
74
Best practice
What is the critical size of a ruleset when the firewall throughput degrades?
It is not possible to give a definite answer to this question, it depends on the
complexity of rules and the use of aliases. In general, a ruleset is considered
large if the number of rules is in the mid four-digit range.
Best practice
A ruleset can outgrow itself over the years and become confusing. Having
a structured approach from the very start is the best way to establish an
organized ruleset.
• Keep it simple. A complicated ruleset only works until the next change
or until an error occurs. And sometimes, when reviewing the firewall
after several months, even your own rules may look strange if they
are too elaborate.
75
Chapter 6. Firewall
• Final-deny-any-rule. Use this last explicit rule to block any traffic and
create a log record for every denied packet. The logging function is
enabled on demand and during troubleshooting.
• Audit. Check the ruleset once per year. Deactivate any unused rules
and delete them later. And how do you recognize a rule that has not
matched any traffic?
The button Inspect of each rule at Firewall → Rules has counters that
enumerate the transmitted data and number of packets. When the
counter is set at 0 for both Packets and Bytes, nothing has been done
and the rule is ready for a cleanup.
Earlier versions of OPNsense show this information in the Section
Rules at Firewall → Diagnostics → pfInfo.
Additional filter
The described methods of packet filtering are usually adequate for enforcing
the security policy in an organization’s network. However, OPNsense has a
couple of tricks up its sleeve to cover special use cases.
76
Additional filter
Time-based rules
A regular firewall rule is enabled all the time without intermission. A time-
based rule has a “timetable” which tells when to filter packets and when
not to. This schedule can consist of simple specifications, e.g. 8 to 11 a.m.,
or it can implement a complex schema comprised of time, date, and day of
the week.
Figure 6.2: This firewall rule is valid only within the selected schedule
The example in Figure 6.2 shows a time-based firewall rule. Its purpose is to
permit traffic to the email server only during the daytime so that employees
will not access their emails at night. The schedule is configured at Firewall
→ Settings → Schedules and includes working days from 6 a.m. to 10 p.m.
(Figure 6.3 on the next page).
77
Chapter 6. Firewall
Note
The schedule only works reliably with a synchronized clock. By default,
OPNsense regularly picks the exact time from a public NTP server under
Services → Network Time → General.
A firewall rule (Figure 6.2) for the LAN interface allows access to this server
only within the times specified in the schedule. Outside this time range,
this rule does not apply, and the email request fails implicitly or due to a
final-deny-any rule.
Anti-spoofing
Intentionally faking the source address of an IP packet is called spoofing.
It differs from Network Address Translation (see Chapter 8) in which the
source or destination address is manipulated on purpose.
It is impossible to identify a fake address just by looking at the packet
headers. The authenticity of an address depends on the environment and
its context. For example, a packet with the source IP 10.1.1.25 is generally
a valid packet from client CL-1 at site 1. However, if this packet enters
at the WAN1 interface of firewall RT-1, then the address is incorrect and
probably fake.
78
Additional filter
Fake addresses are a trick to bypass simple packet filters or provoke certain
response packets.
OPNsense has a built-in method to detect spoofed packets. It inserts a
hidden rule for every network adapter. This rule blocks incoming packets
with a source address that belongs to one of the other adapters. This
method offers acceptable protection against address forgery. A separate
anti-spoofing rule is only necessary if multiple IP networks are reachable
behind a network adapter.
Figure 6.5: A firewall alias includes all subnetworks behind the LAN adapter
79
Chapter 6. Firewall
GeoIP
Filtering IP connections based on the country of origin is called GeoIP. An
IP address does not belong to a country, but it gets assigned to an ISP or
organization inside a country. The magic of GeoIP is basically a mapping
between IP ranges and countries. OPNsense uses the GeoIP databases
of MaxMind [9] for the geo-functions. The provider indicates a 99.8%
accuracy in the assignment of a country’s IP address.
Click on Apply and wait for OPNsense to load all IP ranges from MaxMind,
which effectively creates aliases of the type GeoIP.
Note
By default, OPNsense accepts a maximum number of 200,000 entries
in the firewall table. MaxMind provides significantly more records, so
enlarge OPNsense’s default to 800,000, for example, under Firewall
Maximum Table Entries at Firewall → Settings → Advanced.
A firewall rule for blocking at the country level prohibits all connections
with addresses in one or several countries. If IP addresses are mistakenly
assigned to this country, or if individual servers need to be excluded from
the blocking, the ruleset requires a prior exception rule.
80
Technical background
Technical background
The OPNsense firewall implements the packet filter pf from FreeBSD. Orig-
inally it was developed in 2001 for OpenBSD, but it quickly found its way
to the sources of FreeBSD. Since 2004, pf is a fixed part of the distribution.
Working with pf is performed with the command pfctl on the command
line. pfctl loads new rules, displays the active configuration, checks the
syntax, or deactivates the filter function.
The web interface of OPNsense translates the firewall rules into the syntax
of pf, so there is no need to hassle with the packet filter directly.
OPNsense does not provide a way to create new rules from the command
line. For troubleshooting, it helps to be familiar with the basic options from
pfctl. Table 6.1 shows important commands and its meaning.
When it comes to logging, pf uses a software interface called pflog. Then,
pf sends the header of a packet to pflog and lets this tool do the log house-
keeping. Unfortunately, pflog uses its own binary format, which implicates
tcpdump for the display.
81
Chapter 6. Firewall
Command Description
pfctl -d Disables the packet filter and address translation
pfctl -e Enables the packet filter and NAT
pfctl -s rules Shows the active ruleset from pf
pfctl -s nat Shows the address translation rules
pfctl -s states Shows all connections listed in the state table.
The web UI has this information available at
Firewall → Diagnostics → States Dump
OPNsense makes it easy for its users and converts the log messages from
pflog to Syslog messages using the filterlog service. The syslog service then
places the messages into the file:
/var/log/filter.log
Access this file with clog, a pipe, and the well-known commands tail,
grep or less. For example:
clog /var/log/filter.log | grep 10.5.1.7
Order of processing
The packet filter of OPNsense processes the policy in a particular order.
Each packet first passes through the NAT rules (see Chapter 8), followed by
the system rules, and finally the custom rules of the user.
82
Troubleshooting
The individual areas do not have to contain any rules. If the firewall does
not terminate VPN tunnels, then the automatic VPN rules are empty with-
out blocking traffic.
Depending on the application, more rules might sneak in so that the ap-
plication does not “get stuck” somewhere in the ruleset. These include
OpenVPN with RADIUS authentication and IPsec clients.
Only pf knows about the complete ruleset and its order of execution. It
provides the content in the file /tmp/rules.debug which has all the details
about the individual steps.
Troubleshooting
A new rule might be completely ineffective and not allow established data
traffic, but why? OPNsense provides insight into internal processes, log
files, and network traffic for this situation. The following tips are intended
to provide orientation if you have trouble finding the root cause:
1. Logic check. Is the rule part of the correct network adapter? Do the
rule details include the desired selection of source, ports, protocol,
and destination?
2. Order. Is the rule at a suitable position in the ruleset? The filter lists
are processed from top to bottom. Perhaps another rule is first called
so that the rule in question does not match at all. For the duration
of the troubleshooting, the new rule, with exceptions, may go to the
beginning of the ruleset.
3. Special case: Floating rules. The floating rules are processed before
the normal interface rules if the default option Quick is selected.
Thus, if two rules contradict each other, the floating rule wins.
83
Chapter 6. Firewall
If the logbook has no entries about the new connection, the packets may
not reach the local firewall at all. For definite proof, it is possible to observe
the transmitted packets at Interfaces → Diagnostics → Packet Capture. This
function of OPNsense starts the analysis tool tcpdump for recording and
displaying transmitted packets in real-time.
Summary
The packet filter pf of FreeBSD has quite a clear syntax, but OPNsense deals
with the modifications inside the rule files. It covers these details behind
the web interface. The administrator can then work with rules, groups, and
aliases at a high level to implement a security policy.
The possibilities are manifold: OPNsense filters by source, destination,
protocol, ports, and schedule. And that applies to both IPv4 and IPv6.
OPNsense can even use the rules to influence the Quality of Service param-
eters of the transmitted packets or to change the gateway of the outgoing
packets (see Chapter 16).
84
Chapter 7
Transparent Firewall
85
Chapter 7. Transparent Firewall
The invisibility of the firewall is also its biggest drawback during trou-
bleshooting. Regular network elements are visible and have an IP address.
It is easier to solve an issue when devices are known and appear in the
command output.
Also, the transparent firewall participates on layer 2 of the OSI model and
cannot join a dynamic routing protocol, which operates at layer 3. The
same applies to VPN tunnels and Quality of Service measures that run at IP
level.
Lab setup
The host labsrv is accessible for the remote stations RT-core and RT-3, which
are connected by the shared IP network. RT-1 is located in front of the
server as a transparent firewall. It protects access to labsrv with a sample
set of rules. The network adapters of RT-1 are bridged so that the data
traffic can flow through transparently. Figure 7.1 shows the setup of the
lab devices.
RT-1
OPNsense
em2
10.4.1.3 RT-3
OPNsense
10.4.1.7 em2 em4
eth1 bridge0
DMZ WAN-2 RT-core
labsrv 10.4.1.0/24
fd00:4::/64
10.4.1.0/24
fd00:4::/64
10.4.1.6 OPNsense
CentOS 8 vnet4 vnet6 em2
86
Configuration
Configuration
The network bridge is quickly built from the GUI. The firewall has a few
more options that are important for later operations.
2. Create network adapter. The new network port bridge0 is waiting for
duty at Interfaces → Assignments. When added to the firewall RT-1,
it is assigned the name OPT4 and is immediately present in the list of
interfaces.
4. NAT rules. The firewall is now an Ethernet bridge and must not
perform IP address translation (see Chapter 8). The default setting of
OPNsense regarding NAT is to create NAT rules automatically. They
must be manually disabled at Firewall → NAT → Outbound.
87
Chapter 7. Transparent Firewall
6. Ruleset. Filtering starts right here. After the change in step 5, only
the bridge can filter packets. The rules of their member interfaces
DMZ and WAN2 are not used anymore and should be removed – for
a better overview.
Note
The rules of the network bridge work in both directions, inbound
and outbound.
7. That’s all. Save the interfaces and ruleset and apply them.
The new bridge is ready for operation. The WAN-2 network is now di-
rectly connected to the DMZ network 10.4.1.0/24. As a consequence,
88
Filter operation
both networks share the same IP subnet. The original IP addresses of the
WAN-2 network are omitted, and the participants RT-3 and RT-core need
corresponding IP addresses from the DMZ network (see Table 7.2).
Table 7.2: New addresses for the shared network of DMZ and WAN-2
Filter operation
The firewall RT-1 connects its network adapters DMZ and WAN2 to a new
bridge interface bridge0. This network bridge allows the communication of
all participants on the Ethernet level. The hosts labsrv, RT-3, and RT-core
are now all in the same broadcast domain created by the RT-1 bridge. This
setup resembles a standard switched Ethernet segment without any filtering
operation.
The firewall can examine all packets passing through the Ethernet bridge.
And it can look deeper into the packet and filter for IPv4/IPv6 addresses
and port numbers as well.
The firewall inspects in both directions: rules apply simultaneously in the
incoming and outgoing interfaces. A blocked port 80 without a source or
destination network is therefore valid in both directions. If the direction
is essential, IP addresses in the filter rule must describe the situation
accurately.
Ruleset
The structure of the ruleset and the processing of the individual lines remain
the same with the transparent firewall. The most important difference is
that the rules of a firewall bridge apply in both directions since it is no
longer possible to distinguish between inbound and outbound.
89
Chapter 7. Transparent Firewall
For instance, SSH access from 10.4.1.3 (RT-3) to 10.4.1.7 (labsrv) is shown
in the last line of Figure 7.2. The other rules shown have at least selected a
destination so that web accesses are only allowed into the DMZ.
Figure 7.2: Firewall rules with exact source and destination information
Connection test
Let’s validate the setup: ping, ssh, and curl generate connection requests
which are handled by the rules. The external host RT-core tests the new
firewall rules:
curl --head http://10.4.1.7 # allowed, TCP port 80
ssh -v 10.4.1.7 # denied, TCP port 22
ping 10.4.1.7 # denied, ICMP echo
The connection attempt via SSH fails because only a single rule handles
SSH traffic, but this one is limited to RT-3. As such, traffic from RT-core
matches the default rule and gets discarded.
When testing IPv6 connectivity, RT-core again uses curl, but this time
with an IPv6 address as target:
curl --head "http://[fd00:4::7]/"
The connection should work since access to TCP port 80 is allowed for both
IP protocols originating at RT-1.
90
Uncover transparent firewall
Note
When using an IPv6 address in the URL, use enclosing quotation marks
and square brackets. Otherwise, curl simply reports No match.
Technical background
The transparent firewall uses the same filtering methods as its explicit
variant (see Chapter 6). OPNsense assembles the network bridge using a
software component, which is developed by FreeBSD as the kernel module
if_bridge. This module offers Ethernet packet forwarding between mem-
ber interfaces and Traffic Shaping, which is used to reduce throughput.
The command line has only a single command ifconfig to set up a net-
work bridge, assign its members, and provide IP addresses. An additional
command – like brctl under Linux – is not required in FreeBSD.
91
Chapter 7. Transparent Firewall
Summary
OPNsense makes a great impression as a transparent firewall. Troubleshoot-
ing is somewhat peculiar because the firewall is invisible when running
traceroute and observing its output.
A transparent firewall is best suited for environments which cannot change
the addressing information but require a protective element to secure the
network better.
92
Chapter 8
These changed addresses are mainly transparent for the client and server.
Most applications also work with NAT. As long as the targeted server sends
its data to the requesting client, nobody questions the addressing.
NAT is present in virtually every IPv4 network design which is connected to
the Internet. Including NAT in the design does not mean there is a lack of
imagination but a lack of public IPv4 addresses. IPv6 has more than enough
addresses and therefore does not rely on NAT. In particular scenarios, e.g.
failover with several ISPs, NAT has also crept into IPv6 designs.
93
Chapter 8. Network Address Translation
Lab setup
This chapter is an excellent demonstration of why the site networks have
private addresses, and why the core networks use public addresses. Before
the client’s packets pass the WAN, the firewalls must convert them into
appropriate addresses.
Each site network has an entry point to the Internet, which is simulated by
WAN-1 and WAN-2. The firewall RT-core is used to check the NAT configu-
ration because it will only respond to public IP addresses and discard access
from private IPv4 addresses without notice.
Figure 8.1 shows the lab setup with all NAT gateways.
Site-1 10.1.1.0/24
10.1.1.1
em1 DMZ 10.4.1.0/24
em2 10.4.1.1 10.4.1.7 eth1
RT-1 labsrv
em4
192.0.2.1
em3
198.51.100.1
WAN-1 WAN-2
198.51.100.0/24 192.0.2.0/24
198.51.100.6 192.0.2.3
em1 em2
RT-core RT-3
OPNsense OPNsense
Internet
em1
10.2.1.3
Site-2 10.2.1.0/24
94
Scenarios
Scenarios
OPNsense can now demonstrate its ability to translate source and destina-
tion addresses with and without masquerading and port forwarding.
To make the situation in the core network realistic, the RT-core firewall
will not accept packets with private IP addresses on its WAN adapters.
This method represents the IP routing on the Internet and immediately
shows if the other firewalls have configured their NAT rules correctly. OPN-
sense offers the option Block private networks in the interface settings.
Alternatively, a firewall rule can filter and log requests to private addresses.
One-to-One NAT
In this first scenario, the lab server should be accessible via the public WAN
networks. For this purpose, RT-1 provides an additional IPv4 address in
each WAN network. Clients can reach the lab server using the new ad-
dresses. The addresses must match the IP network and be known to the
clients, e.g. via DNS. Figure 8.2 shows the modification of IPv4 addresses
in incoming data packets by RT-1 for the network adapter of WAN-1.
198.51.100.1 10.4.1.1
em3 em2
RT-1
198.51.100.7 10.4.1.7
NAT
permit 10.4.1.7
1. Virtual IPs. RT-1 is responsible for the two new IPv4 addresses, so
the IPs must be configured first. OPNsense implements multiple IP
95
Chapter 8. Network Address Translation
2. NAT rule. Choose what to do with the virtual IPs at Firewall → NAT
→ One-to-One. The plan is to use them as a public alias for the IP
address of the lab server. Table 8.2 shows the settings of the NAT
rules.
Attribute WAN-1 WAN-2
Interface WAN1 WAN2
Type BINAT BINAT
External network 198.51.100.7 192.0.2.7
Source / Invert 2 2
Source 10.4.1.7 10.4.1.7
Destination / Invert 2 2
Destination any any
NAT reflection Enable Enable
96
Scenarios
Validate the setup using the text-based web client curl, which is available
on all devices. Both IPv4 addresses of the lab server can be accessed from
the firewalls RT-core and RT-3 for testing purposes.
curl --head http://192.0.2.7
curl --head http://198.51.100.7
Access to the public IP address should also be possible from the internal
CL-1 client in the northern site network. Section NAT Reflection explains
why this works. The successful response contains several headers and the
status HTTP/1.1 200 OK.
The lab server logs the request, including the sender’s address, in the
log file of the Apache web server.
root@labsrv ˜> tail /var/log/httpd/access_log
198.51.100.6 - - [20/Feb/2021:20:33:27 +0100] "HEAD / HTTP/1.1" \
200 - "-" "curl/7.74.0"
192.0.2.3 - - [20/Feb/2021:20:32:55 +0100] "HEAD / HTTP/1.1" \
200 - "-" "curl/7.74.0"
10.1.1.25 - - [20/Feb/2021:20:34:16 +0100] "HEAD / HTTP/1.1" \
200 - "-" "curl/7.64.0"
97
Chapter 8. Network Address Translation
Masquerade
This unique form of NAT not only translates the IP addresses but also
remaps the port numbers.
Table 8.4: Site-1 shares the source IP address of RT-1 for outbound traffic
Table 8.4 shows the settings of the required rule. The individual lines read
like an if-then condition: if IPv4 packets are sent from the source network
98
Scenarios
10.1.1.0/24 to the WAN-1 network, then replace the source address by the
address of the WAN-1 network adapter.
By specifying the Interface address, the configuration does not expect a
predefined IP address. Choosing this option is a best practice because
the interface IPv4 address might be dynamic and not known in advance.
OPNsense therefore always uses the address of the specified interface as
the new source address for the address translation.
10.2.1.3 192.0.2.3
em1 em2
RT-3
10.2.1.0/24 192.0.2.48 ̶̵ .55
NAT
This scenario (see Figure 8.3) requires RT-3 to translate all site-local clients
to a WAN address taken from an address pool. This pool contains eight
public IPv4 addresses from 192.0.2.48 to .55. The rule of thumb is to use
one public address for each 256 internal IP addresses. The sample pool is
therefore sufficient for a maximum of 2,048 clients.
The configuration is similar to the scenario from section Simple outbound
translation and is therefore described briefly.
1. Virtual IPs. The firewall in charge RT-3 requires a virtual IP for the
subnet 192.0.2.48/29. The mode of operation is Other or Proxy ARP .
99
Chapter 8. Network Address Translation
3. Firewall rule. If not already present, a new filter rule must allow
access from the LAN to the WAN-2 network. The firewall rule is
created independently of an address translation.
The client CL-2 validates the address translation in site-2. It merely tries
to reach the IP address of the firewall RT-core. The provided website at
http://192.0.2.6/ will only respond to the request if RT-3 is configured
correctly.
Port forward
In this last scenario, RT-3 translates the destination address in the IP header
for incoming requests from the WAN network. This method is commonly
referred to as port forward. The principle is similar to One-to-One-NAT;
however, the port forwarding is different and only handles packets that
match the selected TCP or UDP port number.
In this example, the additional IPv4 address 192.0.2.25 takes over the
public representation of the CL-2 computer in the southern site network.
The TCP port 443 (https) is “forwarded” to the target computer CL-2 by the
firewall RT-3. The configuration process should already be known from the
previous scenarios:
1. Virtual IP. RT-3 requires a virtual IP of type IP Alias for the address
192.0.2.25/24 on network adapter WAN2.
2. Port forward. For two TCP ports, OPNsense also needs two entries
at Firewall → NAT → Port Forward. Table 8.5 shows the selected
settings for the HTTPS port.
The option Filter rule association looks interesting. OPNsense offers
to create a firewall rule to allow data traffic.
100
IPv6
Attribute Value
Interface WAN2
TCP/IP Version IPv4
Protocol TCP
Source / Invert 2
Source 192.0.2.25
Destination port range HTTPS
Redirect target IP 10.2.1.25
Redirect target port HTTPS
Filter rule association Pass
Note
The descriptions show that most NAT configurations require a virtual
IP. A virtual IP is not needed under the condition that the IP addresses
of the local network adapters are used.
IPv6
NAT is a workaround from the address shortage of IPv4. There is no need
for NAT in IPv6 due to the huge address range. However, ten years after the
introduction of IPv6 an address translation was added. This IPv6-to-IPv6
Network Prefix Translation (NPTv6) does not translate individual addresses
but whole IPv6 prefixes.
NPTv6 has advantages when load balancing with multiple Internet provid-
ers over IPv6 networks. In that scenario, the outgoing packet is always
translated into the appropriate IPv6 address of the respective ISP.
101
Chapter 8. Network Address Translation
fd00:2::3 2001:db8:2::3
em1 em2
RT-3
fd00:2::0015:16ff:fe11:0125 2001:db8:22::0015:16ff:fe11:0125
NPTv6
The question then arises: why not use the 2001:db8:2::/64 prefix from
the local WAN interface? The public IPv6 subnet for address translation
must not match the prefix of a local network card. For NPTv6 to work,
an additional prefix has to be added to the firewall. If the same prefix
was used, FreeBSD would have problems with neighbor detection. Also,
duplicate addresses could occur if other devices are present in the prefix of
the WAN.
Note
Unlike the NAT rules for IPv4, the Network Prefix Translation of IPv6
does not require a virtual IP address for its translations.
102
NAT Reflection
NAT Reflection
103
Chapter 8. Network Address Translation
Note
NAT Reflection is just a workaround. The more elegant approach is
to resolve the address of the server via DNS. Inside the LAN the DNS
query for labsrv.example.net returns the internal address 10.4.1.7.
When called from the WAN network, the DNS server replies with the
public address 198.51.100.7.
Technical background
OPNsense operates the address translation with the packet filter pf of
FreeBSD (see Chapter 6). For pf a manipulation of IPv4 addresses is just
another line in the ruleset, which is prefixed with the keywords nat or rdr.
The keyword nat handles the one-to-one implementation and rdr performs
port forwarding. The IPv6 prefix translation has the code word binat at pf.
Summary
OPNsense can manipulate packets in transit and exchange the addresses
in the IP header. The possibilities are manifold: an address translation can
affect only the IPv4 header of selected TCP/UDP ports (Port Forward). It
can also translate all packets to a selected destination address (One-to-One).
OPNsense also masters the standard function of every home router, i.e. to
“hide” your own IP network behind the public IPv4 address (Outbound
NAT).
Caution: an address translation always requires a firewall rule that allows
access. When non-local IP addresses are used, the configuration also needs
a virtual IP.
OPNsense does not even stop at IPv6 and lets NPTv6 rewrite entire prefixes.
This feature is required for Chapter 16 Multi-WAN.
104
Chapter 9
Management Interface
The firewall’s web interface is accessible via the Internet protocol. The
firewall needs a reachable IP address for this. If this address belongs to the
devices’ “normal” IP addresses, it is called in-band management. When this
interface gets disconnected or operates under a high load, the management
access response is slow or fails.
em1
em2 OpenSSH
em3 bsnmpd
RT-1
lighttpd
em4 em0
105
Chapter 9. Management Interface
Avoid this situation and access the firewall over a separate network adapter
with an additional IP address (see Figure 9.1). The management port works
out-of-band and:
106
on the other network adapters’ IP addresses, even if firewall rules allow
this.
The same method for securing access exists for the SSH service. The setup
can be found in the web menu at the bottom of the same page.
Firewall rules scale much better if they contain aliases. The sample alias
mgmt_net contains all IP networks that may access the web interface of the
OPNsense firewall. A list of legitimate TCP/UDP ports is present in alias
mgmt_apps. Figure 9.3 on the next page shows these aliases for multiple
networks and ports. The alias for IP networks may contain IPv4 subnets
and IPv6 prefixes as well.
If the management network is divided into additional IP areas, the firewall
needs a static route to each subnet. Enter the missing routes at System →
Routes → Configuration. That way, OPNsense will reply to the requests
from the management network back to the management network and not
to the Internet via the default route.
107
Chapter 9. Management Interface
Firewall rules
The ruleset of the management adapter at Firewall → Rules → MGMT
requires only two rules. Rule number one allows access from all IP networks
listed in the alias mgmt_net that use a port number of alias mgmt_apps
and are directed to the IP address of the firewall. The second rule blocks
and logs all other connection attempts, as shown in Figure 9.4.
Depending on the initial setup of the local OPNsense machine, the ruleset
of the network adapter MGMT still contains preconfigured entries, which
allow all inbound connections. These rules have the text “Default allow
* to any rule” in the description. They do not belong in the policy of a
management network and must be deleted.
108
Also, avoid the anti-lockout rule. Disable this rule at Firewall → Settings
→ Advanced (Figure 9.5). Once the Disable administration anti-lockout
rule option is selected, OPNsense will remove this particular firewall rule
from all network adapters.
Bandwidth limitation
Denial of service attacks (DoS) usually flood the victim with requests and
packets until they can no longer perform their routine tasks. These attacks
are even possible from inside the management network when a virus or
worm has crept in.
109
Chapter 9. Management Interface
The firewall can mitigate the effect by limiting the number of incoming
packets to a reasonable level. This bandwidth limitation falls under the term
Traffic Shaping. OPNsense offers this feature under Firewall → Shaper.
For the mitigation of DoS attacks, the firewall should transport a fixed
maximum, e.g. 10 Mbit/s, of data through its management interface.
The configuration is split in two sections. First, set a maximum bandwidth
in the Pipes section. Second, assign this restriction to a network adapter at
section Rules. Table 9.1 lists the used exemplary values.
Now all incoming and outgoing data streams of the MGMT adapter are
limited to 10 Mbit/s. For powerful firewalls, this value may be too low, and
for weak embedded systems even 10 Mbit/s might seem dangerously high.
110
Two-factor authentication
Attribute Value
[Section Pipes]
Bandwidth 10
Bandwidth Metric Mbit/s
Mask (none)
Description 10mbps
[Section Rules]
Sequence 1
Interface MGMT
Proto ip
Source any
Destination any
Target 10mbps (from Pipes)
Description mgmt
Two-factor authentication
111
Chapter 9. Management Interface
Note
The term “server” is misleading, the dial-in process does not require
another server, but rather, validates the PIN locally.
The next step is to associate each local OPNsense user account with a token
generator. Using the root user as an example, this user receives a crypto-
graphic key at System → Access → Users in the line OTP seed with the
checkbox Generate new secret (160 bit). After saving, OPNsense generates
the string and presents it as a QR code in Figure 9.7.
The admin needs a token generator, which is software or a smartphone
app that generates time-based PINs. OPNsense’s website lists Google Au-
thenticator and FreeOTP as supported apps for Apple iOS and Android.
Both apps operate in the same way: launch the app, create a new entry,
and finally scan the QR code. Then, based on the shared OTP seed, the
smartphone creates the required six-digit PIN for the root user.
Figure 9.7: Transfer the secret OTP seed by scanning the QR code
with a smartphone
112
Summary
Note
Validate two-factor authentication setup at System → Access → Tester.
In the last step, the web interface must use the second factor for authen-
tication. Select the new server and disable the Local Database in the
Authentication section under System → Settings → Administration.
From now on a successful login is only possible with a PIN and password.
In Figure 9.8 the admin authenticates via FreeOTP and his password. The
combined password is displayed in plain text for demonstration purposes
only. Usually, a browser hides the password entry behind asterisks or dots.
Summary
It is best to manage a firewall using a separate network adapter that is
specially protected and separated from user traffic. That approach increases
the security of the firewall and makes it less vulnerable to attacks or a
denial-of-service attempt.
113
Chapter 9. Management Interface
114
Part III
For Experts
115
Chapter 10
IPsec VPN
If two remote networks want to communicate with each other, they usually
use a private link. Whether this is a leased line or dial-up connection, the
communication is either expensive or slow.
The alternative is to use the Internet as a transit infrastructure between the
networks. The Internet, as a public network, is used as a transport network;
the inter-connection is only virtually available as a Virtual Private Network
(VPN).
IPv4
Ethernet ï 10.1.1.25 IPv4
Ethernet ï 10.1.1.25
TCP/ Data TCP/ Data
ð 10.2.1.25 UDP ð 10.2.1.25 UDP
IPv4 IPv4
ï 198.51.100.1 ESP ï 10.1.1.25 TCP/ Data ESP ESP-
ð 192.0.2.3 header ð 10.2.1.25 UDP trailer auth.
RT-1 encrypted
RT-3
Site-1 authenticated Site-2
10.1.1.0/24 10.2.1.0/24
fd00:1::/64 fd00:2::/64
Figure 10.1: An IP packet gets more headers when traveling through a VPN
Private networks always have private addresses that are not allowed on
the Internet. So, how can an Internet router be convinced to transport
packages between two private locations? The answer is by a tunnel, which
is the technical basis for a VPN. The responsible tunnel router at site 1
117
Chapter 10. IPsec VPN
The Internet routers only see the outer header and transport the VPN
packets, just like all other packets. For them, a VPN packet is nothing
special since they cannot see the inner structure of the packet.
The members of the private networks are not aware of the tunnel. For them,
the remote stations in the other network are directly addressable.
Security
The transmitted packets flow through the public Internet as they pass
through several Internet Service Providers. It is impossible for the operator
of the VPN to influence the path of the VPN. For the security of the VPN
connection, there is a useful recipe against curious readers: encryption.
encrypted
authenticated
In principle, it is quite simple: before a packet enters the Internet, the VPN
router encrypts the content. And after it leaves the Internet and reaches
118
Lab setup
the private network, the content is decrypted again. Figure 10.2 shows the
IP packet with and without IPsec protection.
The technical implementation is more complicated because cryptography
is not just switched on. In practice, there are cryptographic procedures,
algorithms, one-way hashing functions, key exchange, and the magic ques-
tion of authentication. All these methods are intended to help make the
encrypted packets unreadable to an unauthorized person.
Lab setup
The two WAN networks represent the Internet, which does not transport
private IP addresses. The site networks need to communicate with each
other using OPNsense firewalls and VPN tunnels. A VPN tunnel spans from
RT-1 to RT-3, and another VPN tunnel goes from RT-1 to RT-2. The structure
is shown in Figure 10.3.
198.51.100.6 WAN-1
198.51.100.0/24 em3
em1
VPN
2001:db8:1::/64
em2
RT-core 10.4.1.2
tunn
OPNsense
em0
em0– 10.5.1.3
em2 RT-2
el
192.0.2.6 OPNsense
em0
em0– 10.5.1.2
WAN-2
192.0.2.0/24 192.0.2.3
2001:db8:2::/64
em2
10.2.1.0/24
RT-3 10.2.1.3
Site-2 fd00:2::/64
OPNsense em1
em0
em0– 10.5.1.3
Figure 10.3: VPN tunnels connect the site networks over the Internet
119
Chapter 10. IPsec VPN
Connection setup
The setup of a VPN tunnel is more complicated than the three-way hand-
shake of the TCP protocol. That’s because both parties must agree on an
encryption algorithm, authentication, IP networks, and session timeout.
The following configuration process is done on firewall RT-1. For the
other two firewalls, only the IPsec interface, and the IP prefixes have to be
adapted.
120
Connection setup
Note
The rule of thumb for the choice of crypto algorithms is this: the
firewall hardware requires more computing power and stronger
encryption as the number of the algorithms increases.
The example configuration for RT-1 uses the settings from Table 10.1 on
the next page for phase 1 and Table 10.2 for phase 2.
When the configuration is saved with the button Apply changes, the firewall
tries to enter phase 1 with its peer 192.0.2.3 and negotiate the encryption
algorithm and key length.
Next, set up the VPN on firewall RT-3. Step 1 uses the IPv4 address
192.0.2.1 of RT-1. The same applies to the Local Network and Remote
Network entries from step 2, which are mirrored in the configuration of
RT-3.
121
Chapter 10. IPsec VPN
Attribute Value
Connection method default
Key Exchange version auto
Internet Protocol IPv4
Interface WAN-2
Remote gateway 192.0.2.3
Description RT-3 via IPv4
Authentication method Mutual PSK
My identifier My IP address
Peer identifier Peer IP address
Pre-Shared Key e.g. YOGHURT
Encryption algorithm AES / 256
Hash algorithm SHA256
DH key group 14 (2048 bit)
NAT Traversal Disable
Dead Peer Detection 10 seconds, 3 retries
Attribute Value
Mode Tunnel IPv4
Local Network (Type) LAN subnet
Remote Network (Type) Network
Address 10.2.1.0/24
Protocol ESP
Encryption algorithms AES / auto
Hash algorithms SHA256
Table 10.2: What travels through the site-to-site tunnel between RT-1 and RT-3?
Firewall
The IPsec tunnels of the RT-1 and RT-3 gateways are now set up, and both
firewalls try to reach each other. However, the packet filter (see Chapter 6)
might block the traffic. OPNsense is so friendly and creates the necessary
rules automatically. This only works if the IP address of the remote gateway
122
Connection setup
OPNsense also filters within the VPN tunnel. As soon as IPsec is acti-
vated, the firewall suddenly has another interface IPsec, which resembles
the VPN traffic.
The filter rules apply to internal IP addresses and filter packets flowing
through the tunnel. The use of this ruleset is common if the remote network
is unknown or belongs to an untrusted party, e.g. a partner company.
Note
An empty ruleset on the IPsec interface blocks incoming data traffic that
arrives through the VPN tunnel. Outgoing packages are not affected.
For example, firewall RT-1 could only permit requests to specific IP ad-
dresses at its LAN network. In the simplest case, the ruleset allows all
123
Chapter 10. IPsec VPN
packets from the remote network to pass through the IPsec connection, as
configured in Figure 10.4.
Depending on the scenario, it might make sense to filter the network traffic
flowing through the VPN tunnel in an outgoing direction. Then, only specific
connections may enter the tunnel. At this point, the necessary filter rules
belong to the LAN adapter.
Status
If all parts of the VPN connection are configured correctly, the web GUI
should honor VPN → IPsec → Status Overview with connection informa-
tion. Figure 10.5 shows the status of a negotiated VPN connection.
Address translation
A router in the path might change the IP address of packets traveling be-
tween the VPN peers (NAT, see Chapter 8). Then IPsec faces a problem
because it cannot distinguish between an intended IP change and a mali-
cious manipulation of the packet content. As a result, the VPN connection
will fail.
124
Address translation
Next, the lab firewall RT-core performs address translation. The device
overwrites the source address of packets from RT-3 with its IP address of
the em1 interface. The configuration is shown in Figure 10.6.
When the new NAT rule on RT-core is active, the VPN tunnel between RT-1
and RT-3 will not transport any more data. That’s because no packets from
the authenticated IPv4 address arrive at RT-1.
Firewall RT-1 must identify its partner by a criterion other than the sender
address. It is best to recognize the VPN partner by an identifier. In the
simplest case, this identifier is the IP address of the remote gateway. Even
a combination of letters is allowed. The identifier provides a bit more
security when it comes to preventing unknown VPN peers from guessing
the Pre-Shared Key.
125
Chapter 10. IPsec VPN
RT-1 can identify the sender of the IKE packets, even if the IPv4 address
of the incoming packets has been changed on its way. RT-1 will also cease
introducing itself with its IP address, and begin using an identifier.
The NAT detection is located at the bottom of the tunnel settings and is
called NAT Traversal. The option Enable triggers VPN partners to detect an
address translation between them. With Force, a NAT gateway is assumed,
and the VPN packets are unconditionally wrapped in UDP datagrams.
To which IPv4 address should firewall RT-1 send the IKE packets to es-
tablish a VPN tunnel? Since the address of the remote gateway is unknown,
RT-1 can only wait for connection requests – that’s what the connection
method Respond only is all about.
The remote firewall RT-3 needs to start the IPsec connection (connec-
tion type Start immediate). This approach is recommended because RT-3
knows the IP address of firewall RT-1. Compared to the previous configura-
tion, RT-3 requires the same settings, whereby the Distinguished name is
configured in reverse order.
126
IPv6
failed remote peer. If these heartbeats stop for a particular time, the tunnel
is terminated on purpose. Without Dead Peer Detection (DPD), the faulty
tunnel continues to run senselessly until its lifetime is exceeded and a new
tunnel is negotiated. And that could be several hours later.
Fast error detection is necessary so that the firewall can react quickly. The
IKE process immediately starts a new tunnel setup, or the routing protocol
takes care of an alternative path to the target network.
In short, DPD is a kind of ipsec-ping that can act on failure. The configura-
tion expects a time interval between two pings and a maximum number of
retries (Figure 10.8).
If no response arrives within the time span, the firewall discards this tunnel
and negotiates a new one.
IPv6
IPsec is a security protocol from the IPv6 realm. By popular request, it was
also specified for IPv4. Initially, every IPv6 device was obligated to commu-
nicate via IPsec, but during development this requirement was relaxed and
IPsec was downgraded from must to should.
An OPNsense firewall is capable of securing the communication between
two IPv6 sites with IPsec. From the perspective of IPsec and its configura-
tion, the only two changes are the IP addresses of the VPN partners and
the definition of the prefixes. In brief: replace the IPv4 addresses by IPv6
addresses in the configuration.
127
Chapter 10. IPsec VPN
A new VPN tunnel on firewall RT-1 can have the same values for authentica-
tion, algorithms, and key exchange. After setting up the IPv6 tunnel on RT-1
and RT-3, the clients from site 1 (fd00:1::/64) can reach their neighbors in
site 2 (fd00:2::/64). Below is a packet trace from CL-1 to CL-2 showing the
participating firewalls on the way:
root@cl-1 ˜> traceroute6 -In fd00:2::25
traceroute to fd00:2::25 (cl-2), 30 hops max, 80 byte packets
1 2001:db8:2::1 5.277 ms 1.285 ms 1.241 ms
2 * * *
3 fd00:2::25 1.853 ms 2.044 ms 2.118 ms
Figure 10.9: An IPsec tunnel carries local and remote IPv6 prefixes
VPN throughput
A firewall needs more performance when it must encrypt every outgoing
packet and decrypt every incoming packet. The achievable throughput rates
when using IPsec are always below the regular routing performance.
The stronger the crypto algorithms, the more time and performance the
firewall needs to work on the packets. The IPsec bandwidth depends on the
128
Troubleshooting
hardware and software: the overall throughput can increase and decrease
with the operating system, the FreeBSD kernel, and the implementation of
the algorithms.
It also depends on the method of how the performance of the IPsec gate-
way is measured. Short measurements or large IPsec buffers might show
significantly higher bandwidths because packet loss does not occur yet.
Unfortunately, only a few manufacturers disclose in their datasheets which
test methodology they use to determine the IPsec performance. For market-
ing purposes, a high peak value may be more important than the average
value of a long-term measurement.
The correct measurement of the IPsec performance of a firewall is complex
and takes place in Chapter 20.
Troubleshooting
Finding an error in the VPN environment is difficult because many hurdles
can prevent a tunnel from forming correctly. OPNsense assists with VPN →
IPsec → Advanced Settings and multiple debug options.
The most basic tools ping and traceroute can not help too much. They
can merely confirm that the tunnel is interrupted somewhere, even though
DPD has already investigated that.
For structured troubleshooting, one VPN firewall must initiate the tun-
nel setup (connection method Start immediate), and the remote gateway
must react to it (connection method Response only). By default, both sides
will start the negotiations. To begin the investigation, switch to passive
mode on the remote gateway.
Troubleshooting now focuses on the responder, who will reject the VPN
connection for a reason. And this reason will become more or less clear by
analyzing the log file located at VPN → IPsec → Log File.
Error pattern
The passive firewall will report an error in its log file during VPN negotia-
tion. The following messages show typical error patterns for problems or
differences in the configuration.
129
Chapter 10. IPsec VPN
If the encryption key on the VPN endpoints is different, the VPN process
will detect invalid packet content during decryption and discard it.
Solution: compare and correct the keys on both endpoints.
130
Technical background
The firewalls also choose the strongest common encryption for phase 2, but
this time the encryption is used to secure the transferred end-user packets.
The negotiation fails if no similarities are found. Then the responder reports
a similar message as in the IKE negotiation, but with a note about ESP,
which is an indicator for phase 2.
Solution: the offers of Phase2 proposal (SA/Key Exchange) must also have
a common basis for encryption, hash function, DH group, and key length
on both firewalls.
Technical background
OPNsense relies on the software strongSwan [10] for the cryptographic im-
plementation of the VPN tunnels. After the predecessor FreeS/WAN closed
its doors for further development in mid-2004, a total of three projects
forked the code. Besides Openswan and Libreswan, the implementation of
strongSwan is probably the best known and most powerful software. The
OPNsense firewall uses the stable version 5.9.1 of strongSwan.
131
Chapter 10. IPsec VPN
Outlook
VPN is a versatile topic and can fill an entire book. This section briefly
discusses other notable enhancements. The focus is not limited to the
IPsec protocol suite and also includes alternative approaches to setting up a
virtual private network.
IKEv2
The original version of IKE goes back to 1998 and has shown some weak-
nesses in practice. The updated version IKEv2 was launched in 2005 and
was fine-tuned until 2014. IKEv2 is more economical in messaging and
makes DoS attacks more difficult. However, the biggest advantage is the
improved interaction with NAT gateways because IKEv2 already has NAT
traversal built in. If the IKEv2 partners detect an address manipulation
during the negotiation, they encapsulate everything in UDP segments to
operate through the NAT gateway.
IKEv2 has been on the market for a few years, but is not as widespread as
its predecessor. If possible, both VPN peers should use the newer variant. If
in doubt, OPNsense provides the Auto option, which leaves the selection to
the VPN gateways. Then both partners prefer the newer version, but switch
back to IKEv1 to comply with older implementations.
Mobile clients
The tunnels constructed so far are permanent connections between two
VPN gateways. Endpoints of a VPN connection can also be mobile clients
that only sporadically require an encrypted connection to the private net-
work. These include laptops, smartphones, or home offices equipped with
a low-cost VPN router.
132
Outlook
RT-1
WAN-1
LAN Internet
WAN-2
Authentication IPsec VPN
server
Figure 10.10: OPNsense acts as a VPN dial-in gateway for mobile clients
Tinc VPN
Tinc is an open-source VPN software with a focus on full mesh. Tinc
establishes a VPN tunnel between two gateways to connect the attached
private networks, just like any other VPN software. What seems rather
unspectacular here becomes interesting as the number of VPN gateways
increases. With Tinc, all gateways connect to each other, even if there is
no direct tunnel configured between them. The result is a full mesh. If one
133
Chapter 10. IPsec VPN
of the VPN tunnels fails, the software still finds the shortest route to the
destination by choosing one of the remaining VPN nodes.
Tinc wants to be simple, robust, and safe. The simple attribute refers to the
VPN connection setup. There is no exchange of certificates or negotiations
about the best crypto algorithms.
The VPN network is robust because it consists of many tunnels. All Tinc
nodes permanently check their mutual reachability, build new tunnels, and
optimize their path tables for short distances. Manual intervention by the
administrator is not necessary.
The demand for security must not be missing in any VPN software. Tinc
offers, with preselected strong algorithms, the use of stable libraries and
openly deals with program errors and security holes.
Tinc does not encrypt its packets with IPsec but with a customized security
protocol. This protocol uses the OpenSSL library and is based on AES-256
for encryption and SHA-256 for hashing function.
Tinc is not included with OPNsense but is available as a plug-in via System
→ Firmware → Plugins. When installed, Tinc integrates into the web GUI
at VPN → Tinc.
For the firewalls in Figure 10.11, Tinc would automatically connect RT-2 to
RT-3 to allow communication between the connected networks.
RT-1
OPNsense
Con
f ig u r
ed tu
nn el
Con
f ig u r
Internet
ed tu
RT-2
el
nn
OPNsense
n
tun
el
c
mi
na
Dy
RT-3
OPNsense
134
Outlook
ZeroTier
ZeroTier describes itself as a global Ethernet switch to which any end device
can connect. The participants plug to this switch and then communicate
similarly to a local network.
The Ethernet switch is imaginary and serves just as an aid for a better
understanding. In fact, the participants connect via VPN to a predefined
VPN server operated by ZeroTier. The VPN server connects its clients to
each other, so that the devices can also communicate among themselves. It
creates an encrypted peer-to-peer network.
This transport network is responsible for the secure communication and
authentication of all participants. The transport network’s additional task
is to find the routing path efficiently.
Above this network layer, ZeroTier emulates Ethernet. That way, applica-
tions can use familiar protocols to communicate. For the participants, the
VPN appears as an Ethernet switch. The technology behind it resembles
IPsec and VXLAN.
The data packets between the ZeroTier clients are encrypted and only un-
derstood by other clients, not by the ZeroTier servers. The implemented
algorithm comes from the area of elliptic curves and offers a key length of
256 bit. The client software automatically takes care of key exchanges and
certificate management.
What does this global Ethernet switch have to do with OPNsense? OPN-
sense offers the ZeroTier client as a plug-in and thus has a connection to the
global switch. From the perspective of an OPNsense firewall, the ZeroTier
switch is another network adapter with an IP address, routes, and firewall
rules. This adapter can be used to connect locations, mobile clients, or offer
management access.
ZeroTier implements its own software client ZeroTier One, which is offered
in source code and as a precompiled binary for many operating systems
and smartphones.
135
Chapter 10. IPsec VPN
distinguishes the customers from each other and prevents customer A from
reaching the devices in customer B’s network.
The example in Figure 10.12 uses the ZeroTier cloud as a fully meshed back-
bone network, which automatically finds the shortest connections between
locations.
Site A
RT-A RT-C
OPNsense OPNsense
Site C
Site B
RT-B RT-D
OPNsense OPNsense
Ethernet Site D
switch
Overlay
network
Underlay
network ZeroTier
Controller
Internet
ZeroTier
Endpoint
Summary
IPsec is a protocol family with the explicit goal of connecting two remote
networks via an insecure medium. The main priorities are transmission
security and authentication.
OPNsense masters the art of IPsec. The various possibilities of encryption,
authentication, and transportation make initial setup complicated. However,
it also brings flexibility if OPNsense needs to communicate with IPsec peers
of other manufacturers.
The implementation of OPNsense interconnects site networks. It also offers
mobile clients a secure dial-in from anywhere to the company network for
secure access to servers and applications.
136
Chapter 11
OpenVPN
Operation
The functionality of OpenVPN corresponds to that of IPsec, i.e. a VPN gate-
way establishes an encrypted channel to another gateway. Through this
tunnel, both endpoints can communicate securely with each other and also
protect the communication of other devices.
137
Chapter 11. OpenVPN
Authentication
OPNsense supports several methods of authentication, which differ in
complexity and security.
Username
The simplest and most insecure form of authentication is to login with a
username and password. That’s because users write down their passwords
or choose obvious terms. This method of authentication can be combined
with another type to achieve a multi-factor authentication.
Pre-shared key
The OpenVPN peers trust each other if they share the same cryptographic
key and use it for authentication. Before the first authentication, the key
must be distributed to all participating OpenVPN firewalls via a secure
connection.
The authentication by pre-shared key is easy to achieve. The security of
the data transmitted depends on the confidentiality of the key. When a
pre-shared key is compromised, an attacker can decrypt all intercepted
packets.
138
Differences to IPsec
Certificates
When authentication is based on certificates, each communication peer
requires a certificate. Client and server exchange their certificates and check
each other’s validity.
It takes time and energy to implement this method because certificates have
to be created, distributed, and may be revoked. Despite these challenges,
certificate-based authentication is the most secure form of authentication.
Differences to IPsec
OpenVPN and IPsec are both considered mature and secure. OpenVPN
is open-source software and therefore visible to everyone. IPsec is not a
software, but a collection of protocols used by vendors for their software.
The software may be under a proprietary license that does not disclose the
source code.
139
Chapter 11. OpenVPN
1
The authentication with username and password is only available for mobile clients.
140
Lab setup
Lab setup
The two firewalls RT-1 and RT-2 create a VPN tunnel to enable data commu-
nication between their site networks. In this scenario, RT-2 with its DMZ
network becomes a separate site that has no direct connection to the site 1
network.
The VPN service of RT-1 also offers dial-in to the OpenVPN client. Fig-
ure 11.1 shows the connectivity of the VPN gateways and a client in site 2.
The tunnels’ IP addresses derive from 10.6.0.0/16 and fd00:6::/64.
Site-1 10.1.1.0/24
fd00:1::/64 10.1.1.1
RT-2
OPNsense
em1
10.4.1.2
RT-1 S i te -to -s i te
V P N tu n n e l em2
OPNsense
em4 em3 em3
192.0.2.1 198.51.100.1 198.51.100.2
C l i e n t-s e rv e r V P N tu n n e l
WAN-2
192.0.2.0/24 WAN-1 198.51.100.0/24
2001:db8:2::/64 2001:db8:1::/64
192.0.2.3
em2
labsrv
RT-3 CentOS 8 10.4.1.7
OPNsense eth1
em1
10.2.1.3 DMZ
Site-2 10.2.1.0/24 10.4.1.0/24
fd00:2::/64 fd00:4::/64
CL-2
Windows 10
Site-to-Site tunnel
In the first scenario, OpenVPN connects site 1 to the DMZ network through
a site-to-site VPN. This tunnel spans from firewall RT-1 to RT-2 and connects
the attached networks.
141
Chapter 11. OpenVPN
The IP packets from several computers will later flow through the tun-
nel. The confidentiality of many data connections rely on the tunnel’s
security. Depending on the environment, a pre-shared key or a certificate
can achieve the required security level. The following instructions use a
shared key.
The tunnel begins and ends at the public IP addresses of the VPN gateways.
Within the tunnel, OpenVPN uses additional addresses that may originate
from a private IP range. These addresses can later be used to quickly check
whether the tunnel is working or for permanent status checks of a monitor-
ing system.
Attribute Value
Server Mode Peer to Peer (Shared Key)
Protocol UDP
Device Mode tun
Interface WAN1
Local port 1194
IPv4 Tunnel Network 10.6.12.0/24
IPv6 Tunnel Network fd00:6:12::/64
Redirect Gateway 2
IPv4 Local Network 10.1.1.0/24
IPv6 Local Network fd00:1::/64
IPv4 Remote Network 10.4.1.0/24
IPv6 Remote Network fd00:4::/64
Address Pool 2
142
Site-to-Site tunnel
Security
143
Chapter 11. OpenVPN
Client
The remote peer of the OpenVPN server is the firewall RT-2. Configure the
settings of this host to establish a VPN connection with RT-1 at VPN →
OpenVPN → Clients. To do this, the client must provide the IP address of
the server. The specifications for local and remote IP networks are inverted
to RT-1 and listed in Table 11.3. The pre-shared key must be identical on
Attribute Value
Server Mode Peer to Peer (Shared Key)
Protocol UDP
Device mode tun
Interface WAN1
Remote server
Host or address 198.51.100.1
Port 1194
Shared Key insert the key from RT-1
IPv4 Tunnel Network 10.6.12.0/24
IPv6 Tunnel Network fd00:6:12::/64
IPv4 Remote Network 10.1.1.0/24
IPv6 Remote Network fd00:1::/64
both ends of the VPN tunnel. If RT-1 generates this key, copy the generated
key string to RT-2 via a secure channel.
Ruleset
So far, the configured tunnel fails to establish because the packet filter
blocks all OpenVPN connection attempts. Unlike the IPsec VPN, which auto-
matically creates its firewall rules, OpenVPN requires explicit permission in
the WAN interface ruleset. A simple rule that allows access to the network
adapter WAN1 via UDP port 1194 is sufficient. The access should be limited
to the source address of the VPN partner if it has a fixed IP address.
As soon as the missing firewall rule is created, the participants can agree on
a VPN connection. However, the fun does not last long because the tunnel
is not allowed to transport end-user traffic. Also, the firewall now has a
144
Site-to-Site tunnel
special ruleset for the OpenVPN tunnels. This ruleset popped up at Firewall
→ Rules → OpenVPN. It filters or allows data connections that want to
flow through the tunnel. The initial empty ruleset at the OpenVPN interface
blocks all incoming data packets that arrive through the VPN tunnel. The
logic of this additional filter function is similar to IPsec and is described in
section Firewall from Chapter 10 on page 122.
Connectivity
OPNsense provides feedback on the tunnel’s status at VPN → OpenVPN
→ Connection Status. It includes the inner and outer IP addresses of the
remote gateway and the number of bytes transferred (see Figure 11.2).
The display does not show the transported IP networks. These are available
in the routing table, which OPNsense presents at System → Routes →
Status or via the CLI command netstat -nr. The keyword ovpn in the
search field hides anything from the routing table that is not relevant to
OpenVPN.
However, the most reliable test is an end-to-end connectivity check. A quick
test is also possible with ping and traceroute.
root@cl-1:~# traceroute -I 10.4.1.7
traceroute to 10.4.1.7 (10.4.1.7), 30 hops max, 60 byte packets
1 10.1.1.1 (10.1.1.1) 0.613 ms 0.543 ms 0.551 ms
2 10.6.12.2 (10.6.12.2) 2.878 ms 3.079 ms 3.323 ms
3 labsrv (10.4.1.7) 3.430 ms 3.506 ms 3.584 ms
145
Chapter 11. OpenVPN
Client-server tunnel
If OpenVPN operates in a client-server mode, the remote peers can identify
themselves by certificate or username with a password (or both). From a
security standpoint, it is best to use certificates; however, the administrative
overhead is more complicated than a shared key.
If the deployment of certificates is too extensive, the second best option is a
combination of usernames and pre-shared keys.
The following scenario uses a compromise between security and man-
ageability: the server introduces itself with a certificate and the clients
authenticate themselves with a username and password. Also, both sides
trust each other only with a pre-shared key (two-factor authentication).
The VPN server RT-1 will validate its clients during login. First, the server
takes a close look at the offered key string. If it matches its own key, the
server compares the received username with its user list and checks the
password. If the OPNsense firewall has no local user accounts, it can query
a remote directory server via RADIUS or LDAP (see Chapter 15). If client
and server agree, the tunnel setup is complete and starts transporting en-
crypted packets.
Table 11.4 shows the relevant settings. When the interface is set to any,
OpenVPN accepts its clients via all network adapters with IPv4 and IPv6.
The selection of the IP version is located in the Protocol field. UDP4 makes
OPNsense listen to the local IPv4 address, and UDP6 makes the OpenVPN
process listen only to its IPv6 address. Choose UDP to allow both versions
of the Internet protocol. It is also possible to limit access to protocols or
network adapters with firewall rules. The same logic applies to the protocol
TCP.
The client will learn which networks are accessible through the VPN tunnel.
When connected, the client will insert the list of networks into its local
routing table.
146
Client-server tunnel
Attribute Value
Server Mode Remote Access (User Auth)
Backend for
authentication Local Database
Protocol UDP
Device Mode tun
Interface any
Local port 1195
TLS Authentication Automatically generate a shared
2
TLS authentication key
IPv4 Tunnel Network 10.6.1.0/24
IPv6 Tunnel Network fd00:6:1::/64
IPv4 Local Network 10.1.1.0/24
IPv6 Local Network fd00:1::/64
Address Pool 2
Note
OpenVPN can operate in device mode tun and tap. The tun mode is
preferred when connecting mobile devices.
For example, the OpenVPN app on Apple’s iPhone or iPad only offers the
tun mode. It is not possible to communicate with an OpenVPN server using
a tap adapter.
Click on Save and the OpenVPN process of RT-1 listens for connection
requests from clients. Any OpenVPN client with a valid key may connect.
In the lab network, client CL-2 running Windows 10 is ready to dial-in.
Client
As mentioned above, the OpenVPN software must be installed separately.
The installer is available on the OpenVPN [11] website. The software also
includes the TUN/TAP drivers.
OPNsense offers an excellent tool for setting up the VPN client. OPNsense
assembles the appropriate configuration for the selected device at VPN →
147
Chapter 11. OpenVPN
OpenVPN → Client Export (Figure 11.3). The final result is a ZIP archive
which contains the configuration files and all required certificates.
On a Windows client, extract the contents of the ZIP file in the directory:
%ProgramFiles%\OpenVPN\config
The configuration file provides instructions for the OpenVPN process. The
sample configuration from Listing 11.1 starts a client-server connection
(line 7). The communication runs via port 1195 of the UDP protocol (line 9).
If the server is reachable via IPv6, this line must contain the IPv6 address
of the server:
remote 2001:db8:2::1 1195 udp
The server will present a valid certificate that is signed by the trusted CA in
line 12. The client authenticates itself with a user password (line 11).
148
Client-server tunnel
1 dev tun
2 tun-ipv6
3 persist-tun
4 persist-key
5 cipher AES-256-CBC
6 auth SHA256
7 client
8 resolv-retry infinite
9 remote 192.0.2.1 1195 udp
10 lport 0
11 auth-user-pass
12 pkcs12 Dial_In_Server_RT_1_opnsense_lab.p12
13 tls-auth Dial_In_Server_RT_1_opnsense_lab-tls.key 1
OpenVPN will log errors and major events during tunnel setup. While
troubleshooting, it is best to increase the verbosity with the additional
configuration line:
verb 3
149
Chapter 11. OpenVPN
Finally, the OpenVPN server RT-1 requires a ruleset that allows or restricts
client connections through the tunnel. The procedure corresponds to section
Ruleset on page 144 for the site-to-site tunnel.
Troubleshooting
The OpenVPN service can describe the problem in great detail in its log
messages. OPNsense makes the OpenVPN process report to the Syslog
service. The latest messages are displayed on the web interface at VPN →
OpenVPN → Log File.
During troubleshooting, it is helpful to view the log files from both end-
points of the OpenVPN connection. If the messages narrow down the
problem but do not describe it sufficiently, it is best to increase OpenVPN
verbosity.
150
Troubleshooting
The larger the number at Verbosity level, the more detailed OpenVPN
becomes. The default setting of OPNsense is 1, which is a good fit for
normal operation. Values from 3 to 5 are helpful during troubleshooting.
Remember to reset the log level after the troubleshooting has finished.
The following sections describe typical problem scenarios during an Open-
VPN tunnel setup.
Compression
2021-02-19T21:49:28 openvpn[45170] Bad LZO decompression \
header byte: 42
One side of the connection compresses the packets before they are sent,
while the other party expects uncompressed content.
Solution: both firewalls must agree on the compression by picking either
with or without.
The local firewall starts a connection attempt and is directly rejected by its
peer. This failure can have several causes. If the IP address of the remote
gateway is specified correctly, the remote device may not have started the
OpenVPN process or may be listening on another UDP port. Alternatively,
there might be a rule missing in the ruleset that allows communication, or
the policy prohibits the connection.
151
Chapter 11. OpenVPN
Authentication failed
2021-02-19T22:10:24 openvpn[24756] Authenticate/Decrypt \
packet error: cipher final failed
Both VPN peers use a shared key, but authentication has failed. The key is
either different or was modified during transmission.
Solution: both OpenVPN firewalls must have exactly the same key file
inserted in the OPNsense configuration.
Certificates
If there is already a Public Key Infrastructure (PKI) in place, it is an excel-
lent option to use the existing Certificate Authority (CA) to generate the
certificates for clients and servers. If no CA is available, an OPNsense fire-
wall could take over this role. OPNsense includes the necessary functions
to create a CA and issue certificates at System → Trust → Authorities.
Warning
It is best to place the CA on a well-secured server (or OPNsense firewall)
that is not connected to the Internet. If the private key of the CA is
compromised, the security of the OpenVPN network is lost.
152
Technical background
Technical background
OpenVPN is a turnkey VPN software. OPNsense always comes with a cur-
rent version of OpenVPN. The firewall offers two cryptographic libraries.
By default, OpenVPN uses the OpenSSL library. In the past, OpenSSL
has attracted negative attention due to several critical security gaps. As a
consequence, OPNsense offers the alternative library LibreSSL . Change the
library at Firmware Flavour under System → Firmware → Settings. When
saved, the button Check for updates causes reinstallation of all software
components using the selected library.
153
Chapter 11. OpenVPN
Summary
The OPNsense website offers a full-fledged configuration tool that includes
virtually all OpenVPN options. This versatility allows OpenVPN to establish
tunnels against firewalls from other vendors. And if something is missing,
the Advanced configuration section is the right place for extraordinary
settings.
Authentication with certificates is fully integrated into the configuration
process. This collaboration should give self-assurance to use certificates
more frequently to secure VPN tunnels.
Setting up a site-to-site tunnel is a little strange because OPNsense imple-
ments it as a client-server setup.
A special goodie is the feature Client Export, which builds complete con-
figuration packages for various OpenVPN clients and makes them readily
available for download and use.
154
Chapter 12
High Availability
A firewall device will eventually fail someday. And then it will not fulfill its
most fundamental task of protecting computer networks.
Firewalls crash as much as other electronic gear. This constraint is an
accepted fact and the reason why high-end firewall products have additional
power supplies, fans, CPUs, or uplinks. Cheap consumer devices lack these
add-ons so the best workaround is to deploy multiple devices that act as a
group. This cluster of firewalls protects from the failure of a single device
and provides high availability service.
Within the group, the devices agree that one firewall is doing the work and
the other is just observing. Keeping track of the primary device is essential
because the passive device must take over the operation if the primary
firewall fails.
Basics
All firewall devices that join the redundancy group must comply with the
standard Common Address Redundancy Protocol (CARP).
When CARP is enabled, the firewall will listen on its network adapters for
vital signs of other CARP devices. The first member of the group considers
itself the master and sends signs of life every second to the network. The
second member of the group receives the keepalive packets and stays
passive in a backup mode, which means: observe and wait.
155
Chapter 12. High Availability
Note
For CARP to work correctly, it is irrelevant if the group contains fire-
walls, routers, servers, or other gateways. This chapter uses these
terms interchangeably.
As soon as the backup device misses three keepalive packets from the pri-
mary device, it must accept that the master has failed and considers itself
the new master. It will then take over all tasks of the failed device as fast as
possible so that end users will not recognize any outage.
Who is going to tell all computers on the attached network about the new
firewall? Nobody does, because the new firewall also took over the IP
address and MAC address of the redundancy group. For the other network
device, nothing has changed at all – except for a brief interruption.
The life signs, heartbeats, or keepalives, are IPv4 packets sent to multicast
address 224.0.0.18. These packets contain the virtual IPv4 address of the
redundancy group that all CARP routers share. Also, each redundancy
group has its unique group number, which makes it possible to operate
several CARP groups in the same network segment.
The description of CARP and its functionality are very similar to the re-
dundancy protocol Virtual Router Redundancy Protocol (VRRP). That’s
because most parts of CARP are a copy of VRRP. The license terms of the
BSD operating system do not allow the use of VRRP, so the developers just
coded a similar version and published it as CARP.
Lab network
The demonstration lab network contains three firewalls, two of which (RT-1
and RT-2) build a CARP cluster. Figure 12.1 shows the setup as a network
diagram.
All subscribers of site 1 use as a standard gateway, neither of which is the
IPv4 address of RT-1, nor that of RT-2, but rather the additional address
10.1.1.5 belonging to the CARP group. This setup belongs to the LAN side
of the devices. On the WAN side, the firewalls create an additional CARP
address along with their known IP addresses. For the failure protection to
156
Lab network
CL-1
Debian 10
10.5.1.15
10.1.1.0/24
Site-1 fd00:1::/64
lan0
10.1.1.25
RT-1
OPNsense
10.1.1.1 em1 em0
em0– 10.5.1.1 em1 10.1.1.2
10.4.1.1 10.4.1.2
em2 em2
SYNC 10.4.1.0/24 fd00:4::/64
em3 em3
198.51.100.1 198.51.100.2 RT-2
OPNsense
em0
em0– 10.5.1.2
198.51.100.6 WAN-1
198.51.100.0/24
em1 2001:db8:1::/64
RT-core
OPNsense
em0
em0– 10.5.1.6
Figure 12.1: The firewalls RT-1 and RT-2 build a CARP cluster
The filter ruleset of all firewalls are less restrictive and permit everything
so that network traffic can flow between all devices without hassle. Once
all aspects of high availability are in place, the rules should be adjusted to
enforce the local security policy.
The network segment SYNC is required and will be explained later in this
chapter.
157
Chapter 12. High Availability
CARP group
OPNsense becomes a CARP router in the web UI at Interfaces → Virtual IPs
→ Settings. Start the setup with the button Add and pick a unique group
number and a virtual address with a suitable subnet mask. Table 12.1 lists
the settings for the primary firewall RT-1.
The advertising frequency has two meanings. First, it indicates the time (in
seconds) between two heartbeat packets. Second, it reveals the priority of
the sending firewall, whereby a lower value leads to a higher priority. An
advertising frequency of 1 ensures that its sender is selected master. The
other firewall needs a higher value to become the backup device.
The password protects the CARP group from unintended members and
must be identical on all authorized devices.
When ready, hit the Save button and RT-1 will begin sending heartbeat
packets on its LAN and WAN interfaces. Configure RT-2 similarly, with the
exception of a higher advertising frequency, 1/10 for instance.
Several seconds later, both CARP candidates should have agreed on a boss
(master) and an assistant (backup). In this example, RT-1 has won and
assumed the master role (Figure 12.2).
Firewall RT-2 has similar settings, except for the status of BACKUP (Fig-
ure 12.3). If RT-2 also shows MASTER, then both firewalls are in the master
position and will fight for the virtual IP address. This misunderstanding
should not happen during normal operation since it will lead to unexpected
session disconnect on the client’s computers.
Both firewalls will become MASTER if they miss the heartbeat packets of the
other device. In that case, use the ping command to check if the machines
can reach each other by the physical IPv4 address.
158
Lab network
When MASTER and BACKUP reach an agreement, traffic can flow from
client CL-1 through the firewalls to target host RT-core. Use traceroute to
visualize the path through the network. The following listing shows that
RT-1 is responsible for forwarding the traffic.
159
Chapter 12. High Availability
Stateless
No client will notice the outage of firewall RT-1, unless it is sending or
receiving data. So what happens during a file transfer?
The exact behavior of an interruption depends on the application and time-
outs. A download on CL-1 from a public web server will stop during failover
but continue after approximately four seconds.
Address translation
A router with Internet access usually has a packet filter enabled. Most likely
it also modifies the IP address of transmitted packets (NAT, see Chapter 8)
to replace private addresses by public IPs.
Let’s move the lab network a little closer to reality and make the CARP
devices translate addresses from the internal network 10.1.1.0/24 to a
160
Address translation
correct public IPv4 address. Figure 12.4 shows the setup of NAT on firewall
RT-1.
It is important to note that the OPNsense host should not use the IPv4
address of its WAN adapter (198.51.100.1), but uses the common CARP ad-
dress (198.51.100.12) instead. The CARP address is present on all firewalls
within the CARP group, so the address is still reachable if a single firewall
fails.
It is best if RT-1 translates all outgoing packets into the CARP address,
which will move to RT-2 during failover. That way, answering packets will
flow back through RT-2 to the client.
Now the firewalls have to keep exact records: which packet must the
packet filter accept, which IP is connected with which port, and how is the
packet translated?
161
Chapter 12. High Availability
If the CARP master RT-1 fails, first, a data transfer on CL-1 will stop and
break when the failover to the backup firewall is complete. The root cause
of this behavior is the empty firewall table and empty NAT table in the
backup device.
State tables
A state table is generally a security enhancement. It keeps track of all
active sessions that the firewall has permitted. The packet filter and address
translation process consult the session table to determine if the packet in
question belongs to an established connection and may pass, or if the packet
needs additional processing.
When using multiple firewalls, this leads to a problem because each firewall
device maintains its local state table and is unaware of any other sessions.
In a CARP setup, the master router handles all sessions and keeps them in
its state table. The table of the backup router remains empty because this
router has not seen a single connection yet.
Synchronization of sessions
OPNsense approaches the problem of differing session tables with the help
of the operating system. For over ten years, FreeBSD includes the protocol
pfsync for synchronizing packet filters. pfsync is independent of CARP, and
together they make a good team.
Now the CARP master shares its session table knowledge to the backup
router. The master sends all changes in its table to a pre-defined IPv4
address or multicast address. The backup router receives the messages and
updates its local table accordingly. The objective is that all devices within
the CARP group have the same content in the firewall table and NAT table.
It is best to have a separate network segment dedicated to the state synchro-
nization. That’s because synchronization is a real-time business. A session
table dated ten seconds ago is not good for sessions that were established
within the last nine seconds.
162
Address translation
163
Chapter 12. High Availability
Now both CARP devices state each other’s table contents. In principle, it
would be sufficient if the backup router gets updates from the master, but
synchronization runs in both directions.
Synchronization of configuration
OPNsense can do more than keeping the session tables in sync. The firewall
is also able to replicate configuration changes from the primary host to the
backup machine. Any changes to a firewall cluster need to be configured
only on a single device. This consolidation saves time and prevents typing
errors.
Luckily, there is no need to invent a new protocol for this task. OPN-
sense will execute the configuration change on both nodes at the same
time. Communication with the remote firewall utilized plain HTTP. For
that reason, setting up the Synchronization Settings (XMLRPC Sync) at
System → High Availability → Settings requires a username, password,
and the IP address of the secondary node.
Best practice
There are different methods for the redundant design of firewalls, which
offer a smooth deployment and improve availability.
164
Best practice
Asymmetric routing
If a packet takes a different path on the way to the server than on the
way back, the routing is asymmetric. In theory, this is not a problem, but
in practice, stateful firewalls, NAT gateways, or IDS systems prevent a
successful connection.
It is easy with CARP to accidentally end up in asymmetric routing. This
asymmetry even occurs in the laboratory network when RT-1 is master for
the LAN side, and RT-2 is master for the WAN side.
CL-1
RT-1 RT-2
outbound inbound
RT-core
In Figure 12.6 client CL-1 sends a packet to its default gateways, which is
accepted by RT-1 as CARP master. The packet arrives at its destination via
RT-core. On the return path, the packet reaches RT-core. It will pass the
packet to the CARP cluster, and RT-2 receives it because it is master on the
WAN side. However, RT-2 does not know anything about this connection
because it has not seen the initial packet, which was routed by RT-1. If RT-2
is just acting as a stateless router, it will relay all packets solely based on
the routing table. But if RT-2 behaves stateful, it will dismiss packets that
have no record in the session table. This behavior prevents asymmetric
routing and successful communication of CL-1.
165
Chapter 12. High Availability
OPNsense can prioritize the CARP process so that the same firewall is
master for all CARP groups. This election will render the routing symmetric.
Assign priorities using the CARP advertising frequency, which is explained
in section CARP group on page 158.
Master election
The CARP router with the highest advertising frequency will win the election
and become master. The frequency is calculated from the Base and Skew
values. A higher frequency sends keepalive packets more often. CARP then
calculates the interval between two heartbeats with Formula 12.1. In a
nutshell: the higher the numbers, the lower the advertising frequency. A
lower advertising frequency ensures the firewall will not win the election
to become the master router.
Skew
Interval = Base + (12.1)
256
If both candidates use the pre-configured values of Base=1 and Skew=0,
then the router with the largest IP address will win. In this scenario, RT-2
becomes the master because its IPv4 10.1.1.2 is numerically larger than the
IP 10.1.1.1 of its opponent RT-1. The same applies to the WAN subnet.
If RT-1 is the preferred firewall device and is chosen as master, then RT-1
must offer a better advertising frequency. Keep the Base value on both
firewalls low (e.g. 1 second) for a quick failover and increase the Skew
value of RT-2 to a high value of 100.
This modification will downgrade RT-2, and the master role swings from
RT-2 to RT-1.
Synchronization
It is essential to choose the correct devices as source and destination for
synchronization to prevent overwriting new content with old values.
The synchronization of state tables works bidirectionally. On both firewalls,
insert the IP address of the partner node in the field Synchronize Peer IP.
This task will even synchronize client sessions that are flowing over the
backup firewall by accident.
166
Quicker failover
Quicker failover
Other protocols for redundancy and gateway availability achieve failover
times below one second. The keepalive interval is within the range of
several hundred milliseconds with a timeout of one second.
This indulgence timing is not possible with OPNsense. The predefined
interval between two heartbeat packets is at the same time the minimum
value: one second. Higher values are possible, but the timeout is always
three times longer. By default, this is about 3–4 seconds.
A quicker failover is possible with CARP, but the software implementation
of FreeBSD chooses a minimum value of one second. CARP can do a
sub-second failover on OpenBSD, which is not the chosen platform below
OPNsense.
Load balancing
CARP has one designated firewall that is actively working on the client
sessions. Distributing the network load to several firewall devices is possible
with a workaround.
This “poor man’s load balancing” (Figure 12.7 on the next page) assigns an
additional CARP group to every network adapter. This group must elect the
CARP backup of the primary group as master for the new group.
Following the example above, RT-1 is master and RT-2 is backup of the first
CARP group. Within the second CARP group, RT-1 is backup, and RT-2 is
master. While group 1 serves IPv4 address 10.1.1.5, group 2 could provide
address 10.1.1.6. The trick is to make half of the client computers use
10.1.1.5 as the default gateway, and the other half use 10.1.1.6 as their
gateway.
167
Chapter 12. High Availability
Site-1
10.1.1.0/24
10.1.1.5 10.1.1.6
10.1.1.1 10.1.1.2
MASTER for .5 BACKUP for .5
em1 BACKUP for .6 MASTER for .6 em1
Virtual
routers
RT-1 RT-2
MASTER for .12 BACKUP for .12
em3 BACKUP for .21 MASTER for .21 em3
198.51.100.1 198.51.100.2
198.51.100.12 198.51.100.21
168
IP version 6
The reverse settings apply to RT-2, which is backup for group 1 and master
for group 2. Figure 12.8 shows the election result.
Figure 12.8: Firewalls RT-1 and RT-2 share the network traffic
The exact numbers for the time offset are not critical, as long one firewall
uses a higher value than the other.
Now both firewalls share the network traffic. The ratio of each device is
not easily controlled: in a best case scenario, both gateways handle 50% of
all packets, in a worst case RT-1 gets 99% of the sessions and RT-2 is almost
idle with the remaining one percent.
IP version 6
CARP is ready for IPv6. But it needs more than that because the table
synchronization has issues with the new IP version. The protocol pfsync is
not yet migrated to IPv6. The pfsync protocol is not yet ready for IPv6 and
the web interface refuses to accept an IPv6 address at Synchronize Peer
IP. If the synchronization is performed over a separate network segment,
169
Chapter 12. High Availability
nobody will notice the IPv4 connection, even if the remaining network is
running purely IPv6.
Fortunately, the synchronization of configuration (XMLRPC Sync) is more
advanced and appears completely “IPv6 ready”.
Technical background
The web UI of OPNsense discloses many features and protocols that are
used below the surface: CARP, pfsync, and XMLRPC.
CARP takes the user-defined Virtual IP and generates an additional IP ad-
dress for the selected network adapter. This freely selectable IP address
has the fixed MAC address 00:00:5e:00:01:NN, where NN represents the
group number. This number is a unique identifier and permits multiple
CARP groups to act in the same network segment without violating each
other’s operation.
The vital signs from the CARP master are small packets targeted to the
multicast address 224.0.0.18 or ff02::12 when using IPv6. This address
allows all subscribers in the same network to receive the multicast packets.
The command line tool ifconfig shows all settings and election results.
For instance, the LAN adapter of RT-2 reports about IP and CARP:
root@RT-2:˜ # ifconfig em1
em1: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> [...]
options=810098<VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,VLAN_HWFILTER>
ether 00:15:16:02:01:02
inet 10.1.1.2 netmask 0xffffff00 broadcast 10.1.1.255
inet 10.1.1.5 netmask 0xffffff00 broadcast 10.1.1.255 vhid 1
inet 10.1.1.6 netmask 0xffffff00 broadcast 10.1.1.255 vhid 2
inet6 fe80::215:16ff:fe02:102%em1 prefixlen 64 scopeid 0x2
inet6 fd00:1::2 prefixlen 64
inet6 fd00:1::5 prefixlen 32 vhid 15
carp: BACKUP vhid 1 advbase 1 advskew 10
carp: MASTER vhid 2 advbase 1 advskew 0
carp: BACKUP vhid 15 advbase 1 advskew 10
media: Ethernet autoselect (1000baseT <full-duplex>)
status: active
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
170
Summary
even creates a new adapter with the appropriate name pfsync0. RT-2 can
easily display adapter’s settings:
root@RT-2:˜ # ifconfig pfsync0
pfsync0: flags=41<UP,RUNNING> metric 0 mtu 1500
pfsync: syncdev: em2 syncpeer: 10.4.1.1 maxupd: 128 defer: off
groups: pfsync
Summary
High availability of firewalls with CARP and pfsync is a stable and straight-
forward way of lowering the downtime of services. The trick is to use
multiple firewalls: two identical devices build the firewall group of which
the first device runs the business and the second device takes over as soon
as its partner fails.
The additional configuration effort is minimal, and XMLRPC Sync will
replicate the configuration changes to all members of the firewall cluster.
171
Chapter 12. High Availability
172
Chapter 13
NetFlow
173
Chapter 13. NetFlow
The firewall collects this flow record for each connection. It does not matter
if it is a short-lived DNS request of 200 bytes or a large download of 750
megabyte – everything is noted, packaged, and sent to the collector.
Lab setup
As a professional firewall operating system, OPNsense includes a NetFlow
exporter. Before enabling NetFlow, it needs some background information
about the installation: at which local interfaces should traffic be logged?
Who is the NetFlow collector? And which NetFlow version does the receiver
expect?
Site-1
RT-1
OPNsense
10.1.1.0/24 em0
em0– 10.5.1.1
Internet
fd00:1::/64
em1
10.1.1.1 198.51.100.6
CL-1 em1 em3
10.1.1.25 198.51.100.1
em2
10.4.1.1 WAN-1 RT-core
198.51.100.0/24 OPNsense
DMZ 2001:db8:1::/64 em0
em0– 10.5.1.6
10.4.1.0/24
labsrv eth1 fd00:4::/64
CentOS 8
eth0 10.5.1.7 10.4.1.7
For a first example, the firewall RT-1 is ready to report the flows from its
LAN interface to the lab server 10.4.1.7 via NetFlow version 5. Figure 13.1
shows the setup of the lab network.
174
Lab setup
Figure 13.2 explains the different roles of the NetFlow concept. A NetFlow
collector is not limited to a single exporter. Depending on the hardware,
thousands of firewalls can send their NetFlow data to the collector.
CL-1 RT-1
Internet
NetFlow RT-core
exporter
NetFlow
collector
175
Chapter 13. NetFlow
Collector
The NetFlow collector is a software that receives, understands, and stores
the information contained in NetFlow packets. A NetFlow analyzer is almost
always associated with this software and comes to several conclusions with
the collected data.
The lab environment uses a small Linux application, which receives the
flow packets and stores them on the hard drive. The software nfdump [12]
is capable of this task and runs on the lab server making it a true NetFlow
collector. For the Linux distribution CentOS 8 there is even a precompiled
package. The installation is a minimal task:
yum install epel-release
yum install nfdump
The package comes without a start script, so a little extra effort on the
command line is required.
mkdir -p /var/netflow
/usr/bin/nfcapd -D -4 -p 2055 -S 0 -l /var/netflow
These commands create a new directory for NetFlow data below /var.
Then it starts the process in the background (-D), which listens on the local
IPv4 address (-4) on the default UDP port (-p). Newly arriving packets are
first kept in memory and then are written to disk after a maximum of five
minutes (-l). The files on the hard drive have a fixed format:
nfcapd.YYYYMMDDHHMM
Each file contains flow information of exactly five minutes. The data is
written in binary format. Use the command nfdump to nicely sort and
summarize the output data:
nfdump -r /var/netflow/nfcapd.202010090935 -o line -a -O bytes
176
Troubleshooting
Troubleshooting
OPNsense provides a Cache section in the NetFlow configuration area.
On this web page, the Cache gathers all interfaces that report traffic for
NetFlow. It also includes any traffic that has not been sent to the NetFlow
collector.
Insight
If there is no NetFlow collector available in the surrounding network, OPN-
sense can act as a NetFlow collector. This integrated collector is called
Insight. The technical procedure is the same as NetFlow, with one ma-
jor difference: the exporter and collector operate on the same computer.
The checkbox Capture local in the NetFlow configuration enables local
processing. Once this is active, the web GUI adds a destination address
of 127.0.0.1, which represents the local machine. At the same time, the
collector process starts in the background. It presents the collected data in
the section Reporting → Insight of the web UI.
Insight gives a pretty good view of the stored flow data. A time-series
graph shows the throughput of the network adapter. Several pie charts (Fig-
ure 13.4) summarize highly used port numbers and requested IP addresses.
177
Chapter 13. NetFlow
Technical background
OPNsense does not reinvent the wheel to support NetFlow. It just uses
the kernel module ng_netflow, which has been part of FreeBSD for many
years. The module ng_netflow listens on the selected network adapters and
records all IP communication. As soon as a connection terminates, it puts
that information into a NetFlow packet and relays that packet to the local
socket 127.0.0.1:2055 for analysis.
There the packet meets the software samplicator [13]. It accepts the
package and forwards it to the IPv4 addresses configured as Destinations in
the web interface. The only task of samplicate is to distribute the pending
packets to several IPv4 addresses.
If the NetFlow packets are analyzed in a remote collector, OPNsense’s
work is complete. If the local collector Insight is in charge of the analysis,
samplicate passes the packets on to the socket 127.0.0.1:2056. There the
process flowd greedily waits for NetFlow information to make it more
attractive for the web interface. The results are the colorful pie charts and
time series which are visible in the section Reporting → Insight.
IPv6
Version 9 of NetFlow is capable of reporting about IPv6 connections. Un-
fortunately, the implementation of OPNsense has one small limitation: the
web interface only accepts IPv4 addresses for the communication between
exporter and collector. The contents of this IPv4 communication are Net-
Flow packets containing IPv4 and IPv6 flows.
The external NetFlow collector from section Collector on page 176 does not
have this limitation. The service nfcapd handles the different versions of
NetFlow transparently and stores the data independently of its version. The
frontend command nfdump also does not require any modification when
showing IPv6 flows.
The internal NetFlow collector Insight also has no objections to IPv6 ad-
dresses and treats them in the web interface just like IPv4 connections.
178
Summary
Summary
NetFlow is a well-known method for reporting TCP/IP connections in
computer networks. OPNsense comes with an industry standard NetFlow
exporter and even a collector for visualizing simple statistics. These tools
allow NetFlow to be used in all environments.
The view of traffic information gives a good insight into the daily work of
the firewall and addresses several questions such as: which services and
servers do the users communicate with? And how busy are the network
adapters? In addition to graphical evaluations, the web interface also
provides detailed tables with search functions and can thus even help
during troubleshooting.
179
Chapter 13. NetFlow
180
Chapter 14
Web Proxy
Proxy www.google.de
server
Internet
opnsense.org
github.com
Cache
Figure 14.1: The proxy server is a mediator between client and server
181
Chapter 14. Web Proxy
Proxy servers are available for many services: email, FTP, DNS, SIP and
application independent proxies called SOCKS. OPNsense includes a proxy
server for HTTP(s) and FTP. Further proxies for SIP, DNS, and IGMP are
available as a plug-in.
In the good old days, the advantage of a proxy server was its cache for
locally storing and providing web content because the Internet bandwidth
was low. For example, 64 kilobits per second could be transferred between
client and server, but up to 100 Mbps were possible between client and
proxy.
Nowadays, the caching of web content does not bring any noticeable speed
gain. There are two main reasons for this:
• The ratio between static and dynamic content of web pages has
changed in favor of dynamics. Google’s website displays different
content when a registered user or new visitor accesses it. In this
context, what web content would the proxy store?
However, these changes do not make the proxy service obsolete, because
the web has new demands for the network infrastructure.
Antivirus
All data flows through the proxy, providing a central location to check
the data for viruses. As soon as a virus is detected, the proxy blocks the
download and presents the user with a warning website.
Authentication
Access to the web is only permitted with user login. For this reason, only
selected users can enjoy the Internet.
182
Lab setup
Accounting
The proxy knows exactly which computer is accessing which website. When
the user has to authenticate before browsing the web, the proxy service will
add the username next to the visited web pages. The log file is therefore a
powerful data source for evaluation, statistics, and verification.
Warning
The username and even the IP address are personal data and may not
be stored without the consent of the person concerned. Check the legal
situation first!
Filter
A restricted website protects an employee or family from dangerous or
immoral content. Blocking is done by IP address, web page, web do-
main, or category. The first three groups may be blocked individually,
but this is tedious and time consuming because it involves creating filter
lists. On the other hand, using categories simplifies blocking: websites are
grouped into categories according to their content, e.g. Shopping. If the
category Shopping is allowed, many more domains like www.amazon.com
or www.ebay.com are automatically approved. If the category Shopping is
on the blacklist, the proxy forbids access to all known websites for shopping.
OPNsense itself does not assign the website category. The firewall makes
use of publicly available category lists.
Lab setup
A proxy server is no fun without real web browsing, so the lab network
needs access to the Internet. Firewall RT-core will become the gateway to
the Internet, as described in the lab set up in Chapter 1.
The proxy service runs on RT-1 and, for the high availability cluster, on
RT-2. Computer CL-1 from site-1 will access the Internet through the proxy
service.
Figure 14.2 on the following page shows the network diagram with IP
addresses and interfaces.
183
Chapter 14. Web Proxy
WAN-2
Site-1 RT-1 192.0.2.0/24
10.1.1.0/24 2001:db8:2::/64
fd00:1::/64
10.1.1.1 192.0.2.6
Internet
em1 em4
CL-1 192.0.2.1
10.1.1.25 em2
em1
10.1.1.2 198.51.100.2
em1 em3
198.51.100.6
WAN-1
RT-core
198.51.100.0/24
2001:db8:1::/64
RT-2
Figure 14.2: Lab setup with a proxy server and Internet access
Explicit proxy
The explicit proxy server expects to be addressed by the clients. The appli-
cation must therefore be proxy-enabled, and the user (or administrator)
must have configured the application accordingly. The well-known web
browsers have excellent proxy support in their settings. They just require
the IP address and port number of the server that provides the proxy service.
That’s all on the client side.
1
3
Internet
4
Proxy server
Figure 14.3: Clients communicate with the servers only through the web proxy
184
Explicit proxy
The proxy on the server side is an OPNsense firewall with a running proxy
service. Figure 14.3 illustrates how the OPNsense proxy software works. A
client requests the web proxy and reaches the service Squid (step 1). Squid
will look into its local cache first to see whether the web page is already
present, or at least some parts of it (step 2). If the web page is not in the
cache and the policy allows access, Squid retrieves the web content from
the Internet (step 3). The content is placed in the cache and passed on to
the client (step 4). The filter lists shown are used in the section Filter by
category on page 186.
At Services → Web Proxy → Administration OPNsense enables the proxy
service with the single checkbox next to Enable proxy. Confirm the changes
with the button Apply, and the first OPNsense firewall RT-1 will accept proxy
requests. Without specifying a TCP port number, OPNsense picks 3128.
A client in site-1 addresses the proxy via 10.1.1.1:3128 in its application
settings.
URL filter
The proxy now processes all web requests without restriction. Unlimited
access is good, but it could be better. It makes sense to filter inconvenient
web pages so that the client cannot reach them.
The filter function is hidden behind the middle tab of Access Control List.
The Blacklist field expects all web pages that should no longer be available
for clients. There is no need to specify the web address precisely. The URL
filter also accepts parts of the address – much like the search function in a
word processor.
For example, the website http://www.example.net should no longer be
accessible via the proxy. The entry example.net blocks not only the desired
website but also all other subdomains and even websites that have this text
somewhere in the URL, e.g.
http://open-source-firewall.com/search.html?q=example.net
Note
Blacklist entries should be as precise as possible and as general as
necessary. This principle prevents the blocking of legitimate web
addresses that match the blacklist due to an inaccurate entry.
185
Chapter 14. Web Proxy
To cut a long story short: the syntax of the blacklist utilizes regular expres-
sions. These expressions are used for matching patterns in strings. Their
syntax is peculiar, but they are extremely flexible and powerful. The black-
list of the web proxy, as well as the search function of many applications,
use the Perl Compatible Regular Expressions (PCRE) for pattern recognition
so that some knowledge will also help in other areas.
A small introduction to the regular expressions with examples is given in
Appendix B.
Filter by category
It is a time-consuming task to organize long lists of web pages. OPNsense
can group web pages with similar content into categories and apply its filter
to all records in the selected category.
It is true that the categories also contain long lists of web pages, but they
are assembled by someone else. Set up the category at the web proxy
administration section below Remote Access Control Lists. There are no
entries by default, so it is up to the administrator to add the URL of a
supplier.
186
Explicit proxy
Attribute Value
Enabled 2
Filename Shalla
URL http://www.shallalist.de/Downloads/shallalist.tar.gz
Username blank
Password blank
Categories blank
SSL ignore cert 2
Description Shalla
Table 14.1: OPNsense fetches the category listings from supplier Shalla
sense use its scheduler to update the records regularly. It is best to run this
update during the night so that the user is not disturbed during their web
surfing activities.
Restrictions
The selected rules, filters, and categories apply to all clients using the proxy
service. There is no selection by subnet, user, group, or IP range.
When a category is classified as bad, it blocks traffic from all clients to
websites that fall into that category.
OPNsense does not disclose which URLs and web domains fall into which
category. If a legitimate website is blocked, use the search function on the
provider’s website [14] to find out why this website is classified that way.
The first principle is easy to implement because it is the default setting for
OPNsense. In its original state, the proxy will not block a single web page.
187
Chapter 14. Web Proxy
The client browser will only receive an error message in its browser if the
requested web address matches an entry of the blacklist from section URL
filter on page 185.
Troubleshooting
If the proxy does not work as expected, take a look at the log file. The web
proxy keeps its log records organized at Services → Web Proxy → Access
Log.
188
Proxy cluster
SquidLog Description
2021-02-10T20:35:35 Timestamp
284 How long was the web cache working on the
transaction (in ms)
10.1.1.25 Client IP address
TCP_MISS/200 Result of the transaction (MISS reveals, that
the cache did not contain the web page)
1175 The amount of data transferred to the client
GET HTTP request method
http://example.com/ Requested URL
- Username (if the proxy requires authenti-
cation)
ORIGINAL_DST/ Cache hierarchy information
93.184.216.34
text/html HTML type and format
Proxy cluster
Multiple OPNsense firewalls with activated proxy service and identical
ruleset can increase the availability of the Internet access or can balance
the load. If an OPNsense pair is already in use as the default gateway (see
Chapter 12), the proxy service can also use this infrastructure.
A proxy cluster consisting of a primary firewall (active) and a secondary
device (passive) achieves higher availability as a single system. In the lab,
the firewall RT-1 is the preferred proxy, and RT-2 comes into play when RT-1
fails. The high availability relies on the protocol CARP (see Chapter 12).
189
Chapter 14. Web Proxy
10.1.1.1 em1
Primary
10.1.1.5 RT-1 Internet
CARP
Backup
10.1.1.5
Site-1
10.1.1.0/24
10.1.1.2 em1
RT-2
Figure 14.5: Active–passive cluster with CARP and Squid
Figure 14.5 shows the interaction between the two devices. Both firewalls
of site-1 share the highly available IPv4 address 10.1.1.5. Priorities and
other details of CARP are skipped to keep it simple.
In addition, the proxy cluster requires bidirectional State Synchronization
via the LAN adapters of RT-1 and RT-2. Also, OPNsense provides Configu-
ration Synchronization (XMLRPC Sync) to synchronize the list of allowed
categories, blacklist, and proxy settings, but only from RT-1 to RT-2. That
way, RT-1 becomes the firewall for the configuration and both firewalls
become the workers. Unfortunately, the download task to fetch the ACLs
is not synchronized and needs a manual click on the button Download ACLs.
It is no surprise that both firewalls also need an extra line in the filter-
ing ruleset, which permits XMLRPC communication between them. The
new rule allows TCP port 80 from the partner firewall on its LAN adapter.
Next, the leading firewall RT-1 requires configuration to become an explicit
web proxy. The resulting configuration must be identical on both devices to
achieve the same application behavior.
190
Proxy cluster
Note
If the proxy service is configured to listen on a specific network adapter,
it accepts requests from clients at all available addresses (IPv4, IPv6,
CARP, virtual IP) of that adapter.
The clients from site-1 communicate with their new proxy cluster by its
virtual IP address. The web browser uses the address 10.1.1.5 and the TCP
port 3128 in the proxy settings.
Clients still address the same proxy, but by its new IP address. That way, if
the firewall RT-1 crashes, the IPv4 10.1.1.5 moves to the backup device and
the proxy on RT-2 accepts the web requests from all clients.
Now it becomes clear why the proxy configuration should be identical on
both firewalls: when RT-2 has different filter lists than RT-1, the Internet
will behave strangely for the user should RT-1 crash.
Functional test
191
Chapter 14. Web Proxy
TLS Inspection
If web pages are secured with Transport Layer Security, then the server and
client first negotiate a secure connection between each other before they
exchange web content. The encrypted communication is visible by the https
in a web address. This technique is a security gain for both participants, as
routers, firewalls, and proxies no longer have access to the content. The
advantage is at the same time a disadvantage because the firewall cannot
understand the clear text messages and is unable to check the content for
virus or malware.
Note
The encryption protocol Secure Socket Layer (SSL) was continued
around the turn of the millennium under the abbreviation TLS. That
makes TLS the successor of SSL. In this chapter, both abbreviations are
used interchangeably.
2. The proxy receives that request and asks the client to wait. It will
then masquerade as a client and set up a separate https connection
to the target server.
192
TLS Inspection
Server Original
certificate server
signed by proxy
certificate
TLS TLS
4. The proxy has now established an https connection with the server.
5. Next, the proxy will relay the data from the server back to the client.
In fact, the client only has an encrypted connection with the proxy
without the user knowing it.
6. Normally the client web browser will notice this trick due to a cer-
tificate mismatch. It is up to the proxy to manipulate the presented
certificate so that the client browser will not notice.
Technically, the proxy holds two TCP connections per web request: the
first connection is between client and proxy, and the other one is between
proxy and server. Before the data from the server connection flows into the
client connection, the proxy can see the plaintext data. Now it is possible
to inspect the data for virus, malware, ransomware, in addition to content
detection and category filters.
This is where the journey ends because certificates strengthen the trust of
https connections, and the proxy server does not own the server’s crypto
material. Consequently, the proxy server must establish the https communi-
cation with the client through another certificate. And that manipulation
must be noticed by the client browser, which immediately presents its user
with a certificate warning.
193
Chapter 14. Web Proxy
How does the TLS inspection work unnoticed? The client browser must
somehow trust the TLS proxy so that it will not issue that nasty certificate
warning. The trick lies in the certificate store: the certificate of the Cer-
tificate authority (CA) that the proxy uses must be present on the client
computer. As soon as that certificate is inserted as “trusted root certificate,”
the client will have confidence in all web pages that are served by the proxy.
The certificate trick is required on every client computer that uses the proxy
server.
Certificate Authority
For the TLS proxy to work without warning, it needs its own Certificate
Authority (CA). It can be a self-created root CA (see Chapter 24), or an
intermediate CA by an existing root CA.
For the lab environment, the OPNsense firewall RT-1 becomes a new root
CA. The certificates are located at System → Trust → Authorities. The
orange button Add may create a new certificate or import an existing one.
When the Method field is set to Create an internal Certificate Authority, it
will ask all the necessary questions that make up a certificate. Table 14.3
shows sample answers for the intended Certificate Authority.
Attribute Value
Descriptive name opnsense.lab
Method Create an internal Certificate Authority
Lifetime (days) 3650
Country Code DE (Germany)
State or Province NRW
City Cologne
Organization Practical OPNsense
Email Address opnsense@example.net
Common Name opnsense-ca
The Save button will store the changes, generate cryptographic keys, and
sign them. That results in a self-signed CA hosted on the OPNsense firewall.
194
TLS Inspection
Export that CA certificate using the export CA cert button and store its
content in a file named opnsense.lab.crt. The clients later require that
file.
Configuration
The conversion to a TLS-sniffing proxy is surprisingly simple. Go to Services
→ Web Proxy → Administration and navigate to the section Forward Proxy.
Tick the field Enable SSL inspection and select the CA certificate from
Table 14.3 in the list at CA to use. Activate the eavesdropping proxy by
clicking the Apply button. Figure 14.8 shows the final configuration.
Client
In contrast to the regular web proxy, a modification at the client is es-
sential for the TLS examination to work without permanent certificate
warnings at the client browser. That will only succeed if the certificate
opnsense.lab.crt from section Certificate Authority finds its way into the
client browser.
195
Chapter 14. Web Proxy
For Internet Explorer and Google Chrome a double click on the file is suffi-
cient, after that a wizard takes over this task. During the import process,
it must be ensured that the certificate is placed in the store Trusted Root
Certification Authorities.
Firefox starts the process in the browser’s options. In the Privacy & Security
area, the certificates section is at the bottom of the page. The View Cer-
tificates button leads to a dialog box with an Import button. Firefox then
needs the certificate file opnsense.lab.crt for the import process. In the
last window, trust the certificate to identify websites explicitly. Now the
client trusts the TLS proxy.
Functional test
Nothing is easier than verification because that final check consists of
surfing to the Google website from the prepared client browser. Google’s
search engine has been using https for some time, so the TLS proxy is
triggered.
Figure 14.9 shows the web page visible inside Firefox. The certificate that
reaches the client is conspicuous. It shows the web page www.google.com,
but it is issued by the certification authority of the OPNsense proxy. For the
web browser, it is a valid certificate, because web address, issuing CA, and
(not visible in the screenshot) expiration date are genuine.
196
Transparent proxy
Transparent proxy
The normal web proxy is no secret to the user, but the transparent proxy
remains as invisible as possible. This type of proxy does not have to be
configured in the application because it works unnoticed by the user and the
application. The transparent proxy “snaps” the HTTP stream and forwards
it to its proxy service. Then the regular work begins: filtering, caching,
and fetching web pages for the client. This process requires some technical
tricks in order for it to work. For users, it is an unwanted forced proxy.
The initial setup is hardly different from the explicit proxy, except that
the option Enable Transparent HTTP proxy in the Forward Proxy tab of
Services → Web Proxy → Administration must be selected.
Now the proxy server is working in transparent mode, which means OPN-
sense does not have its fingers directly in the data stream. Without direct
contact, the proxy can not influence web access and filter anything.
It is not enough for the data to flow through the firewall. The web requests
must reach the proxy service inside the firewall. OPNsense solves this
problem by redirecting the traffic with an address translation (see Chap-
ter 8). Incoming HTTP packets from the clients on the LAN adapter are
manipulated using a NAT rule so that their destination address is the local
firewall (127.0.0.1) and their destination port is the TCP port of the proxy
(3128). The same trick works for HTTPS, where the destination port is
different and uses 3129.
Technically it is a Port Forward on the LAN adapter. Figure 14.10 shows
the two NAT rules to connect the proxy to the data stream.
After that, the further configuration behaves exactly like the standard web
proxy. It can prohibit, allow, filter, block, and categorize.
197
Chapter 14. Web Proxy
Figure 14.10: The transparent proxy requires two Port Forward rules
Technical background
OPNsense uses the number one free proxy in the open-source world: Squid.
Squid started in 1996 as a simple web cache and has worked his way up to
a full HTTP/1.1 proxy. In addition to the functions OPNsense uses, Squid
also supports Reverse Proxy and WCCP.
For content control, OPNsense uses its own methods for obtaining and
organizing categories. Based on the configured blacklist, OPNsense loads
all selected categories from the provider’s website. OPNsense then fills
these data sets of web pages, domains, and IP addresses into a simple file.
Squid then uses that file as a source for blocking its clients’ requests.
Limitations
The OPNsense web proxy is rich in features but is obviously designed for
smaller environments. This is shown by the fact that all settings, categories
and filter lists apply equally to all clients. There is no option to apply
different rules to one group of clients as to another group. This distinction
is essential in a business environment if the web proxy is implemented in a
company, hotel, or hospital.
198
Outlook
Outlook
OPNsense has some plug-ins available for the web proxy. Solid virus
protection is provided by ClamAV, which is listed as os-clamav on the
firewall. After installation, ClamAV joins the web UI under Services →
ClamAV.
The web proxy can interact with the virus scanner via the Internet Content
Adaptation Protocol (ICAP). Both services communicate through a locally
installed ICAP service, which needs a further plug-in os-c-icap installed in
the local system.
The interaction with the web proxy starts with Services → Web Proxy
→ Administration in the ICAP Settings section. The mandatory fields of
Request Modify URL and Response Modify URL refer to the local ICAP
service:
icap://[::1]:1344/avscan
As soon as all services are working and ClamAV has updated its virus
database with the latest signatures, it is time for a small security audit.
That’s basically a web download of the EICAR virus [15]. Its purpose is to
test an Antivirus installation without harming your computer.
If everything works as expected, the browser should issue a Virus warning
and the ICAP service logs:
Thu Feb 11 20:24:35 2021, 99094/4247910144, VIRUS DETECTED: \
Win.Test.EICAR_HDB-1 , http client ip: 10.1.1.25, http user: -, \
http url: https://secure.eicar.org/eicar.com
If the web proxy also inspects HTTPS connections (see section TLS Inspec-
tion on page 192), then ClamAV will even analyze the encrypted content.
199
Chapter 14. Web Proxy
Unfortunately, the plug-in does not use the category lists from section Filter
by category on page 186. It is limited to manually crafted lists. However, it
can create exceptions for individual users or groups.
Summary
OPNsense shines with the functional diversity of its proxy, which is a
fantastic product free of charge. In addition to the pure web proxy with
caching function, OPNsense also provides URL and category filters. The
latter blocks (or allows) a website based on its genre.
The transparent proxy demonstrates its strengths when the clients do not
allow or want proxy configuration. In networks without administrative
access to the clients (e.g. anonymous Wi-Fi networks and unknown devices)
this proxy type is the best choice.
OPNsense even ventures into the premier class and authorizes the proxy
service for a careful look into the encrypted web communication. Now,
even HTTPS data streams are subject to category filters and blacklists.
200
Chapter 15
Central Authentication
Protocols
There must be a common language between the authentication server and
the client. A proper protocol protects the transmitted information from
prying eyes, interconnects systems of different vendors, and checks the user
password (authentication) as well as its authority (authorization). OPN-
sense supports the proven and widely used protocols LDAP and RADIUS.
201
Chapter 15. Central Authentication
Table 15.1 shows the compatibility of various firewall services and authenti-
cation sources.
Warning
If SSH, web GUI, or console authentication uses a remote server, a
local fallback login should be allowed as the secondary method. Thus,
a login is possible even if the LDAP/RADIUS server is not reachable.
LDAP
The Lightweight Directory Access Protocol (LDAP) is a language for query-
ing and manipulating records in a directory service. Since usernames and
passwords are stored in a central directory, LDAP is suitable for authentica-
tion and authorization.
Microsoft’s Active Directory is compatible with LDAP. Therefore, a do-
main controller also acts as an LDAP server. Access to the service and its
information is only password-protected and encrypted if required.
RADIUS
A genuine authentication protocol is the Remote Authentication Dial-In User
Service (RADIUS) because it is primarily concerned with authentication
1
There must be a local user account that contains the authorization details. The password
is then checked against the authentication server via LDAP or RADIUS.
202
Lab setup
and authorization. RADIUS does not care about the management of user
accounts, so it expects some kind of directory service in the backend.
The possibilities of RADIUS are more versatile than with LDAP, because the
decision for authentication can also include the date and time as well as
the IPv4/IPv6 address of the requestor.
A Windows Server includes support for RADIUS with Network Policy Server
(NPS), which is installed as an additional service.
Lab setup
The RT-1 firewall becomes the central hub for authentication. Clients can
log on to this firewall via all available services that require authentication.
RT-1 provides OpenVPN, IPsec VPN, a web proxy, and the mandatory web
GUI for administration. The user accounts for accessing these services are
stored in an additional server, which the OPNsense firewall contacts via
LDAP and RADIUS.
203
Chapter 15. Central Authentication
Microsoft Server
Microsoft’s directory server represents an authentication system that man-
ages user data and accepts logins via LDAP and RADIUS. Alternative
products are OpenLDAP or Novell eDirectory. The examples and tutorials
refer to the Microsoft Windows Server, as this solution is widely used in
enterprise networks.
The Windows Server stores user accounts, passwords, and group member-
ships in its directory service Active Directory (AD). The server acts as a
Domain Controller and automatically provides an LDAP service. For the
lab environment, the root domain is opnsense.lab and the path within the
LDAP structure is derived from it.
Note
The Microsoft Windows Server will never disclose a user password via
LDAP.
How can the firewall verify a user password against the LDAP server? The
firewall does not check the authenticity of the password itself but logs on
to the LDAP server on behalf of the user. A successful login shows that the
user’s password is valid.
Usually, LDAP is secured with TLS to protect the transmitted data from
interceptions and manipulation. LDAP becomes LDAPS – the functionality
remains unchanged.
Note
By default, the Windows Server only offers unencrypted LDAP. The
server requires a certificate for encrypted LDAP communication.
A newly installed server has not yet activated any functions and requires
the Active Directory Domain Services role to get promoted to a domain
controller. For demonstration purposes, populate the initially blank Active
204
Microsoft Server
Directory with several organizational units (OU) to provide storage for the
user objects.
Figure 15.2 shows the administrative view of the constructed Active Direc-
tory opnsense.lab with the organizational units Admins, User, Services and
Groups. The OU Services contains AD objects from non-personal accounts,
such as a firewall that wants to connect via LDAP.
LDAP
The directory structure visible via LDAP depends on the structure of the
Windows domain and the organizational units. For that reason, config-
ure the LDAP authentication of OPNsense to match the Active Directory
structure. The examples in this chapter work only in combination with the
installed Windows Server.
On the client side, OPNsense expects an LDAP server to be configured. This
can be done by navigating to: System → Access → Servers.
205
Chapter 15. Central Authentication
Table 15.2 contains settings to match the constructed AD. Any valid AD
account is suitable to bind to the LDAP server. However, a dedicated service
account (here rt1 ) is recommended, which can be deactivated and moni-
tored in case of emergency.
Attribute Value
Descriptive name winsrv_ldap
Type LDAP
Hostname or IP address 10.1.1.16
Port value 389
Transport TCP - Standard
Protocol version 3
Bind credentials
User DN rt1
Password Password
Search scope Entire Subtree
Base DN dc=opnsense,dc=lab
Authentication containers Click Select button, tick all
container/OUs that contain user
objects
Extended Query blank
User naming attribute sAMAccountName
With this configuration, every Active Directory user is authorized to use the
services of the OPNsense firewall. Typically, a firewall service is limited to a
group of users, which OPNsense implements via the Extended query. Their
content is an LDAP filter, which is applied during authentication. The login
only succeeds if the authenticating user passes the LDAP filter and provides
a valid password.
206
Microsoft Server
LDAP filters have the operator in front, with the conditions following in
round brackets. The following practical examples provide possibilities to
limit the services of a firewall to selected users or groups.
&(ObjectClass=user)(memberOf=CN=VPN,OU=Groups,DC=OPNsense,DC=lab)
The condition of the filter is fulfilled if both checks within the brackets are
true. The authenticating user must be a user object, which is a member
(memberOf) of the VPN group. The leading symbol & represents the logic
operator AND.
|(memberOf=CN=VPN,OU=Groups,DC=OPNsense,DC=lab)(memberOf= \
CN=Webproxy,OU=Groups,DC=OPNsense,DC=lab)
The filter allows access if the applicant is a member of one of the two groups:
VPN or Webproxy. The leading symbol | represents the logical operator OR.
msNPAllowDialin=TRUE
The LDAP filter returns success if the AD user object has the attribute
msNPAllowDialin and the Boolean value true. This strange attribute is
Microsoft’s way of saying that a user may dial in. The AD user object shows
this property in the Dial-in tab. The LDAP filter evaluates to true when
Network Access Permission is set to “Allow Access”.
RADIUS
Windows Server provides a RADIUS service, which is not installed by de-
fault. Install the additional server role Network Policy and Access Services
and wait for the setup to complete. After installation, the Network Policy
Server (NPS) is available, which accepts RADIUS authentication requests
and decides the replies.
Microsoft gives its RADIUS service more decision power than LDAP. The
conditions, whether a login will be successful or not, can include a user-
name, computer, or Windows group. Besides, the registration can depend
on the time of day (e.g. only during lunch break from 12 p.m. to 1 p.m.) or
207
Chapter 15. Central Authentication
on certain days of the week (e.g. Monday to Friday). Finally, even the IPv4
or IPv6 address can be part of the decision.
Figure 15.3: Network Policy Server is an add-on role of the Windows Server
When authenticating via RADIUS, the server takes over the decision logic.
The client does not care about internal structures such as groups or or-
ganizational units. The client only sends a username with password and
expects a reply. As a result, setting up the network policy server is more
complicated than running an LDAP query.
The configuration starts in the Windows Server at Start → Windows Admin-
istrative Tools → Network Policy Server. In the RADIUS Clients section, the
NPS expects an IPv4/IPv6 address from each RADIUS client that is allowed
to use the service.
The shared secret key is a pre-shared short text that is used to encrypt parts
of the RADIUS package during communication. Figure 15.3 shows the
RADIUS client for firewall RT-1.
The Network Policies section defines the conditions under which a user may
dial in through any of the RADIUS clients. The settings in Figure 15.4 verify
whether the authenticating user is a member of the VPN group. It is even
208
Microsoft Server
Figure 15.4: The Network Policy Server grants access if the user
matches the policy
If the condition is met, the NPS behaves as specified in the Settings. In this
case, access is granted.
The network policies can consist of several individual policies that are
processed from top to bottom. If the condition of a policy matches the
authenticating user, the policy action is applied. If no policy is found, the
RADIUS server always rejects its clients. The processing logic corresponds
to the packet filter of a firewall, not for IP packets, but rather for RADIUS
clients.
Note
The network policy server logs its decisions by default to a log file in
the directory
C:\Windows\system32\LogFiles\
209
Chapter 15. Central Authentication
Attribute Value
Descriptive name winsrv_radius
Type Radius
Hostname or IP address 10.1.1.16
Shared Secret OPNsense18 (example)
Services offered Authentication
Authentication port value 1812
Authentication Timeout 5
Use the Tester to perform an authentication with a valid user account. The
NPS will reply to this request and print a message in its log file (abbrevi-
ated):
"WINSRV","IAS",02/11/2021,20:50:07, 2 ,,"opnsense.lab/User/ \
James Bond",,,,,,,,0,"10.1.1.1","RT-1",,,,,1,2,1,"VPN",0, \
"311 1 10.1.1.16 02/11/2021 19:39:49 1", [...]
A failed attempt is also logged. The log file then contains the (shortened)
entry:
"WINSRV","IAS",02/11/2021,20:52:28, 3 ,,"OPNSENSE\jbond",,,,,,,,0, \
"10.1.1.1","RT-1",,,,,,,1,,16, \
"311 1 10.1.1.16 02/11/2021 19:39:49 2", [...]
The main difference between the two lines is the marked response from the
RADIUS server: packet type 2 stands for a successful login (Access-Accept)
and type 3 represents a rejection (Access-Reject).
210
Directory-as-a-Service
Directory-as-a-Service
In the as-a-Service concept, the prefix “Directory” represents an authentica-
tion system, which is obtained from a provider in the cloud. The directory
server is located somewhere and is installed, maintained, secured, and
updated by the service provider. The critical issues of availability, quality of
service, and licensing also fall within the provider’s responsibility.
The provider offers a configuration interface and authentication service;
this lab logs in with the LDAP and RADIUS directory service.
Site-1
RT-1
RADIUS OPNsense We b p ro x y
V P N tu n n e l
Internet
LDAP
We b GU I
211
Chapter 15. Central Authentication
Figure 15.6 shows a created user. The firewall needs an additional account
to connect to the LDAP directory and search for users. From this service
account, only the DN and the password are used later on. The unique LDAP
name of the newly created account for the RT-1 firewall is:
uid=rt1,ou=Users,o=59fd73a440c56b1234567890,dc=jumpcloud,dc=com
Now the authentication service from the cloud is ready for requests.
212
Directory-as-a-Service
1. Add the certification authority that JumpCloud uses for its servers.
This step is optional but recommended.
2. Next, create a trust relationship to the LDAP server.
3. Finally, configure the firewall services to use the LDAP authentication
for user login.
If the LDAP communication passes insecure networks, it is best to encrypt
the connection. JumpClouds authentication servers are somewhere on
the Internet, so TLS must protect the connection. Ironically, JumpCloud
allows unencrypted communication, which should not be used – not even
for testing purposes.
The LDAP server will identify itself with a certificate. The OPNsense firewall
can verify this certificate only if it knows the content or if it trusts the issuing
certificate authority. The Internet service provider GoDaddy currently
generates JumpClouds certificates. The Support Center of JumpCloud [17]
explains where to obtain the corresponding CA certificates. Download the
files and add them to the firewall at System → Trust → Authorities.
If there is any doubt as to which of the numerous GoDaddy certificates is
the right one, use the following command to reveal which GoDaddy CA is
responsible for JumpCloud:
echo -n | openssl s_client -connect ldap.jumpcloud.com:636 | \
sed -ne ’/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p’
Now the firewall should have the correct certificate installed, so the con-
figuration of the LDAP client can begin. Navigate to System → Access →
Servers and click the button Add to set up a new LDAP server. Table 15.4 on
the following page shows the selected values for encrypted communication
with the JumpCloud LDAP server.
In the presented configuration, each user is authorized to use any service.
Use the Extended Query to apply any desired restrictions. That way, a user
must first match the conditions of an LDAP query before a service can be
used. Only if members of the VPN group receive a positive response from
the directory server, should the following filter be added as an Extended
query:
memberOf=cn=VPN,ou=Users,o=59fd73a440c56b1234567890, \
dc=jumpcloud,dc=com
213
Chapter 15. Central Authentication
Attribute Value
Descriptive name jumpcloud_ldap
Type LDAP
Hostname or IP address ldap.jumpcloud.com
Port value 636
Transport SSL – Encrypted
Protocol version 3
Bind credentials
User DN uid=rt1,ou=Users,o=59fd73a440c56 \
b1234567890,dc=jumpcloud,dc=com
Password Password
Search scope One Level
Base DN o=59fd73a440c56b12345,dc= \
jumpcloud,dc=com
Authentication containers ou=Users,o=59fd73a440c56b12345 \
67890,dc=jumpcloud,dc=com
User naming attribute uid
Note
The LDAP directory of JumpCloud stores the created groups in the
organizational unit Users.
Warning
Each object in JumpCloud can have individual attributes in the section
Custom User Attributes. These attributes remain hidden from an LDAP
query and cannot be used for authentication.
214
Directory-as-a-Service
Note
RADIUS is a client-server model. The OPNsense firewall acts as a
RADIUS client and sends user information to the RADIUS server in the
JumpCloud data center. The web console of JumpCloud incorrectly
describes the RADIUS objects as RADIUS servers, which can lead to
confusion, as explained in the upcoming illustrations.
215
Chapter 15. Central Authentication
Figure 15.7 on the previous page shows the configuration settings of the
RADIUS client. JumpCloud does not deliver any predefined groups, so
create them manually in the web menu at Groups. The new group type
must be Group of Users. Unlike the Active Directory, the group type is
already defined during creation.
The setup starts the remote side in OPNsense at System → Access →
Servers. The Add button immediately requests the login values shown in
Figure 15.8.
216
Directory-as-a-Service
The Shared Secret in OPNsense must match the long string specified as
Shared Secret in the JumpCloud web console. JumpCloud only offers
the Authentication service. The additional service accounting refers to
statistical data, which is intended for billing and monitoring. Each record
includes a timestamp of when users logged on and off, and the amount of
data transferred. JumpCloud silently ignores such Accounting Requests.
Dynamic IP address
JumpCloud expects a static IP address for the RADIUS client. This expec-
tation works well for customers with a fixed IP range on their Internet
connection. A RADIUS client behind a DSL connection, with a dynamic
IP address must inform JumpCloud of the new address after each change.
JumpCloud is kind enough to provide a programming interface (API) so
that the exchange of information can take place by a script.
Every JumpCloud customer has access to the API. The key for the API is
provided in the web console at API Settings. A key looks like this:
7e2563b0ca4c7988cf8ce0ec644da128351a3b62
and is required for every API access. The JumpCloud website provides a
simple example that uses the API to list all configured RADIUS clients:
1 curl --silent -X GET -H "Content-Type: application/json" \
2 -H "Accept: application/json" \
3 -H "x-api-key: 7e2563b0ca4c7988cf8ce0ec644da128351a3b62" \
4 "https://console.jumpcloud.com/api/radiusservers/" \
5 | json_pp -json_opt pretty
The addition in line 5 is optional and formats the output for better readabil-
ity. If the API key is correct, the command returns a JSON-formatted list of
RADIUS clients.
{
"totalCount" : 1,
"results" : [
{
"userPasswordExpirationAction" : "REMOVE",
"_id" : "59fe206fb63f4b3053ce0e55",
"networkSourceIp" : "203.0.113.226",
"organization" : "59fd73a440c56b127e6b7753",
217
Chapter 15. Central Authentication
"name" : "rt1.opnsense.lab",
"sharedSecret" : "hVXm5nxjZQzj5UtkKB9J@im4",
"id" : "59fe206fb63f4b3053ce0e55",
"userLockoutAction" : "REMOVE",
"mfa" : "DISABLED"
}
]
}
218
Troubleshooting
Before starting, insert API’s own key and the ID of the RADIUS client into
the script text. Appendix A explains how to modify files from the command
line.
Troubleshooting
The OPNsense web GUI comes with a quick authentication test at System
→ Access → Tester. This feature performs a login with real user credentials.
The user password is also required, which the administrator should not
know. For this reason, it is best to create a “demonstration” user, which is
not assigned to any person and is only used for troubleshooting.
Note
The methods and commands for troubleshooting are equally suitable
for the Active Directory and the services of JumpCloud.
219
Chapter 15. Central Authentication
If the authentication with the Tester fails for no apparent reason, continue
troubleshooting on the command line. The authentication process is the
same but the command line tool provides a much better error message.
Use the search command ldapsearch to run an authentication via LDAP.
When using RADIUS to debug, the os-freeradius plug-in provides a RADIUS
server as well as a command line version.
LDAP
The LDAP login from the command line expects – just like the web page – a
username and password. When the service user rt1 applies the ldapsearch
command, the user should connect to the JumpCloud server and issue the
following output:
ldapsearch -H ldaps://ldap.jumpcloud.com -n \
-b "ou=Users,o=59fd73a440c56b1234567890,dc=jumpcloud,dc=com" \
-D "uid=rt1,ou=Users,o=59fd73a440c56b1234567890,dc=jumpcloud,dc=com" \
-w my_secret_password
The command is successful if it does not issue an error message. The option
-n does nothing except for an LDAP-BIND. In case of a failure, the command
line displays an error message:
220
Troubleshooting
RADIUS
OPNsense does not provide a separated RADIUS client. The os-freeradius
plug-in is suitable for troubleshooting on the command line. It is installed
either via the web GUI or by the command:
The username and password appear in clear text in both the command and
in the output that follows. The puzzling string in line 2 is the Shared Secret
used by the RADIUS server.
A successful login with a valid user and correct group membership reports
this output:
If the RADIUS server refuses the login, use the command line tool radtest
to get the error message from the server. Ignore the Id field because it
changes in every response and is not relevant to the investigation:
221
Chapter 15. Central Authentication
No reply from server for ID 157 The RADIUS server fails to send a
response. Use the ping command to briefly validate the end-to-end com-
munication at the network level. Incidentally, a RADIUS server also refuses
to reply if the Shared Secret is different, so double-check the settings on
both sides.
Note
Use the command pkg remove os-freeradius to remove the RADIUS
plug-in from the firewall after successfully troubleshooting.
Technical background
The possibility of an external authentication differs between the OPNsense
firewall services.
The IPsec VPN service strongSwan (see Chapter 10) and the web proxy
Squid (see Chapter 14) utilize a Pluggable Authentication Module (PAM)
for the user login. The PAM module pam_opnsense.so calls the method
authenticate() from the back-end framework AuthenticationFactory.
The authentication of OpenVPN users passes through a script to validate
a user account independent of their certificate. The final step during
validation ends in the AuthenticationFactory.
The services web GUI, SSH, and Console use the integrated authentication
of OPNsense. To authenticate the user, the services use the library:
/usr/local/etc/inc/authgui.inc
The Captive Portal asks the user to enter their credentials on a web page.
The service performs the authentication in the background and validates by
script if the user has the appropriate permissions:
/usr/local/opnsense/mvc/app/controllers/OPNsense/CaptivePortal/ \
Api/AccessController.php
In the background, the AuthenticationFactory will once again decide on the
validity of usernames and passwords.
The AuthenticationFactory mentioned several times is a PHP script of the
MVC framework of OPNsense (see Chapter 24). OPNsense uses this soft-
ware to implement the configuration backup, the API, and create various
firewall functions.
222
Summary
Summary
The central authentication of OPNsense is a smart method to standardize
the login of users and administrators. Thus, the time of local accounts is a
thing of the past, since every authorized person can access every firewall
depending on his or her roles. Accounts can also be quickly disabled in case
of a security breach.
OPNsense limits protocol support to LDAP and RADIUS, which is compatible
with all popular authentication servers. For unsecured connections, LDAP
cooperates with TLS and encrypts the packet contents. RADIUS already has
a built-in crypto function, but it only protects the password.
Only the firewall troubleshooting is limited. The only way to track down a
serious problem is to use the command line.
OPNsense has more tricks that are not mentioned in this chapter. For
example, an additional one-time password can strengthen the local login
(see Chapter 9). OPNsense regulates Internet access for guests, as offered
in hotels and restaurants via a Voucher. With these generated vouch-
ers, a guest is authenticated without having to create a new user account
beforehand.
223
Chapter 15. Central Authentication
224
Part IV
For Hackers
225
Chapter 16
Multi-WAN
227
Chapter 16. Multi-WAN
This chapter examines the parallel use of multiple uplink connections that
do not belong administratively to the local network. Typically, these are
Internet connections, but they could also be MPLS or leased lines. Generally,
it is expected that the network traffic must follow the rules of the ISP –
especially concerning public IP addresses.
Requirements
What does load distribution over several links achieve? The exact require-
ments depend on the surrounding infrastructure but share the following
key points:
• Outbound load distribution: the clients in the LAN reach the web
servers on the Internet via several links.
This wish list only concerns the area of load distribution. The rules for
security on the Internet with a firewall policy still apply!
228
Load distribution in the WAN
Lab environment
This chapter includes almost all firewalls, clients, and networks of the demo
lab (Figure 16.1 on the next page). Site 1 represents an internal network
that communicates with the simulated Internet via the load-sharing service
of RT-1. Firewall RT-1 has two Internet connections: the first ISP offers
an imaginary bandwidth of 34 Mbit/s via the WAN-1 network and RT-core
via an ISP router. The second Internet link uses the WAN-2 network with
10 Mbit/s to the ISP router RT-3.
ISP routers RT-3 and RT-core have a connection to site 2. In site 2, the
host CL-2 is used as a web server to offer a few demo pages. These web
pages should only check whether a TCP connection between the sites is
established and indicate from which ISP the request originates.
229
Chapter 16. Multi-WAN
ISP-1 / WAN-1
CL-1 198.51.100.0/24
RT-core
OPNsense
Debian 10 10.2.1.25
RT-1 198.51.100.6
em1 lan0
OPNsense
198.51.100.1 em2
192.0.2.6
10.1.1.1 em3
lan0
em1 em4
10.1.1.25
192.0.2.1 CL-2
Windows 10
em2 em1
192.0.2.3 10.2.1.3
Web server
For better traceability, the Site-2 network needs an HTTP server to display
a small web page with status information. The existing CL-2 machine is
suitable for this purpose, but any other server with web service does a good
job here.
An Apache HTTP server is quickly installed under Windows. The offered
CGI script ip.cgi (see Appendix C) provides a status page and belongs to
the folder
c:\apache2\cgi-bin\
Encryption and certificates improve security on the web but do not help
build the lab, so they are not used here. TCP port 80 is sufficient for
checking the load distribution.
Operation
OPNsense couples the gateway decision with the ruleset of the firewall.
This combination allows each firewall rule to make its own decision as to
which gateway or gateway group will forward the outgoing packets.
The firewall also needs to compare its getaway decision with the availability
of that gateway. Forwarding can only be used with a fully functional
gateway and a working Internet link.
230
Configuration
Configuration
First, create a gateway and assign availability checks. Then use that gateway
in a firewall rule.
Gateways
The OPNsense firewall sends the regular network traffic via the configured
default gateway to the Internet. If there are multiple Internet links, OPN-
sense requires multiple gateways – one for each uplink and IP protocol. For
example, if two ISPs provide both IPv4 and IPv6 access, OPNsense needs
four gateways.
Use the web menu at System → Gateways → Single to create the new
gateways. The mandatory specifications for a gateway are an interface,
address family, gateway (IP address), a monitor IP, and a name that cannot
be changed later. Figure 16.2 on the following page shows the uplinks of
RT-1 and the configured gateways.
The Advanced button hides sophisticated options that allow fine-tuning of
the gateway monitoring. The weighting of a gateway is vital for a smooth
operation. It decides how much data traffic the configured gateways can
handle as soon as they are used in a gateway group – a higher weighting
number results in more network traffic.
The next section explains the selection of a good monitor IP and further
fine-tuning of the checks.
Health check
Checking the status of neighboring devices is usually performed by a routing
protocol. Routers exchange packets in regular intervals to show life and
to validate that the network adapter and the cable connection work. On
the Internet, this works via the Border Gateway Protocol (BGP), which
is intended for large scenarios. For modest environments, the OPNsense
firewall uses its own methods to check its uplinks for functionality.
In the simplest case, RT-1 regularly sends a ping to the gateway and waits
for a response. Whether or not the uplink works cannot be determined with
this check. It is best to pick a monitor IP located behind the ISP gateway.
The monitoring address must respond reliably because missing responses
231
Chapter 16. Multi-WAN
deactivate the gateway. Google’s DNS servers have achieved a high degree
of reliability, with addresses that are easy to remember: 8.8.8.8, 8.8.4.4,
2001:4860:4860::8888, and 2001:4860:4860::8844.
The frequency of the pings and the tolerance for unsuccessful tests have a
significant influence on the duration during which a broken link remains
undetected. Smaller values detect a failed Internet connection faster, but
increase the base load of the line and it may lead to false alarms.
Note
For comparison: routing protocols use a ping interval of 10 seconds
and alert after 40 seconds.
232
Configuration
Gateway Groups
A gateway group combines several gateways. Within the group, each
gateway gets a priority, which OPNsense calls Tier. It certifies to the
gateway how crucial it is within the group.
These groups and priorities can be used to configure multiple scenarios:
233
Chapter 16. Multi-WAN
The mentioned thresholds for packet loss and latency are a property of the
gateway and not of its group. The configuration is located at System →
Gateways → Single.
There are several grouping options for the two gateways in the lab network.
The example gateway group GOLD prefers a higher bandwidth gateway
RT-core and only switches to the reserve gateway RT-3 in case of an error
(Figure 16.3).
Group SILVER does it the other way around and sends traffic through the
smaller pipe to Gateway RT-3. In case of a failure, the traffic switches to
RT-core and then benefits from the higher bandwidth. Applications without
bandwidth requirements fit the DEFAULT group, which randomly passes IP
connections to a gateway.
234
Configuration
Firewall
The OPNsense firewall controls how the gateway groups are used. The
firewall rules select an individual gateway or a gateway group. The default
gateway is preselected for each new rule. For policy-based routing, simply
replace the default gateway by one of the configured gateway groups. It
is possible for every single firewall rule to use a different routing path by
modifying the Gateway field. The available options in the dropdown menu
on the left contain the gateway groups (Figure 16.4).
Address translation
The two WAN networks represent the Internet and, like the real Internet,
do not tolerate private IPv4 addresses. In addition to its new function as
load-balancer, the firewall RT-1 must replace the sender address of the
outgoing IPv4 packets with a public address (see Chapter 8). That way, the
packets of the internal clients in site 1 are also routable in the lab Internet.
When the ruleset (or randomness) has chosen the outbound interface,
OPNsense masquerade the outgoing IP packets with the IPv4 address of
that interface.
235
Chapter 16. Multi-WAN
Scenario
The load-balancer RT-1 is now ready for action but does not yet have any
rules for special routing. In this first scenario, the web access from client
CL-1 from site 1 to the remote server CL-2 in site 2 will be treated specially:
traffic to port 81 is sent over WAN-1 and RT-core, while traffic to port 82
travels over WAN-2 and RT-3.
The load-balancer will distribute access to port 80 evenly over both lines.
The web server on CL-2 provides information on the load balancing decision
based on the query’s source address.
A single firewall rule would be enough to permit the traffic. But since
there are three different requirements, three similar rules are needed. The
rules always have Allow as an action but differ in the destination port
number and their gateway decision. Figure 16.5 shows the required rules
for load balancing based on TCP ports.
Failure
The time has come: the Internet link via RT-core fails to work. The health
check detects this condition because there is no response to a ping. It is
easy to simulate the failure by using a filter rule on RT-core that does not
return pings or by deleting the virtual IP address from RT-core.
Shortly afterwards, the gateway status of load balancer RT-1 changes be-
cause the gateway RT-core is no longer available. In Figure 16.6 all gateway
groups are affected by this failure.
236
Monitoring
The outgoing connections switch to the alternative link. Existing TCP con-
nections will terminate because the outgoing packets arrive at the target
system with a different source address. The TCP protocol does not like
changing IP addresses at all. Robust applications immediately establish a
new connection so that the user does not receive an error message.
Monitoring
Without monitoring, the failure of a single link remains unnoticed until the
second Internet link also dies and makes the web inaccessible. It is therefore
important that at least a responsible administrator or a monitoring team is
informed of the status change to begin troubleshooting.
OPNsense monitors the Internet links through the dpinger process and
shows availability at System → Gateways → Single or Group.
If an uplink fails, OPNsense can contact the admin by email. The settings
for email address and mail server are located at Services → Monit. The
pre-installed check gateway_alert interacts with dpinger, learns about the
faulty gateways, and alerts via email.
237
Chapter 16. Multi-WAN
Warning
Monit sends an email for each gateway’s state change and at regular
intervals. That way, OPNsense can become very chatty on flapping
Internet links.
IPv6
When using this concept in the lab network, firewall RT-1 changes packets
from clients in their local network fd00:1::/64 to a global address from the
prefix 2001:db8:11::/64 using NPTv6. The client fd00:1::25 is then visible
on the Internet as 2001:db8:11::25. Section IPv6 of Chapter 8 on page 101
describes the configuration of NPTv6.
Warning
NPTv6 requires a static prefix, which is a feature of an Internet link for
business customers. Multi-WAN with OPNsense does not work with a
dynamic prefix on a private customer connection.
OPNsense requires two prefix translations for two gateways. The config-
uration in Figure 16.7 translates outgoing traffic into an IPv6 address of
the selected provider. If the corresponding interface or gateway has failed,
OPNsense ignores that prefix so that the IPv6 address always matches the
provider’s address range.
238
Technical background
Technical background
When dealing with numerous Internet links, OPNsense combines several
techniques that work together hand in hand: the service dpinger checks
the gateways and the packet filter pf makes a decision about routing.
OPNsense uses the software dpinger to check whether the neighboring
gateways are accessible. The name indicates its duties, dpinger is a small
service in the background that sends ICMP echos to its configured target at
regular intervals. The results are stored in a UNIX socket and provided to
the web GUI for graphical representation. A separate dpinger instance runs
for each gateway, so that four gateways require four sockets. If a connection
fails, the socket provides the following exemplary content:
root@RT-1:˜ # cat /var/run/dpinger_*.sock
RT_3_ip4 383 79 0
RT_3_ip6 375 84 0
RT_core_ip4 779 206 36
RT_core_ip6 803 221 36
The first number shows the round-trip time in milliseconds, followed by the
deviation. The last number corresponds to the packet loss in percent.
When a firewall rule references a gateway group, the generated pf rule
uses the route-to keyword to bypass the main routing table. The routing
decision then returns the value of route-to, which consists of the outgoing
interface and the IP address of the neighboring gateway.
The syntax of an example rule at the LAN adapter for incoming TCP traf-
fic to port 3389 uses route-to to send the IP packets to the gateway
198.51.100.6.
239
Chapter 16. Multi-WAN
In the case of load sharing and load distribution, i.e. with multiple routing
candidates, route-to may decide between several gateways.
pass in quick on em1 \
route-to {( em3 198.51.100.6 ) ( em4 192.0.2.6 )} \
round-robin inet proto tcp from {any} to {any} port {3389} \
keep state label "6b4c56b73616a0602b3f7830bc89d1e2"
If the gateway status changes, dpinger will notice this and adjust the pf
rules accordingly.
Summary
OPNsense is fully prepared for multiple Internet links to distribute the
data streams and satisfy the bandwidth needs of modern applications.
The firewall rules and gateway group can control very granularly which
traffic reaches the Internet via which ISP. And if an Internet connection
is interrupted, the health checks detect this issue, and OPNsense uses a
different uplink within seconds.
The load-sharing even works with IPv6, but only when the ISPs offer a fixed
prefix.
240
Chapter 17
DSL router
A regular DSL connection includes a DSL router that takes care of dialing
in, address translation, and securing the internal network. The home-use
routers are even equipped with wireless access point, telephony system,
and mini NAS. In the business environment, the router provides VPN and
manageability.
This chapter describes the use of an OPNsense firewall as a DSL router for a
VDSL2 connection with the German provider Deutsche Telekom using IPv4
and IPv6.
DSL types
The usable bandwidth of a DSL connection has increased over the last
decade. Initially, an IDSL link was capable of 144 kbit/s, which equals
two bundled ISDN channels. Nowadays, the throughput matches Ethernet
speeds at 100 Mbit/s. The available bandwidth allows Internet Service
Providers to offer more than just simple Internet access. Depending on
the bandwidth, customers can subscribe to telephony, television, or video
streaming. All provided services are wrapped in IP packets and sent through
the DSL link.
The marketing department of each ISP has picked great product names for
its services. However, the DSL technique below is similar and complies with
the rules of the International Telecommunication Union (ITU-T). Some
popular standards are briefly summarized:
241
Chapter 17. DSL router
• ITU-T G.993.5. When the VDSL2 access link uses vectoring, it can
improve the data rates by mitigating negative physical effects of the
line signals. The speeds reach 100 Mbit/s downstream and 40 Mbit/s
upstream.
The offered bandwidth and implemented technology depend on the ISP
and regulatory situation in each country.
Lab setup
In this chapter, OPNsense runs on the physical hardware board APU 1D4
[7], manufactured by PC-Engines. Chapter 2 on page 42 describes the
equipment and installation.
The APU board is not equipped with an internal VDSL modem, so an
additional modem device must terminate the VDSL line. The APU is directly
connected to this modem.
Figure 17.1 shows the connectivity of a typical DSL router.
Note
The hardware platform uses RealTek network adapters, which FreeBSD
names re0 to re2. The naming convention is described in Chapter 4
on page 53.
The adapter re2 is connected to the modem and will negotiate the dial-in.
When the dial-in process is complete, it provides access to the Internet. The
re0 interface allows SSH and HTTPS access to the OPNsense management
interface. The remaining interface re1 leads to the internal network and
provides Internet communication to the client.
242
PPPoE Dial-in
Site-2 RT-3
10.2.1.0/24 APU 1D4
OPNsense
fd00:2::/64 Internet
re1 pppoe
10.2.1.3 re2
Modem DTAG
re0 80.128.0.0/12
10.5.1.3 2003::/19
Management
10.5.1.0/24
fd00:5::/64
Figure 17.1: PC-Engines APU 1D4 as VDSL router
The separation of management access and internal LAN port only increases
security if there is a separate management network in the surrounding
infrastructure. If this is not the case, the interface re0 can also be used as a
regular LAN adapter to provide another physical port.
PPPoE Dial-in
Even though DSL is a leased line technology, most Internet providers expect
a prior dial-up with session negotiation. That way, the ISP can force a user
authentication, address assignment, and billing.
European ISPs mainly use PPPoE, which combines the point-to-point proto-
col (PPP) for dial-in lines with the fast Ethernet protocol.
The provider may even go one step further and use a VLAN tag for the
PPPoE packets. The VLAN concept divides one vast shared network into
several smaller subnets to isolate services, departments, or data centers
from each other.
For example, the ISP Deutsche Telekom implements this approach to sepa-
rate its IP-based television offering (IPTV) from regular Internet operations.
The IPTV platform initially transported packets parallel to normal Internet
243
Chapter 17. DSL router
Attribute Value
Enable 2
Description VDSL
Block private networks 2
Block bogon networks 2
IPv4 Configuration Type PPPoE
IPv6 Configuration Type None
MTU 1500
Username 001234567890123456789012#0001 \
@t-online.de
Password 12345678
Right now, the focus of dial-in is limited to IPv4. The specifications for IPv6
are relevant in section IPv6 with prefix delegation on page 249 and can be
ignored for now.
Due to the additional PPPoE header with a fixed length of 8 bytes, the
Ethernet frame loses some space. Of the maximum 1500 bytes, the PPPoE
has already reserved eight bytes so that the maximum size is reduced to
244
PPPoE Dial-in
1492 bytes (see Chapter 20). OPNsense does the math: for the dial-up
process to use the appropriate MTU of 1492 bytes, the web UI must show
the real value of the Ethernet interface of 1500 bytes.
The strange format of the login credentials is unique to this ISP and has its
origin in the ISDN era.
Note
If the modem takes over the VLAN marking or if the ISP does not use
VLANs at all, the firewall no longer needs a VLAN identifier and can
execute a regular PPPoE dial-in. In this case, skip the VLAN number
configuration on the firewall.
245
Chapter 17. DSL router
If the cabling is correct, the firewall should have already started the PPPoE
negotiations. If the DSL modem reports a synchronized connection, nothing
stands in the way of a dial-in.
After a successful PPPoE connection, the firewall receives IP addresses,
and the interface pppoe0 or VDSL appears in the list of network adapters
(Figure 17.3). In case of failure, take a look in the log file at Interfaces →
Point-to-Point → Log File.
LAN adapters
The network adapter re1 connects to the local network. Its IPv4 address
is a static address from the local IP range, as shown in Figure 17.1 on
page 243. The IPv6 address is unknown because the firewall receives it in
the following section IPv6 with prefix delegation from the Internet provider.
246
DNS and DHCP
Network Bridge
If the employed hardware has multiple LAN ports, they can be used to
connect additional devices that require Internet access. These ports behave
like a small switch and are configured as a network bridge.
At Interfaces → Other Types → Bridge the available physical adapters
join the internal switch bridge0. The client’s devices now attached to the
network can communicate with each other through the bridge without
further setup. Promote the network bridge to a real interface at Interfaces
→ Assignments. The IPv4 address of the bridge is also the default gateway
for the connected computers.
When it comes to DNS, OPNsense plays the role of mediator. The fire-
wall accepts all requests from the clients and relays them to an external
DNS server to get a qualified response.
OPNsense offers two competing services: Unbound DNS and Dnsmasq DNS,
the first being the acceptable default.
OPNsense learns the address of a DNS server during PPPoE negotiation. If
the ISP does not provide this information, the firewall gets its DNS servers
during the initial setup (see Chapter 4). If you do not know the IP addresses
of your provider’s DNS servers, use the easily remembered Google Public
DNS address (see Figure 17.4 on the next page).
247
Chapter 17. DSL router
The gateway for the clients is the local firewall since it knows the path
to the Internet. For name resolution, the clients can choose between the
248
IPv4 with Address Translation
Figure 17.6: All IPv4 clients share the firewall’s public address
249
Chapter 17. DSL router
Address translation is not a good option with IPv6, so the firewall must ap-
ply for another IPv6 prefix and share it with its clients by prefix delegation.
Deutsche Telekom generously provides its customers with /56 prefixes.
This prefix length allows the OPNsense machine to address 256 additional
internal IPv6 networks and to have control over more IPv6 addresses than
the entire IPv4 address space can provide.
First, the firewall has to request a modest /64 IPv6 prefix when dialing in.
With this inch, OPNsense takes a mile and immediately demands a big /56
prefix.
To get started with IPv6, OPNsense uses the same network adapter that was
used in the previous section PPPoE Dial-in. The method is different only
because IPv6 goes for DHCPv6. Table 17.2 lists the new adapter settings at
Interfaces → [VDSL] that comply with the German ISP. The configurations
for IPv4 and PPPoE remain unchanged.
Attribute Value
IPv6 Configuration Type DHCPv6
Configuration Mode Basic
Request only an IPv6 prefix 2
Prefix delegation size 56
Send IPv6 prefix hint 2
Use IPv4 connectivity 2
This additional IPv6 network is intended for the client’s devices. The LAN
adapter of the firewall also provides itself with an IPv6 address from the
received prefix. The IPv6 configuration type Track Interface is best suitable
for this. That way, the firewall monitors the IPv6 address of the VDSL
interface (Figure 17.7). If this interface receives a new address, the LAN
adapter reserves a subnet of it. The firewall picks the address based on the
number selected at IPv6 Prefix ID.
The OPNsense firewall provides the IPv6 prefix in the LAN via Stateless Ad-
dress Auto-Configuration (SLAAC). With this method of address allocation,
OPNsense gives just enough information via Router Advertisement that the
client functions in the network.
250
IPv6 with prefix delegation
Figure 17.7: OPNsense assigns a single IPv6 address to its LAN adapter
Figure 17.8: The dashboard shows the local IPv4 and IPv6 addresses
251
Chapter 17. DSL router
Firewall
A dual-stack firewall has its feet in both IP worlds and is vulnerable to attack
via both Internet protocols. Therefore, it needs a firewall policy for both
protocols to protect itself and its clients. OPNsense always distinguishes
between IPv4, IPv6, or acts for both protocols in the firewall rules (see
Chapter 6).
Note
The following firewall guidelines are minimal and should be adapted to
your own requirements before being used productively. The examples
do not claim to be complete.
IPv4/IPv6
The IPv4 traffic filtering policy limits access from the Internet to internal
resources or the DSL router. For data communication over IPv6, the same
rules apply as IPv4, but with a few extensions.
The Internet Protocol version 6 relies more on the Internet Control Message
Protocol (ICMP). The functionality of the Address Resolution Protocol
(ARP) in IPv4 belongs to IPv6, in the ICMP department. The SLAAC also
uses ICMPv6. For this reason, it is not possible to block ICMP completely.
The firewall policy must permit at least some features of ICMP. OPNsense
creates the necessary rules to permit an address assignment, and to not
accidentally block it. The same applies to rules for Bogon networks, i.e.
attempts to access IP addresses from unassigned prefixes.
Without an existing firewall rule on the VDSL adapter, OPNsense only
allows the packets required for Internet dial-up with PPPoE or DHCPv6.
Also, the firewall engine permits reply packets from existing connections.
The only custom rules for connections from the Internet apply to services
provided behind the DSL line.
252
Technical background
Custom rules for connections from the Internet are only required if the DSL
line is used to offer services to the public (see Chapter 6).
The ruleset for the internal network of a DSL router looks rather tolerant.
OPNsense even has two default rules preconfigured. Rule one: allow any
IPv4 traffic to the Internet. Rule two: permit the same freedom for IPv6.
Management access
In environments with increased security requirements, it is best to separate
the administrative access to the firewall from the user traffic (see Chapter 9).
The SSH and web services are allowed only on the management adapter
and blocked on the other interfaces.
Also, the firewall rules can restrict access to its services by specific IP
networks. It also prevents the management interface from being misused
for transit traffic.
The firewall policy of this chapter allows incoming sessions via HTTPS,
SSH, and ping from the IP network 10.5.1.0/24. If additional IP networks
need access to the configuration interface, the firewall needs static routes
that send the traffic back to the correct gateway. Otherwise, the response
packets leak to the Internet via the default route.
Technical background
OPNsense realizes the PPPoE dial-up to the Internet with the netgraph multi-
link PPP daemon. (MPD5). This service works mostly in user space and
uses only a few methods from kernel space. MPD5 justifies the separation
between user and kernel space with security and speed. For the negotiation
and encapsulation of PPP packets, MPD5 uses the netgraph subsystem.
OPNsense writes the settings for PPPoE to the file mpd_opt2.conf and then
starts the MPD5 process. This process begins the dial-up, negotiation, and
monitoring of the Internet connection.
For IPv6, Internet Service Providers use the Router Advertisements from
ICMP to assign the IPv6 address, DNS server, and MTU to the requesting
clients. The DSL routers need to request a further prefix for their attached
clients. The ISP also uses DHCPv6 to offer an additional IPv6 network.
On the customer side, this setup expects a DHCPv6 client process that
253
Chapter 17. DSL router
requests the IPv6 network for delegation. This process is dhcp6c, and it
runs permanently because the ISP also announces changes in the network
via DHCPv6. The configuration is in the file:
/var/etc/dhcp6c_opt2.conf
The same service determines to which network adapter this prefix should
belong.
Who informs all the clients in the network about the IPv6 environment?
OPNsense provides this information via Router Advertisements and the
service radvd. This service floods its information every 3-10 seconds via a
multicast into the local network. The announcements contain the learned
DNS server and the IPv6 prefix. Further settings are present in radvd’s
configuration file:
/var/etc/radvd.conf
Summary
With the described configuration and a suitable modem, OPNsense becomes
a fully-fledged DSL router on any hardware, allowing clients to access the
Internet via IPv4 and IPv6.
OPNsense has other tricks that go beyond regular router operation:
• Dynamic DNS. With the plug-in DynDNS , the firewall deposits its
public IP address with a DNS provider and is thereby accessible under
its name on the Internet.
• Traffic shaping. The firewall can purposefully slow down data streams
by the IP address or application. For example, administrators can
restrict the bandwidth for the next Windows update to 6 Mbit/s in
order to free up bandwidth for other uses.
254
Chapter 18
Intrusion Detection
The IDS is limited to recognition, so the break-in is noticed but not pre-
vented. If the detected break-in must be stopped directly, the system
changes its role to an Intrusion Prevention System (IPS). The difference
between IPS and IDS lies only in the last step: while the IDS squeals on
255
Chapter 18. Intrusion Detection
any suspicious activity, the IPS will directly prevent the action by halting
packets or actively terminating connections.
In theory, the Intrusion Prevention System is a miracle drug: place the IPS
in the network and break-ins stop. Unfortunately, many IPS environments
suffer from a high rate of false positives. If legitimate connections break off
during regular communication because the IPS has discovered a seemingly
malicious packet, then this has nothing to do with protection against break-
ins. The IDS, on the other hand, does not modify the network connections
and becomes a pain in the neck for the monitoring team receiving the
inaccurate reports.
The art of intrusion detection is in the fine-tuning! After the initial setup,
the alarm rate is high because the IDS must first become familiar with the
new network. It is essential to minimize the number of false alarms by
exceptions or individual deactivated checks. The IDS get promoted to an
IPS only if the system classifies the environment as normal and no longer
produces a false alarm.
Network integration
For its investigation, the IDS requires insight into each network packet. The
system does not necessarily have to be in the data path for this, but can also
complete the analysis using packet copies. For real-time intrusion detection,
a mirror port at the network switch (see Chapter 23) is suitable, which
sends a copy of each transmitted packet to the IDS. Alternatively, the IDS
can analyze a recording of network traffic to find suspicious activity.
The IPS needs to be close to the network packets because it must be able to
discard packets, if necessary, to prevent worse things from happening. An
IPS usually sits in the data path and examines the packet contents in real
time.
256
Lab setup
Lab setup
10.4.1.1 10.4.1.7
em2 eth1
RT-1 labsrv
OPNsense CentOS 8
em0
em0– 10.5.1.1 eth0
eth0– 10.5.1.7
em3
198.51.100.1
WAN-1
198.51.100.22 198.51.100.0/24
2001:db8:1::/64
eth1
OpenVAS
Greenbone OS
eth0
eth0– 10.5.1.22
The firewall RT-1 works on the level of a router (see Chapter 6) and is
visible by the attackers. Figure 18.1 shows the lab setup.
Other vulnerability scanners are also suitable for testing the IDS, as long as
they have good intentions and report the vulnerability instead of exploiting
it.
The vendor offers excellent documentation and a ready-to-use virtual appli-
ance making it straightforward to set up a vulnerability scanner in a short
time.
257
Chapter 18. Intrusion Detection
Attack
The vulnerability scanner audits the lab server and reports detected weak-
nesses. The firewall RT-1 implements only a few loose filter rules that allow
unrestricted access to the target server.
Note
This ruleset is unsuitable for practical use since the firewall is supposed
to be the first line of defense and the IDS is watching its back.
The vulnerability scanner begins its first investigation with the IDS still
turned off. This basic measurement shows whether the connection between
the systems is working and determines the target host’s security level.
Activate IDS
Just like a virus scanner, the IDS first needs a current status of attack signa-
tures and filters.
The web interface of OPNsense integrates the IDS at Services → Intrusion
Detection. The Download tab offers almost 60 pre-built rulesets that can
be included in the analysis (Figure 18.2). By default, all rules are inactive
and do not exist locally.
The title of each ruleset gives a good hint about their content: the strength
of ET open/emerging-imap obviously lies in the IMAP protocol and the
ruleset ET open/emerging-voip deals with Internet telephony. If the name
is meaningless, the ruleset details link to a web page that provides helpful
information.
After selecting multiple – or all – rulesets, hit the Download & Update Rules
button at the bottom of the page. The firewall will then download the latest
signatures and store them in the local system. The Enabled column informs
which rulesets are actively used for the intrusion analysis.
Every single rule is now listed in the Rules tab and can be switched on and
off individually – which is essential for suppressing false alarms!
With the current rules in an active state, the IDS is armed for the first
deployment and should carefully inspect the network packets. The small
258
Activate IDS
Figure 18.2: The pre-built rulesets make it easier to get started with the IDS
checkbox Enabled at the tab Settings starts the background processes, and
monitoring begins. The emphasis is on monitoring, because in this mode
OPNsense only watches a break-in and reports any anomaly in the Alerts
tab.
Next attack
259
Chapter 18. Intrusion Detection
Fine tuning
In recognition mode, the IDS does not affect the excellent reputation of the
network because it does not actively prevent communication. Now it is
time to fine tune the IDS. It is essential to minimize the number of false
alarms because they are incredibly annoying once in IPS mode.
A good approach is to manually check the alert messages over several days
or weeks after the start of the IDS. If the IDS has triggered an alarm without
any real threat, set the rule action to Alert instead of Drop. If the target of
the attack does not exist in the network at all, it is acceptable to deactivate
the rule involved (register Rules). In both cases, the connection is not
interrupted in IPS mode, but an inactive rule will never look for this attack
again.
Activate IPS
The next step against break-ins is detection with immediate prevention: the
IDS becomes an IPS.
260
Transparent IDS
Enable the checkbox IPS mode in the area Intrusion Detection → Ad-
ministration of the OPNsense web interface (Figure 18.4) to change the
well-tuned IDS to an IPS.
The IPS should now prevent the next planned attack on the lab server’s
services. The vulnerability scanner security report is now shorter and no
longer contains the exploits that the IPS has successfully prevented.
Even a good IPS cannot completely prevent any attack since many attacks
are masked by encrypted protocols such as HTTPS or SSH.
Transparent IDS
An Intrusion Detection System can work transparently on the Ethernet level,
just like a firewall (see Chapter 7). Thus, the IDS is mostly invisible to
261
Chapter 18. Intrusion Detection
10.4.1.7
eth1
RT-1
bridge0 em2 OPNsense
10.4.1.1 em3 em0
em0– 10.5.1.1
labsrv
CentOS 8
eth0
eth0– 10.5.1.7
WAN-1
10.4.1.22
eth1
OpenVAS
Greenbone OS
eth0
eth0– 10.5.1.22
262
Transparent IDS
Network bridge
OPNsense needs a network bridge for transparent operation. This bridge
connects the DMZ and WAN adaptors. Data traffic can pass through the
network bridge, just like an Ethernet switch. There is no need for the hosts
to address the OPNsense firewall directly by its IP or MAC address.
Note
In a virtual environment, the two adapters of the network bridge
require extended access to the host system’s network card. In the
configuration interface of the virtual machine, this permission is called
Promiscuous mode.
The setup of the bridge starts at Interfaces → Other Types. Figure 18.6
shows the newly created network bridge bridge0, which has two members
and presents itself as a regular network adapter in the system after setup.
Add the new bridge interface at Interfaces → Assignments. It will show
up in the list of interfaces as OPT3, whereby the number can vary. Go to
Interfaces → [OPT3] and give the new adapter a better name like br0.
263
Chapter 18. Intrusion Detection
Before the network bridge br0 gets an IPv4 address, the members DMZ
and WAN1 must lose their addresses. The former IPv4 address of the DMZ
adapter becomes the new address of the bridge interface.
Warning
If the firewall is managed by the IP address of the DMZ or WAN1
adapter, the web connection will fail once the addresses are removed.
In this case, access the firewall via a separate management adapter,
which is exclusively used for configuration.
Next, move the packet filter from the physical interfaces DMZ and WAN1
to the logical adapter br0. Unfortunately, OPNsense has no convenient
switch built into the web GUI but uses various kernel options. At least,
it is possible to change these options from the web interface: the kernel
parameters wait for their adjustment at System → Settings → Tunables.
• net.link.bridge.pfil_member = 0
prevents the kernel from filtering on the incoming and outgoing
member interfaces.
• net.link.bridge.pfil_bridge = 1
enables the kernel to filter on the bridge interface.
Finally, the network bridge is operational and receives a basic set of rules
similar to the one initially used for the physical WAN interface in section
Attack on page 258.
The IDS does not need any changes. For the intrusion detection soft-
ware, it makes no difference whether the packets have entered the local
system through an IP interface or a network bridge.
Technical background
The system for intrusion detection of OPNsense is Suricata [21] and is
licensed under the free GNU license. Suricata has been developed since
2010 and is an open-source IDS and IPS for all major operating systems.
FreeBSD provides the Suricata software as a single process that is configured
and controlled by the web interface.
264
Technical background
/usr/local/bin/suricata -D --netmap \
--pidfile /var/run/suricata.pid \
-c /usr/local/etc/suricata/suricata.yaml
This process receives a copy of all network packets and compares the
content with its known rules describing attack patterns. For Suricata, it
does not matter how the packets reach the analysis engine. The IDS will
investigate and alert as long as the data stream can be examined locally.
Rules
The inspection rules are selected in advance from the web page, as described
in section Activate IDS on page 258. In the background, the script
/usr/local/opnsense/scripts/suricata/rule-updater.py
Finally, the Suricata process receives the new rule material and loads it into
its memory. The update procedure does not interrupt the IDS.
OPNsense uses the rules of the community project Emerging Threats [22].
All offered rules are under the BSD license and can be used free of charge.
The provider gives direct insight into the rules on its public download page
https://rules.emergingthreats.net/open/suricata/rules/
The content of these files consists of individual rules that describe attacks
or unusual IP packets. A Suricata rule is similar to the rule of a packet filter.
It has a fixed syntax against which all fields of each IP packet are checked.
action protocol source port -> destination port (options)
If the conditions from the protocol, source, destination, ports, and options
apply, the action is executed.
For example, the file dns-events.rules contains a rule with the content:
alert dns any any -> any any (msg:"SURICATA DNS Z flag set"; \
app-layer-event:dns.z_flag_set; \
classtype:protocol-command-decode; sid:2240006; rev:2;)
265
Chapter 18. Intrusion Detection
The specified rule checks whether a DNS packet has set the Z flag. RFC
1035 specifies that the Z flag is reserved for future use and must always be
zero. If the Z flag is not zero, the packet is suspicious.
If the condition of the rule applies, the selected action alert will report
an alarm and forward the packet. The Z flag was later used in the DNS
extension of RFC 2535 for authentication. DNS packets with this security
extension can only pass the intrusion system if the rule action is not set to
drop. The choice between alert and drop therefore depends on the DNS
implementation of the network that the IDS is supposed to protect.
Summary
A system for intrusion detection is only as good as the quality and accuracy
of its rules. Suricata does an excellent job as a free and open-source IDS. It
runs in the heart of the firewall, which is a perfect location for analyzing
traffic streams both as IDS and IPS.
The programming language of the rules for detecting suspicious packets
remains in the background because Suricata uses pre-built rulesets from
different providers. The Schedule function always keeps the rules up to
date.
It is mandatory to fine-tune every Intrusion Detection System. Only when
the false alarm rate reaches an acceptable minimum, does the IDS become
a valuable member of the security family and possibly get promoted to IPS.
266
Chapter 19
Command Line
This chapter shows the text console possibilities. While the functions
of OPNsense are menu-based, the underlying UNIX operating system ex-
pects written commands. For convenient access to the file system via a file
browser, see the section Access from Windows on page 316.
configd
The central point of contact for services is configd, which also runs as a
service. It monitors, starts, and stops other services when the configuration
has changed or when the web UI queries for statistics. It is best to use the
wrapper script configd instead of messing directly with a system service.
configd does not restrict access but implements new features. For example,
in addition to the usual tasks start, stop, and status, configd enhances the
proxy service to understand the tasks fetchacls and downloadacls.
267
Chapter 19. Command Line
The configd service is accessible from the command line using the com-
mand configctl. The desired service and its task are appended to the
command:
configctl service task
and the task is a section within that file. Following the example of cron,
the content of actions_cron.conf is the simple code used to restart the
cron daemon.
[restart]
command:/etc/rc.d/cron restart
parameters:
type:script
message:restarting cron
268
Configuration changes
Configuration changes
The development of a full command line for configuration changes is not
on the roadmap of OPNsense. The strategy is heading towards a RESTful
API, which in turn could be used by the CLI.
For this reason, modifications on the command line are not a good idea
and should be limited to emergencies. OPNsense wants to be admired as a
beautiful website and not in a black-and-white console window.
If you are fearless (but with a backup copy), you are welcome to open
the main configuration file /conf/config.xml and modify the respective
settings. The file uses the eXtensible Markup Language (XML) and is there-
fore an open text format. Direct changes are possible, but require a basic
understanding of the format.
The developers use the usual combination of language code and country
code to define the language of the web GUI. Finally, the save command
writes the changes back to the XML file.
The configuration file config.xml is modified accordingly, but the OPN-
sense services do not know about this new feature yet. Also, the configd
or the XML file is not queried, because this is an unexpected type of system
change.
269
Chapter 19. Command Line
Two unattractive solutions force the services to take a look at the main
configuration file:
• Restart the services. This is quick and painless, but does not work
with the IPS and the web proxy. The command
/usr/local/etc/rc.reload_all
• Restart the firewall. This task works 100% of the time but interrupts
all services for several minutes.
Both approaches clearly show that OPNsense is not yet ready for daily
configuration work from the command line.
In case of an unusual security policy or a stubborn admin, it is best to
implement a high availability setup (see Chapter 12) to minimize the
outage duration. Administrative overhead increases, but users do not
notice configuration changes and downtime.
Undo changes
Critical configuration changes from the web interface can have devastating
consequences. Not to mention that a restart of the firewall usually does
not solve the problem, since OPNsense has already stored the error in the
configuration file.
OPNsense has integrated the Undo function into its text console. When
management access is out-of-band, access via SSH should still be possible
so that the text menu is still accessible (see Chapter 9).
Menu item 13 Restore a backup is responsible for managing recent config-
uration states. From the list of configuration files, an older, but working,
configuration is quickly identified by its timestamp. When selected, the
entry becomes the current configuration, and OPNsense recommends a
restart to finish the restore.
270
Updates
Enter an option: 13
cp /conf/backup/./config-1612262004.2927.xml /conf/config.xml
Do you want to reboot to apply the backup cleanly? [y/N]:
Updates
Manually repeating similar tasks for a large number of OPNsense firewalls
is time-consuming. It is best to automate regular tasks, like updates. The
command line is much better suited for this than the web interface.
The Check for updates button runs a command in the background:
configctl firmware check
/usr/local/opnsense/scripts/firmware/check.sh
This script takes a look at the public package repository. As soon as a newer
version appears, the web UI offers an update to the administrator.
The OPNsense developers use the FreeBSD package system to update their
software. The update process is essentially based on the command
/usr/local/sbin/opnsense-update
If the process requires a restart, the update dialog will inform and perform
the reboot when the software update is complete. It is best practice to run
the update task outside working hours.
A fully automated update with an optional subsequent reboot uses the
command
/usr/local/etc/rc.firmware
or
271
Chapter 19. Command Line
Using the FreeBSD package structure does not reflect a lack of imagination
but is instead an excellent integration with the underlying operating system.
Packages
All available plug-ins at System → Firmware → Plugins are also software
packages managed by the package system.
In a virtual environment based on VMware, the installation of VMware
tools is mandatory for best performance and drivers. The CLI command
pkg install os-vmware
fetches the necessary packages and their dependencies from the public
OPNsense repository. It then installs the VMware tools and reports its
success to the packaging system. The installed package is now usable and
visible from the website.
In environments with minimal resources, it is recommended to install the
plug-in via the command line. The pkg command displays interaction
possibilities and also discloses the required memory space.
Summary
The functionality of the OPNsense command line is limited to the initial
setup of the network adapters and various connectivity tests. The web
interface provides a much better way to change configuration and gather
information. Troubleshooting is mainly done using the commands of the
FreeBSD operating system.
272
Chapter 20
Performance Tuning
Lab setup
This chapter examines the OPNsense firewall RT-1 for performance in pro-
cessing packets with and without encryption. The measurement endpoints
are two adjacent firewalls: RT-2 sends packets with maximum bandwidth
through RT-1 to RT-3, which then receives, measures, and discards the data.
Benchmarking the crypto performance uses a VPN tunnel between RT-1
273
Chapter 20. Performance Tuning
and RT-3, where RT-3 is also the measurement host. Figure 20.1 shows the
experimental assembly.
RT-2
OPNsense
em1
10.1.1.2 Site-1 Site-2
10.1.1.0/24 10.2.1.0/24
10.1.1.1 10.2.1.3
RT-1
em1 OPNsense
RT-3 em1
OPNsense
em4 em2
192.0.2.1 WAN-2 192.0.2.3
192.0.2.0/24
Baseline
274
Virtual network adapter
ifinfo The ifinfo command lists information about the network adapt-
ers that refer to the physical layer. Also, it presents the number of bytes,
multicasts, collisions, discarded, and transferred packets.
iftop What does the firewall currently process for its clients? iftop shows
this information via the interface and lists the session as well as the transfer
rate of the last 2, 10, and 40 seconds.
pfctl controls the packet filter and returns the contents of the firewall
status table, statistics, and timeouts. With the -s all option, pfctl reveals
everything it knows.
pftop is a real-time display for the packet filter session table. For each
connection, pftop shows the duration and amount of transferred data.
systat reveals the processes that are currently creating the highest CPU
loads. Alternatively, systat also searches for processes with many disk I/O,
network accesses, or large memory requirements.
275
Chapter 20. Performance Tuning
200
150
Throughput (Mbit/s)
e1000
pcnet2
100
virtio
50
0
128 256 512 1024 1280 1420
276
Virtual network adapter
400 e1000e
vmxnet3
vlance
300
Throughput (Mbit/s)
200
100
0
128 256 512 1024 1280 1420
Modifications to the vmx-file are only effective when the virtual machine is
turned off. Possible adapter types are vlance, vmxnet3, e1000, and e1000e.
Which adapter is the best one? Figures 20.2, 20.3 and 20.4 show the
throughput rates of various adapters with different packet sizes and hyper-
visors. The exact measured bandwidth is less important than the difference
in adapter’s performance.
These measurements show the throughput rates of a virtual OPNsense
firewall without the enabled features IPsec, NetFlow, or NAT. Only the
packet filter is active and allows all traffic with a single rule.
Different operating systems and different drivers may lead to different
results. Therefore, the statements in the diagrams only apply to OPN-
sense 21.1.r_44 on the used host systems.
The measurement of the different adapter types is biased and only considers
the pure throughput rate. Capabilities such as jumbo frames, TCP checksum
offload, TCP segmentation offload, and the CPU load caused during the
measurement are not taken into account.
277
Chapter 20. Performance Tuning
400
300
Throughput (Mbit/s)
200
100
vlance
vmxnet3
e1000
0
128 256 512 1024 1280 1420
Routing throughput
If the firewall hardware is expected to deliver high data rates, it is worth
taking a look at the assignment of the network adapters to processor cores.
By default, OPNsense leaves this decision to the UNIX kernel.
In a worst-case scenario or a weak setup, the most active network adapters
end up on the same processor and fight for CPU cycles, while other cores
are underutilized.
Under those circumstances, it is time for intervention, and FreeBSD will
provide commands that can send the interrupts of a network card to a
specific CPU kernel. Check the list of interrupts to determine if this uneven
load distribution exists on the local system.
root@RT-3:˜ # vmstat -i | egrep "em|total"
interrupt total rate
irq16: em2:irq0 55810 157
irq17: em3:irq0+ 108623 305
irq18: em0:irq0+ 180 1
irq19: em1:irq0 7 0
278
Routing throughput
In this example, the network adapter em3 drags the most cycles from the
CPU. During this load situation the two CPU cores are indeed unevenly
loaded, as the command top -P CC confirms:
CPU 0: 0.0% user, 0.0% nice, 3.5% system, 6.2% interrupt, 90.3% idle
CPU 1: 0.0% user, 0.0% nice, 100% system, 0.0% interrupt, 0.0% idle
The next step is to distribute the work of the network adapters among the
CPU cores. The workhorse em3 (IRQ 17) could be assigned on the first core
while the adapters em0 (IRQ 18) and em2 (IRQ 16) operate on the second
core. FreeBSD provides the cpuset command for this purpose, which will
display or set an assignment:
root@RT-3:˜ # cpuset -l 0 -x 17
root@RT-3:˜ # cpuset -l 1 -x 16
root@RT-3:˜ # cpuset -l 1 -x 18
root@RT-3:˜ # cpuset -g -x 16 ; cpuset -g -x 17 ; cpuset -g -x 18
irq 16 mask: 1
irq 17 mask: 0
irq 18 mask: 1
Note
The addressing of the CPUs, or cores, is based on a bitmask shown
with examples in Table 20.1.
279
Chapter 20. Performance Tuning
With this configuration, the high-speed NIC gets its own CPU core, and
the total load is distributed better. Subsequent performance measurement
should either show more throughput or cause less CPU load on the firewall
hardware.
CPU 0: 0.0% user, 0.0% nice, 73.5% system, 0.0% interrupt, 26.5% idle
CPU 1: 0.0% user, 0.0% nice, 88.1% system, 11.9% interrupt, 0.0% idle
IPsec throughput
If high throughput rates are required for an IPsec tunnel, it is best to
investigate the hardware specs first. Unfortunately, the IPsec performance
is not a fixed value printed in a datasheet. The encryption performance
depends on several factors:
• The processor. The CPU might have hardware support for crypto-
graphic instructions. This command set extension accelerates encryp-
tion and decryption, but the VPN software must also use it to take
full advantage of it.
• The bus. How fast does the internal data bus bring the packet contents
from the network card to the CPU and back?
280
IPsec throughput
175
150
Throughput (Mbit/s)
125
100
75
50
no encryption
AES-128 SHA-1
AES-192 SHA-256
25 AES-256 SHA-512
3DES MD5
128 256 512 1024 1280 1420
size and algorithm vary to illustrate the influence on the measured through-
put rate.
Finally, the results also depend on the duration of the test. A short ex-
amination of fewer than 10 seconds hardly generates any packet loss,
because the firewall does not have to discard the overload packets, but can
cache them in buffers. This caching approach is basically an advantageous
behavior, but it tampers with the result. A meaningful result is achieved
after 60 to 120 seconds.
Measuring method
Several RFCs and Internet drafts propose differing ways to measure the
throughput. To determine the IPsec throughput rate, use a neutral mea-
suring computer to send packets to the first IPsec gateway. This device
encrypts the packets and forwards them to the second gateway for decryp-
281
Chapter 20. Performance Tuning
tion. Finally, the packages reach their destination. Figure 20.1 shows the
unmodified experimental setup.
The framework conditions of a measurement are:
• Test duration of 60 seconds,
• IPsec in tunnel mode,
• Repeat each test three times to find abnormalities and determine an
average value,
• Modify the packet size in fixed steps, as RFC 2544 recommends.
OPNsense has an excellent tool to run the measurement: iperf3 [23]. iperf3
transmits packets with maximum throughput between two devices and then
displays the achieved transfer rate. On the first host, start iperf as a server
that receives the measurement packets. The second host starts iperf as a
client with the IP address of the specified server. The client immediately
starts generating packets and sends them to the server.
The iperf client learns about the different package sizes via script:
#!/bin/csh
set INTERFACE=vmx1
foreach MTU ( 1420 1280 1024 512 256 128 )
echo "*** Benchmark with MTU $MTU Bytes"
ifconfig $INTERFACE mtu $MTU
sleep 2
iperf3 --client 192.0.2.3 --window 128K --time 60 \
--interval 60 | grep bits
end
Warning
iperf is available in versions 2 and 3, which are incompatible. The
examples in this book use version 3 because it is easier to install on
OPNsense.
282
Increasing performance
Note
Performance and throughput of virtual machines vary depending on
the load situation of the host system and other guests.
The measurement results are then less meaningful and can have a certain
deviation.
Increasing performance
The performance improvement success depends on the hardware equipment
and network components. The following recommendations may, but do not
promise, improvements.
AES-NI
Recent CPUs from Intel and AMD support the AES-NI command set ex-
tension to handle encryption better (and faster). The name suggests its
purpose: the instruction set deals only with the AES algorithm. To take
advantage of AES-NI, the processor, software, and configuration must use
the new commands.
However, does the processor understand AES-NI? To do so, the CPU info
must include the keyword aes.
root@RT-1:˜ # dmesg | grep aes
aesni0: <AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS> on motherboard
The hardware support of AES-NI is only half the battle because the kernel
must also support the command extension. OPNsense has outsourced its
knowledge of the crypto language to a kernel module, which is easy to
verify:
root@RT-1:˜ # kldstat | grep aes
19 1 0xffffffff82a31000 8d50 aesni.ko
OPNsense offers two algorithms, 3DES and AES, with different key lengths
for VPN tunnels. Today, AES is the preferred crypto algorithm because:
• Most x86 processors offer the optimized command set for AES.
283
Chapter 20. Performance Tuning
3DES was developed for hardware use. When comparing the crypto perfor-
mance of a router with additional hardware acceleration, AES might lose
to 3DES regarding throughput.
IPsec
An IPsec VPN is negotiated through the charon process, and during opera-
tion is maintained by the kernel. The software code for AES encryption is
located in the kernel module aesni and offers all available key lengths.
OPNsense uses strongSwan [10] version 5.9.1 as an IPsec engine, which has
used AES-NI since mid-2015. The kernel module accelerates the encryption
of the VPN tunnels. StrongSwan does not use an additional SSL library
so AES-NI is used automatically if the VPN tunnel uses AES for encryption
instead of 3DES.
The only way to improve the IPsec VPN performance is to use the AES
encryption algorithm.
OpenVPN
The OpenVPN software developers use a library for all crypto operations.
OPNsense picks the two best libraries on the market and lets the administra-
tor choose which library to use. By default, the programs utilize OpenSSL,
but LibreSSL is an appealing alternative.
OPNsense automatically uses AES-NI, even if it is not selected in the web
GUI. And that is a good thing, because the VPN tunnel’s throughput is
30-50% more when AES-NI is activated. The pure crypto performance of
the CPU is many times higher; however, the path of the packet through
the firewall is not just encryption. The firewall also spends some time on
packet filtering, routing, address translation, and queueing of the packets.
284
Increasing performance
100
Throughput (Mbit/s)
75
50
AES-128 SHA-1
25 Blowfish SHA-1
Camellia-256 SHA-1
AES-256 SHA-256
DES MD5
3DES MD5
128 256 512 1024 1280 1420
The results in Figure 20.6 show the throughput of an OpenVPN tunnel with
different crypto algorithms compiled against OpenSSL. The diverging rates
show the advantages of AES.
If desired, change the crypto library to LibreSSL from the web UI at Sys-
tem → Firmware → Settings. The next step is a firmware update because
OPNsense says goodbye to OpenSSL and installs the predefined software
packages with LibreSSL support.
Why make the effort to change the crypto library? OpenSSL and LibreSSL
both have their strengths and weaknesses. OpenSSL has been around for
almost 20 years and has had a few security incidents.
After the last major security breach in 2014, the OpenBSD developers
grabbed the source code from OpenSSL, removed a lot of useless lines,
and released LibreSSL. Since then, LibreSSL has scored when it comes to
security – so far no critical vulnerability has been noticed.
285
Chapter 20. Performance Tuning
125
100
Throughput (Mbit/s)
75
50
25 no tunnel
IPsec
OpenVPN with OpenSSL
OpenVPN with LibreSSL
128 256 512 1024 1280 1420
286
Increasing performance
287
Chapter 20. Performance Tuning
The less academic way is to guess and try the right MSS. For this, ping is a
good partner: the packet size is slowly increased with a sweeping ping until
it reaches maximum size. As soon as the packets have to be fragmented,
the MTU is found.
root@RT-2:˜ # ping -D -g 1280 -G 1500 -i 0.2 10.2.1.3
PING 10.2.1.3 (10.2.1.3): (1280 ... 1500) data bytes
1288 bytes from 10.2.1.3: icmp_seq=0 ttl=64 time=2.578 ms
1289 bytes from 10.2.1.3: icmp_seq=1 ttl=64 time=0.383 ms
[...]
1419 bytes from 10.2.1.3: icmp_seq=131 ttl=64 time=0.199 ms
1420 bytes from 10.2.1.3: icmp_seq=132 ttl=64 time=0.638 ms
36 bytes from localhost: frag needed and DF set ( MTU 1440 )
Vr HL TOS Len ID Flg off TTL Pro cks Src Dst
4 5 00 05a1 0000 0 0000 40 01 ffbc 192.0.2.1 10.2.1.3
If both methods are too complex, or do not provide meaningful values, there
is still the tried-and-true value of 1300 byte. This MSS takes into account
the different tunnel modes, encryption types, and NAT traversal. However, a
few bytes remain unused in each packet, which slightly reduces throughput.
Figure 20.8 shows how the firewalls apply the determined MSS to the
communication path. When establishing a TCP connection, both partici-
pants announce their preferred MSS (steps 1 and 3), which will be used for
the duration of the connection.
RT-1 RT-3
10.1.1.1 VPN/WAN 10.2.1.3
em1 MTU 1361 em1
MTU 1500 MTU 1500
MSS=1460 MSS=1321
1 2
MSS=1321
4 MSS=1460
3
Figure 20.8: The OPNsense firewall manipulates the MSS values
OPNsense must now interfere with the TCP negotiation and overwrite the
MSS values (steps 2 and 4). From the point of view of the participants, the
respective partner proposed the lower MSS. In fact, the OPNsense gateway
has automatically changed the MSS to prevent fragmentation.
288
Increasing performance
The firewalls have to manipulate the MSS at both ends of the VPN tunnel.
OPNsense does this by using Normalization rules, which is a bit outside
the web GUI at Firewall → Settings → Normalization. The normalization
rules are similar to the regular firewall rules, i.e. they do not allow or deny
data connections, but manipulate with packet properties. The intentional
manipulation is the MSS reduction. The required rule has the following
parameters:
• Interface: LAN
• Direction: Any
• Protocol: TCP
• Source: 10.1.1.0/24
• Destination: 10.2.1.0/24
• Max mss: 1321
This rule is required twice in total, whereby the second rule has swapped
the Source and Destination values. The rules ensure that both sides of the
connection use the lower MSS.
Figure 20.9 shows both rules with inverted IP networks for source and
destination.
The normalization rules reduce the MSS for all types of VPN tunnels. Also,
OpenVPN has the built-in parameter mssfix, which influences the MSS
without a normalization rule. The option is not visible in the web UI, but in
the Advanced field at VPN → OpenVPN → Servers. This location accepts
non-default configuration, so add the line
289
Chapter 20. Performance Tuning
mssfix 1321
to the Advanced options and OpenVPN will reduce the MSS to 1321 byte.
1 #!/bin/sh
2
3 NEIGHBORS4=$(/usr/bin/netstat -4rn | grep UGS | awk ’{ print $2 }’)
4 for NHR in $NEIGHBORS4 ; do
5 arp -n $NHR >/dev/null || ping -t1 -c1 -W1 $NHR >/dev/null
6 done
7
8 NEIGHBORS6=$(/usr/bin/netstat -6rn | grep UGS | awk ’{ print $2 }’)
9 for NHR in $NEIGHBORS6 ; do
10 ndp $NHR >/dev/null || ping6 -X1 -c1 -x1 $NHR >/dev/null
11 done
The script only sends packets if the ARP cache lacks the neighbor record.
When called repeatedly, the script will not generate a high network load.
Starting in line 8, the script also populates the IPv6 neighbor list, which is
basically the same as the ARP cache, but for IPv6.
Copy the script from Listing 20.1 to the directory /usr/local/sbin/. Since
the OPNsense developers use the same directory for many other scripts, it
is an excellent choice for the new script.
290
Summary
Next, use the task scheduler to execute the script with the exemplary name
fill_arp_cache.sh at regular intervals (see Chapter 22).
( crontab -l ; \
echo "*/5 * * * * /usr/local/sbin/fill_arp_cache.sh" ) \
| crontab -
Summary
An increase in performance is feasible if the settings and configuration
are inappropriately applied. The increase begins by measuring the current
performance. Next, the virtual network adapters show their performance
values. If the CPU has more than a single core, the scheduler can share
the load over multiple cores. Encryption is subject to further obstacles
because IPsec and OpenVPN depend on very different methods to increase
performance. And if the maximum amount of data per packet is exceeded,
there is an unexpected loss of performance.
If the firewall is already working with applied optimal settings, the next
step for more throughput is to use more powerful hardware.
291
Chapter 20. Performance Tuning
292
Part V
For Admins
293
Chapter 21
Best Practice
Factory reset
Any professional network device has the ability to discard all changes and
return to the default state. These factory settings are required when the
firewall is sold, or the demo equipment must be returned to the manufac-
turer. In the simplest case, the device only changes its intended use and
should not have any existing configuration. Basically, when a firewall is set
to Factory Default, it will delete all traces.
There are three ways to bring an OPNsense firewall to its factory status:
The firewall then shuts down and powers off. When booted again, the
device assigns the initial IPv4 address 192.168.1.1 to the LAN network
295
Chapter 21. Best Practice
adapter. The WAN adapter queries for its address via DHCP. The password
for the admin account is opnsense.
Thorough
If the firewall changes hands, it is mandatory to really remove all traces. It
is best to double check the factory settings and inspect the remaining file
system.
The safest and most thorough way to remove all traces is to reinstall the
operating system. This procedure is advisable if the firewall was enhanced
with custom modifications (see Chapter 15, 19, and 23). Also, not every
service or plug-in sticks to the folder structure and stores configuration data
in non-default paths. Finally, perform a new installation of OPNsense to
have a clear conscience that no sensitive data leaked.
Benchmark throughput
The available throughput between two firewalls is, in theory, the bandwidth
of the network interfaces. In reality, there are usually limiting factors that
reduce the performance.
It is possible to measure the available bandwidth between two OPNsense
firewalls quickly with iperf3 tool. Install iperf3 from the web UI as a plug-in
or via the command line. The tool is easy to use, delivers meaningful results,
and runs on Linux and Windows, with a Java-based GUI.
With iperf3 the sender sends data at maximum speed to the receiver, and
both determine the throughput.
One OPNsense firewall is the receiver, and the other is the sender. These
roles determine the direction of the measurement because the calculated
bandwidth applies from the transmitter to receiver. For an analysis in the
opposite direction, repeat the benchmark, but with interchanged roles.
Keep in mind that a firewall rule must allow the measurement packets from
iperf3 to port 5201. For this purpose, a floating firewall rule is a good
choice because this type of rule applies to multiple network adapters. Con-
296
Benchmark throughput
figure that rule at the web UI at Firewall → Rules → Floating. Table 21.1
shows the details of the new rule.
Attribute Value
Action Pass
Interface LAN,DMZ,MGMT
TCP/IP Version IPv4+IPv6
Protocol TCP/UDP
Source any
Destination This Firewall
Destination port range 5201
Description iperf3
The receiver starts in server mode with iperf3 -s and the sender executes:
iperf3 -c <IP_of_the_remote_station>
Listings 21.1 and 21.2 show an example in which RT-1 sends data to RT-2
and both display the calculated bandwidth. The measurement duration of
10 seconds is the default setting. Pick a higher value with the command
line argument --time. A comprehensive list of all options is available with
iperf3 --help.
297
Chapter 21. Best Practice
SSH authentication
with
uses validates
Private key Public key
Figure 21.1: SSH login with cryptographic keys
298
SSH login without password
To use this login method, first generate and distribute the key material:
1. Generate a key pair. Each administrator needs a separate pair of keys.
3. Enter the public key on all OPNsense firewalls that the admin is
expected to manage.
A Linux distribution often comes preinstalled with OpenSSH as an SSH
client. On Windows, the software PuTTY [25] has gained wide acceptance,
probably because it is versatile and easy to use. Furthermore, PuTTY does
not have to be installed and is free of charge.
Note
OPNsense version 17.7.7 and above does not accept SSH keys with a
length of fewer than 1024 bit.
The Generate button starts collecting random numbers and then displays
the generated public key. In the next steps, both parts of the key are
required so they must be stored with the save buttons. The file names do
not matter. With this in mind, an excellent choice to name the key files is
id_rsa.pub for the public key and id_rsa for the private key.
Under Linux, the tool for creating keys is pre-installed with the OpenSSH
package. The following command generates a key of the same type:
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -N ’’
The resulting files are stored in the .ssh/ folder of the home directory.
The private part of the key must be secret, hidden, secure, and should be
password-protected. If the private key is compromised, the security is gone!
When that happens, it is mandatory to quickly remove the corresponding
public keys from all firewalls to prevent misuse.
299
Chapter 21. Best Practice
Note
Each user can have multiple keys, which helps when exchanging keys
or when multiple admins share an account.
Warning
The public key is a one-line string. Even if puttygen stores the key in
multiple lines, OPNsense only accepts the key without line breaks.
300
Password reset
In the background, the web GUI stores the key string in the local text file
authorized_keys in the subfolder .ssh in the user’s home directory.
ssh root@10.5.1.1
PuTTY’s path to the private key file is at Connection → SSH → Auth. Then
the SSH login uses the crypto keys instead of the password.
Password reset
At some point in time there will be a situation where access to the firewall
fails due to an incorrect password or key. If it is not possible to log in to
the web GUI and SSH console, OPNsense provides a procedure to reset the
password.
Note
This method overwrites the lost password. It is not displayed later or
stored in plain text.
This procedure requires access to the text console. Choose menu item 3)
Reset the root password to set the password of root to a known value.
301
Chapter 21. Best Practice
2. Wait for the boot menu (see Figure 21.2) and choose item 2. Boot
Single User to skip the password authentication.
mount -o rw /
/usr/local/opnsense/scripts/shell/password.php
302
Password reset
6. Reboot. The final restart brings the system back to normal mode. A
login with the new password should be possible via the text console,
SSH console, or web interface.
303
Chapter 21. Best Practice
304
Chapter 22
Configuration
The unique thing about an OPNsense firewall is the content of the configu-
ration file. Everything else comes from the installation medium. For this
reason, it is best to keep a copy of the configuration file in a safe place. This
chapter enhances a configuration backup with remote file storage.
OPNsense saves the configuration to the local hard disk after each change
and remembers the last sixty changes by default.
Warning
The following steps make changes to the operating system and may
affect the stability of the OPNsense firewall.
Dropbox
Dropbox is the top dog for inexpensive storage space on the Internet. A
few gigabyte are free, and Dropbox accepts all kinds of files – including
configuration files.
305
Chapter 22. Configuration
The command exits successfully, but only the web interface of Dropbox can
validate an uploaded file, as Figure 22.1 confirms.
306
Dropbox
Figure 22.1: Dropbox stores the configuration file from OPNsense’s firewalls
Note
If the upload was successful but Dropbox does not display the new file,
then the uploaded file does not contain any changes.
Automatic backup
The new backup method is a bit cumbersome because OPNsense only saves
to the local hard drive by default and ignores Dropbox.
OPNsense needs the help of its scheduler cron to save a copy of the con-
figuration file into Dropbox automatically. The scheduler can execute
commands regularly at fixed times and run the backup to the Dropbox.
The following command causes a daily upload to keep the dropbox archive
synchronized. If an error occurs, the process reports the failure to the logfile
(line 3).
1 cat <<EOF >> /etc/periodic/daily/900.backup-config
2 dropbox -q upload /conf/config.xml ‘hostname -s‘.config.xml || \
3 logger -t dropbox -p local0.error \
4 "Failed to upload the configuration file to Dropbox"
5 EOF
6 configctl cron restart
307
Chapter 22. Configuration
Note
Upload the configuration file as often as required, but specify the
interval and commands for the upload process in /etc/crontab. The
man page of cron provides information and examples about the syntax:
man 5 crontab
308
Google Drive
Google Drive
The configuration backup with Google Drive differs from Dropbox in that
OPNsense has integrated Configuration Cloud Backup in its web GUI since
March 2015. The setup on the client side does not require any unofficial
modifications to the operating system.
OPNsense uses the Google API to communicate with Google Drive. The
result is the same as with Dropbox: the configuration file is stored – after
some setup effort – in the cloud.
Hit the button Create Project to start a new project. The project name is
insignificant, but it does not hurt if it describes the subject, such as “OPN-
sense config backup.”
Next, go to the APIs & Services section of the Dashboard and click the
button Enable APIs and Services. After that, Google wants to know which
API because there is a long list of applications. Choose the Google Drive
API, which is enabled a few seconds later.
Navigate to APIs & Services → Credentials. Hit the button Create cre-
dentials to start the authorization procedure. Choose Service account from
the provided list (Figure 22.3). Pick something from “Service Accounts” for
the access role. Next, create a key for this new service account. OPNsense
requires the key type P12.
309
Chapter 22. Configuration
Now Google creates a P12 certificate and offers it for download. This file is
imported into OPNsense later.
Also, take note of the service account ID. The ID looks like an email address
and matches the private key of the P12 file. For example:
rt-989@opnsense-config-backup-187541.iam.gserviceaccount.com
Both the service account ID and certificate file are required to access the
Google Drive API.
Set up Drive
Google Drive has an easy-to-remember URL https://drive.google.com.
Access that web page with a registered Google account. To keep Drive
organized, create a new folder for the configuration files. Push the New
button in the upper left corner to create this folder and choose a descriptive
name like “RT-1”. Next, share the newly created folder by clicking on the
folder icon and choose Share from the context menu.
Who may access this folder? Here is where the service account from the
previous section comes into play. Insert the service account ID into the field
people and groups. The last piece of the puzzle is the folder ID that Google
hides behind the Advanced button. Locate the Get link button and extract
the folder identifier. The following link shows an example URL for folder
sharing with the folder ID highlighted.
310
Google Drive
https://drive.google.com/drive/ \
folders/ 0B65iPldCRuWEeF0hc1KpapnqkRK ?usp=sharing
Upload
Unlike Dropbox, Google Drive does not automatically collect a version
history of files with the same name. After ten uploads from ten firewalls,
there are already one hundred files with similar names in the drive, which
only differ in timestamps. Now it’s OPNsense’s turn to connect its web UI
at System → Configuration → Backups to Google Drive.
Table 22.1 summarizes the sample values from the previous sections. Fur-
thermore, OPNsense needs a password to encrypt the configuration file so
that no plain text files reside in Drive.
Attribute Example
Enable 2
Email Address rt1-669@opnsense-config-backup-178 \
541.iam.gserviceaccount.com
P12 key Certificates file from section
Access the API (page 309)
Folder ID 19ZO8tCjGpFs-jS0WP_xnem1Zl3_R3PFL
Prefix hostname to
backupfile 2
Backup Count 10
Finally, it gets exciting, because the button Setup/Test Google Drive val-
idates the access to the Google API and uploads the first configuration
directly into Drive.
311
Chapter 22. Configuration
Figure 22.4: Google Drive only gets the encrypted firewall configuration
Automatic backup
A single backup to Drive is good, but a regular backup is better. And this is
where OPNsense automatically loads the configuration to Drive once a day.
Manual intervention is not necessary. After several days the drive fills up
and may resemble the folder in Figure 22.5.
312
Summary
Summary
OPNsense can upload its configuration to remote servers and cloud pro-
viders. This form of outsourcing stores the configuration file in an off-site
location so that the OPNsense firewall can be rebuilt in the event of a
disaster.
The default cloud provider is Google Drive, but it is also possible to use
other providers with an open API. This chapter presented the integration of
Dropbox. OPNsense can even talk to the private cloud provider Nextcloud
or run a simple backup via SCP.
313
Chapter 22. Configuration
314
Chapter 23
Life Hacks
Warning
The sections in this chapter expect to modify files, specifically those
not intended to be modified during normal firewall operation. A copy
of the data should be made before the first edit so that the original
state can be quickly restored in case of problems.
Those familiar with Linux or UNIX can edit the files directly on the com-
mand line using available editors vim, nano, joe, emacs or Easy Editor.
Find a quickstart guide for working with UNIX files in Appendix A.
There is good news: Windows users will not have to mess with the UNIX
command line. There are some excellent graphical file explorers for Win-
dows. A well-known representative is introduced in the following section
Access from Windows.
315
Chapter 23. Life Hacks
Span port
Copying frames from one port to another has many names: Port mirroring,
Port monitoring, Switched Port Analyzer (SPAN), or Span port. It all means
the same, a Span port copies all incoming and outgoing network packets
from one interface to another interface. The traffic is not modified or
blocked in any way.
The target port receives a copy of every frame. It is typically connected to
some kind of network analyzer or Intrusion Detection system.
In the following example, all packets between RT-1 and the DMZ should be
sent as a copy to interface MIRROR.
Next, set up a new network bridge in OPNsense at Interfaces → Other
Types → Bridge. The new bridge has only a single member interface –
the network adapter to be monitored (e.g. DMZ ). The mirror options are
hidden in the advanced options. Find the option Span port and choose
DMZ from the list, as shown in Figure 23.1.
Save the configuration by hitting the button Save at the bottom. OPNsense
will create the bridge and begin mirroring traffic. The network bridge
monitors traffic on its member interface and sends a copy of each packet
to the selected Span port. It is even possible to mirror multiple source
316
Telegram
adapters to a single span port – just add more member interfaces to the
bridge. Keep in mind that the span port must not be a member of the bridge!
Telegram
Critical messages must trigger an alert on the administrator’s smartphone –
in real time.
So far, all log entries remain in the file system of the firewall or at the
central log host. If this logging server does not have a method for sending
critical messages, OPNsense’s syslog service can perform this task.
Strictly speaking, Syslog passes the copy of a critical message to a script for
review. If Syslog classifies the message as reportable, it will send an alarm
message to a smartphone.
Syslog-NG expects the configuration file telegram.conf from Listing 23.1
in the directory /usr/local/etc/syslog-ng.conf.d/. Restarting the ser-
vice will apply the change:
The strings are just examples and do not represent a complete monitoring
solution. The new configuration file will relay critical syslog messages
(lines 7–10) to the telegram.sh script (line 2) in addition to its regular
317
Chapter 23. Life Hacks
1 destination d_telegram {
2 program("/usr/local/bin/telegram.sh"
3 template("${DATE} ${HOST} ${MESSAGE}\n") );
4 };
5
6 filter f_telegram {
7 message(".*telegram-test.*")
8 or message(".*error for illegal user.*")
9 or message(".*GATEWAY ALARM.*")
10 or message(".*failed waiting for configd.*")
11 };
12
13 log {
14 source(s_all);
15 filter(f_telegram);
16 destination(d_telegram);
17 };
processing. Syslog sends the messages through STDIN. Also, make sure the
telegram.sh script from Listing 23.2 has the executable flag set:
chmod +x /usr/local/bin/telegram.sh
Note
This policy does not inspect old syslog messages for alerts. It will
process only new messages, which are currently logged in the system.
318
Telegram
1 #!/bin/csh
2 set API_KEY=419840419:AAGPcJDOIe5YCS_ep3yfgPJM-f7E7m8MMMY
3 set CHAT_ID=273171581
4 set LINE = "$<"
5 /usr/local/bin/curl --insecure \
6 --data "chat_id=${CHAT_ID}&text=${LINE}" \
7 https://api.telegram.org/bot${API_KEY}/sendMessage
Note
The regular logging process of an OPNsense firewall is not interrupted
if the telegram script is malfunctioning or unexpectedly stops.
Until now, the syslog process did not know anything about a Telegram
script. Therefore, OPNsense must reload the configuration file. This action
is done at System → Diagnostics → Services by pressing the button restart
next to syslog-ng or by rebooting the system. Next, generate a harmless
message from the command line to validate the new syslog alerter:
root@RT-1:˜ # logger telegram-test
This message is considered as an error and will trigger an alert. The script
will attach the syslog text in the message to the smartphone. Figure 23.2
shows the smartphone app, which has received various alarms from the lab
firewall.
319
Chapter 23. Life Hacks
Next, it is time to choose a category text for each rule. There are major
benefits in creating firewall filters using the categories rule, and it is well
worth the effort. The selection of rules in Figure 23.4 shows only the rules
categorized with the word dns. Multiple choices are possible.
320
Quick search
Note
The categories aim is to improve the organization of the ruleset. It
does not affect the operation of the packet filter.
Quick search
Users switching from pfSense will miss some topics because OPNsense has
placed the menu item elsewhere in the web interface. Manually searching
the web UI is quite frustrating and complicates the transition to OPNsense.
The web GUI offers a quick search in the title bar, which shows all available
topics as soon as the first letters are typed in. In Figure 23.5 the admin
searches for the magic word dns and immediately sees all areas dealing
with name resolution. A mouse click on the desired records leads to the
selected topic.
Figure 23.5: OPNsense will find the missing item with the quick search
321
Chapter 23. Life Hacks
322
Chapter 24
Application Programming
Interface
Access to an OPNsense system is not only possible via a web interface, but
also a programming interface. While web access is designed for the human
user, the programming interface is accessible from machines, scripts, and
monitoring systems.
OPNsense considers itself an open platform and presents its methods via
Application Programming Interface (API) so that the community can inte-
grate additional commands and processes.
Using the API is independent of any programming language, but requires
basic knowledge in Python to create new commands or modify existing
ones.
The open programming interface is a new concept of OPNsense, and did
not exist under the predecessor pfSense.
323
Chapter 24. Application Programming Interface
granted on an equal basis via a web UI and API. Both methods have their
own view (View; see section Model View Controller on page 325) of the
OPNsense firewall. The requests end up in the background at the Controller,
which takes care of the internal processing.
The communication between client and server use the well-known HTTP
protocol. The client rephrases its question as a web address, e.g.
http://opnsense.example.net/api/core/firmware/running
324
How does the API work?
{"status":"busy"}
The client can also change the OPNsense firewall configuration with the
API. How does the server know if the client wants to change something
or just gather information? This is where HTTP commands come in handy.
These commands are hidden in the background when using a regular web
browser.
The client can request information with the API using either the GET or
POST method. The GET method simply requests information while the
POST method sends additional data to the server, i.e. information not in-
cluded in the web address but required by the server. The GET request has
the reputation of an informational request without changes to the firewall.
The POST request, on the other hand, is generally used to change the state
in the server; in OPNsense this leads to a configuration change.
The HTTP protocol offers additional methods that are not used by the
OPNsense API.
• The model contains the data of the system, which it gets from the
controller and represents in the view.
325
Chapter 24. Application Programming Interface
• The controller receives commands from the view and thus from the
user. The controller evaluates the commands, manipulates the model,
and updates the view, as a confirmation for the user.
View
Notify Write
Manipulate
Model Controller
Update
Figure 24.2: The interaction of Model, View, and Controller
Figure 24.1 on page 324 shows the usage at OPNsense. HTTP clients
observe the firewall through the view. One way to access the view, is from a
web browser. The replies are formatted as colorful web pages. Another view
for the API answers in pure JSON style and does not care about graphical
representation.
The view interacts with the controller, which is the central element of
data processing. The controller understands the requests and uses them
to create commands for the background services (see Chapter 19). For
change requests, the controller contacts the model that holds the data
inventory. With OPNsense, the data comprise of the current configuration,
which is primarily located in the central file config.xml. Simple or not, the
controller can not directly interact with the config.xml, but rather direct
all change requests to the model. This division occurs because the model is
the data expert and knows how to restart services after specific changes.
Documentation
The official documentation contains exact details about the usage and an
overview of the available options for each module and plug-in. Unfortu-
nately, the explanation is a bit too short – the meaning of an API resource is
326
Read Access
only revealed by the function’s name. For example, it might be obvious that
the function upgrade from firmware starts a software update. However, it
is less obvious what the function dirty from monitoring tool Monit might
trigger.
The error messages are also of little help. If a call fails, the API returns only
brief responses such as
{"result":"failed"}
Extending the API with custom calls is well documented on the homepage
of OPNsense. In addition to examples, naming conventions, and guidelines,
there are templates to quick start the module programming.
Read Access
Before the client starts, it needs permission to call the API functions. This
access is comparable to a user login on a web interface because without
authentication the API refuses to answer anything.
The API key is available in the web menu under System → Access → Users.
The default account root of a test firewall is suitable for experiments. Locate
the API key section of the root user and click on the plus icon, which will
create an API key and offer to download it. The API access consists of a key
and a secret password. Together they allow access to the API.
key=j8i0QGGOq/vLJBq4RJpjZAheOvKYDFJYMuktnb2wo7EFwx/hxZo9nAdU[...]
secret=YbDe5YoLv7Cep8ATduQRr8StPO+YL3NE6l9EDiAEZS9OqJ4H0H1vh[...]
Each user account can create any number of API keys. The official OPN-
sense documentation recommends a separate key for every purpose.
Note
The API key is shortened in the following examples to improve the
readability of the commands.
327
Chapter 24. Application Programming Interface
The command curl uses the API key as a combination of username and
password to authenticate a web page. Each request requires a repeated
authorization.
If the key is correct and the remote firewall is reachable, it will respond
with a JSON message:
{"connection":"ok","downgrade_packages":[],"download_size":"","l \
ast_check":"Tue Feb 2 09:29:51 CET 2021","new_packages":[],"os_ \
version":"FreeBSD 12.1-RELEASE-p12-HBSD","product_name":"opnsens \
e","product_version":"21.1","reinstall_packages":[],"remove_pack \
ages":[],"repository":"ok","updates":"0","upgrade_major_message" \
:"","upgrade_major_version":"","upgrade_needs_reboot":"0","upgra \
de_packages":[],"all_packages":[],"status_msg":"There are no upd \
ates available on the selected mirror.","status":"none"}
The command line is the ideal location for scripting. However, to explore
the API, there are graphical tools, one of which is introduced in the section
API browser on page 331.
328
Write Access
Write Access
Accessing the API can also bring changes to the configuration. This write
access requires the HTTP method POST.
Using the intrusion detection system example (see Chapter 18), the API
shows the details of an IDS rule and allows changes to it. The information
for rule number 2013861 is provided by the subsequent call to getRuleInfo,
where the API key is expected in the environment variables.
The API lists all information and states of the requested IDS rule.
{
"action": {
"alert": {
"value": "Alert",
"selected": 1
},
"drop": {
"value": "Drop",
"selected": 0
}
},
"action_default": "alert",
"classtype": "bad-unknown",
"created_at": "2011_11_07",
"enabled": 1,
"enabled_default": 1,
"former_category": "HUNTING",
"gid": null,
"matched_policy": null,
"msg": "ET DNS Query for Suspicious .nl.ai Domain",
"reference": null,
"rev": 3,
"sid": 2013861,
"source": "emerging-dns.rules",
"status": "enabled",
"updated_at": "2020_09_17"
}
329
Chapter 24. Application Programming Interface
The python script json.tool modifies the answer optically to make it more
readable to the human eye. The action of this rule is Alert.
Use the API command setRule to change the rule action to Drop.
1 curl --silent --user "${key}":"${secret}" \
2 --request POST \
3 --data ’{"action":"drop"}’ \
4 --header "Content-Type: application/json" \
5 http://10.5.1.1/api/ids/settings/ setRule /2013861
The API command requires the HTTP method POST (line 2), the intended
action (line 3) and the JSON format (line 4). The rule number in question
remains in the web address (line 5).
Note
Most of curl ’s options have an abbreviated form (e.g. -d) and a long
form (e.g. --data). The examples in this chapter print the longer
arguments for better readability.
Moving the result through json.tool is not necessary because the answer is
also clearly recognizable in its raw state:
{"result":"saved"}
Call getRuleInfo again to confirm that the rule action changed to Drop.
• Captive Portal
• Cron
• Diagnostics
• Firewall
• Firmware
• Interfaces
330
API browser
• Intrusion Detection
• IPsec
• Monit
• NetFlow and Insight
• OpenVPN (only Client Export)
• Routes
• Shutdown and Reboot
• Syslog
• Traffic Shaper
• Web Proxy
Otherwise, the configuration is still in the original code from the predecessor
pfSense, which was not intended to work with an API:
http://rt-1.opnsense.lab/system_general.php
API browser
Easy access to the programming interface comes with a graphical API
browser. This software generates the same syntax for a request, but the
parameters and headers section is crafted from an intuitive interface.
API browsers are available as a stand-alone software or as a plug-in for a
web browser. The Chrome extension RESTED [30] does an excellent job
and provides all necessary functions for the web API of OPNsense.
331
Chapter 24. Application Programming Interface
Figure 24.3 shows how to use RESTED to read the IDS rule from section
Write Access. First, insert the API key material into the browser; the API
key is stored as a username and the API secret as a password.
Do not get confused: the return code 200–OK means that the server has
understood and processed the request. Nevertheless, the answer can return
a negative result, which is not OK depending on the expected output.
Security
Nowadays, unencrypted access with HTTP to the API is only tolerated in
an isolated demo environment. It is best to use encrypted communication
with HTTPS in the local network and on the Internet.
However, the improved security of HTTPS only works if the client can
validate the server certificate. For this reason, the OPNsense firewall must
own and present a correctly issued certificate.
For environments with a weak security level, the OPNsense firewall can
act as a Certificate Authority (CA) and issue a certificate for its website
332
Technical background
(see Chapter 14). If the API client trusts this CA, subsequent attacks on the
connection can be detected, and HTTPS saves the day.
First, convince the client to trust the CA. OPNsense hosts the certificate
management under System → Trust → Authorities. The export CA cert
button offers the certificate for download. If this button is missing, the
web server does not have a properly signed CA certificate, but still uses the
self-signed certificate from the initial installation. Give the downloaded CA
certificate the suitable filename ca-opnsense-lab.crt and copy it to the
client, which will be used later during negotiation with the API.
Finally, switch the protocol of the web UI from HTTP to HTTPS at System
→ Settings → Administration. From now on, the web interface of OPNsense
is only accessible through encrypted channels.
Armed with the CA certificate, the client can now validate the server’s
authenticity. The curl command either raises an alert while negotiating the
HTTPS connection or is silent when everything is fine.
1 curl --silent --user "${key}":"${secret}" \
2 --cacert ca-opnsense-lab.crt \
3 https ://rt-1.opnsense.lab/api/core/firmware/running
curl learns about the certificate by the command option in line 2. Once the
protocol is changed to https (line 3), the communication is encrypted and
authenticated between API client and server
Technical background
The HTTP server lighttpd is the service responsible for the configuration
interface and the web API. The service distinguishes the requests by the
path of a URL: a configuration web page always begins with /ui, while the
API uses the unique prefix /api.
All requests to the API end up in the script api.php on the server side. The
front-end of the API is the PHP file:
/usr/local/opnsense/www/api.php
The path of the web address is passed as an argument when calling api.php
so that the script knows what to do.
333
Chapter 24. Application Programming Interface
The API only works for the parts of the source code that have already been
rewritten from the pfSense code to the new OPNsense style. The developers
of OPNsense divide their software into components (see section Model
View Controller on page 325) and expect better code quality and shorter
development times.
Outlook
The OPNsense programming interface is still under construction, but the
basic structure is already in place. When the API is complete, it will be
possible to completely control an OPNsense firewall by scripts or a command
line. A uniform management software would also be imaginable, which
centrally manages all OPNsense firewalls in the corporate network and
configures them via the API.
Until now, the API has been used by the web interface to populate the page
with additional content, such as the current utilization of network cards and
system states. In this area, too, the developers are committed to dynamic
content, which is loaded via API calls.
Summary
The OPNsense programming interface is a way to query and configure the
firewall using a text-based web client. The syntax of the commands and
formats resemble common techniques so that a software developer can
quickly learn how to use the API.
Unfortunately, the API does not yet cover all areas offered in the web
interface. Virtually every new version of OPNsense enables more categories
to be used with the API.
334
Bibliography
[4] OPNsense project team: OPNsense is a true open source firewall. 2021.
https://opnsense.org/
335
Bibliography
[25] Simon Tatham: PuTTY: a free SSH and Telnet client. 2020.
https://www.chiark.greenend.org.uk/~sgtatham/putty/
336
Bibliography
[29] Daniel Stenberg et al.: curl: command line tool and library. 2021.
https://curl.se/
[30] Espen Henriksen: RESTED – A REST client for the rest of us. 2019.
https://github.com/RESTEDClient/RESTED
337
Bibliography
338
Index
339
Index
340
Index
341
Index
342
Index
343
Index
344
Index
WAN, 227
web cache, see cache
web GUI, 56
345
Index
346
Appendix A
OPNsense has forked the web interface of pfSense, but unfortunately not
all commands of the operating system. Several chapters require access to
the underlying operating system FreeBSD because the intended task is not
available from the web interface. OPNsense does not put any barriers in the
way and allows access to the operating system level with the menu item 8
Shell in the text console.
The contents of a text file are displayed page by page with the more com-
mand. Jump to the next page with the spacebar, while using the Enter key
to move to the next line.
For example, have a look at the configuration file of PHP with:
more /usr/local/etc/php.ini
While browsing within the file, the v key switches to the text editor if
changes to the content are desired. Quit using more with the q key.
347
Appendix A. Editing Files in FreeBSD
Edit a file
Do not change anything without backing up! Make a copy of the original
file before your fingers modify its content. The effort is minimal and keeps
emergencies caused by fatal changes from turning into disastrous results.
The command to copy files under UNIX is cp. Append the source file and
destination file as arguments. Create a backup copy of the above text file
with the following command:
cp /usr/local/etc/php.ini /usr/local/etc/php.ini .orig
OPNsense provides two editors which differ in their operation and complex-
ity. For users with little previous knowledge of UNIX, the Easy Editor is a
good choice. If you want to learn more about UNIX, take a look at the vim
editor.
Easy Editor
The Easy Editor is a lightweight editor, which implements essential func-
tions for editing files. The starter command ee expects the file name as an
argument. It will then open the editor window and present the file content.
ee /usr/local/etc/php.ini
The top lines of the ee window are filled with commonly used commands.
The abbreviations ^v stands for the keyboard shortcut Ctrl-v and will jump
to the next page. The editor will save the file to disk when exited with
Escape+Enter. The editor offers more information in the integrated help
section at Escape+b and inside the man-page.
Vi IMproved
The vim text editor is an enhanced version of the older vi. It is more
user-friendly and has more features. Actually, it is overqualified for the task
of editing plain text files.
348
The vim has two modes of operation: normal mode and insert mode. During
normal mode all user input is recognized as commands. These commands
may delete lines, copy words, search-and-replace, and move around inside
the file. The i key (for insert) switches the vim to insert mode. Within this
mode, all user input is recognized as text. It is inserted in the text at the
cursor position. The Esc key switches the editor back to normal mode.
It is common to switch modes frequently while working with vim. Using
vim for the first time is somewhat counterintuitive, but with knowledge of
the most important commands, files can be edited very efficiently.
The editor is called by the command vi and expects a file name as argument:
vi /usr/local/etc/php.ini
Table A.1 contains frequently used command for the vim editor.
Command Effect
:w write. Save the file.
:q quit. Quit the editor.
:q! Quit the editor without saving the file.
:wq Quit the editor and save the file.
i insert text before the cursor.
I Insert text at the beginning of the current line.
a append text after the cursor.
A Append text at the end of the current line.
o Create a new line below the current line.
O Create a new line above the current line.
x Delete the character at the position of the cursor.
D delete from the cursor to the end of the current line.
dd Delete the current line.
yy yank. Copy the current line to the clipboard.
p paste. Insert the content of the clipboard after the cursor.
u undo. Revert the last action.
349
Appendix A. Editing Files in FreeBSD
If the vim has changed or deleted too much, pressing the u key several
times will undo the changes until the file is back to a known state. And if
the file content is hopelessly destroyed, hit :q! to exit the editor without
saving.
Complete books have been written about the vim, and the developer’s web-
site http://www.vim.org/ offers a good introduction.
350
Appendix B
Pattern Matching
OPNsense uses the regular expressions for access control lists in the web
proxy. The help function of the website gives a few brief examples, which
are described in more detail here.
In the simplest case, the search pattern consists of only one word without
special characters. The string example.net finds all websites that have this
text somewhere in the URL. The letters in front and behind the string are
irrelevant.
The following text would match the pattern example.net:
mail.example.net
www.example.net/wp-content/uploads
http://docs.opnsense.org/search.html?q=example.net
This text does not contain example.net
351
Appendix B. Pattern Matching
Note
If the regular expression contains a space, the system searches for a
space in the text. The space is therefore not suitable for making the
Regex more readable.
Selections
The strength of the regular expressions lies in the wildcard characters,
which do not search for a specific letter, but a selection of characters. The
expression a-z finds all lowercase letters. With E-H, Regex will match the
capital letters E, F, G, and H. And 0-9 will match a single digit. When using
these tricks, the expression must be enclosed in square brackets.
It is even possible to combine multiple selections:
[a-zA-Z0-9]
The square brackets may also appear several times or be mixed with normal
text. The following expression searches for the digits 1 to 3, followed by a
fixed text, the digits 6-9, any digit, and finally the word Meters written in
lowercase letters, beginning with either an upper- or lowercase letter.
[123]times [6-9][0-9] [Mm]eters
Quantifiers
If the pattern should match several characters of the same type, the plus
sign comes into play because plus searches “one or more” of the previous
expressions. The following pattern recognizes a multi-digit hexadecimal
number:
[a-fA-F0-9]+
This expression searches for the lower- and uppercase letter a to f or a digit.
And the string may appear multiple times, but at least once.
352
The asterisk has a similar effect as the plus, but it also accepts when
the desired pattern does not occur.
The following example shows a pattern that is optional by specifying *
behind the selection. That way, it may appear after the word July, but could
also be missing.
Between zero and any number of times, the regular expressions allow
precise specifications or a range.
• The range {3,} has no upper limit. It will match all patterns with at
least three characters.
[0-9]{4,5}0{3} [A-Z]{3}
Characters
The regular expression of [A-Za-z0-9_] may be abbreviated as \w. Like-
wise, [0-9] can disguise itself as \d and \s stands for space bar, line feed,
and tabulator – basically anything that creates distance.
The pattern
\d{5}\s\w{4,}
matches a five-digit zip code for a city whose name has four or more letters.
353
Appendix B. Pattern Matching
Special characters
The period is a wildcard character and represents every single character
except the line break. The pattern fire.all matches firewall and fireball
but also senseless words like firenall and fire1all.
Table B.1 contains the most essential special characters.
Character Match
. Matches any single character (except a line break).
[ ] Matches any character within the square brackets.
[^] Matches anything except the characters in the square
brackets.
^ Matches any string that starts with the pattern.
$ Matches any string that ends with the pattern.
(|) Alternative. (gif|png|jpg)
* The pattern may appear once, multiple times, or not at all.
+ The previous pattern must appear once or more.
? The previous pattern may appear once or not at all.
\ Converts the following special character back to a regular
character.
Examples
^https?:\/\/[a-zA-Z]+\.example\.(net|org|com)
The leading ^ character forces the string to begin with http. The http may
have an additional s, but that is not required. Next is a colon and two
forward slashes. The forward slash is also a special character, though it
needs a backslash to be interpreted as a regular character.
354
Next, the pattern expects one or more lower-/uppercase letters, followed
by a period and the fixed string example and another period. The period
also requires a backslash. Otherwise, it acts as wildcard and matches any
character. The pattern continues with either net or org or com.
The Regex stops after the top-level domain, but the matching string may
contain more characters. If that is not intended, complete the pattern with
the $ character to indicate that no characters may follow.
^(https?|ftp):\/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/
This Regex catches all http, https, and ftp requests that contain one to three
digit figures (\d{1,3}). The figures are separated by periods. When applied
to the blacklist of a proxy server, it will filter all requests that contain an
IPv4 address.
This method obviously only works for IPv4 addresses. The variable spelling
of IPv6 addresses (see Chapter 5) requires a more complex regular expres-
sion.
Testing
Regular expressions are an excellent source of errors and typos because a
complex Regex looks similar to encrypted text. It is best to develop and
validate the Regex in a Regex Tester. These web services expect the regular
expression and a text. The text then highlights what matches the selected
expression.
The websites listed below are a good starting point. Also, a web search with
the keywords pcre regex tester will provide a large selection.
https://regex101.com/
https://www.regextester.com/
355
Appendix B. Pattern Matching
356
Appendix C
Bonus Material
The printed examples in all chapters represent only a fraction of the infor-
mation relevant to the respective topic. The full configuration files of all
devices are available online at
https://practical-opnsense.github.io
https://github.com/practical-opnsense
or at
https://practical-opnsense.sourceforge.io
https://sourceforge.net/projects/practical-opnsense/
The URL contains all material that did not fit in this book, namely:
• All scripts that are just mentioned in this book or printed in part.
357