Snort3: 开发环境(二)

发布时间 2023-11-16 22:45:53作者: 魔神八号

Snort-3-logo_v.png

1 系统选择

snort3要求编译器版本g++>=5.0.0,这里使用Ubuntu1804。

(部分)操作系统和默认编译器对照表


系统版本 默认编译器版本
1 Ubuntu 1804 7.5.0
2 Ubuntu 2004 9.4.0
3 Centos 7.8 4.8.5

2 依赖安装

依赖

  1. libpcap:https://www.tcpdump.org/ 。对具体版本似乎没有特别要求。这里使用1.9.1。注意libpcap要先于 daq安装,否则会出现snort运行失败的问题。
  2. g++ >= 5 or other C++14 compiler
  3. cmake:要求版本不低于 3.4.3。这里使用3.18.4
  4. daq:https://github.com/snort3/libdaq
  5. hwloc:https://www.open-mpi.org/projects/hwloc/
  6. lua:http://luajit.org/
  7. pcre: https://sourceforge.net/projects/pcre/files/pcre/

这里只列出部分依赖,其它可参考 https://github.com/snort3/snort3,如下:
image.png

3 编译和安装

3.1 源码下载

git clone https://github.com/snort3/snort3.git                         # 直接使用master分支最新代码。commit fa5e6e5ce3f46
admin@adminsnort3(master)$ll
总用量 420
-rw-rw-r--.  1 admin admin 243836 4月   6 11:37 ChangeLog.md
drwxrwxr-x.  2 admin admin   4096 4月   6 11:37 cmake                                       
-rw-rw-r--.  1 admin admin   4957 4月   6 11:37 CMakeLists.txt
-rw-rw-r--.  1 admin admin   1034 4月   6 11:35 cmake_uninstall.cmake.in
-rw-rw-r--.  1 admin admin   4573 4月   6 11:37 config.cmake.h.in
-rwxrwxr-x.  1 admin admin  19885 4月   6 11:37 configure_cmake.sh                       # 编译脚本
-rw-rw-r--.  1 admin admin  21011 4月   6 11:35 COPYING
-rw-rw-r--.  1 admin admin  70959 4月   6 11:37 crusty.cfg
drwxrwxr-x.  2 admin admin     82 4月   6 11:37 daqs
drwxrwxr-x.  6 admin admin     85 4月   6 11:37 doc                                      # 文档源文件
-rw-rw-r--.  1 admin admin  21017 4月   6 11:35 LICENSE
drwxrwxr-x.  2 admin admin    244 4月   6 11:37 lua                                      # 配置文件
-rw-rw-r--.  1 admin admin   6585 4月   6 11:37 README.md                         
-rw-rw-r--.  1 admin admin    915 4月   6 11:37 snort.pc.in
drwxrwxr-x. 47 admin admin   4096 4月   6 11:37 src                                      # 源码目录
drwxrwxr-x.  5 admin admin    109 4月   6 11:37 tools

3.2 编译安装

可使用snort3源码根目前中的configure_cmake.sh脚本来进行编译。

3.2.1 常用编译选项

  • --prefix=,安装目录。
  • ---generator=xxx ,编译文件生成器,默认Makeifle。可选如 Ninja等。
  • --enable-docs,使能文档编译。
  • --build-type=[Debug|Release|RelWithDebInfo|MinSizeRel]。
  • --enable-shell,启用交互式命令行。暂未使用过。

因为以后可能会对snort源码进行gdb跟踪。所以这里均编译为debug版本。

3.2.2 编译debug版本

$ ./configure_cmake.sh --prefix=/snort3 --build-type=Debug              # 1. 执行cmake,当前目录下自动生成build目录。
$ cd build                                                              # 2. 切换到 build 目录。
$ make -j4                                                              # 3. 多线程编译
$ sudo make install                                                     # 4. 安装

3.2.3 使用Ninja编译

