如何在写入文件之前过滤tshark结果?

Max*_*tin 5 linux tshark

我尝试GET从我的服务器计算请求.

我用tshark.

我运行follow命令来过滤传入流量并仅获取GET请求:

/usr/sbin/tshark   -b filesize:1024000  -b files:1  \
'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' \
-w samples.pcap -R 'http.request.method == "GET"'  
Run Code Online (Sandbox Code Playgroud)

如您所见,我定义了将过滤结果存储到1个文件,最大大小为1G,名称为:samples.pcap.

问题是,当我尝试打开pcap文件时,我看到tshark stored all traffic there:

3245 172.692247  1.1.1.1 -> 2.2.2.2 HTTP [TCP Retransmission] Continuation or non-HTTP traffic
3246 172.730928  1.1.1.1 -> 2.2.2.2 HTTP Continuation or non-HTTP traffic
3247 172.731944  1.1.1.1 -> 2.2.2.2 HTTP Continuation or non-HTTP traffic
3248 172.791934  1.1.1.1 -> 2.2.2.2  HTTP GET /services/client/client.php?cnc=13 HTTP/1.1
3249 172.825303  1.1.1.1 -> 2.2.2.2 HTTP HTTP/1.1 200 OK [Unreassembled Packet [incorrect TCP checksum]]
3250 172.826329  1.1.1.1 -> 2.2.2.2 HTTP Continuation or non-HTTP traffic
3251 172.826341  1.1.1.1 -> 2.2.2.2 HTTP Continuation or non-HTTP traffic
3252 172.826347  1.1.1.1 -> 2.2.2.2 HTTP Continuation or non-HTTP traffic
3253 172.826354  1.1.1.1 -> 2.2.2.2 HTTP Continuation or non-HTTP traffic
3254 172.826359  1.1.1.1 -> 2.2.2.2 HTTP Continuation or non-HTTP traffic
Run Code Online (Sandbox Code Playgroud)

我有很大的流量,在10分钟内我得到pcap文件大小950M.解析它需要大约4分钟.

有趣的是当我尝试运行它而不将它存储到本地文件(但在/ tmp下):

/usr/sbin/tshark \
'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' \
-R 'http.request.method == "GET"':

3.776587 1.1.1.1 -> 2.2.2.2  HTTP GET /services/client/client.php?cnc=13 HTTP/1.1
4.775624 1.1.1.1 -> 2.2.2.2  HTTP GET /services/client/clsWebClient.php HTTP/1.1
8.804702 1.1.1.1 -> 2.2.2.2  HTTP GET /services/client/client.php?cnc=13 HTTP/1.1
Run Code Online (Sandbox Code Playgroud)

它工作,但在这种情况下,我有/ tmp几个临时文件与巨大的1G +.

我错过了什么?

谢谢

================================================== =====

编辑

拉尔斯要求补充说-f:

sudo /usr/sbin/tshark   -T fields -e 'http.request.uri contains "cnc=13"'  \
         -b filesize:1024000  -b files:1  \
         -f 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'  \
         -w samples.pcap
Run Code Online (Sandbox Code Playgroud)

没有帮助,仍然samples.pcap存储所有流量:

 74   6.908388  172.20.0.23 -> 89.78.170.96 HTTP Continuation or non-HTTP traffic
 75   6.908394  172.20.0.23 -> 89.78.170.96 HTTP Continuation or non-HTTP traffic
Run Code Online (Sandbox Code Playgroud)

Emi*_*ben 4

当您想要组合 -w 和 bpf 数据包过滤器(即您在 -f 上放置的内容)时,这似乎有效:

 tcpdump -nli en1 -w - 'tcp port 80' | tshark -i - -R'http.request.method == "GET"'
Run Code Online (Sandbox Code Playgroud)

(用 tshark 替换初始 tcpdump 会导致我的本地系统出现此错误: tshark: Unrecognized libpcap format )

自版本 1.4.0 起,在捕获(或从捕获中读取)并再次写出结果时,似乎不再支持保存读取过滤器 (-R) 的结果(请参阅:http://ask.wireshark .org/questions/10397/read-filters-arent-supported-when-capturing-and- saving-the-captured-packets)。据推测,1.4.0 之前的版本将允许写入 pcap 并限制输出-b(尚未测试)。

如果您只想要 -R 的文本输出(而不是 pcap 输出)。我认为上面的命令将是您的解决方案。

要限制您的输出(即您提到您只想取样),您可以head -c <bytes>在处理管道中的任何点使用:

tcpdump -nli en1 -w - 'tcp port 80' | \
  tshark -i - -R'http.request.method == "GET"' | \
  head -c 1024000 > output.txt
Run Code Online (Sandbox Code Playgroud)

生成一个名为 output.txt 的 1024000 字节文本输出文件或

tcpdump -nli en1 -w - 'tcp port 80' | \
  head -c 1024000 | \
  tshark -i - -R'http.request.method == "GET"' > output.txt
Run Code Online (Sandbox Code Playgroud)

处理针对 TCP 端口 80 预过滤的 102400 字节 pcap 输入,并将文本输出放入名为 output.txt 的文件中