加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:00:54 所属栏目:Linux 来源:网络整理
导读:我尝试从我的服务器计算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.r
我尝试从我的服务器计算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"'

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

问题是,当我尝试打开pcap文件时,我看到tshark存储了所有流量:

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

我有很大的流量,在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

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

我错过了什么?

谢谢

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

编辑

Lars要求添加-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

没有帮助,仍然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

解决方法

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

tcpdump -nli en1 -w - 'tcp port 80' | tshark -i - -R'http.request.method == "GET"'

(用tshark替换初始tcpdump会导致本地系统出现此错误:
?????tshark:无法识别的libpcap格式)

从版本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

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

tcpdump -nli en1 -w - 'tcp port 80' | 
  head -c 1024000 | 
  tshark -i - -R'http.request.method == "GET"' > output.txt

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读