Installation and use of tcpdum under mac

Install:        

        On macOS, the tcpdump tool can be installed via Homebrew. Here are the steps to install and use tcpdump:

  1. Open the Terminal application and install Homebrew by entering the following command (you can skip this step if you already have Homebrew installed):

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. Install tcpdump:

    brew install tcpdump
    
  3. Confirm that the installation was successful:

    tcpdump --version
    
  4. Use tcpdump to capture network packets:

    sudo tcpdump -i en0
    
    # 这将抓取 en0 网络接口的数据包,并在终端中显示它们的内容。
    
    # 注意:需要使用 sudo 命令运行 tcpdump,因为它需要 root 权限才能访问网络接口。

  5.  Use tcpdump to capture the specified ip command:

    sudo tcpdump host <IP地址>
    # 将 <IP地址> 替换为您要抓取的IP地址。此命令需要使用管理员权限,因为它需要访问网络设备。
    

use:

command function

tcpdump: Capture and dump traffic packets on the network.

command format

tcpdump uses the command line to filter and capture the data packets of the interface, and its rich features are reflected in the flexible expressions.

tcpdump without any options will capture the first network interface by default, and the packet capture will not stop until the tcpdump process is terminated.

Usage: tcpdump [-aAbdDefhHIJKlLnNOpqStuUvxX#] [ -B size ] [ -c count ]
        [ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
        [ -i interface ] [ -j tstamptype ] [ -M secret ] [ --number ]
        [ -Q in|out|inout ]
        [ -r file ] [ -s snaplen ] [ --time-stamp-precision precision ]
        [ --immediate-mode ] [ -T type ] [ --version ] [ -V file ]
        [ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]
        [ -Z user ] [ expression ]
  
注意:
    tcpdump只能抓取流经本机的数据包!!


Command parameters:

tcpdump -a    |--将网络地址和广播地址转变成名字;
        -A 
        -B
        -c    |--指定要抓取的包数量。注意,是最终要获取这么多个包。例如,指定"-c 10"将获取10个包,但可能已经处理了100个包,只不过只有10个包是满足条件的包。
        -C    |--在将一个原始分组写入文件之前,检查文件当前的大小是否超过了参数file_size中指定的大小。如果超过了指定大小,则关闭当前文件,然后在打开一个新的文件。
        -d    |--将匹配信息包的代码以人们能够理解的汇编格式给出;
        -dd   |--将匹配信息包的代码以c语言程序段的格式给出;
        -ddd  |--将匹配信息包的代码以十进制的形式给出;
        -D    |--列出可用于抓包的接口。将会列出接口的数值编号和接口名,它们都可以用于"-i"后。
        -e    |--输出的每行中都将包括数据链路层头部信息,例如源MAC和目标MAC。
        -G
        -f    |--将外部的Internet地址以数字的形式打印出来;
        -F    |--从文件中读取抓包的表达式。若使用该选项,则命令行中给定的其他表达式都将失效。
        -i    |--指定tcpdump需要监听的接口。若未指定该选项,将从系统接口列表中搜寻编号最小的已配置好的接口
        -I    |--使标准输出变为缓冲行形式;
        -j
        -m
        -M
        -n    |--对地址以数字方式显式,否则显式为主机名,也就是说-n选项不做主机名解析。
        -nn   |--指定将每个监听到的数据包中的域名转换成IP、端口从应用名称转换成端口号后显示
        -N    |--不打印出host的域名部分。例如tcpdump将会打印'nic'而不是'nic.ddn.mil'。
        -p    |--将网卡设置为非混杂模式,不能与host或broadcast一起使用;
        -P    |--指定要抓取的包是流入还是流出的包。可以给定的值为"in"、"out"和"inout",默认为"inout"。
        -q    |--快速打印输出。即打印很少的协议相关信息,从而输出行都比较简短。
        -r    |--从指定的文件中读取包(这些包一般通过-w选项产生);
        -S    |--将tcp的序列号以绝对值形式输出,而不是相对值。
        -s    |--从每个分组中读取最开始的snaplen个字节,而不是默认的68个字节。 
        -v    |--输出一个稍微详细的信息,例如在ip包中可以包括ttl和服务类型的信息;
        -vv   |--输出详细的报文信息;
        -V
        -s    |--设置tcpdump的数据包抓取长度为len,如果不设置默认将会是68字节。
        -t    |--在输出的每一行不打印时间戳;
        -T    |--将监听到的包直接解释为指定的类型的报文,常见的类型有rpc (远程过程调用)和snmp(简单网络管理协议;)
        -v    |--当分析和打印的时候,产生详细的输出。
        -vv   |--产生比-v更详细的输出。
        -vvv  |--产生比-vv更详细的输出。
        -w    |--将抓包数据输出到文件中而不是标准输出。
        -W
        -X    |--输出包的头部数据,会以16进制和ASCII两种方式同时输出。
        -XX   |--输出包的头部数据,会以16进制和ASCII两种方式同时输出,更详细。
        -E
        -y
        -z
        -Z 


Commonly used tcpdump expressions:

The expression is used to filter which types of data packets to output. If no expression is given, all data packets will be output, otherwise only the packets whose expression is true will be output. Shell metacharacters appearing in expressions are recommended to be surrounded by single quotes.

The expression of tcpdump consists of one or more "units", each unit generally contains ID modifiers and an ID (number or name). There are three modifiers:

(1).type: Specifies the type of ID.

The values ​​that can be given are host/net/port/portrange. For example "host foo", "net 128.3", "port 20", "portrange 6000-6008". The default type is host.

(2).dir: Specifies the direction of the ID.

The values ​​that can be given include src/dst/src or dst/src and dst, and the default is src or dst. For example, "src foo" means a data packet whose source host is foo, "dst net 128.3" means a data packet whose destination network is 128.3, and "src or dst port 22" means a data packet whose source or destination port is 22.

(3).proto: Limit the matching packet type by a given protocol.

Commonly used protocols include tcp/udp/arp/ip/ether/icmp, etc. If no protocol type is given, all possible types will be matched. For example "tcp port 21", "udp port range 7000-7009".

So, a basic expression cell has the format "proto dir type ID"

Examples of common commands:


Designated network port


tcpdump -i eth1 -n
    //监视指定网口eth1上的数据包(如果不用-i参数,默认tcpdump只会监视第一个网络接口,如eth0。-n是以数字方式显示地址)


Specifies the listening protocol type

tcpdump -i eth1 -n tcp
tcpdump -i eth1 -n udp
tcpdump -i eth1 -n arp
tcpdump -i eth1 -n ip
tcpdump -i eth1 -n ether
tcpdump -i eth1 -n icmp
    //监视 eth1 上的 icmp 数据包(也可以是其他类型的包 如tcp/udp/arp/ip/ether/icmp等)


Specify listening IP

tcpdump -i eth1 -n host 172.16.81.25
tcpdump -i eth1 -n src host 172.16.81.25
tcpdump -i eth1 -n dst host 172.16.81.25
    //监视 eth1 上的 IP 为 xxx.xxx.xxx.xxx 的数据包(不区分源IP和目的IP)
    //监视 eth1 上的 IP 为 xxx.xxx.xxx.xxx 的数据包(src 就是过滤源地址)
    //监视 eth1 上的 IP 为 xxx.xxx.xxx.xxx 的数据包(dst 就是过滤目的地址)


Specify the listening port

tcpdump -i eth1 -n port 22223 
tcpdump -i eth1 -n src port 22223 
tcpdump -i eth1 -n dst port 22223 
    //监视指定网口 eth1 上 端口 为 22223 的数据包(不区分源端口和目的端口)
    //监视指定网口 eth1 上 端口 为 22223 的数据包(src 过滤源端口)
    //监视指定网口 eth1 上 端口 为 22223 的数据包(dst 过滤目的端口)


Specify the listening network segment

tcpdump -i eth1 -n net 172.16
tcpdump -i eth1 -n src net 172.16
tcpdump -i eth1 -n dst net 172.16
    //监视指定网口 eth1 上 网段 为 xxx.xxx 的数据包(不区分源地址和目的地址)
    //监视指定网口 eth1 上 网段 为 xxx.xxx 的数据包(src 过滤源地址)
    //监视指定网口 eth1 上 网段 为 xxx.xxx 的数据包(dst 过滤目的地址)


Specify the listening mac address

tcpdump -i eth1 -n ether src 00:a0:c9:01:01:01
tcpdump -i eth1 -n ether dst 00:a0:c9:01:01:01
    //监视指定网口 eth1 上 mac 地址为 xx:xx:xx:xx:xx:xx 的数据包(源mac地址)
    //监视指定网口 eth1 上 mac 地址为 xx:xx:xx:xx:xx:xx 的数据包(目的mac地址)


Combine the above rules together.

tcpdump -i eth1 -n host 172.16.81.25 and port 22223 and net 172.16 and ether src 00:a0:c9:01:01:01 and icmp -w dump.pcap
    //将上面的规则整合在一起


Analyze the monitoring content

tcpdump -r dump.pcap
    //读取 dump.pcap 中的内容

Guess you like

Origin blog.csdn.net/weixin_65784341/article/details/130137283