CENTOS7 snort 轻量级入侵检测系统安装与使用

snort 是一个开源的轻量级入侵检测系统(NIDS),使用C语言编写。支持windows、Linux平台,我比较喜欢linux操作系统,所以在linux上学习研究snort。snort有三种工作模式,包括:嗅探、记录数据包、入侵检测。但是,可以把snort配置成入侵防御的模式,过程算是复杂。作为一个轻量级的入侵检测系统,snort功能算是单一,配置复杂,有利于入侵检测系统源码研究与规则编写。snort规则动作有五种,常用为报警、忽略、记录等,详细的后面简说明。

        使用环境:ubuntu15.10+snort2.9.8.0+daq2.0.4,snort可以用命令行方式安装,非常方便,安装成功即可配置使用。大部分人喜欢用源代码的方式安装,方便以后学习研究源码,也可以自行调试snort,编写snort规则,测试snort功能。snort的各种插件使用起来也不是很方便,源码安装需要大量时间去折腾。

1、安装依赖软件

1.1安装daq

下载daq源码:

[cpp]  view plain  copy
  1. https://www.snort.org/downloads#snort 下载
  2. daq-2.0.6.tar.gz
  3. snort-2.9.9.0.tar.gz


 
 
 

解压daq源码包,此时直接安装daq会有报错,缺少各种各样的依赖包,所以要先安装依赖包:bison、flex、libpcap

1.2其他依赖

先安装bison 、flex,命令行输入:

[cpp]  view plain  copy
  1. liang@ubuntu:~/snort/daq$ sudo apt-get install bison flex  

下载libpcap源码  :

[cpp]  view plain  copy
  1. liang@ubuntu:~/snort/libpcap$ wget http://www.tcpdump.org/release/libpcap-1.7.4.tar.gz       

解压并且安装libpcap:

[cpp]  view plain  copy
  1. liang@ubuntu:~/snort/libpcap$ tar -zxvf libpcap-1.7.4.tar.gz  
  2. liang@ubuntu:~/snort/libpcap/libpcap-1.7.4$ ./configure  
  3. liang@ubuntu:~/snort/libpcap/libpcap-1.7.4$ sudo make  
  4. liang@ubuntu:~/snort/libpcap/libpcap-1.7.4$ sudo make install   
 

1.3编译安装daq

再次配置daq:

[cpp]  view plain  copy
  1. liang@ubuntu:~/snort/daq$ ./configure  


打印如下即配置成功,可以安装daq:

[cpp]  view plain  copy
  1. Build AFPacket DAQ module.. : yes  
  2. Build Dump DAQ module...... : yes  
  3. Build IPFW DAQ module...... : yes  
  4. Build IPQ DAQ module....... : no  
  5. Build NFQ DAQ module....... : no  
  6. Build PCAP DAQ module...... : yes  
  7. Build netmap DAQ module...... : no  


编译daq:

[cpp]  view plain  copy
  1. liang@ubuntu:~/snort/daq$ sudo autoreconf -ivf  
  2. liang@ubuntu:~/snort/daq$ sudo make  
  3. liang@ubuntu:~/snort/daq$ sudo make install

到此为止安装daq成功。

   

2、安装snort

2.1其他依赖:

libdumbnet-dev

下载地址:https://packages.debian.org/sid/libdumbnet-devl


  1. liang@ubuntu:~/snort/libpcap/libpcap-1.7.4$ tar -zvxf libdumbnet_1.12.orig.tar.gz 
  2. liang@ubuntu:~/snort/libpcap/libpcap-1.7.4$ ./configure  
  3. liang@ubuntu:~/snort/libpcap/libpcap-1.7.4$ sudo make  
  4. liang@ubuntu:~/snort/libpcap/libpcap-1.7.4$ sudo make install  


i

2.3安装snort

安装snort:

[cpp]  view plain  copy
  1. liang@ubuntu:~/snort/snort$ ./configure --enable-sourcefire   
  2. liang@ubuntu:~/snort/snort$ sudo make  
  3. liang@ubuntu:~/snort/snort$ sudo make install  

snort会被安装到,如下目录:

 /usr/local/bin/snort /usr/local/lib/snort


3、启动snort

输入命令启动snort:

liang@ubuntu:~/snort/snort$ sudo snort


 提示如下错误:

snort: error while loading shared libraries: libdnet.1: cannot open shared object file: No such file or directory

ln -s /usr/lib/libdnet.so.1.0.1 /usr/lib/libdnet.1

再次运行 snort

打印如下即启动成功:


查看snort使用帮助,输入命令:
[cpp]  view plain  copy
  1. liang@ubuntu:/etc/snort$ snort --help  

可以看到输出如下,全是英文:
[cpp]  view plain  copy
  1.    ,,_     -*> Snort! <*-  
  2.   o"  )~   Version 2.9.8.0 GRE (Build 229)   
  3.    ''''    By Martin Roesch & The Snort Team: http://www.snort.org/contact#team  
  4.            Copyright (C) 2014-2015 Cisco and/or its affiliates. All rights reserved.  
  5.            Copyright (C) 1998-2013 Sourcefire, Inc., et al.  
  6.            Using libpcap version 1.7.4  
  7.            Using PCRE version: 8.35 2014-04-04  
  8.            Using ZLIB version: 1.2.8  
  9.   
  10. USAGE: snort [-options] <filter options>  
  11. Options:  
  12.         -A         Set alert mode: fast, full, console, test or none  (alert file alerts only)  
  13.                    "unsock" enables UNIX socket logging (experimental).  
  14.         -b         Log packets in tcpdump format (much faster!)  
  15.         -B <mask>  Obfuscated IP addresses in alerts and packet dumps using CIDR mask  
  16.         -c <rules> Use Rules File <rules>  
  17.         -C         Print out payloads with character data only (no hex)  
  18.         -d         Dump the Application Layer  
  19.         -D         Run Snort in background (daemon) mode  
  20.         -e         Display the second layer header info  
  21.         -f         Turn off fflush() calls after binary log writes  
  22.         -F <bpf>   Read BPF filters from file <bpf>  
  23.         -g <gname> Run snort gid as <gname> group (or gid) after initialization  
  24.         -G <0xid>  Log Identifier (to uniquely id events for multiple snorts)  
  25.         -h <hn>    Set home network = <hn>  
  26.                    (for use with -l or -B, does NOT change $HOME_NET in IDS mode)  
  27.         -H         Make hash tables deterministic.  
  28.         -i <if>    Listen on interface <if>  
  29.         -I         Add Interface name to alert output  
  30.         -k <mode>  Checksum mode (all,noip,notcp,noudp,noicmp,none)  
  31.         -K <mode>  Logging mode (pcap[default],ascii,none)  
  32.         -l <ld>    Log to directory <ld>  
  33.         -L <file>  Log to this tcpdump file  
  34.         -M         Log messages to syslog (not alerts)  
  35.         -m <umask> Set umask = <umask>  
  36.         -n <cnt>   Exit after receiving <cnt> packets  
  37.         -N         Turn off logging (alerts still work)  
  38.         -O         Obfuscate the logged IP addresses  
  39.         -p         Disable promiscuous mode sniffing  
  40.         -P <snap>  Set explicit snaplen of packet (default: 1514)  
  41.         -q         Quiet. Don't show banner and status report  
  42.         -Q         Enable inline mode operation.  
  43.         -r <tf>    Read and process tcpdump file <tf>  
  44.         -R <id>    Include 'id' in snort_intf<id>.pid file name  
  45.         -s         Log alert messages to syslog  
  46.         -S <n=v>   Set rules file variable n equal to value v  
  47.         -t <dir>   Chroots process to <dir> after initialization  
  48.         -T         Test and report on the current Snort configuration  
  49.         -u <uname> Run snort uid as <uname> user (or uid) after initialization  
  50.         -U         Use UTC for timestamps  
  51.         -v         Be verbose  
  52.         -V         Show version number  
  53.         -X         Dump the raw packet data starting at the link layer  
  54.         -x         Exit if Snort configuration problems occur  
  55.         -y         Include year in timestamp in the alert and log files  
  56.         -Z <file>  Set the performonitor preprocessor file path and name  
  57.         -?         Show this information  
  58. <Filter Options> are standard BPF options, as seen in TCPDump  
  59. Longname options and their corresponding single char version  
  60.    --logid <0xid>                  Same as -G  
  61.    --perfmon-file <file>           Same as -Z  
  62.    --pid-path <dir>                Specify the directory for the Snort PID file  
  63.    --snaplen <snap>                Same as -P  
  64.    --help                          Same as -?  
  65.    --version                       Same as -V  
  66.    --alert-before-pass             Process alert, drop, sdrop, or reject before pass, default is pass before alert, drop,...  
  67.    --treat-drop-as-alert           Converts drop, sdrop, and reject rules into alert rules during startup  
  68.    --treat-drop-as-ignore          Use drop, sdrop, and reject rules to ignore session traffic when not inline.  
  69.    --process-all-events            Process all queued events (drop, alert,...), default stops after 1st action group  
  70.    --enable-inline-test            Enable Inline-Test Mode Operation  
  71.    --dynamic-engine-lib <file>     Load a dynamic detection engine  
  72.    --dynamic-engine-lib-dir <path> Load all dynamic engines from directory  
  73.    --dynamic-detection-lib <file>  Load a dynamic rules library  
  74.    --dynamic-detection-lib-dir <path> Load all dynamic rules libraries from directory  
  75.    --dump-dynamic-rules <path>     Creates stub rule files of all loaded rules libraries  
  76.    --dynamic-preprocessor-lib <file>  Load a dynamic preprocessor library  
  77.    --dynamic-preprocessor-lib-dir <path> Load all dynamic preprocessor libraries from directory  
  78.    --dynamic-output-lib <file>  Load a dynamic output library  
  79.    --dynamic-output-lib-dir <path> Load all dynamic output libraries from directory  
  80.    --create-pidfile                Create PID file, even when not in Daemon mode  
  81.    --nolock-pidfile                Do not try to lock Snort PID file  
  82.    --no-interface-pidfile          Do not include the interface name in Snort PID file  
  83.    --disable-attribute-reload-thread Do not create a thread to reload the attribute table  
  84.    --pcap-single <tf>              Same as -r.  
  85.    --pcap-file <file>              file that contains a list of pcaps to read - read mode is implied.  
  86.    --pcap-list "<list>"            a space separated list of pcaps to read - read mode is implied.  
  87.    --pcap-dir <dir>                a directory to recurse to look for pcaps - read mode is implied.  
  88.    --pcap-filter <filter>          filter to apply when getting pcaps from file or directory.  
  89.    --pcap-no-filter                reset to use no filter when getting pcaps from file or directory.  
  90.    --pcap-loop <count>             this option will read the pcaps specified on command line continuously.  
  91.                                    for <count> times.  A value of 0 will read until Snort is terminated.  
  92.    --pcap-reset                    if reading multiple pcaps, reset snort to post-configuration state before reading next pcap.  
  93.    --pcap-reload                   if reading multiple pcaps, reload snort config between pcaps.  
  94.    --pcap-show                     print a line saying what pcap is currently being read.  
  95.    --exit-check <count>            Signal termination after <count> callbacks from DAQ_Acquire(), showing the time it  
  96.                                    takes from signaling until DAQ_Stop() is called.  
  97.    --conf-error-out                Same as -x  
  98.    --enable-mpls-multicast         Allow multicast MPLS  
  99.    --enable-mpls-overlapping-ip    Handle overlapping IPs within MPLS clouds  
  100.    --max-mpls-labelchain-len       Specify the max MPLS label chain  
  101.    --mpls-payload-type             Specify the protocol (ipv4, ipv6, ethernet) that is encapsulated by MPLS  
  102.    --require-rule-sid              Require that all snort rules have SID specified.  
  103.    --daq <type>                    Select packet acquisition module (default is pcap).  
  104.    --daq-mode <mode>               Select the DAQ operating mode.  
  105.    --daq-var <name=value>          Specify extra DAQ configuration variable.  
  106.    --daq-dir <dir>                 Tell snort where to find desired DAQ.  
  107.    --daq-list[=<dir>]              List packet acquisition modules available in dir.  Default is static modules only.  
  108.    --dirty-pig                     Don't flush packets and release memory on shutdown.  
  109.    --cs-dir <dir>                  Directory to use for control socket.  
  110.    --ha-peer                       Activate live high-availability state sharing with peer.  
  111.    --ha-out <file>                 Write high-availability events to this file.  
  112.    --ha-in <file>                  Read high-availability events from this file on startup (warm-start).  
  113.    --suppress-config-log           Suppress configuration information output.  


4、配置snort

snort的配置文件在:snort/etc/snort.conf 目录,可以结合官方说明文档去进行配置,也有中文版本的,但是并不完善。

5、snort规则

自行编写是snort规则:

[cpp]  view plain  copy
  1. alert tcp any any -> 192.168.213.170 80 (msg:"Telnet Login";sid:26287)  
  2. alert icmp any any -> 192.168.213.170 any (msg:"ICMP PING";sid:8886288)  

 

写入到一个新的规则文件里面,在snort启动配置文件里面添加该新规则文件即可使上面的两条规则生效。

        snort规则大体划分为规则头和规则选项两个部分,规则头主要是数据包的协议、端口、地址信息以及数据包的处理动作。数据包的协议、端口、地址信息是网络最常见的基础信息,当然端口和地址包括目的与源头。而数据包的处理动作有报警、忽略、记录、报警并启动另外一个动态规则链、由其它的规则包调用五种方式。

规则选项中内容比较多,繁琐。 sid为必要的内容, msg是报警记录信息content为数据包匹配内容。其他的详细项目可以参考官方文档,我一直想content这里的内容可以不可以实现恶意代码的检测,如果可以的话恶意代码的报警与处理直接在网关上面处理,并不会被传播到主机上面。

6、测试用例

6.1启动本机snort

[cpp]  view plain  copy
  1. sudo snort -i eth0 -c/etc/snort/etc/snort.conf -A fast -l /var/log/snort  

 

6.2实时查看snort报警日志:

[cpp]  view plain  copy
  1. liang@ubuntu:~/snort/run/log$ tail -f /var/log/snort/alert   

6.3测试规则

另外主机ping snort主机,报警:

[cpp]  view plain  copy
  1. 03/21-16:15:13.164956  [**] [1:8886288:0] "ICMP PING" [**] [Priority: 0] {ICMP} 192.168.213.162 -> 192.168.213.170  
  2. 03/21-16:15:14.164567  [**] [1:8886288:0] "ICMP PING" [**] [Priority: 0] {ICMP} 192.168.213.162 -> 192.168.213.170  
  3. 03/21-16:15:15.164590  [**] [1:8886288:0] "ICMP PING" [**] [Priority: 0] {ICMP} 192.168.213.162 -> 192.168.213.170  
  4. 03/21-16:15:16.166108  [**] [1:8886288:0] "ICMP PING" [**] [Priority: 0] {ICMP} 192.168.213.162 -> 192.168.213.170  


另外主机访问扫描snort主机80端口(snort主机配置apache服务器),报警:

[cpp]  view plain  copy
  1. 03/21-14:43:04.242200  [**] [1:26287:0] "Telnet Login" [**] [Priority: 0] {TCP} 192.168.213.162:38250 -> 192.168.213.170:80  
  2. 03/21-14:45:46.621115  [**] [1:26287:0] "Telnet Login" [**] [Priority: 0] {TCP} 192.168.213.162:38270 -> 192.168.213.170:80  
  3. 03/21-14:45:46.621268  [**] [1:26287:0] "Telnet Login" [**] [Priority: 0] {TCP} 192.168.213.162:38270 -> 192.168.213.170:80  
  4. 03/21-14:45:46.621409  [**] [1:26287:0] "Telnet Login" [**] [Priority: 0] {TCP} 192.168.213.162:38270 -> 192.168.213.170:80  
  5. 03/21-14:45:46.629345  [**] [1:26287:0] "Telnet Login" [**] [Priority: 0] {TCP} 192.168.213.162:38322 -> 192.168.213.170:80  
  6. 03/21-14:45:46.629466  [**] [1:26287:0] "Telnet Login" [**] [Priority: 0] {TCP} 192.168.213.162:38322 -> 192.168.213.170:80  
  7. 03/21-14:45:46.633859  [**] [1:26287:0] "Telnet Login" [**] [Priority: 0] {TCP} 192.168.213.162:38322 -> 192.168.213.170:80  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值