如何在 Linux 系统上安装 Suricata 入侵检测系统
随着安全威胁的不断发生,入侵检测系统(IDS)在如今的数据中心环境中显得尤为必要。然而,随着越来越多的服务器将他们的网卡升级到10GB/40GB以太网,对如此线路上的硬件进行计算密集型的入侵检测越来越困难。其中一种提升入侵检测系统性能的途径是多线程入侵检测系统,它将 CPU 密集型的深度包检测工作并行的分配给多个并发任务来完成。这样的并行检测可以充分利用多核硬件的优势来轻松提升入侵检测系统的吞吐量。在这方面有两个知名的开源项目,分别是 Suricata 和 Bro。
这个教程里,我会向大家演示如何在 Linux 服务器上安装和配置 Suricata 入侵检测系统。
在 Linux 上安装 Suricata IDS
让我们从源文件来构建 Suricata,但在此之前,需要按如下所示先安装几个依赖包。
在 Debian, Ubuntu 或者 Linux Mint 操作系统上安装依赖包
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install </span><span class="kwd">wget</span><span class="pln"> build</span><span class="pun">-</span><span class="pln">essential libpcre3</span><span class="pun">-</span><span class="pln">dev libpcre3</span><span class="pun">-</span><span class="pln">dbg automake autoconf libtool libpcap</span><span class="pun">-</span><span class="pln">dev libnet1</span><span class="pun">-</span><span class="pln">dev libyaml</span><span class="pun">-</span><span class="pln">dev zlib1g</span><span class="pun">-</span><span class="pln">dev libcap</span><span class="pun">-</span><span class="pln">ng</span><span class="pun">-</span><span class="pln">dev libjansson</span><span class="pun">-</span><span class="pln">dev</span>
在 CentOS, Fedora 或者 RHEL 操作系统上安装依赖包
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">yum</span><span class="pln"> install </span><span class="kwd">wget</span><span class="pln"> libpcap</span><span class="pun">-</span><span class="pln">devel libnet</span><span class="pun">-</span><span class="pln">devel pcre</span><span class="pun">-</span><span class="pln">devel </span><span class="kwd">gcc</span><span class="pun">-</span><span class="pln">c</span><span class="pun">++</span><span class="pln"> automake autoconf libtool </span><span class="kwd">make</span><span class="pln"> libyaml</span><span class="pun">-</span><span class="pln">devel zlib</span><span class="pun">-</span><span class="pln">devel </span><span class="kwd">file</span><span class="pun">-</span><span class="pln">devel jansson</span><span class="pun">-</span><span class="pln">devel nss</span><span class="pun">-</span><span class="pln">devel</span>
一旦将所有依赖包安装完毕,我们就可以继续安装 Suricata 了。
首先从 http://suricata-ids.org/download/ 下载 Suricata 源代码,然后构建它。撰写这篇文章的时候,其最新版本号为 2.0.8 。
<span class="pln">$ </span><span class="kwd">wget</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//www.openinfosecfoundation.org/download/suricata-2.0.8.tar.gz</span>
<span class="pln">$ </span><span class="kwd">tar</span><span class="pun">-</span><span class="pln">xvf suricata</span><span class="pun">-</span><span class="lit">2.0</span><span class="pun">.</span><span class="lit">8.tar</span><span class="pun">.</span><span class="pln">gz</span>
<span class="pln">$ </span><span class="kwd">cd</span><span class="pln"> suricata</span><span class="pun">-</span><span class="lit">2.0</span><span class="pun">.</span><span class="lit">8</span>
<span class="pln">$ </span><span class="pun">./</span><span class="pln">configure </span><span class="pun">--</span><span class="pln">sysconfdir</span><span class="pun">=</span><span class="str">/etc --localstatedir=/</span><span class="kwd">var</span>
以下是配置信息的样例。
<span class="typ">Suricata</span><span class="typ">Configuration</span><span class="pun">:</span>
<span class="pln">AF_PACKET support</span><span class="pun">:</span><span class="kwd">yes</span>
<span class="pln">PF_RING support</span><span class="pun">:</span><span class="kwd">no</span>
<span class="typ">NFQueue</span><span class="pln"> support</span><span class="pun">:</span><span class="kwd">no</span>
<span class="pln">NFLOG support</span><span class="pun">:</span><span class="kwd">no</span>
<span class="pln">IPFW support</span><span class="pun">:</span><span class="kwd">no</span>
<span class="pln">DAG enabled</span><span class="pun">:</span><span class="kwd">no</span>
<span class="typ">Napatech</span><span class="pln"> enabled</span><span class="pun">:</span><span class="kwd">no</span>
<span class="typ">Unix</span><span class="pln"> socket enabled</span><span class="pun">:</span><span class="kwd">yes</span>
<span class="typ">Detection</span><span class="pln"> enabled</span><span class="pun">:</span><span class="kwd">yes</span>
<span class="pln">libnss support</span><span class="pun">:</span><span class="kwd">yes</span>
<span class="pln">libnspr support</span><span class="pun">:</span><span class="kwd">yes</span>
<span class="pln">libjansson support</span><span class="pun">:</span><span class="kwd">yes</span>
<span class="typ">Prelude</span><span class="pln"> support</span><span class="pun">:</span><span class="kwd">no</span>
<span class="pln">PCRE jit</span><span class="pun">:</span><span class="kwd">yes</span>
<span class="pln">LUA support</span><span class="pun">:</span><span class="kwd">no</span>
<span class="pln">libluajit</span><span class="pun">:</span><span class="kwd">no</span>
<span class="pln">libgeoip</span><span class="pun">:</span><span class="kwd">no</span>
<span class="typ">Non</span><span class="pun">-</span><span class="pln">bundled htp</span><span class="pun">:</span><span class="kwd">no</span>
<span class="typ">Old</span><span class="pln"> barnyard2 support</span><span class="pun">:</span><span class="kwd">no</span>
<span class="pln">CUDA enabled</span><span class="pun">:</span><span class="kwd">no</span>
现在可以编译、安装了。
<span class="pln">$ </span><span class="kwd">make</span>
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">make</span><span class="pln"> install</span>
Suricata 源代码带有默认的配置文件。按照如下方法安装这些默认配置文件即可。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">make</span><span class="pln"> install</span><span class="pun">-</span><span class="pln">conf</span>
正如你所料,如果没有IDS规则集的话,Suricata 什么用也没有。幸好 Makefile 为我们提供了 IDS 规则集的安装选项。安装方法如下。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">make</span><span class="pln"> install</span><span class="pun">-</span><span class="pln">rules</span>
以上的规则安装命令会从 EmergingThreats.net 上下载可用的社区规则集快照,并且将其存储在 /etc/suricata/rules 目录下。
首次配置 Suricata IDS
现在到了配置 Suricata 的时候了。配置文件的位置是 /etc/suricata/suricata.yaml。参照以下命令,用文本编辑器打开这个文件。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">suricata</span><span class="pun">/</span><span class="pln">suricata</span><span class="pun">.</span><span class="pln">yaml</span>
文件中有一些运行所需的基本配置。
为default-log-dir
关键字指定 Suricata 日志文件所在的位置。
<span class="kwd">default</span><span class="pun">-</span><span class="pln">log</span><span class="pun">-</span><span class="kwd">dir</span><span class="pun">:</span><span class="str">/var/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">suricata</span><span class="pun">/</span>
在vars
部分下方,你会发现几项对 Suricata 来说很重要变量。HOME_NET
变量需要指定 Suricata 检查的网络。被分配给 EXTERNAL_NET
变量的 !$HOME_NET
代表除本地网络之外的其他网络。XXX_PORTS
变量用来辨别不同服务所用到的端口号。需要注意的是无论使用什么端口,Suricata 都可以自动检测 HTTP 流量。所以是不是正确指定端口就显得没那么重要了。
<span class="pln">vars</span><span class="pun">:</span>
<span class="pln">HOME_NET</span><span class="pun">:</span><span class="str">"[192.168.122.0/24]"</span>
<span class="pln">EXTERNAL_NET</span><span class="pun">:</span><span class="str">"!$HOME_NET"</span>
<span class="pln">HTTP_PORTS</span><span class="pun">:</span><span class="str">"80"</span>
<span class="pln">SHELLCODE_PORTS</span><span class="pun">:</span><span class="str">"!80"</span>
<span class="pln">SSH_PORTS</span><span class="pun">:</span><span class="lit">22</span>
host-os-policy
部分用于防御利用操作系统网络栈的自身行为来逃避检测的一些知名攻击手段(例如:TCP reassembly)。作为对策,通过针对目标操作系统而对检测引擎算法进行微调,现代 IDC 提供了“基于目标”的检测手段。因此,如果你知道某台主机运行了什么操作系统的话,将这个信息提供给 Suricata 就可以大幅提高检测的成功率。这就是 host-os-policy
存在的意义。本例中,默认的 IDC 策略是 Linux 系统。如果针对某个 IP 地址没有指定操作系统信息,Suricata 会默认应用基于 Linux 系统的检测策略。如下,当捕获到对 192.168.122.0/28 和 192.168.122.155通讯时,Suricata 就会应用基于 Windows 系统的检测策略。
<span class="pln">host</span><span class="pun">-</span><span class="pln">os</span><span class="pun">-</span><span class="pln">policy</span><span class="pun">:</span>
<span class="com">#</span><span class="typ">These</span><span class="pln"> are </span><span class="typ">Windows</span><span class="pln"> machines</span><span class="pun">.</span>
<span class="pln">windows</span><span class="pun">:</span><span class="pun">[</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">122.0</span><span class="pun">/</span><span class="lit">28</span><span class="pun">,</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">122.155</span><span class="pun">]</span>
<span class="pln">bsd</span><span class="pun">:</span><span class="pun">[]</span>
<span class="pln">bsd</span><span class="pun">-</span><span class="pln">right</span><span class="pun">:</span><span class="pun">[]</span>
<span class="pln">old</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">:</span><span class="pun">[]</span>
<span class="com">#</span><span class="typ">Make</span><span class="pln"> the </span><span class="kwd">default</span><span class="pln"> policy </span><span class="typ">Linux</span><span class="pun">.</span>
<span class="pln">linux</span><span class="pun">:</span><span class="pun">[</span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">/</span><span class="lit">0</span><span class="pun">]</span>
<span class="pln">old</span><span class="pun">-</span><span class="pln">solaris</span><span class="pun">:</span><span class="pun">[]</span>
<span class="pln">solaris</span><span class="pun">:</span><span class="pun">[</span><span class="str">"::1"</span><span class="pun">]</span>
<span class="pln">hpux10</span><span class="pun">:</span><span class="pun">[]</span>
<span class="pln">hpux11</span><span class="pun">:</span><span class="pun">[]</span>
<span class="pln">irix</span><span class="pun">:</span><span class="pun">[]</span>
<span class="pln">macos</span><span class="pun">:</span><span class="pun">[]</span>
<span class="pln">vista</span><span class="pun">:</span><span class="pun">[]</span>
<span class="pln">windows2k3</span><span class="pun">:</span><span class="pun">[]</span>
在 threading
部分下,你可以为不同的 Suricata 线程指定 CPU 关联。默认状态下,CPU 关联 是被禁止使用的 (set-cpu-affinity: no
),这意味着 Suricata 会分配其线程到所有可用的 CPU 核心上。Suricata 会默认为每一个 CPU 核心创建一个检测线程。你可以通过指定 detect-thread-ratio: N
来调整此行为。此处会创建 N*M 个检测线程,M 代表 CPU 核心总数。
<span class="pln">threading</span><span class="pun">:</span>
<span class="kwd">set</span><span class="pun">-</span><span class="pln">cpu</span><span class="pun">-</span><span class="pln">affinity</span><span class="pun">:</span><span class="kwd">no</span>
<span class="pln">detect</span><span class="pun">-</span><span class="pln">thread</span><span class="pun">-</span><span class="pln">ratio</span><span class="pun">:</span><span class="lit">1.5</span>
通过以上对线程的设置,Suricata 会创建 1.5*M 个检测线程,M 是系统的 CPU 核心总数。
如果你想对 Suricata 配置有更多的了解,可以去翻阅默认配置文件。里边配有有大量的注释以供你清晰理解。
使用 Suricata 进行入侵监控
现在是时候让 Suricata 跑起来了,但在这之前还有一个步骤需要去完成。
当你使用 pcap 捕获模式的时候,强烈建议关闭 Suricata 监听网卡上的任何的包卸载(例如 LRO/GRO)功能。这些功能会干扰包的实时捕获行为。
按照以下方法关闭 eth0 接口的 LRO/GRO 功能。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> ethtool </span><span class="pun">-</span><span class="pln">K eth0 gro off lro off</span>
这里要注意,在使用某些网卡的情况下,你会看到如下警告信息。忽略它们就行了,这些信息只不过告诉你你的网卡不支持 LRO 功能而已。
<span class="typ">Cannot</span><span class="pln"> change large</span><span class="pun">-</span><span class="pln">receive</span><span class="pun">-</span><span class="pln">offload</span>
Suricata 支持许多运行模式。运行模式决定着 IDC 会使用何种线程。以下命令可以查看所有 可用的运行模式。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">suricata </span><span class="pun">--</span><span class="kwd">list</span><span class="pun">-</span><span class="pln">runmodes</span>
Suricata 使用的默认运行模式是 autofp(auto flow pinned load balancing(自动流绑定负载均衡) 的缩写)。这个模式下,来自某一个流的包会被分配到一个单独的检测线程中。这些流会根据未被处理的包的最低数量来分配相应的线程。
最后,让我们将 Suricata 运行起来,看看它表现如何。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">suricata </span><span class="pun">-</span><span class="pln">c </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">suricata</span><span class="pun">/</span><span class="pln">suricata</span><span class="pun">.</span><span class="pln">yaml </span><span class="pun">-</span><span class="pln">i eth0 </span><span class="pun">--</span><span class="kwd">init</span><span class="pun">-</span><span class="pln">errors</span><span class="pun">-</span><span class="pln">fatal</span>
本例中,我们在一个8核心系统中监控 eth0 网络接口。如上所示,Suricata 创建了13个包处理线程和3个管理线程。包处理线程中包括一个 PCAP 包捕获线程,12个检测线程(由8*1.5得出)。这表示 IDS 内的1个包捕获线程均衡负载到12个检测线程中。管理线程包括1个流管理和2个计数/统计相关线程。
以下是一个关于Suricata处理的线程截图(由 htop 绘制)。
Suricata 检测日志存储在 /var/log/suricata 目录下。
<span class="pln">$ </span><span class="kwd">tail</span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">suricata</span><span class="pun">/</span><span class="pln">fast</span><span class="pun">.</span><span class="pln">log</span>
<span class="lit">04</span><span class="pun">/</span><span class="lit">01</span><span class="pun">/</span><span class="lit">2015</span><span class="pun">-</span><span class="lit">15</span><span class="pun">:</span><span class="lit">47</span><span class="pun">:</span><span class="lit">12.559075</span><span class="pun">[**]</span><span class="pun">[</span><span class="lit">1</span><span class="pun">:</span><span class="lit">2200074</span><span class="pun">:</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> SURICATA </span><span class="typ">TCPv4</span><span class="pln"> invalid checksum </span><span class="pun">[**]</span><span class="pun">[</span><span class="typ">Classification</span><span class="pun">:</span><span class="pun">(</span><span class="kwd">null</span><span class="pun">)]</span><span class="pun">[</span><span class="typ">Priority</span><span class="pun">:</span><span class="lit">3</span><span class="pun">]</span><span class="pun">{</span><span class="pln">TCP</span><span class="pun">}</span><span class="lit">172.16</span><span class="pun">.</span><span class="lit">253.158</span><span class="pun">:</span><span class="lit">22</span><span class="pun">-></span><span class="lit">172.16</span><span class="pun">.</span><span class="lit">253.1</span><span class="pun">:</span><span class="lit">46997</span>
<span class="lit">04</span><span class="pun">/</span><span class="lit">01</span><span class="pun">/</span><span class="lit">2015</span><span class="pun">-</span><span class="lit">15</span><span class="pun">:</span><span class="lit">49</span><span class="pun">:</span><span class="lit">06.565901</span><span class="pun">[**]</span><span class="pun">[</span><span class="lit">1</span><span class="pun">:</span><span class="lit">2200074</span><span class="pun">:</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> SURICATA </span><span class="typ">TCPv4</span><span class="pln"> invalid checksum </span><span class="pun">[**]</span><span class="pun">[</span><span class="typ">Classification</span><span class="pun">:</span><span class="pun">(</span><span class="kwd">null</span><span class="pun">)]</span><span class="pun">[</span><span class="typ">Priority</span><span class="pun">:</span><span class="lit">3</span><span class="pun">]</span><span class="pun">{</span><span class="pln">TCP</span><span class="pun">}</span><span class="lit">172.16</span><span class="pun">.</span><span class="lit">253.158</span><span class="pun">:</span><span class="lit">22</span><span class="pun">-></span><span class="lit">172.16</span><span class="pun">.</span><span class="lit">253.1</span><span class="pun">:</span><span class="lit">46317</span>
<span class="lit">04</span><span class="pun">/</span><span class="lit">01</span><span class="pun">/</span><span class="lit">2015</span><span class="pun">-</span><span class="lit">15</span><span class="pun">:</span><span class="lit">49</span><span class="pun">:</span><span class="lit">06.566759</span><span class="pun">[**]</span><span class="pun">[</span><span class="lit">1</span><span class="pun">:</span><span class="lit">2200074</span><span class="pun">:</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> SURICATA </span><span class="typ">TCPv4</span><span class="pln"> invalid checksum </span><span class="pun">[**]</span><span class="pun">[</span><span class="typ">Classification</span><span class="pun">:</span><span class="pun">(</span><span class="kwd">null</span><span class="pun">)]</span><span class="pun">[</span><span class="typ">Priority</span><span class="pun">:</span><span class="lit">3</span><span class="pun">]</span><span class="pun">{</span><span class="pln">TCP</span><span class="pun">}</span><span class="lit">172.16</span><span class="pun">.</span><span class="lit">253.158</span><span class="pun">:</span><span class="lit">22</span><span class="pun">-></span><span class="lit">172.16</span><span class="pun">.</span><span class="lit">253.1</span><span class="pun">:</span><span class="lit">46317</span>
日志也可以提供 Json 格式以便导入:
<span class="pln">$ </span><span class="kwd">tail</span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">suricata</span><span class="pun">/</span><span class="pln">eve</span><span class="pun">.</span><span class="pln">json</span>
<span class="pun">{</span><span class="str">"timestamp"</span><span class="pun">:</span><span class="str">"2015-04-01T15:49:06.565901"</span><span class="pun">,</span><span class="str">"event_type"</span><span class="pun">:</span><span class="str">"alert"</span><span class="pun">,</span><span class="str">"src_ip"</span><span class="pun">:</span><span class="str">"172.16.253.158"</span><span class="pun">,</span><span class="str">"src_port"</span><span class="pun">:</span><span class="lit">22</span><span class="pun">,</span><span class="str">"dest_ip"</span><span class="pun">:</span><span class="str">"172.16.253.1"</span><span class="pun">,</span><span class="str">"dest_port"</span><span class="pun">:</span><span class="lit">46317</span><span class="pun">,</span><span class="str">"proto"</span><span class="pun">:</span><span class="str">"TCP"</span><span class="pun">,</span><span class="str">"alert"</span><span class="pun">:{</span><span class="str">"action"</span><span class="pun">:</span><span class="str">"allowed"</span><span class="pun">,</span><span class="str">"gid"</span><span class="pun">:</span><span class="lit">1</span><span class="pun">,</span><span class="str">"signature_id"</span><span class="pun">:</span><span class="lit">2200074</span><span class="pun">,</span><span class="str">"rev"</span><span class="pun">:</span><span class="lit">1</span><span class="pun">,</span><span class="str">"signature"</span><span class="pun">:</span><span class="str">"SURICATA TCPv4 invalid checksum"</span><span class="pun">,</span><span class="str">"category"</span><span class="pun">:</span><span class="str">""</span><span class="pun">,</span><span class="str">"severity"</span><span class="pun">:</span><span class="lit">3</span><span class="pun">}}</span>
<span class="pun">{</span><span class="str">"timestamp"</span><span class="pun">:</span><span class="str">"2015-04-01T15:49:06.566759"</span><span class="pun">,</span><span class="str">"event_type"</span><span class="pun">:</span><span class="str">"alert"</span><span class="pun">,</span><span class="str">"src_ip"</span><span class="pun">:</span><span class="str">"172.16.253.158"</span><span class="pun">,</span><span class="str">"src_port"</span><span class="pun">:</span><span class="lit">22</span><span class="pun">,</span><span class="str">"dest_ip"</span><span class="pun">:</span><span class="str">"172.16.253.1"</span><span class="pun">,</span><span class="str">"dest_port"</span><span class="pun">:</span><span class="lit">46317</span><span class="pun">,</span><span class="str">"proto"</span><span class="pun">:</span><span class="str">"TCP"</span><span class="pun">,</span><span class="str">"alert"</span><span class="pun">:{</span><span class="str">"action"</span><span class="pun">:</span><span class="str">"allowed"</span><span class="pun">,</span><span class="str">"gid"</span><span class="pun">:</span><span class="lit">1</span><span class="pun">,</span><span class="str">"signature_id"</span><span class="pun">:</span><span class="lit">2200074</span><span class="pun">,</span><span class="str">"rev"</span><span class="pun">:</span><span class="lit">1</span><span class="pun">,</span><span class="str">"signature"</span><span class="pun">:</span><span class="str">"SURICATA TCPv4 invalid checksum"</span><span class="pun">,</span><span class="str">"category"</span><span class="pun">:</span><span class="str">""</span><span class="pun">,</span><span class="str">"severity"</span><span class="pun">:</span><span class="lit">3</span><span class="pun">}}</span>
总结
这篇教程中,我为大家演示了如何在一台多核 Linux 服务器上安装 Suricata 入侵检测系统。不同于单线程的 Snort IDS ,Suricata 可以很容易的从多核硬件的多进程特性所带来的好处中获益。定制 Suricata 来最大化其效能和检测范围是一个很好的主意。Suricata 的粉丝们维护着一个 在线 Wiki,如果你打算将 Suricata 部署到你的环境中,我强烈建议你去那儿取取经。
如果你现在已经开始使用 Suricata 了的话,把你的经验也分享出来吧。
via: http://xmodulo.com/install-suricata-intrusion-detection-system-linux.html