python-2.7 - 使用 python 和 pyshark 跟踪 TCP 流
问题描述
在 Wireshark 中手动执行此操作时,我右键单击一个数据包 -> 跟随 -> TCP 流,将打开一个包含相关信息的新窗口。有没有办法通过使用 pyshark 模块和 python 2.7 来做同样的事情并获取这些信息?注意:我通过发送无效的 HTTP 方法进行请求测试,因此在这里寻找 HTTP 层不起作用。
解决方案
是的,您可以使用 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只支持 python 3.5+,如果你必须使用 python 2.7,你会被pyshark-legacy pip 包卡住。
推荐阅读
- mysql - MySQL 中的 ON DUPLICATE KEY UPDATE 是否会导致使用额外的磁盘空间?
- android - 插件 `google_map_location_picker` 使用了已弃用的 Android 嵌入版本
- ruby-on-rails - 访问 StandardError 哈希
- azure-devops - 有关新测试失败的 Azure 管道电子邮件通知
- vue.js - 输入占位符值莫名其妙地移动到其父元素
- python - 在将数据导入 MySQL 之前,在 Python 中的数据清理活动期间将所有反斜杠“\”替换为前斜杠“/”
- flutter - 如何在flutter中自定义水平时间选择器?
- laravel - Wordpress 面临的帖子名称永久链接问题无法正常工作,但在将网站迁移到 live aws elastic beanstalk 后,普通链接正在工作
- android - 我可以做什么?在这个错误中 Fastlane [!] Google Api Error: Invalid request - UploadException:ADHOC_PACKAGE_NAME_MISMATCH [[]]
- fortify - 强化报告生成器