为 CentOS 5 构建 Wireshark 1.12.5 静态二进制文件





5.00/5 (1投票)
如何为 CentOS 5 构建 Wireshark 1.12.5 静态二进制文件
本教程将向您展示如何在 CentOS 5.9 上编译最新版本的 Wireshark(截至 2015 年 5 月为 v1.12.5),并支持 SSL/TLS。这很有用,因为 CentOS 5.x 存储库中可用的最后一个 Wireshark 版本是 1.0.15,发布于 2010 年,而更高版本仅发布于 CentOS 6 或 7。您也可以使用本教程中描述的步骤,为 Redhat 5 或其他带有过时 Wireshark 版本的 Linux 发行版编译 Wireshark。
背景
首先,我必须说明为什么 CentOS 5 没有自动提供新版 Wireshark 是有原因的。Wireshark 1.6 及更高版本需要 GLib 2.16 或更高版本才能编译,而 CentOS 5.9 的最后一个官方 GLib 版本仅为 2.14。与 Wireshark 的许多其他软件包要求(如 autoconf
、automake
、flex
等)不同,这些软件包的更高版本可以通过 yum install 或从代码编译安装,但 GLib
是 CentOS(以及大多数 Linux 操作系统)的核心组件,无法在不使用更新版本重新编译内核的情况下进行升级。这也许就是 Wireshark 开发团队选择只支持 CentOS 6 和 7 的新版本的原因。
幸运的是,有一种方法可以克服这个限制,那就是执行 Wireshark 的 静态构建,它在编译时包含所有必需的库,并且可以在基本的 CentOS 安装上运行。我在进行三天的研究后,从这篇 帖子 中发现了这种方法,并选择详细描述编译步骤,以帮助其他遇到类似问题的人。为了尽可能清晰地演示整个过程,我们将在一个仅具有 CentOS 5.9 基本安装的虚拟机上开始构建。
在开始之前,请确保您的计算机上已安装 C/C++ 编译器。您可以使用以下命令安装 GCC:
yum install gcc-c++
运行 autogen.sh
首先,从 这里下载源代码并解压。在解压的源代码文件夹中,运行以下命令准备构建:
./autogen.sh
您将看到以下错误——缺少 autoconf
、automake
和 libtool
软件包
./autogen.sh: line 55: autoconf: command not found
You must have autoconf 2.60 or later installed to compile Wireshark.
Download the appropriate package for your distribution/OS,
or get the source tarball at ftp://ftp.gnu.org/pub/gnu/autoconf/
./autogen.sh: line 69: automake: command not found
You must have automake 1.9 or later installed to compile Wireshark.
Download the appropriate package for your distribution/OS,
or get the source tarball at ftp://ftp.gnu.org/pub/gnu/automake/
./autogen.sh: line 99: libtool: command not found
You must have libtool 1.4 or later installed to compile Wireshark.
Download the appropriate package for your distribution/OS,
or get the source tarball at ftp://ftp.gnu.org/pub/gnu/libtool/
安装缺失的 libtool
和 automake
软件包
yum install libtool automake
对于 autoconf
,存储库中的最新版本仅为 2.59。我们需要在此 处下载一个更新的版本(我尝试了 2.63,它运行正常),解压源代码,并从解压的源代码文件夹中运行以下命令将 autoconf
安装到 /usr/bin/autoconf
./configure
make install
现在再次运行 autogen.sh。它应该会成功,并显示以下内容:
Now type "./configure [options]" and "make" to compile Wireshark.
构建配置
下一步是配置源代码以进行构建。这意味着选择将包含在编译后的二进制文件中的功能、插件等。在此示例中,我们将编译 Wireshark 的命令行版本 tshark,不带用户界面,并支持 SSL/TLS。要尝试此操作,请运行以下命令:
./configure --disable-wireshark --with-ssl
这将花费一段时间,并很可能以以下消息结束:
configure: error: I couldn't find yacc (or bison or ...);
make sure it's installed and in your path
要修复此消息及类似错误消息,您需要安装一些额外的软件包:
yum install flex bison pkgconfig zlib zlib-devel libpcap libpcap-devel
openssl-devel gnutls gnutls-devel libgcrypt libgcrypt-devel
之后,再次运行 configure,您将看到另一个抱怨:
./configure: line 21144: PKG_PROG_PKG_CONFIG: command not found
checking for special C compiler options needed for large files... no
checking for _FILE_OFFSET_BITS value needed for large files... no
./configure: line 21777: syntax error near unexpected token `LIBGNUTLS,'
./configure: line 21777: ` PKG_CHECK_MODULES(LIBGNUTLS, gnutls >= 3.1.10 ,'
要解决此问题,您需要指定 aclocal
(automake 软件包的一部分)在您的系统上的位置。运行以下命令:
whereis aclocal
您将看到以下输出:
aclocal: /usr/bin/aclocal /usr/share/aclocal
记下位置,然后运行以下命令设置 aclocal
的路径:
ACLOCAL_FLAGS="-I /usr/share/aclocal"
再次运行 autogen
和 autoconfigure
。现在您将遇到最大的障碍,即 GLib
要求:
checking for GLIB - version >= 2.16.0... no
*** Could not run GLIB test program, checking why...
*** The test program failed to compile or link. See the file config.log for the
*** exact error that occurred. This usually means GLIB is incorrectly installed.
configure: error: GLib 2.16.0 or later distribution not found.
对于新手来说,GLib
是操作系统的核心组件,您无法通过 yum 升级它。切勿盲目尝试并执行 yum remove glib
或 yum remove glib2
等命令,希望升级库——您很可能会得到一个需要重新安装的损坏的系统。OpenSSL 也是如此,我们稍后会讲到,切勿尝试 yum remove openssl
,这会删除数百个软件包并需要之后重新安装。
现在正确的做法是从这里下载 更新版本的 GLib,例如 2.26(更高版本需要 CentOS 5 不自带的 Python 2.5),并执行静态构建以提取 .a 库文件。
./configure --enable-static --prefix=/tmp/glib
make
make install
之后,配置 Wireshark 以使用编译的 GLib 2.26 库执行 static
构建:
LDFLAGS=' -L/tmp/glib/lib ' CFLAGS='-fPIC' LD_LIBRARY_PATH=/tmp/glib/lib \
PKG_CONFIG_PATH=/tmp/glib/lib/pkgconfig ./configure \
--prefix=/usr/local/myshark --disable-wireshark \
--with-ssl --enable-static=yes --enable-shared=no --with-krb5=no
上述配置选项告诉 Wireshark 执行 static
构建(-fPIC
),使用 /tmp/glib/lib 中的 glib static
库(LD_LIBRARY_PATH
),并附带捆绑的 pkgconfig
(PKG_CONFIG_PATH
),并将 static
二进制文件输出到 /use/local/myshark(–prefix
)。此外,LDFLAGS
允许链接器将输出与提供的 static GLib
二进制文件链接,否则在 make 过程中会出现链接器错误。Kerberos
支持也需要禁用(–with-krb5=no
)以避免在编译过程中丢失头文件。我不确定为什么这个错误在正常构建中不会出现。
配置应以以下结果完成:
The Wireshark package has been configured with the following options.
Build wireshark (Gtk+) : no
Build wireshark-qt : no
Build tshark : yes
Build capinfos : yes
Build captype : yes
Build editcap : yes
Build dumpcap : yes
Build mergecap : yes
Build reordercap : yes
Build text2pcap : yes
Build randpkt : yes
Build dftest : yes
Build rawshark : yes
Save files as pcap-ng by default : yes
Install dumpcap with capabilities : no
Install dumpcap setuid : no
Use dumpcap group : (none)
Use plugins : yes
Use Lua library : no
Use Python binding : no
Build rtp_player : no
Build profile binaries : no
Use pcap library : yes
Use zlib library : yes
Use kerberos library : no
Use c-ares library : no
Use GNU ADNS library : no
Use SMI MIB library : no
Use GNU crypto library : yes
Use SSL crypto library : yes
Use IPv6 name resolution : yes
Use gnutls library : yes
Use POSIX capabilities library : no
Use GeoIP library : no
Use nl library : no
Use SBC codec library : no
如果结果显示 Use gnutls library : no,请确保已安装 gnutls
和 gnutls-devel
软件包。如果遇到错误 SSL crypto library was requested, but is not available,请检查您是否已安装 openssl-devel
软件包。如果 Use GNU crypto library 为 no,请安装 libgcrypt
和 libgcrypt-devel
软件包。
构建 tshark
键入 make
开始构建过程,这可能需要一段时间(在我的 Intel Core i7 处理器上需要 5 分钟)。完成后,键入 make install
,您将在 /usr/local/myshark/bin 中看到以下文件:
-rwxr-xr-x 1 root root 572K Jun 18 12:21 capinfos
-rwxr-xr-x 1 root root 557K Jun 18 12:21 captype
-rwxr-xr-x 1 root root 66M Jun 18 12:21 dftest
-rwxr-xr-x 1 root root 106K Jun 18 12:21 dumpcap
-rwxr-xr-x 1 root root 590K Jun 18 12:21 editcap
-rwxr-xr-x 1 root root 570K Jun 18 12:21 mergecap
-rwxr-xr-x 1 root root 564K Jun 18 12:21 randpkt
-rwxr-xr-x 1 root root 69M Jun 18 12:21 rawshark
-rwxr-xr-x 1 root root 561K Jun 18 12:21 reordercap
-rwxr-xr-x 1 root root 56K Jun 18 12:21 text2pcap
-rwxr-xr-x 1 root root 70M Jun 18 12:21 tshark
与正常构建的几百 KB 相比,70MB 的大文件大小是 tshark
二进制文件现在是 static
并且包含所有必需的库以独立运行而无需任何其他软件包的明显标志。为了测试构建是否确实是 static
,我将 /usr/local/myshark 复制到一个仅包含基本软件包的新精简版 CentOS 安装中,并运行 tshark -v
。
它成功了!现在我们可以在精简版的 CentOS 5 上运行功能齐全的最新版本 Wireshark 1.12.5 了。为了了解 Wireshark 1.12.5 相对于 Wireshark 1.0.15 的可能改进,请尝试使用 TLS_RSA_WITH_AES_256_CBC_SHA256
加密解密 TLS 1.2 流量,并使用自签名证书。在我的测试中,Wireshark 1.0.15 甚至无法识别 SSL 握手,而最新版本可以正常解密流量。
构建 mergecap 和其他实用程序
我的下一个挑战是在 CentOS 5 基本安装上运行 mergecap 时出现的。尽管 mergecap -h
运行正常,并显示了版本号和帮助文本,但实际的 pcap 合并操作因以下错误而失败:
undefined symbol: g_malloc_n
似乎即使请求了静态构建配置,glib
库也没有与 mergecap
捆绑,因此当 GLib
未安装在机器上时,方法 g_malloc_n(GLib
的一部分)将无法加载。mergecap
的小文件大小(570KB)解释了这一点,如果 GLib
和其他库是静态链接的,它应该更大。其他实用程序(editcap
、dumpcap
等)也是如此,它们的小尺寸表明所需的库可能没有与可执行文件链接。
由于似乎没有开箱即用的配置选项来解决此问题,因此在不编辑 configure/make 脚本的情况下克服此问题的唯一方法是使用 gcc
来 static-link
对象文件及其所需的库:
gcc -ldl -W1,-Bstatic mergecap-mergecap.o /usr/local/myshark/lib/*.a \
/usr/lib64/libz.a /tmp/glib/lib/libgio-2.0.a /tmp/glib/lib/libgmodule-2.0.a \
/tmp/glib/lib/libgthread-2.0.a /tmp/glib/lib/libglib-2.0.a -o mergecap-new
此方法生成一个 1565KB 的较大二进制文件,该文件可以在 CentOS 基本安装上正常运行。我想,如果需要将其他可执行文件(editcap
、dumpcap
等)独立运行在 CentOS 5 上,也可以采用相同的方法。
不幸的是,您无法使用此方法在 CentOS 5.9 上构建 Wireshark 的 RPM。命令 make dist
和 make rpm-package
将因 uic not found 消息而失败。这是因为即使我们只对 tshark 感兴趣而对 Wireshark 的 GUI 版本不感兴趣,RPM 构建的 Wireshark 也会以某种方式使用 uic。
顺便说一句,最后一个可以在 CentOS 5 上本地编译而无需诉诸 GLib 静态构建的 Wireshark 版本是 1.5.0,发布于 2011 年 4 月。
Cent OS 5 的 Wireshark 1.12.5 静态构建可在此 处下载。mergecap 的静态构建可在此 处找到。我猜下一个挑战可能是 手动编译更高版本的 GCC 来用于 CentOS 5,并使用该版本的 GCC 来编译 Wireshark。一旦 Wireshark 团队决定停止支持 CentOS 5 上可用的最新版本 GCC 4.1.2,这将是必需的。