跳转到主要内容

用户友好的数据包捕获

项目描述

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 (4.8 kB 查看哈希值)

上传时间

构建分发

capture_packets-0.1.2-py3-none-any.whl (5.3 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面