gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:22:25 26 4
gpt4 key购买 nike

我尝试计算来 self 的服务器的 GET 请求。

我使用 tshark

我运行以下命令来过滤传入流量并仅获取 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 分钟内我得到了 950M 的 pcap 文件。解析它大约需要 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"'

(将初始 tcpdump 替换为 tshark 会导致我的本地系统出现此错误: 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

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

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 的文件中

关于linux - 如何在写入文件之前过滤 tshark 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16058088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com