$ ./configure_cmake.sh --generator=Ninja --prefix=/snort3 --build-type=Debug       # 1. 执行cmake,当前目录下自动生成build目录。
$ cd build                                                                         # 2. 切换到 build 目录。
$ ninja                                                                            # 3. 编译
$ sudo ninja install                                                               # 4. 安装

:::warning
注意
脚本中应该是存在bug,导致指定--generator时会出错。详见3.3.2 章节。
:::

3.2.4 使用cmake编译(不使用编译脚本)

$ mkdir build                                                       # 1. 创建build目录 
$ cmake --prefix=/snort3 -G Ninja -DCMAKE_BUILD_TYPE=Debug ..       # 2. 执行cmake
$ ninja                                                             # 3. 编译
$ sudo ninja install                                                # 4. 安装

3.3 编译脚本中的问题

configure_cmake.sh 脚本可能有一些细节问题,可能到导致无法编译或编译脚本中抛出语法警告。

3.3.1 tcp/jem 未定义,抛出"[: Illegal number" 语法警告

不会影响编译。这是因为tcm、和jem两个变量未定义和初始化导致。在合适的位置增加两行 tcm=0tcm=0 即可


if [ "$tcm" -eq 1 -a "$jem" -eq 1 ] ; then
    echo "--enable-jemalloc and --enable-tcmalloc are mutually exclusive; enable at most one"
    exit 2
fi

3.3.2 生成器选项错误

如果指定了编译文件生成器,则会抛出如下错误:
image.png

需要在configure_cmake.sh去掉 "$gen" 的双引号

 [ "$CMakeGenerator" ] && gen="-G $CMakeGenerator"
 
cmake "$gen" \                                           # 这里需要去掉 ""
    -DCMAKE_CXX_FLAGS:STRING="$CXXFLAGS $CPPFLAGS" \
    -DCMAKE_C_FLAGS:STRING="$CFLAGS $CPPFLAGS" \
    -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
    -DCMAKE_BUILD_TYPE:STRING=$build_type \
    $CMakeCacheEntries $sourcedir

:::info
备注
cmake "-G Ninja" .. 会抛出同样错误。
:::

3.4 安装后的目录结构

安装后的目录结构

  • /snort3/
    • bin # 可执行文件
    • etc # 配置文件
    • include
    • lib
    • share

4 依赖安装问题

4.1 daq 错误


daq编译报错1,缺少 autocon 等依赖。

+ autoreconf -ivf --warnings=all
./bootstrap: 4: ./bootstrap: autoreconf: not found

执行sudo apt-get install autoconf automake libtool即可解决。

daq编译报错2

admi@admilibdaq-master$./bootstrap 
+ autoreconf -ivf --warnings=all
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force --warnings=all -I m4
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize --copy --force
libtoolize: putting auxiliary files in '.'.
libtoolize: copying file './ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.
libtoolize: copying file 'm4/libtool.m4'
libtoolize: copying file 'm4/ltoptions.m4'
libtoolize: copying file 'm4/ltsugar.m4'
libtoolize: copying file 'm4/ltversion.m4'
libtoolize: copying file 'm4/lt~obsolete.m4'
autoreconf: running: /usr/bin/autoconf --force --warnings=all
configure.ac:133: error: possibly undefined macro: AC_CHECK_HEADERS
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
configure.ac:152: error: possibly undefined macro: AC_MSG_WARN
configure:18288: error: possibly undefined macro: dnl
configure:18293: error: possibly undefined macro: AS_IF
autoreconf: /usr/bin/autoconf failed with exit status: 1

需安装pkt-config。 sudo apt-get install pkg-config

4.2 dnet

admi@admilibdnet(master)$./configure 
configure: error: cannot find required auxiliary files: compile

在执行libnet目录下小执行 automake --add-missing,然后安装check依赖即可,见INSTALL文件。

4.3 zma

image.png

sudo apt install liblzma-dev

4.4 snort 运行错误

image.png

应该是先编译daq,后编译pcap导致。需要重新完整编译daq和snort。