tcpdump抓包,wireshark分析进阶

抓包进阶

抓包命令

tcpdump -i eth1 -vvv -w test.pcap
# -i 指定网口
# -vvv 在shell中显示抓到的包
# -w 将抓到的包写入文件
  • 指定主机名

    tcpdump -i eth1 host www.baidu.com
    
  • 指定ip

     tcpdump -i eth1 host 192.168.1.12
    
  • 指定端口

     tcpdump -i eth1 port 80
     # 指定源端口
     tcpdump -i eth1 src port 80
     # 指定目的端口
     tcpdump -i eth1 dst port 80
    
  • 指定协议

     # 对于传输层之上的协议需加port
     tcpdump -i eth1 port http
    
  • 指定icmp

     # ping打包
     ping -s 4800 ip
     # 抓取ping包
     # 传输层及传输层之下的协议直接跟协议名
     tcpdump -i eth1 icmp
    
  • 当需要长时间抓包时,为避免单个抓包文件过大导致打开查询缓慢,可设置当个抓包文件大小

    # 指定单个抓包文件大小为100M,超过100M会保存在新文件中
    tcpdump -i eth1 -C 100M -w test.pcap
    

拆分包

当单个文件报文个数过多时,打开和查询报文缓慢。可以使用wireshark相关工具对其进行拆分。

进入wireshark的安装目录,可以看到有一个editcap的程序。

# -c 指定多少个包拆分为一个文件, src待拆分的包文件路径及文件名,dst是拆分后的文件路径及文件名
editcap.exe -c 200000 src dst

合并包

Mergecap 从名字上 可以看出该命令的功能是合并多个报文为一个报文。

C:\Program Files\Wireshark>mergecap -h
Mergecap (Wireshark) 3.4.2 (v3.4.2-0-ga889cf1b1bf9)
Merge two or more capture files into one.
See https://www.wireshark.org for more information.

Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]

Output:
  -a                concatenate rather than merge files.
                    default is to merge based on frame timestamps.
  -s <snaplen>      truncate packets to <snaplen> bytes of data.
  -w <outfile>|-    set the output filename to <outfile> or '-' for stdout.
  -F <capture type> set the output file type; default is pcapng.
                    an empty "-F" option will list the file types.
  -I <IDB merge mode> set the merge mode for Interface Description Blocks; default is 'all'.
                    an empty "-I" option will list the merge modes.

Miscellaneous:
  -h                display this help and exit.
  -v                verbose output.
mergecap -v -a -F pcap -w test.pcap
mergecap -v -a userip-1.pcap userip-2.pcap -F pcap -w merge_out.pcap
    -v参数表示打印每一片报文的编号,报文较多的话会打屏。
    -a参数表示的是按照文件中顺序将报文进行合并,默认情况是按照时间戳的顺序进行合并。由于每一片报文头部都是由有时间戳信息的,不明白的可以查看这里。
    -F表示文件的存储格式,例如pcap,pcapng等等。在-F后面参数为空的情况下,会列出该命令支持的所有文件格式,对照选择即可。
    -w即文件输出的

合并一定比例的包

使用python脚本构造固定比例的包,如1:1024等。

import os

base_file = "5gc.pcap"
arp_file = "arp_request.pcap"
ratio = 0
dst_file = ""
pwd = os.curdir
for num in range(2,20):
    ratio = pow(2,num)
    copy_file = "5gc_copy_" + str(num) + ".pcap"
    os.system('copy %s %s' % (base_file, copy_file))
    dst_file = "5gc_" + str(ratio) + ".pcap"
    os.system("mergecap.exe -v -a %s %s -F pcap -w %s" % (base_file, copy_file, dst_file))
    base_file = dst_file
    dst_arp_file = "5gc_arp_1_" + str(ratio) + ".pcap"
    os.system("mergecap.exe -v -a %s %s -F pcap -w %s" % (base_file, arp_file, dst_arp_file))

查询包

一般获取抓包文件后,需要使用wireshark对包进行查询和分析。常见的查询命令如下:

tcp # 过滤tcp报文
ip.addr == 127.0.0.1 # 根据ip地址过滤
tcp.flags.reset == 1 # 过滤reset报文
tcp.port == 8080 # 根据端口过滤
tcp.len > 0 # 根据tcp数据长度过滤

reference

1.https://www.freesion.com/article/9386244731/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

growdu_real

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值