首先明确操作系统平台是Ubuntu 14.04 LTS
现在我们要在Ubuntu 14.04上部署snort NIDS(入侵检测系统)。
需要这些东西:
SNORT / Barnyard2 / Mysql / Apache2 / BASE
在进行所有工作之前,请执行以下命令,确保安装必要的软件(工具链)
<span style="font-family:Hiragino Sans GB;">sudo apt-get install -y build-essential </span><span style="font-family:Comic Sans MS;">libpcap0.8-dev</span><span style="font-family: 'Hiragino Sans GB';"> </span><span style="font-family:Hiragino Sans GB;">libpcre3-dev libdumbnet-dev bison flex zlib1g-dev</span>
SNORT
在Ubuntu 14.04安装snort可以有两种方式
一种:
sudo apt-get install snort
另外一种是从源代码安装。
既然有简单地方法,那就选择从软件源安装,即选择第一种。
如果这种方式出错的话,你可能需要依次执行:
sudo apt-get update
sudo apt-get upgrade
测试看看,执行:
snort -V
如果显示出以下:
,,_ -*> Snort! <*- o" )~ Version 2.9.7.0 GRE (Build 149) '''' By Martin Roesch & The Snort Team: http://www.snort.org/contact#team Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved. Copyright (C) 1998-2013 Sourcefire, Inc., et al. Using libpcap version 1.5.3 Using PCRE version: 8.31 2012-07-06 Using ZLIB version: 1.2.8
说明安装成功了!祝贺!
接着我们要修改snort的配置文件(注意我们使用的软件源方式,故配置文件路径较为固定)
sudo vim /etc/snort/snort.conf
将115行(大概位置)修改如下(如果一样的话则不修改)
var RULE_PATH /etc/snort/rules var SO_RULE_PATH /etc/snort/so_rules var PREPROC_RULE_PATH /etc/snort/preproc_rules
目的在于指定规则文件存放的路径。
在第51行 :
ipvar HOME_NET 192.168.1.0/24
第536行,修改成如下所示 :
output unified2: filename snort.log, limit 128, mpls_event_types, vlan_event_types
(如果文件本身就和我这里修改后的一样则没必要修改)
现在启动测试SNORT :
sudo service snort restart
删除之前日志中得内容(我们改变了日志格式,使用了时间戳格式)
sudo rm /var/log/snort/snort.log
(题外话:第51行没必要修改,因为被/etc/snort/snort.debian.conf覆盖了)
现在我们要测试下snort的规则。
/etc/snort/rules是我们的用于存放规则文件的路径。以后snort就是根据诸多的规则文件给我们提供预警和提示。
打开规则文件:
sudo vim /etc/snort/rules/local.rules
local.rules是用于自定义规则的规则文件。然后添加自己的规则到local.rules:
alert icmp any any -> $HOME_NET any (msg:"ICMP Test NOW!!!"; classtype:not-suspicious; sid:1000001; rev:1;) alert tcp any any -> $HOME_NET 80 (msg:"HTTP Test NOW!!!"; classtype:not-suspicious; sid:1000002; rev:1;)
这两条规则大概就是说任何发往本机的ICMP和HTTP数据包都会触发警告!
保存退出。
为了便于测试,继续修改snort.conf文件。大概在573行的位置。作如下操作:
将除了local.rules之外的规则文件全部去除(不包含进来)。(也就是接下来数十行的include)。
最终这样:
# site specific rules include $RULE_PATH/local.rules
(这是为了便于测试)
sudo snort -T -c /etc/snort/snort.conf
如果看到显示结果如下:
... Snort successfully validated the configuration! Snort exiting
并且还能在诸多输出信息中找到如下输出:
+++++++++++++++++++++++++++++++++++++++++++++++++++ Initializing rule chains... 1 Snort rules read 1 detection rules 0 decoder rules 0 preprocessor rules 1 Option Chains linked into 1 Chain Headers 0 Dynamic rules +++++++++++++++++++++++++++++++++++++++++++++++++++ +-------------------[Rule Port Counts]--------------------------------------- | tcp udp icmp ip | src 0 0 0 0 | dst 0 0 0 0 | any 0 0 1 0 | nc 0 0 1 0 | s+d 0 0 0 0 +----------------------------------------------------------------------------
再次祝贺!规则成功加载,测试通过!
作了以上修改之后,下面开始真正的测试。
sudo snort -A console -q -u snort -g snort -c /etc/snort/snort.conf -i eth0
随后,你可以使用另一台电脑或者开另外一个terminal使用ping命令,ping测试主机。
比如:ping localhost
你应该能在测试主机上看到相似的如下输出:
10/31-02:27:19.663643 [**] [1:10000001:1] ICMP test [**] [Priority: 0] {ICMP} 10.0.0.74 -> 10.0.0.64 10/31-02:27:19.663675 [**] [1:10000001:1] ICMP test [**] [Priority: 0] {ICMP} 10.0.0.64 -> 10.0.0.74 10/31-02:27:20.658378 [**] [1:10000001:1] ICMP test [**] [Priority: 0] {ICMP} 10.0.0.74 -> 10.0.0.64 10/31-02:27:20.658404 [**] [1:10000001:1] ICMP test [**] [Priority: 0] {ICMP} 10.0.0.64 -> 10.0.0.74 10/31-02:27:21.766521 [**] [1:10000001:1] ICMP test [**] [Priority: 0] {ICMP} 10.0.0.74 -> 10.0.0.64 10/31-02:27:21.766551 [**] [1:10000001:1] ICMP test [**] [Priority: 0] {ICMP} 10.0.0.64 -> 10.0.0.74 10/31-02:27:22.766167 [**] [1:10000001:1] ICMP test [**] [Priority: 0] {ICMP} 10.0.0.74 -> 10.0.0.64 10/31-02:27:22.766197 [**] [1:10000001:1] ICMP test [**] [Priority: 0] {ICMP} 10.0.0.64 -> 10.0.0.74 ^C*** Caught Int-Signal
(按ctrl+c结束)
然后你去查看/var/log/snort,你也会发现有被命令为snort.log.nnnnnn(nnnn代表数字),这些文件中包含的和屏幕上打印出来的是一样的信息。
祝贺!现在我们已经能够根据自己的规则显示警告信息了。
Barnyard
这是来干嘛的?简单地说,他将读取snort的二进制文件,并将其存储在MySQL数据库中。
执行以下命令会安装一些必要的软件。还会用到LAMP,如何快速安装?
(请确保之前没有安装过LAMP组件中任何一个)请参考:点击打开链接
sudo apt-get install -y libmysqlclient-dev autoconf libtool
在/etc/snort/snort.conf文件的520行附近(大概位置)添加如下并保存文件。
output unified2: filename snort.u2, limit 128
接下来安装和配置Barnyard:
下载Barnyard:
wget https://github.com/firnsy/barnyard2/archive/v2-1.13.tar.gz -O barnyard2-2-1.13.tar.gz
依次执行:
tar zxvf barnyard2-2-1.13.tar.gz cd barnyard2-2-1.13 autoreconf -fvi -I ./m4
64bit和32bit机器分别执行以下:
./configure --with-mysql --with-mysql-libraries=/usr/lib/x86_64-linux-gnu --with-mysql-includes=/usr/include/ ./configure --with-mysql --with-mysql-libraries=/usr/lib/i386-linux-gnu --with-mysql-includes=/usr/include/
继续:
make sudo make install
sudo cp /usr/local/etc/barnyard2.conf /etc/snort
sudo mkdir /var/log/barnyard2 sudo chown snort.snort /var/log/barnyard2
sudo cp schemas/create_mysql /usr/src
编辑:
sudo gedit /etc/snort/barnyard2.conf
#227行 改为:
output alert_fast ( instead of output alert_fast: stdout )
#348行, 去掉注释:
output database: log, mysql, user=snort password=secret2 dbname=snort host=localhost
( 用你的snort用户的密码代替secret2 ,接下来在MySQL中会用到secret2)
MySQL
登录MySQL,使用你在安装lamp时设置的MySQL的root密码登录。
sudo mysql -u root -p
[ 输入MySQL的root密码 ]
在MySQL控制台依次输入以下内容(注意分号结尾):
create database snort;
create database archive;
grant usage on snort.* to [email protected];
grant usage on archive.* to [email protected];
set password for [email protected]=PASSWORD(‘secret2‘);
grant all privileges on snort.* to [email protected];
grant all privileges on archive.* to [email protected];
flush privileges;
exit
重新登录MySQL控制台,依次键入:
use snort; source /usr/src/create_mysql; show tables; exit
Snort 和Barnyard 测试
运行:
<span style="font-size:12px;">sudo snort -q -u snort -g snort -c /etc/snort/snort.conf -i eth0 -D</span>
你不会看到输出结果,因为程序在后台运行的。
同样ping你的主机。
继续运行以下:
<span style="font-size:12px;">sudo barnyard2 -c /etc/snort/barnyard2.conf -d /var/log/snort -f snort.u2 -w /var/log/snort/barnyard2.waldo -g snort -u snort</span><span style="font-size: 18.6666660308838px;"> </span>
应该能看到以下输出:
--== Initialization Complete ==-- ______ -*> Barnyard2 <*- / ,,_ \ Version 2.1.13 (Build 327) |o" )~| By Ian Firns (SecurixLive): http://www.securixlive.com/ + '''' + (C) Copyright 2008-2013 Ian Firns <[email protected]> Using waldo file '/var/log/snort/barnyard2.waldo': spool directory = /var/log/snort spool filebase = snort.u2 time_stamp = 1412527313 record_idx = 16 Opened spool file '/var/log/snort/snort.u2.1412527313' Closing spool file '/var/log/snort/snort.u2.1412527313'. Read 16 records Opened spool file '/var/log/snort/snort.u2.1412528990' Waiting for new data
使用ctrl+c结束。
如果你好奇数据库发生了什么?可以检测下:
mysql -u snort -p -D snort -e "select count(*) from event"
应该会出现以下结果:
+----------+ | count(*) | +----------+ | 4 | +----------+
***如果出现少了一个sid-msg.map文件的错误,以下方式修复:
cd /usr/share/oinkmaster sudo bash -c "sudo ./create-sidmap.pl /etc/snort/rules > /etc/snort/sid-msg.map"
BASE
首先:
sudo apt-get install libphp-adodb
继续修改配置文件:
编辑 "/etc/php5/apache2/php.ini", 找到这一行"error_reporting" ,修改它:
error_reporting = E_ALL & ~E_NOTICE
编辑/etc/apache2/apache2.conf 添加许可www/base :
----------------------------------------------------------------------------------------------------- <Directory /var/www/html/base> AllowOverride All Require all granted </Directory> -----------------------------------------------------------------------------------------------------
重启apache2 :
sudo service apache2 restart
安装BASE依赖:
sudo apt-get install php-pear sudo apt-get install libwww-perl sudo apt-get install php5-gd sudo pear config-set preferred_state alpha sudo pear channel-update pear.php.net sudo pear install --alldeps Image_Color Image_Canvas Image_Graph
如果得到以下错误: ‘ could not extract the package.xml file
‘ ,那么以下方法可以修复这个错误。
当下载了这6个pear包之后,我们手动安装 ( 3 + 3 dependencies ).
如下:
cd /build/buildd/php5-5.5.9+dfsg/pear-build-download ls
这里应该有6个 .tgz 包. 手动安装:
sudo tar zxf Image_Color*.tgz
sudo cp package.xml ./Image_Color*/ cd Image_Color* sudo pear install package.xml cd ..
按照上面方法依次安装其余的五个包。
Image_Canvas
Numbers_Roman
Math_BigInteger
Numbers_Words
Image_Graph
BASE的安装:
cd /usr/src sudo wget http://sourceforge.net/projects/secureideas/files/BASE/base-1.4.5/base-1.4.5.tar.gz sudo tar -zxf base-1.4.5.tar.gz sudo cp -r base-1.4.5 /var/www/html/base sudo chown -R www-data:www-data /var/www/html/base sudo service apache2 restart
BASE设置:
浏览器输入:http://localhost/base
(语言就选择默认的英文)
Step 1) 输入路径 : /usr/share/php/adodb
Step 2) Database Name : snort
Database Host : localhost
Database User Name: snort
Database Password : secret2(之前输入的)
( tick ‘Use Archive Database‘ )
Archive Database Name : archive
Archive Database Host : localhost
Archive Database User Name : snort
Archive Database Password : secret2(之前输入的)
Step 3)
Full admin name ( xxx )
[GUI password] ( Secret3 )
Full admin name ( XXXX )
Step 4) 点击 ‘ Create baseAG‘
Step 5) 点击 ‘ Now continue to Step 5 ‘ and login ( XXX/ secret3 )
然后ping主机的话,不出意外每3min就会跟新显示snort的信息。