此文介绍在 Linux 上编译 Wireshark 的相关内容. 在 Windows/MacOS 上编译在 Wireshark 的二次开发中, 那文章写的比较全, 但是距离现在太久了. 后续文章基于新版本 Wireshark (3.x).
1 准备工作
- 准备源码
可使用 Git:
$ git clone https://gitlab.com/wireshark/wireshark.git
或从 https://www.wireshark.org/index.html#download 下载源码包.
- 安装 CMake, Ninja, Lua, flex, bison 等工具
- 安装 glib, pcap, gcrypt, c-ares 等开发包
如果不知道需要安装哪些包, 可以直接跳到下面的步骤进行编译, 编译时缺少了什么包 CMake 会提示的. 如果是 Ubuntu 系统可以用 apt search
或到 https://packages.ubuntu.com/ 搜索.
2 设置编译选项
以下使用 <wireshark>
来指 Wireshark 源码主目录.
可以使用 cmake -LH <wireshark>
来查看可用的编译选项. 这里罗列一些.
选项 | 类型 | 说明 |
---|---|---|
BUILD_tshark | BOOL | 是否编译tshark |
BUILD_wireshark | BOOL | 是否编译wireshark, 不需要图形界面时可以关闭 |
CMAKE_BUILD_TYPE | STRING | 编译类型, 可取值Debug Release RelWithDebInfo MinSizeRel |
ENABLE_PCAP | BOOL | 是否支持libpcap, 需要抓包时必须开启 |
ENABLE_CARES | BOOL | 是否支持c-ares, 默认开 |
ENABLE_LUA | BOOL | 是否支持Lua脚本解析器 |
ENABLE_PLUGINS | BOOL | 是否支持(二进制)插件 |
ENABLE_STATIC | BOOL | 是否编译为静态库, 默认关 |
:::warning
调用 cmake -LH
命令后源码目录会生成 CMake 相关文件, 如果不删除后续编译可能会失败, 如果报错请删除源码目录的 CMakeCache.txt 文件 和 CMakeFiles/ 目录
:::
3 编译(no GUI)
我选择编译 no GUI 的 wireshark, 并以 Debug 模式编译:
$ makdir -p wireshark_build
$ cd wirehark_build/
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_wireshark=off -DENABLE_LUA=on <wireshark>
$ ninja
编译 ninja
等命令编译前, cmake 命令会显示当前配置信息, 如已经安装了哪些依赖库, 这经常会很有用, 如:
zzq@vbox:~/dev/wireshark_build
$cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_wireshark=off -DENABLE_LUA=on ../wireshark
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
...
-- Found python module make-plugin-reg: /home/zzq/dev/wireshark/tools/make-plugin-reg.py
-- C-Flags: -fvisibility=hidden -fexcess-precision=fast -Wall -Wextra -Wendif-labels -Wpointer-arith -Wformat-security -fwrapv -fno-strict-overflow -Wvla -Waddress -Wattributes -Wdiv-by-zero -Wignored-qualifiers -Wpragmas -Wno-overlength-strings -Wno-long-long -Wframe-larger-than=32768 -Wc++-compat -Wunused-const-variable -Wshadow -Wold-style-definition -Wstrict-prototypes -Wlogical-op -Wjump-misses-init -Werror=implicit -Wno-pointer-sign -std=gnu99 -fmacro-prefix-map=/home/zzq/dev/wireshark/= -fmacro-prefix-map=/home/zzq/dev/wireshark_build/= -g
-- CXX-Flags: -fexcess-precision=fast -Wall -Wextra -Wendif-labels -Wpointer-arith -Wformat-security -fwrapv -fno-strict-overflow -Wvla -Waddress -Wattributes -Wdiv-by-zero -Wignored-qualifiers -Wpragmas -Wno-overlength-strings -Wno-long-long -Wframe-larger-than=32768 -Wextra-semi -fmacro-prefix-map=/home/zzq/dev/wireshark/= -fmacro-prefix-map=/home/zzq/dev/wireshark_build/= -g
-- Warnings as errors disabled
-- The following OPTIONAL packages have been found:
* Git
* GMODULE2
* PCAP
* ZLIB
* LUA (required version >= 5.1)
* SETCAP
-- The following REQUIRED packages have been found:
* GLIB2 (required version >= 2.32.0)
* GTHREAD2
* GCRYPT (required version >= 1.4.2)
* CARES (required version >= 1.5.0), Library for asynchronous DNS requests, <https://c-ares.haxx.se/>
DNS name resolution for captures
* LEX
* YACC
* Perl
* Python3 (required version >= 3.4)
* M
* POD
-- The following OPTIONAL packages have not been found:
* Gettext
* LIBSSH (required version >= 0.6), Library for implementing SSH clients, <https://www.libssh.org/>
extcap remote SSH interfaces (sshdump, ciscodump)
* Systemd, System and Service Manager (libraries), <https://freedesktop.org/wiki/Software/systemd/>
Support for systemd journal extcap interface (sdjournal)
* MaxMindDB, C library for the MaxMind DB file format, <https://github.com/maxmind/libmaxminddb>
Support for GeoIP lookup
* SMI
* GNUTLS (required version >= 3.2.0)
* KERBEROS
* Minizip, C library for supporting zip/unzip functionality, <https://www.winimage.com/zLibDll/minizip.html>
Support for profiles import/export
* BROTLI
* LZ4, LZ4 is lossless compression algorithm used in some protocol (CQL...), <http://www.lz4.org>
LZ4 decompression in CQL and Kafka dissectors
* SNAPPY, A fast compressor/decompressor from Google, <https://google.github.io/snappy/>
Snappy decompression in CQL and Kafka dissectors
* ZSTD (required version >= 1.0.0), A compressor/decompressor from Facebook providing better compression than Snappy at a cost of speed, <https://facebook.github.io/zstd/>
Zstd decompression in Kafka dissector
* NGHTTP2, HTTP/2 C library and tools, <https://nghttp2.org>
Header decompression in HTTP2
* NL, Libraries for using the Netlink protocol on Linux, <https://www.infradead.org/~tgr/libnl/>
Support for managing wireless 802.11 interfaces
* SBC, Bluetooth low-complexity, subband codec (SBC) decoder, <https://git.kernel.org/pub/scm/bluetooth/sbc.git>
Support for playing SBC codec in RTP player
* SPANDSP, a library of many DSP functions for telephony, <https://www.soft-switch.org>
Support for G.722 and G.726 codecs in RTP player
* BCG729, G.729 decoder, <https://www.linphone.org/technical-corner/bcg729/overview>
Support for G.729 codec in RTP player
* ILBC, iLBC decoder, <https://github.com/TimothyGu/libilbc>
Support for iLBC codec in RTP player
* OPUS, opus decoder, <https://opus-codec.org/>
Support for opus codec in RTP player
* LibXml2
* CAP, The Libcap package implements the user-space interfaces to the POSIX 1003.1e capabilities available in Linux kernels, <https://sites.google.com/site/fullycapable/>
Allow packet captures without running as root
* DOXYGEN
* SpeexDSP, SpeexDSP is a patent-free, Open Source/Free Software DSP library, <https://www.speex.org/>
RTP audio resampling
* Asciidoctor (required version >= 1.5)
* XSLTPROC
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zzq/dev/wireshark_build
编译后, 可执行程序在 run 目录:
$./tshark -v
TShark (Wireshark) 3.4.5 (v3.4.5-2-gd4f6e13cae41)
Copyright 1998-2021 Gerald Combs <gerald@wireshark.org> and contributors.
License GPLv2+: GNU GPL version 2 or later <https://www.gnu.org/licenses/gpl-2.0.html>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Compiled (64-bit) with libpcap, without POSIX capabilities, without libnl, with
GLib 2.64.6, with zlib 1.2.11, without SMI, with c-ares 1.15.0, with Lua 5.2.4,
without GnuTLS, with Gcrypt 1.8.5, without Kerberos, without MaxMind DB
resolver, without nghttp2, without brotli, without LZ4, without Zstandard,
without Snappy, without libxml2.
Running on Linux 5.4.0-73-generic, with Intel(R) Core(TM) i5-9400F CPU @ 2.90GHz
(with SSE4.2), with 1987 MB of physical memory, with locale en_US.UTF-8, with
libpcap version 1.9.1 (with TPACKET_V3), with Gcrypt 1.8.5, with zlib 1.2.11,
binary plugins supported (0 loaded).
Built using gcc 9.3.0.