[Day 9] 4. BARNYARD2 and SNORT
Objective: Snort generates binary (U2) output, which Barnyard2 processes and writes into an SQL database. Finally, Apache with PHP will display this data.
Setup Overview
- Debian 12 () – Main configuration machine.
192.168.80.128 - Debian 8 – ().
192.168.80.132
Steps in Debian (Snort Configured Machine)
Prerequisite: You should have snort configured in your Linux Machine:
1. Install Required Packages
Run the following command to install all necessary dependencies:
bashsudo apt-get install -y autoconf automake default-libmysqlclient-dev dos2unix libmariadb-dev-compat libmariadb-dev libmariadbd-dev libtool mariadb-client mariadb-server unzip
2. Install Compatible libpcap
for Barnyard2
libpcap-
Check the current version of
installed on your system:libpcapbashdpkg -s libpcap0.8 | grep Version dpkg -s libpcap0.8-dev | grep Version -
Purge the newer versions of
that are incompatible with Barnyard2:libpcapbashsudo apt-get purge libpcap0.8 libpcap0.8-dev -
Install the compatible version of
provided by sir:libpcapbashsudo dpkg -i libpcap0.8_1.8.1-6+deb10u1_amd64.deb libpcap0.8-dev_1.8.1-6+deb10u1_amd64.debNote: This ensures compatibility for Barnyard2.
3. Configure Snort for Unified2 Output
-
Open the Snort configuration file:
bashsudo vim /etc/snort/snort.conf -
Locate Step 6 in the configuration file.
-
Add the following line below the line starting with
:output unified2:plaintextoutput unified2: filename snort.u2, limit 128Explanation: This configures Snort to output its logs in Unified2 format, with files named
and a size limit of 128MB.snort.u2
4. Download and Prepare Barnyard2 Source Code
-
Obtain the Barnyard2 source code (provided by sir.
-
Unzip the downloaded file and navigate to the extracted folder:
bashunzip <filename> cd <foldername> -
Run the
script to generate theautogen.shfile:configurebashsudo ./autogen.sh
5. Create Required Symbolic Link
Before proceeding with the
configuredumbnet.hbashsudo ln -s /usr/include/dumbnet.h /usr/include/dnet.h
sudo ldconfig
6. Configure, Build, and Install Barnyard2
-
Run the
script to specify that Barnyard2 will use MySQL:configurebashsudo ./configure --with-mysql --with-mysql-libraries=/usr/lib/x86_64-linux-gnu/ -
Compile and install Barnyard2:
bashsudo make sudo make install
7. Verify Barnyard2 Installation
Check if Barnyard2 is installed and accessible:
bashwhich barnyard2
If installed correctly, this will return the path to the
barnyard28. Prepare Barnyard2 Configuration and Log Directory
-
While still in the Barnyard2 source code directory, run the following commands:
bashsudo cp etc/barnyard2.conf /etc/snort/ sudo mkdir /var/log/barnyard2 sudo chown snort:snort /var/log/barnyard2/ sudo touch /var/log/snort/barnyard2.waldo sudo chown snort:snort /var/log/snort/barnyard2.waldoExplanation:
- The file is copied to Snort’s configuration directory.
barnyard2.conf - The log directory is created, and permissions are set for the
barnyard2user.snort - A file is created for Barnyard2 to track processed log files.
.waldo
- The
9. Configure the MySQL Database
-
Start the MySQL database:
bashsudo mysql -u root -p -
In the MySQL prompt, execute the following commands:
mysqlCREATE DATABASE IF NOT EXISTS snort; USE snort; SOURCE /home/shuhari/barnyard2-master/schemas/create_mysql; SHOW TABLES; CREATE USER 'snort'@'localhost' IDENTIFIED BY 'toor'; GRANT CREATE, INSERT, SELECT, DELETE, UPDATE ON snort.* TO 'snort'@'localhost'; EXIT;Explanation:
- A database named is created, and the schema for Snort is loaded into it.
snort - A user with password
snortis created and granted permissions to manage the database.toor
- A database named
10. Configure Barnyard2 for MySQL Integration
-
Open the
file:barnyard2.confbashsudo vim /etc/snort/barnyard2.conf -
Add the following line to the end of the file:
plaintextoutput database: log, mysql, user=snort password=toor dbname=snort host=localhostExplanation: This tells Barnyard2 to log Snort’s data into the MySQL database using the configured credentials.
11. Restrict Permissions on the Barnyard2 Configuration File
-
Run the following command to restrict access to the
file:barnyard2.confbashsudo chmod o-r /etc/snort/barnyard2.confExplanation: This ensures that unauthorized users cannot view the configuration file, enhancing security.
12. Configure Snort Rules with Revision Numbers
-
Open the Snort local rules file:
bashsudo vim /etc/snort/rules/local.rules -
Add the following rules with revision numbers:
plaintextalert ip any any -> any any (msg: "Kuch Bhi"; sid:1000001; rev:1;) alert ip 192.168.0.10 any -> any any (msg: "From 10"; sid:1000002; rev:1;) alert ip 192.168.0.11 any -> any any (msg: "From 11"; sid:1000003; rev:1;) alert ip 192.168.0.12 any -> any any (msg: "From 12"; sid:1000004; rev:1;) alert ip 192.168.0.13 any -> any any (msg: "From 13"; sid:1000005; rev:1;)Explanation: Each rule includes a unique
(Signature ID) andsid(revision number), which are critical for managing and identifying alerts.rev
13. Generate SID-to-Message Map
-
Download the
Perl script provided by sir.create-sidmap.pl -
Set execution permissions and execute the script:
bashchmod 755 create-sidmap.pl ./create-sidmap.pl /etc/snort/rules -
Redirect the output of the script to generate the
file:sid-msg.mapbashsudo sh -c "./create-sidmap.pl /etc/snort/rules > /etc/snort/sid-msg.map"Explanation:
- The Perl script extracts numbers and their corresponding messages from the
sidfile.local.rules - The file is used by Snort and Barnyard2 for better management and logging of alerts.
sid-msg.map
- The Perl script extracts
Now, we will require 3 putty windows in order to monitor everything:
- 1st Putty - Running Snort
- 2nd Putty - Checking Logs
- 3rd Putty - Running Barnyard2
14. Running Snort, Checking Logs, and Starting Barnyard2
Step 1: Running Snort (First Putty Window)
-
List the contents of the Snort log directory: After execution of this command, you won't be able to see
file:snort.u2bashls -l /var/log/snort -
Optionally, test the Snort configuration:
bashsudo snort -T -c /etc/snort/snort.confExplanation:
- The option tests the Snort configuration file for syntax errors.
-T
- The
-
Run Snort to capture packets:
bashsudo snort -q -i ens33 -u snort -g snort -c /etc/snort/snort.confExplanation:
- The option runs Snort in quiet mode.
-q - Packets are logged in the binary file under
snort.u2./var/log/snort
- The
Step 2: Monitoring Logs (Second Putty Window)
-
Check if the
file is created:snort.u2bashls -l /var/log/snort -
Observe the file size grow as Snort captures more packets.
-
Connect to the MySQL database and verify the
table:iphdrbashsudo mysql -u root -pmysqlUSE snort; SHOW TABLES; SELECT COUNT(*) FROM iphdr;Explanation:
- Initially, the table will not have any entries because Barnyard2 has not processed the
iphdrfile yet.snort.u2
- Initially, the
Step 3: Running Barnyard2 (Third Putty Window)
-
Run Barnyard2 with the following command:
bashsudo barnyard2 -c /etc/snort/barnyard2.conf -d /var/log/snort/ -f snort.u2 -w /var/log/snort/barnyard2.waldo -g snort -u snortExplanation:
- : Specifies the configuration file.
-c - : Sets the directory for Snort log files.
-d - : Indicates the file to be processed (
-f).snort.u2 - : Uses the
-wfile to track progress..waldo - and
-g: Define the group and user as-u.snort
What is a .waldo
- A file acts as a bookmark, recording the progress of Barnyard2.
.waldo - When Barnyard2 runs again, it resumes from where it left off instead of reprocessing already-logged data.
15. Setting Up BASE (Basic Analysis and Security Engine)
We will be using BASE which is the Analysis Console for Intrusion Databases. This was developed in PHP and the last version was released in 2013. However, this is compatible with Debian 8. So, we will be running the image of Debian 8. The username is
roottoorStep 1: Configure Debian 8 Sources List
-
Open the
file:sources.listbashnano /etc/apt/sources.list -
Add the following repository:
plaintextdeb http://archive.debian.org/debian/ jessie main contrib non-free -
Save and exit the file.
16. Configuring MariaDB in Debian 12
Step 1: Allow External Connections to MariaDB
-
Open the MariaDB configuration file:
bashsudo vim /etc/mysql/mariadb.conf.d/50-server.cnf -
Add or update the following line:
plaintextbind-address = 0.0.0.0Explanation:
- This allows MariaDB to accept connections from all IP addresses.
Step 2: Restart MariaDB
-
Restart and check the status of MariaDB:
bashsudo systemctl restart mariadb sudo systemctl status mariadb
17. Grant Permissions for Debian 8 Machine in MariaDB
-
Log in to MariaDB on the Debian 12 machine:
bashsudo mysql -u root -p -
Grant permissions to the Debian 8 machine:
mysqlGRANT CREATE, INSERT, SELECT, DELETE, UPDATE ON snort.* TO 'snort'@'<ip>' IDENTIFIED BY 'toor'; exit;Replace
with the IP address of the Debian 8 machine.<ip>
Explanation:- This step allows the Debian 8 machine to access the Snort database on the Debian 12 machine.
18. Install MariaDB Client on Debian 8
-
Update the package list:
bashapt-get update -
Install the MariaDB client:
bashapt-get install mariadb-client
19. Test Database Connectivity Between Debian 8 and Debian 12
Step 1: From Debian 8, connect to the Snort database on Debian 12
-
Use the following command:
bashmysql -u snort -p'toor' -h 192.168.80.128Replace
with the actual IP of the Debian 12 machine.192.168.80.128 -
Once connected, verify the databases:
mysqlSHOW DATABASES; exit;
Step 2: Install Required Packages on Debian 8 for BASE
-
Install Apache, PHP, and additional dependencies:
bashapt-get install apache2 php5 p7zip libapache2-mod-php5 php5-mcrypt php5-mysqlnd libphp-adodb unzipNote: During installation, a path will be shown. Note it down for later use.
-
Verify the installation of BASE:
bashls -l | grep base -
Extract and move the BASE archive (provided by your instructor):
bashtar -zxf base-1.4.5.tar.gz mv base-1.4.5 /var/www/html/base chown -R www-data:www-data /var/www/html/base
20. Configure PHP for BASE
-
Open the PHP configuration file:
bashnano /etc/php5/apache2/php.ini -
Search for the
directive (on or near line 449):error_reporting
Replace the second occurrence with:plaintextE_ALL & ~E_NOTICE -
Restart the Apache service:
bashsudo systemctl restart apache2 sudo systemctl status apache2
21. Access BASE via Browser
-
Determine the IP address of the Debian 8 machine:
baship addr -
On your browser, go to:
http://<debian8_ip>/baseReplace
with the actual IP address of the Debian 8 machine.<debian8_ip>
22. Configure BASE in the Browser
-
Ensure all indicators on the screen are green.
-
Replace the path displayed during the PHP package installation with the noted path.
-
Provide the following database details:
- Name: Snort
- Hostname: IP of the Debian 12 machine
- Database Name: snort
- Password: toor
-
Continue to the authentication setup:
- Admin Username: snort
- Password: toor
- Full Name: Snort
23. Finalize BASE Setup
-
Click on
and proceed to Step 5.CREATE BASE AG -
Start Barnyard2 from the third Putty window on Debian 12:
bashsudo barnyard2 -c /etc/snort/barnyard2.conf -d /var/log/snort/ -f snort.u2 -w /var/log/snort/barnyard2.waldo -g snort -u snort -
Refresh the BASE web interface on Debian 8.
Outcome: You should see changes and entries updated dynamically in the BASE web interface as Snort logs are processed by Barnyard2.
Read Next
[Day 9] 5. Suricata
**Suricata** is an open-source, high-performance Intrusion Detection and Prevention System (IDS/IPS) that analyzes network traffic for malicious activ...
[Day 9] 6. Blockchain
**Blockchain** is like a chain made of blocks. Each block holds some information, and the blocks are connected in a sequence. You can store anything i...