使用 python 和 pyshark 跟踪 TCP 流
Follow TCP stream using python and pyshark
在 Wireshark 中手动执行时,我右键单击数据包 -> 跟随 -> TCP 流
将打开一个包含相关信息的新 window。
有没有办法通过使用 pyshark 模块和 python 2.7 来做完全相同的事情并获取此信息?
注意:我通过发送无效的 HTTP 方法进行请求测试,因此寻找 HTTP 层在这里不起作用。
我不知道 pyshark。
但也许 https://jon.oberheide.org/pynids/ 会起作用,因为它也使用 Python:
pynids is a python wrapper for libnids, a Network Intrusion Detection System library offering sniffing, IP defragmentation, TCP stream reassembly and TCP port scan detection. Let your own python routines examine network conversations.
我没有使用 pynids 的个人经验,但我使用它的底层库 nids 取得了很多成功,您可以在 http://libnids.sourceforge.net/
尽管名称为网络入侵检测系统,但它的用途远不止检测网络入侵。它本质上是一个帮助您重新组装 TCP 流的库,例如 Wireshark 的 Follow TCP Stream。
虽然 nids 很棒,但它确实需要 TCP 流的开头在您的捕获文件中。如果您丢失了开头而无法捕获它,tshark 可能会有所帮助:
tshark 是 Wireshark 附带的命令行实用程序。例如:
tshark -r t.pcap -q -z follow,tcp,ascii,18
它向 stdout 输出与您在 Wireshark 的 GUI 的 Follow TCP Stream 中看到的相同的内容 window。
上面命令中的18是流索引。要确定在那里使用的数字,您可以让 python 脚本从 0 开始迭代并打印出每个流,直到找到正确的流。
另一种查找流索引的方法是首先在 Wireshark 中单击感兴趣的流中的数据包。然后展开传输控制协议部分以显示流索引,如下图所示:
How to find a stream index
是的,您可以使用 python 和 pyshark 跟踪 TCP 流。下面是一个基本的概念证明。
"""
Follow a TCP stream with pyshark.
"""
import pyshark
# Change FILENAME to your pcap file's name.
FILENAME = "myfile.pcap"
# Change STREAM_NUMBER to the stream number you want to follow.
STREAM_NUMBER = 0
# open the pcap file, filtered for a single TCP stream
cap = pyshark.FileCapture(
FILENAME,
display_filter='tcp.stream eq %d' % STREAM_NUMBER)
while True:
try:
p = cap.next()
except StopIteration: # Reached end of capture file.
break
try:
# print data from the selected stream
print(p.data.data.binary_value)
except AttributeError: # Skip the ACKs.
pass
我验证了上面的代码适用于 python 2.7.13 和 python 3.6.6。
注意: 自 pyshark only support python 3.5+, if you must use python 2.7, you're stuck with the pyshark-legacy pip 包的较新版本以来。
在 Wireshark 中手动执行时,我右键单击数据包 -> 跟随 -> TCP 流 将打开一个包含相关信息的新 window。 有没有办法通过使用 pyshark 模块和 python 2.7 来做完全相同的事情并获取此信息? 注意:我通过发送无效的 HTTP 方法进行请求测试,因此寻找 HTTP 层在这里不起作用。
我不知道 pyshark。
但也许 https://jon.oberheide.org/pynids/ 会起作用,因为它也使用 Python:
pynids is a python wrapper for libnids, a Network Intrusion Detection System library offering sniffing, IP defragmentation, TCP stream reassembly and TCP port scan detection. Let your own python routines examine network conversations.
我没有使用 pynids 的个人经验,但我使用它的底层库 nids 取得了很多成功,您可以在 http://libnids.sourceforge.net/
尽管名称为网络入侵检测系统,但它的用途远不止检测网络入侵。它本质上是一个帮助您重新组装 TCP 流的库,例如 Wireshark 的 Follow TCP Stream。
虽然 nids 很棒,但它确实需要 TCP 流的开头在您的捕获文件中。如果您丢失了开头而无法捕获它,tshark 可能会有所帮助:
tshark 是 Wireshark 附带的命令行实用程序。例如:
tshark -r t.pcap -q -z follow,tcp,ascii,18
它向 stdout 输出与您在 Wireshark 的 GUI 的 Follow TCP Stream 中看到的相同的内容 window。
上面命令中的18是流索引。要确定在那里使用的数字,您可以让 python 脚本从 0 开始迭代并打印出每个流,直到找到正确的流。
另一种查找流索引的方法是首先在 Wireshark 中单击感兴趣的流中的数据包。然后展开传输控制协议部分以显示流索引,如下图所示: How to find a stream index
是的,您可以使用 python 和 pyshark 跟踪 TCP 流。下面是一个基本的概念证明。
"""
Follow a TCP stream with pyshark.
"""
import pyshark
# Change FILENAME to your pcap file's name.
FILENAME = "myfile.pcap"
# Change STREAM_NUMBER to the stream number you want to follow.
STREAM_NUMBER = 0
# open the pcap file, filtered for a single TCP stream
cap = pyshark.FileCapture(
FILENAME,
display_filter='tcp.stream eq %d' % STREAM_NUMBER)
while True:
try:
p = cap.next()
except StopIteration: # Reached end of capture file.
break
try:
# print data from the selected stream
print(p.data.data.binary_value)
except AttributeError: # Skip the ACKs.
pass
我验证了上面的代码适用于 python 2.7.13 和 python 3.6.6。
注意: 自 pyshark only support python 3.5+, if you must use python 2.7, you're stuck with the pyshark-legacy pip 包的较新版本以来。