tshark使用记录

https://segmentfault.com/a/1190000018886363

https://segmentfault.com/a/1190000018886731

Tshark

详细参数参见tshark的manpage。

// 列出可监听流量的网络接口列表。tshark使用1,2,...等数字来标识eth0,eth1...
# tshark -D

// 监听接口eth0上的UDP端口为1234的流量
# tshark -f "udp port 1234" -i 1

tshark的强悍之处在于对协议进行完全解码,甚至对分片的TCP包进行重组再行解码,例如

// 监听接口eth0上目标端口为80的http流量,并将http请求头的host和location打印
# tshark -f "dst port 80" -T fields -e http.host -e http.location -i 1
其中 -f 参数指定过滤表达式(即等同tcpdump的 filter_expression)
-T fields 指定屏幕输出信息类型为指定的协议字段(用-e添加指定字段),仅在wireshark的0.99.6以后的版本支持。
-i 1为指定监听的网络接口为1号

// 监听http流量,仅过滤GET请求, 监听10秒钟,打印出HTTP HOST和URL
c:\Program Files\Wireshark\tshark.exe -i 4 -n -f "tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420" -T fields -e http.host -e http.request.uri -a duration:10

 

实时打印当前mysql查询语句

tshark -s 512 -i eth1 -n -f 'tcp dst port 3306' -R 'mysql.query' -T fields -e mysql.query

说明:

  • -s 512 :只抓取前512个字节数据
  • -i eth0 :监听eth0网卡
  • -n :禁止域名解析
  • -f ‘tcp dst port 3306’ :只捕捉协议为tcp,目的端口为3306的数据包
  • -R ‘mysql.query’ :过滤出mysql.query查询语句的报文
  • -T fields -e mysql.query :打印mysql查询语句

实时打印当前http请求的url(包括域名)

tshark -s 512 -i eth1 -n -f 'tcp dst port 8000' -R 'http.host and http.request.uri' -T fields -e http.host -e http.request.uri -l | tr -d 't'

说明:

  • -s 512 :只抓取前512个字节数据
  • -i eth1 :监听eth1网卡
  • -n :禁止网络对象名称解析
  • -f ‘tcp dst port 8000’ :只捕捉协议为tcp,目的端口为8000的数据包
  • -R ‘http.host and http.request.uri’ :过滤出http.host和http.request.uri
  • -T fields -e http.host -e http.request.uri :打印http.host和http.request.uri
  • -l :输出到标准输出

读取之前抓包文件进行报文数据分析

需要从抓包的文件evidence04.pcap中提取出报文相关数据信息,如时间、源IP、目的IP、协议名、源Port、标Port、包大小等信息,最后输出到csv文件。

tshark -r evidence.pcap -T fields -e frame.time_relative -e ip.src -e ip.dst -e ip.proto -e tcp.srcport -e tcp.dstport -e frame.len -E header=n -E separator=, -E quote=n -E occurrence=f > output.csv

说明:

  • -r evidence.pcap 需要分析的报文记录文件(pcap格式)
  • -T fields 输出格式,选fields按字段,也可以选json等其他格式,需结合-e 及 -E使用
  • -e frame.time_relative 取出封包的相对时间
  • -e ip.src 提取源IP
  • -e ip.dst 提取目的IP
  • -e ip.proto 提取协议名
  • -e tcp.srcport 提取源Port
  • -e tcp.dstport 提取目的Port
  • -e frame.len 提取数据帧大小
  • -E header=n 是否输出字段名称(cvs的第1行)
  • -E separator=, 指定分割符,/t是tab,/s是一格空格
  • -E quote=n 指定是否对字段用引号,d是双引号,s是单引号,n是不用
  • -E occurrence=f 多值时是否保留,f是第一个值,l是最后一个值,a是所有值都列出,默认全部
  • output.csv 输出文件路径及名称

DNS报文过滤

使用tshark过滤dns cap包中源ip、目的ip、request请求

tshark -r test.cap -T fields -e frame.time -e ip.src -e ip.dst -e dns.qry.name -R 'udp.dstport==53 || dns'

说明:

  • -r test.pcap 需要分析的报文记录文件(pcap格式)
  • -T fields 输出格式,选fields按字段,也可以选json等其他格式,需结合-e 及 -E使用
  • -e frame.time 提取数据帧时间
  • -e ip.src 提取源IP
  • -e ip.dst 提取目的IP
  • -e dns.qry.name 提取dns查询的域名信息
  • -R 'udp.dstport==53 || dns' 显示过滤,仅对udp目标端口为53或者dns协议的报文进行处理
  • 默认直接显示在终端上,不记录文件。

editcap命令

To remove vlan tags from all packets within an Ethernet-encapsulated capture file, use:

    editcap -L -C 12:4 capture_vlan.pcapng capture_no_vlan.pcapng

To chop both the 10 byte and 20 byte regions from the following 75 byte packet in a single pass, use any of the 8 possible methods provided below:

    <--------------------------- 75 ---------------------------->

    +---+-------+-----------+---------------+-------------------+
    | 5 |   10  |     15    |       20      |         25        |
    +---+-------+-----------+---------------+-------------------+

    1) editcap -C 5:10 -C -25:-20 capture.pcapng chopped.pcapng
    2) editcap -C 5:10 -C 50:-20 capture.pcapng chopped.pcapng
    3) editcap -C -70:10 -C -25:-20 capture.pcapng chopped.pcapng
    4) editcap -C -70:10 -C 50:-20 capture.pcapng chopped.pcapng
    5) editcap -C 30:20 -C -60:-10 capture.pcapng chopped.pcapng
    6) editcap -C 30:20 -C 15:-10 capture.pcapng chopped.pcapng
    7) editcap -C -45:20 -C -60:-10 capture.pcapng chopped.pcapng
    8) editcap -C -45:20 -C 15:-10 capture.pcapng chopped.pcapng

text2pcap

可以用来封装数据包头到自己的原始text上。用来构造网络包

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章