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上。用來構造網絡包

 

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