1.资源准备:
(1)下载tcpdump源码:https://www.tcpdump.org/index.html#latest-releases
(2)下载两个文件(tcpdump与libpcap):tcpdump-4.99.1.tar.gz和libpcap-1.10.1.tar.gz
2.在linux编译机中,新建一个tcpdump文件夹(路径建议全英文;另外,如果编译机是虚拟机不要放在共享文件夹里,否则会导致编译报错),把上述两个压缩包拷贝到此文件夹内,解压;并给tcpdump下所有文件赋予权限(chmod 777 *);
3.使用root用户进行以下操作;
4.首先编译libpcap
在libpcap-1.10.1文件夹中执行以下命令
(1)./configure
(如果出现"configure: error: Neither flex nor lex was found."错误,则安装对应组件即可ubuntu下执行
"apt-get install flex bison"缺省安装即可,安装完再执行步骤(1))
(2)make
(3)make install
5.编译tcpdump
在tcpdump-4.99.1文件夹中执行以下命令
(1)./configure
(2)make
6.查看tcpdump可执行文件是否生成
7.测试运行一下
Demo1:
root@root:/home/root123/code/tcpdump-4.99.1# ./tcpdump
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on enp0s3, link-type EN10MB (Ethernet), snapshot length 262144 bytes
15:04:23.698809 IP6 root.mdns > ff02::fb.mdns: 0 [2q] PTR (QM)? _ipps._tcp.local. PTR (QM)? _ipp._tcp.local. (45)
15:04:23.698839 IP root.mdns > 224.0.0.251.mdns: 0 [2q] PTR (QM)? _ipps._tcp.local. PTR (QM)? _ipp._tcp.local. (45)
15:04:23.793857 IP root.40935 > ad.goodwe.com.cn.domain: 4294+ [1au] PTR? b.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.f.f.ip6.arpa. (101)
15:04:24.009623 IP ad.goodwe.com.cn.domain > root.40935: 4294 NXDomain 0/1/1 (165)
15:04:24.009773 IP root.40935 > ad.goodwe.com.cn.domain: 4294+ PTR? b.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.f.f.ip6.arpa. (90)
15:04:24.014792 IP ad.goodwe.com.cn.domain > root.40935: 4294 NXDomain 0/1/0 (154)
15:04:24.015339 IP root.57005 > ad.goodwe.com.cn.domain: 36592+ [1au] PTR? 5.2.4.6.0.1.a.c.2.9.e.9.c.9.a.f.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa. (101)
Demo2:(以虚拟机网卡enp0s3为例)
生成抓包文件test.pcap
root@root:/home/root123/code/tcpdump-4.99.1# ./tcpdump -i enp0s3 -w test.pcap
tcpdump: listening on enp0s3, link-type EN10MB (Ethernet), snapshot length 262144 bytes
^C2 packets captured
4 packets received by filter
0 packets dropped by kernel