用户友好的数据包捕获
项目描述
capture-packets: 用户友好的数据包捕获
使用前请阅读
您的机器上发生的所有网络流量都会被捕获(除非您指定了更具体的接口,默认为所有接口)。在capture_packets
中发生的任何TLS握手都会将其机密信息导出,以便解密数据包捕获中的TLS流量。不在capture_packets
上下文管理器中发生的任何TLS握手都不会受到影响。
不要将此数据发送给您不信任的人。如果您正在使用任何身份验证,那些机密信息很可能会包含在数据包捕获中。您应该在捕获数据包后更换凭据,以确保没有服务被破坏的风险。使用安全通道(如magic-wormhole)分发这些文件。
任何收到此实用程序文件的维护者应在完成所需任务后尽快删除它们。
安装和说明
要使用此库,您必须安装tshark的dumpcap
实用程序。 了解如何安装dumpcap。
现在我们从PyPI安装Python包
$ python -m pip install capture-packets
安装完成后,我们创建一个脚本并将有问题的代码放在capture_packets
上下文管理器中
from capture_packets import capture_packets
# Wrap *all* of your networking code
# in the capture_packets() context manager:
with capture_packets() as pcap:
# You put the code that you want to capture below here:
import urllib3
http = urllib3.PoolManager()
http.request("GET", "https://service-that-is-not.working")
# By the way, it's okay if an error happens in here. The
# context manager still works and outputs the paths to stdout.
# After capturing you can explore the captured packets:
from scapy.layers.dns import DNSQR
packets = pcap.packets(layers=DNSQR)
assert packets[0][DNSQR].qname == b"service-that-is-not.working."
一旦您看到最后一条消息,您的数据包已捕获并存储在显示的路径(在上述示例中,在/tmp/tmpcaxb58kt/captured-packets.tar
)中。您可以向请求数据包的维护者发送这个压缩包。
重要:请确保所有代码都包含在 capture_packets()
上下文中。否则,配置 TLS 密钥的至关重要的设置步骤可能会被遗漏。
import urllib3
# === DON'T DO THIS ===
# This won't work because TLS will get
# configured outside the context manager.
http = urllib3.HTTPSConnectionPool("service-that-is-not.working", 443)
with capture_packets():
http.request("GET", "/")
# === DO THIS INSTEAD ===
with capture_packets():
# TLS is configured within capture_packets() block :tada:
http = urllib3.HTTPSConnectionPool("service-that-is-not.working", 443)
http.request("GET", "/")
这有什么用?
有些网络问题没有数据包捕获是无法调试的,而用户自行捕获数据包和 TLS 密钥通常是一项艰巨的任务。这个库旨在让用户尽可能简单地捕获数据包,同时仍然全面。
支持哪些库?
如果没有使用 TLS,那么理论上任何网络库都可以工作。
如果使用 TLS,则库必须支持 SSLKEYLOGFILE
环境变量,以便自动将 TLS 密钥转储。例如,urllib3、Requests 以及任何使用这两个库的 HTTP 库都将与 TLS 一起工作。
许可证
MIT
项目详情
capture-packets-0.1.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f6e54973d221d3a0319908841b4c94405c087ce7b0525471c0acb3cd00c9a2e4 |
|
MD5 | 5a9310d5116cd1ef5c87e053c50a5ec5 |
|
BLAKE2b-256 | 49d587b613617bb3bb0cb59b86a373101effd0b760ada7a091ef7e2bdf2ce958 |
capture_packets-0.1.2-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1496d5ecc961750ba1e4cefc01645553bd533f8881cf7b248d472a12797bc85d |
|
MD5 | 370a23c10b81d928e63ae20b5244674d |
|
BLAKE2b-256 | 6652952c5df9d13b8076ff7d036f61204627fc2688899509fb1fab61dfccab77 |