Install and Configure Snort 3 NIDS on Ubuntu 20.04

0
509

In this tutorial, you will learn how to install and configure Snort 3 NIDS on Ubuntu 20.04. Snort is a lightweight network intrusion detection system. It features rules-based logging and can perform content searching/matching in addition to detecting a variety of other attacks and probes, such as buffer overflows, stealth port scans, CGI attacks, SMB probes, and much more. Snort has a real-time alerting capability, with alerts being sent to
syslog, a separate “alert” file, or even to a Windows computer via Samba.

Some of the Snort 3 features include;

  • Support multiple packet processing threads
  • Shared configuration and attribute table
  • Use a simple, scriptable configuration
  • Make key components pluggable
  • Autodetect services for portless configuration
  • Support sticky buffers in rules
  • Autogenerate reference documentation
  • Provide better cross platform support

Install and Configure Snort 3 NIDS on Ubuntu 20.04

At the moment, Ubuntu 20.04 provides snort 2.9 on its default Universe repos;

apt show snort
Package: snort
Version: 2.9.7.0-5build1
Priority: optional
Section: universe/net
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Javier Fernández-Sanguino Peña <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 1,987 kB
Pre-Depends: adduser (>= 3.11)
Depends: snort-common-libraries (>= 2.9.7.0-5build1), snort-rules-default (>= 2.9.7.0-5build1), snort-common (>= 2.9.7.0-5build1), debconf (>= 0.5) | debconf-2.0, rsyslog | system-log-daemon, logrotate, net-tools, libc6 (>= 2.16), libdaq2, libdumbnet1 (>= 1.8), liblzma5 (>= 5.1.1alpha+20120614), libpcap0.8 (>= 1.0.0), libpcre3, zlib1g (>= 1:1.1.4)
Recommends: iproute2
Suggests: snort-doc
Conflicts: snort-mysql, snort-pgsql
Replaces: snort-common (<< 2.0.2-3)
Homepage: http://www.snort.org/
Download-Size: 656 kB
APT-Sources: http://ke.archive.ubuntu.com/ubuntu focal/universe amd64 Packages

Build and Install Snort 3 from Source Code on Ubuntu 20.04

In order to install and configure Snort 3 NIDS on Ubuntu 20.04, you need to build it from the source.

Run System Update

To begin with, run system package cache update;

apt update
apt upgrade

Install Required Build Tools

For a successful build and installation of Snort 3 on Ubuntu 20.04, there are a number of build tools and dependencies that needs to be installed prior to the build process as outlined on the Dependencies page.

apt install build-essential libpcap-dev libpcre3-dev libnet1-dev zlib1g-dev luajit hwloc libdnet-dev libdumbnet-dev bison flex liblzma-dev openssl libssl-dev pkg-config libhwloc-dev cmake cpputest libsqlite3-dev uuid-dev libcmocka-dev libnetfilter-queue-dev libmnl-dev autotools-dev libluajit-5.1-dev libunwind-dev

Download and install latest version of the Snort DAQ (Data Acquisition library) . DAQ is not available on the default Ubuntu repos and hence, you need to build and install it from the source;

mkdir snort-source-files
cd snort-source-files
git clone https://github.com/snort3/libdaq.git
cd libdaq
./bootstrap
./configure
make
make install

Download and install google’s thread-caching malloc, Tcmalloc, a memory allocator optimized for high concurrency situations which will provide better speed for the trade-off of higher memory usage. This is an optional dependency but highly recommended.

cd ../
wget wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.8/gperftools-2.8.tar.gz
tar xzf gperftools-2.8.tar.gz
cd gperftools-2.8/
./configure
make
make install

Install Snort 3 from Source Code on Ubuntu 20.04

Now that we have all required dependencies in place, download and install Snort 3 on Ubuntu 20.04;

Clone Snort 3 Github source code;

cd ../
git clone git://github.com/snortadmin/snort3.git

Navigate to Snort 3 source directory, compile and install it;

cd snort3/
./configure_cmake.sh --prefix=/usr/local --enable-tcmalloc
...
-------------------------------------------------------
snort version 3.0.2

Install options:
    prefix:     /usr/local
    includes:   /usr/local/include/snort
    plugins:    /usr/local/lib/snort

Compiler options:
    CC:             /usr/bin/cc
    CXX:            /usr/bin/c++
    CFLAGS:            -fvisibility=hidden   -DNDEBUG -g -ggdb  -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free 
    CXXFLAGS:          -fvisibility=hidden   -DNDEBUG -g -ggdb  -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free 
    EXE_LDFLAGS:        
    MODULE_LDFLAGS:     

Feature options:
    DAQ Modules:    Static (afpacket;bpf;dump;fst;nfq;pcap;trace)
    Flatbuffers:    OFF
    Hyperscan:      OFF
    ICONV:          ON
    Libunwind:      ON
    LZMA:           ON
    RPC DB:         Built-in
    SafeC:          OFF
    TCMalloc:       ON
    UUID:           ON
-------------------------------------------------------

-- Configuring done
-- Generating done
-- Build files have been written to: /root/snort-source-files/snort3/build

Navigate to the build directory and compile and install Snort 3 on Ubuntu 20.04;

cd build
make
make install

Once the installation completes, update shared libraries;

ldconfig

Verify Snort 3 Installation by checking the version;

snort -V
   ,,_     -*> Snort++ <*-
  o"  )~   Version 3.0.2 (Build 5)
   ''''    By Martin Roesch & The Snort Team
           http://snort.org/contact#team
           Copyright (C) 2014-2020 Cisco and/or its affiliates. All rights reserved.
           Copyright (C) 1998-2013 Sourcefire, Inc., et al.
           Using DAQ version 3.0.0
           Using LuaJIT version 2.1.0-beta3
           Using OpenSSL 1.1.1f  31 Mar 2020
           Using libpcap version 1.9.1 (with TPACKET_V3)
           Using PCRE version 8.39 2016-06-14
           Using ZLIB version 1.2.11
           Using LZMA version 5.2.4

The above confirms that Snort 3 installation is successful and is working fine.

Obtaining Snort Command Line Help

To obtain Snort command line help, simply execute either of the commands below and check the difference;

snort --help
snort -?
-? <option prefix> output matching command line option quick help (same as --help-options) (optional)
-A <mode> set alert mode: none, cmg, or alert_*
-B <mask> obfuscated IP addresses in alerts and packet dumps using CIDR mask
-C print out payloads with character data only (no hex)
-c <conf> use this configuration
-D run Snort in background (daemon) mode
-d dump the Application Layer
-e display the second layer header info
-f turn off fflush() calls after binary log writes
-G <0xid> (same as --logid) (0:65535)
-g <gname> run snort gid as <gname> group (or gid) after initialization
-H make hash tables deterministic
-i <iface>... list of interfaces
-k <mode> checksum mode; default is all (all|noip|notcp|noudp|noicmp|none)
-L <mode> logging mode (none, dump, pcap, or log_*)
-l <logdir> log to this directory instead of current directory
-M log messages to syslog (not alerts)
-m <umask> set the process file mode creation mask (0x000:0x1FF)
-n <count> stop after count packets (0:max53)
-O obfuscate the logged IP addresses
-Q enable inline mode operation
-q quiet mode - suppress normal logging on stdout
-R <rules> include this rules file in the default policy
-r <pcap>... (same as --pcap-list)
-S <x=v> set config variable x equal to value v
-s <snap> (same as --snaplen); default is 1518 (68:65535)
-T test and report on the current Snort configuration
-t <dir> chroots process to <dir> after initialization
-U use UTC for timestamps
-u <uname> run snort as <uname> or <uid> after initialization
-V (same as --version)
-v be verbose
-X dump the raw packet data starting at the link layer
-x same as --pedantic
-y include year in timestamp in the alert and log files
-z <count> maximum number of packet threads (same as --max-packet-threads); 0 gets the number of CPU cores reported by the system; default is 1 (0:max32)
...

Configuring Snort 3 NIDS on Ubuntu 20.04

Configure Network Interface Cards

First off, put the interface on which Snort is listening for network traffic on promiscuous mode so that it can be able to see all of the network traffic sent to it rather than seeing only the traffic originating from within the Snort 3 server alone.

ip link set dev enp0s8 promisc on

Verify;

ip add sh enp0s8
3: enp0s8: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:7f:84:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.57.3/24 brd 192.168.57.255 scope global dynamic enp0s8
       valid_lft 446sec preferred_lft 446sec
    inet6 fe80::a00:27ff:fe7f:8415/64 scope link 
       valid_lft forever preferred_lft forever

Disable Interface Offloading to prevent Snort from truncating large packets larger than 1518 bytes. You can check if this feature is enabled;

ethtool -k enp0s8 | grep receive-offload
generic-receive-offload: on
large-receive-offload: off [fixed]

GRO is enabled while LRO is fixed and hence cannot be changed.

Then disable;

ethtool -K enp0s8 gro off lro off

The two NIC changes are temporary. To ensure the changes persists across system reboot, create and enable a systemd service unit to implement the changes;

vim /etc/systemd/system/snort3-nic.service
[Unit]
Description=Set Snort 3 NIC in promiscuous mode and Disable GRO, LRO on boot
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/sbin/ip link set dev enp0s8 promisc on
ExecStart=/usr/sbin/ethtool -K enp0s8 gro off lro off
TimeoutStartSec=0
RemainAfterExit=yes

[Install]
WantedBy=default.target

Reload systemd configuration settings;

systemctl daemon-reload

Start and enable the service on boot;

systemctl enable --now snort3-nic.service

Install Snort 3 Rulesets on Ubuntu 20.04

Rulesets is the main artery for Snorts intrusion detection engine. There are three types of Snort Rules:

  • Community Rules
  • Registered Rules
  • Subscriber Rules

In this tutorial, we will install the community Snort rules;

Create Snort Rules directory. In the /usr/local/etc/snort/snort_defaults.lua config file, the default rules path (RULE_PATH), is defined as /usr/local/etc/rules.

mkdir /usr/local/etc/rules

Download Snort 3 community rules from Snort 3 downloads page;

wget https://www.snort.org/downloads/community/snort3-community-rules.tar.gz

Extract the rules and store them on Snort rules directory;

tar xzf snort3-community-rules.tar.gz -C /usr/local/etc/rules/
ls /usr/local/etc/rules/snort3-community-rules/
AUTHORS  LICENSE  sid-msg.map  snort3-community.rules  VRT-License.txt

Now that we have the rules to get us started in place, you need to configure Snort 3. Open the main configuration file for editing;

vim /usr/local/etc/snort/snort.lua

Set the networks to protect against attacks as the value for the HOME_NET variable. For simplicity, i just set this to the subnet of Snort 3 interface. The EXTERNAL_NET is anything other than our HOME_NET;

...
-- HOME_NET and EXTERNAL_NET must be set now
-- setup the network addresses you are protecting
HOME_NET = '192.168.57.3/32'

-- set up the external network addresses.
-- (leave as "any" in most situations)
-- EXTERNAL_NET = 'any'
EXTERNAL_NET = '!$HOME_NET'
...

You can edit Snort defaults in the /usr/local/etc/snort/snort_defaults.lua configuration file.

Under IPS section, define the location to your rules;

ips =
{
    -- use this to enable decoder and inspector alerts
    --enable_builtin_rules = true,

    -- use include for rules files; be sure to set your path
    -- note that rules files can include other rules files
    include = '/usr/local/etc/rules/snort3-community-rules/snort3-community.rules'
}
...

Save and exit the configuration file.

Installing Snort OpenAppID

OpenAppID is an application layer plugin that enables Snort to detect various applications,  Facebook, Netflix, Twitter, and Reddit, used in the network. Run the commands below download from Snort 3 downloads page and install Snort OpenAppID;

wget https://snort.org/downloads/openappid/12159 -O OpenAppId-12159.tgz
tar -xzvf OpenAppId-12159.tgz
cp -R odp /usr/local/lib/

Next, edit the Snort 3 configuration file and define the location of the OpenAppID libraries;

vim /usr/local/etc/snort/snort.lua
appid =
{
    -- appid requires this to use appids in rules
    --app_detector_dir = 'directory to load appid detectors from'
    app_detector_dir = '/usr/local/lib',
    log_stats = true,

}

Save and exit the configuration file.

Create Snorts Log directory;

mkdir /var/log/snort

Next, run syntax checking;

snort -c /usr/local/etc/snort/snort.lua
...
Finished /usr/local/etc/snort/snort.lua:
Loading /usr/local/etc/rules/snort3-community-rules/snort3-community.rules:
Finished /usr/local/etc/rules/snort3-community-rules/snort3-community.rules:
--------------------------------------------------
rule counts
       total rules loaded: 829
               text rules: 829
            option chains: 829
            chain headers: 56
--------------------------------------------------
port rule counts
             tcp     udp    icmp      ip
     any      63       3       0       0
     src     124       2       0       0
     dst     539      98       0       0
    both       0       1       0       0
   total     726     104       0       0
--------------------------------------------------
ips policies rule stats
              id  loaded  shared enabled    file
               0     829       0     829    /usr/local/etc/snort/snort.lua
--------------------------------------------------
flowbits
                  defined: 20
              not checked: 11
                  not set: 3
--------------------------------------------------
service rule counts          to-srv  to-cli
                      dns:       89       2
                      ftp:        7       2
                 ftp-data:        0       8
                     http:      489      92
                    http2:      489      92
                     imap:        0       8
                      irc:        4       1
              netbios-ssn:       15       1
                     pop3:        0       8
                     smtp:       16       0
                      ssl:       14      31
                   telnet:        1       0
                    total:     1124     245
--------------------------------------------------
fast pattern port groups        src     dst     any
                   packet:       11      24       2
--------------------------------------------------
fast pattern service groups  to-srv  to-cli
                   packet:        9       7
                      key:        2       0
                   header:        2       5
                     body:        2       0
                     file:        2       5
                   method:        2       0
--------------------------------------------------
search engine
                instances: 70
                 patterns: 1715
            pattern chars: 36451
               num states: 27885
         num match states: 1724
             memory scale: KB
             total memory: 785.997
           pattern memory: 102.521
        match list memory: 280.07
        transition memory: 394.656
--------------------------------------------------
pcap DAQ configured to passive.

Snort successfully validated the configuration (with 0 warnings).
o")~   Snort exiting

Create Custom local rules for the purposes of testing our Snort setup.

vim /usr/local/etc/rules/local.rules

Create a rule to detect ping tests;

alert icmp any any -> $HOME_NET any (msg:"ICMP connection test"; sid:1000001; rev:1;)

Save and exit the local rules file. Check the syntax;

snort -c /usr/local/etc/snort/snort.lua -R /usr/local/etc/rules/local.rules

Next, run the test by executing the command below;

snort -c /usr/local/etc/snort/snort.lua -R /usr/local/etc/rules/local.rules -i enp0s8 -A alert_fast -s 65535 -k none

On another terminal, ping your Snort server. While the ping runs, you should see the alert lines written to standard output;

...
--------------------------------------------------
search engine
                instances: 70
                 patterns: 1715
            pattern chars: 36451
               num states: 27885
         num match states: 1724
             memory scale: KB
             total memory: 785.997
           pattern memory: 102.521
        match list memory: 280.07
        transition memory: 394.656
--------------------------------------------------
pcap DAQ configured to passive.
Commencing packet processing
++ [0] enp0s8
08/22-19:20:23.502536 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:20:24.526491 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:20:25.550241 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:20:26.574652 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:20:27.598509 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
...

Configure Snort 3 Logging

To write Snort 3 events to log files, you need to enable configure alert settings. There are different Snort logging options that are explained well in the Snort 3 manual, Logger Modules section. To output the event data to a file, in brief format (as defined in the command line above by option -A alert_type), open the snort.lua configuration and head over to the outputs section.

vim /usr/local/etc/snort/snort.lua
---------------------------------------------------------------------------
-- 7. configure outputs
---------------------------------------------------------------------------

-- event logging
-- you can enable with defaults from the command line with -A 
-- uncomment below to set non-default configs
--alert_csv = { }
alert_fast = { 
        file = true, 
        packet = false,
        limit = 10,
}
--alert_full = { }
--alert_sfsocket = { }
--alert_syslog = { }
--unified2 = { }

The setting will cause snort to write logs to alert_fast.txt file. Save and exit the configuration and run syntax checking.

snort -c /usr/local/etc/snort/snort.lua

Run the command again, this time, without the option, -A alert_fast, but with an option to specify the log directory, -l /var/log/snort.

snort -c /usr/local/etc/snort/snort.lua -R /usr/local/etc/rules/local.rules -i enp0s8 -s 65535 -k none -l /var/log/snort/

Run the ping test again. If you check on the logs directory, you should see an alert_fast.txt file created. You can tail this file;

tail -f /var/log/snort/alert_fast.txt
08/22-19:30:41.554941 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:30:42.578554 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:30:43.602594 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:30:44.626660 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:30:45.650654 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
08/22-19:30:46.674630 [**] [1:1000001:1] "ICMP connection test" [**] [Priority: 0] [AppID: ICMP] {ICMP} 192.168.57.1 -> 192.168.57.3
...

You can include the local rules in snort.lua;

ips =
{
    -- use this to enable decoder and inspector alerts
    -- enable_builtin_rules = true,

    -- use include for rules files; be sure to set your path
    -- note that rules files can include other rules files
    include = '/usr/local/etc/rules/snort3-community-rules/snort3-community.rules',
    include = '/usr/local/etc/rules/local.rules'
}

Running Snort as a Service

While it is possible to run Snort as a daemon in the background with command line option -D, it is also possible to create a systemd service unit for Snort.

If you are going to run Snort as a service, it is prudent to run it a non privileged system user. Hence, create a non login system user account for Snort;

useradd -r -s /usr/sbin/nologin -M -c SNORT_IDS snort

Create a systemd service unit for Snort to be run as snort user. Adjust your interfaces accordingly.

vim /etc/systemd/system/snort3.service
[Unit]
Description=Snort 3 NIDS Daemon
After=syslog.target network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/snort -c /usr/local/etc/snort/snort.lua -s 65535 -k none -l /var/log/snort -D -i enp0s8 -m 0x1b -u snort -g snort

[Install]
WantedBy=multi-user.target

Reload systemd configs;

systemctl daemon-reload

Set the ownership and permissions on the log file;

chmod -R 5775 /var/log/snort
chown -R snort:snort /var/log/snort

Start and enable Snort to run on system boot. The service will run as root and then drop the privileges to Snort user created.

systemctl enable --now snort3

Check the service to confirm if it is running;

systemctl status snort3
● snort3.service - Snort 3 NIDS Daemon
     Loaded: loaded (/etc/systemd/system/snort3.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-08-22 20:50:26 UTC; 1min 14s ago
   Main PID: 43673 (snort)
      Tasks: 2 (limit: 2282)
     Memory: 145.3M
     CGroup: /system.slice/snort3.service
             └─43673 /usr/local/bin/snort -c /usr/local/etc/snort/snort.lua -s 65535 -k none -l /var/log/snort -D -i enp0s8 -m 0x1b -u snort -g snort

Aug 22 20:50:26 ubuntu20 systemd[1]: Started Snort 3 NIDS Daemon.

That marks the end of our tutorial on how to install and configure Snort 3 NIDS on Ubuntu 20.04.

Note that we have just scratched the service on what the configuration and what Snort 3 is capable of.

Reference

Snort 3 Installation

Further Reading

Snort 3 User Manual

Related Tutorials

Install and Configure AIDE on Ubuntu 20.04

Install and Configure Tripwire Security Monitoring tool on CentOS 8

Install and Setup Suricata on CentOS 8

Install and Setup Suricata on Ubuntu 18.04

LEAVE A REPLY

Please enter your comment!
Please enter your name here