seladb/PcapPlusPlus

GitHub: seladb/PcapPlusPlus

一个跨平台 C++ 网络数据包捕获、解析与构造库,封装多种高性能捕获引擎并支持广泛的协议栈。

Stars: 3067 | Forks: 733

[![PcapPlusPlus Logo](https://pcapplusplus.github.io/img/logo/logo_color.png)](https://pcapplusplus.github.io) [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/seladb/PcapPlusPlus/build_and_test.yml?branch=master&label=Actions&logo=github&style=flat)](https://github.com/seladb/PcapPlusPlus/actions?query=workflow%3A%22Build+and+test%22) [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/seladb/PcapPlusPlus/codeql.yml?branch=master&label=CodeQL&logo=github&style=flat)](https://github.com/seladb/PcapPlusPlus/actions?query=workflow%3A%22CodeQL%22) [![Codecov](https://img.shields.io/codecov/c/github/seladb/PcapPlusPlus?logo=codecov&logoColor=white)](https://app.codecov.io/github/seladb/PcapPlusPlus) [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/seladb/PcapPlusPlus/badge)](https://scorecard.dev/viewer/?uri=github.com/seladb/PcapPlusPlus) [![GitHub contributors](https://img.shields.io/github/contributors/seladb/PcapPlusPlus?style=flat&label=Contributors&logo=github)](https://github.com/seladb/PcapPlusPlus/graphs/contributors)
[PcapPlusPlus](https://pcapplusplus.github.io/) 是一个多平台 C++ 库,用于网络数据包的捕获、解析和构建。它旨在提供高效、强大且易于使用的功能。 PcapPlusPlus 为多种网络协议提供解码和构建能力。它还为最流行的数据包处理引擎提供了易于使用的 C++ 封装,例如 [libpcap](https://www.tcpdump.org/)、[WinPcap](https://www.winpcap.org/)、[Npcap](https://nmap.org/npcap/)、[DPDK](https://www.dpdk.org/)、[eBPF AF_XDP](https://www.kernel.org/doc/html/next/networking/af_xdp.html)、[WinDivert](https://reqrypt.org/windivert.html) 和 [PF_RING](https://www.ntop.org/products/packet-capture/pf_ring/)。 翻译:English · [正體中文](./translation/README-zh-tw.md) · [한국어](./translation/README-kor.md) ## 目录 - [目录](#table-of-contents) - [下载](#download) - [GitHub Release 页面](#github-release-page) - [Homebrew](#homebrew) - [Vcpkg](#vcpkg) - [Conan](#conan) - [自行构建](#build-it-yourself) - [验证您的安装包](#verify-your-packages) - [功能概览](#feature-overview) - [入门指南](#getting-started) - [API 文档](#api-documentation) - [多平台支持](#multi-platform-support) - [支持的网络协议](#supported-network-protocols) - [数据链路层 (L2)](#data-link-layer-l2) - [网络层 (L3)](#network-layer-l3) - [传输层 (L4)](#transport-layer-l4) - [会话层 (L5)](#session-layer-l5) - [表示层 (L6)](#presentation-layer-l6) - [应用层 (L7)](#application-layer-l7) - [DPDK 和 PF_RING 支持](#dpdk-and-pf_ring-support) - [性能基准测试](#benchmarks) - [提供反馈](#provide-feedback) - [贡献](#contributing) - [许可证](#license) ## 下载 您可以选择从 GitHub release 页面下载,使用包管理器,或者自行构建 PcapPlusPlus。欲了解更多详情,请访问 PcapPlusPlus 网站上的[下载](https://pcapplusplus.github.io/docs/install)页面。 [![GitHub all releases](https://img.shields.io/github/downloads/seladb/PcapPlusPlus/total?style=flat&label=Downloads&logo=github)](https://tooomm.github.io/github-release-stats/?username=seladb&repository=PcapPlusPlus) ### GitHub Release 页面 ### Homebrew ``` brew install pcapplusplus ``` Homebrew formulae: ### Vcpkg Windows: ``` .\vcpkg install pcapplusplus ``` MacOS/Linux: ``` vcpkg install pcapplusplus ``` Vcpkg port: ### Conan ``` conan install "pcapplusplus/[>0]@" -u ``` ConanCenter 中的包: ### 自行构建 克隆 git 仓库: ``` git clone https://github.com/seladb/PcapPlusPlus.git ``` 请按照 PcapPlusPlus 网站上[从源码构建](https://pcapplusplus.github.io/docs/install#build-from-source)页面中针对您平台的构建说明进行操作。 ### 验证您的安装包 v23.09 之后发布的 PcapPlusPlus 版本均使用 GitHub attestation 进行签名。所有的 attestation 都可以在[这里](https://github.com/seladb/PcapPlusPlus/attestations)找到。您可以使用 GitHub CLI 验证这些包的 attestation。要验证包,您可以遵循 [gh attestation verify](https://cli.github.com/manual/gh_attestation_verify) 提供的最新说明。对于简单的说明,您可以使用以下命令: ``` gh attestation verify --repository seladb/PcapPlusPlus ``` 您应该在终端中看到以下输出: ``` ✓ Verification succeeded! ``` ## 功能概览 - __数据包捕获__:通过易于使用的 C++ 封装器支持流行的数据包捕获引擎,例如 [libpcap](https://www.tcpdump.org/)、[WinPcap](https://www.winpcap.org/)、[Npcap](https://nmap.org/npcap/)、[Intel DPDK](https://www.dpdk.org/)、[eBPF AF_XDP](https://www.kernel.org/doc/html/next/networking/af_xdp.html)、[WinDivert](https://reqrypt.org/windivert.html)、[ntop’s PF_RING](https://www.ntop.org/products/packet-capture/pf_ring/) 和 [raw sockets](https://en.wikipedia.org/wiki/Network_socket#Raw_socket) [[了解更多](https://pcapplusplus.github.io/docs/features#packet-capture)] - __数据包解析和构建__:包括对协议和层的详细分析,以及针对多种[网络协议](https://pcapplusplus.github.io/docs/features#supported-network-protocols)的数据包生成和编辑 [[了解更多](https://pcapplusplus.github.io/docs/features#packet-parsing-and-crafting)] - __从/向文件读写数据包__:支持 __PCAP__ 和 __PCAPNG__ 两种格式 [[了解更多](https://pcapplusplus.github.io/docs/features#read-and-write-packets-fromto-files)] - __线速数据包处理__:通过针对 [DPDK](https://www.dpdk.org/)、[eBPF AF_XDP](https://www.kernel.org/doc/html/next/networking/af_xdp.html) 和 [PF_RING](https://www.ntop.org/products/packet-capture/pf_ring/) 的高效且易于使用的 C++ 封装器实现 [[了解更多](https://pcapplusplus.github.io/docs/features#dpdk-support)] - __多平台支持__ - PcapPlusPlus 完全支持 Linux、MacOS、Windows、Android 和 FreeBSD - __数据包重组__ - __TCP 重组__ 的独特实现,包括 TCP 重传、TCP 乱序包和 TCP 数据丢失处理,以及用于创建和重组 IPv4 和 IPv6 分片的 __IP 分片与重组__ [[了解更多](https://pcapplusplus.github.io/docs/features#packet-reassembly)] - __数据包过滤__:让 libpcap 的 BPF 过滤器更加用户友好 [[了解更多](https://pcapplusplus.github.io/docs/features#packet-filtering)] - __TLS 指纹识别__ - [JA3 和 JA3S](https://github.com/salesforce/ja3) TLS 指纹识别的 C++ 实现 [[了解更多](https://pcapplusplus.github.io/docs/features#tls-fingerprinting)] ## 入门指南 使用 PcapPlusPlus 编写应用程序非常简单直观。以下是一个简单的应用程序示例,展示了如何从 PCAP 文件读取数据包并对其进行解析: ``` #include #include "IPv4Layer.h" #include "Packet.h" #include "PcapFileDevice.h" int main(int argc, char* argv[]) { // open a pcap file for reading pcpp::PcapFileReaderDevice reader("1_packet.pcap"); if (!reader.open()) { std::cerr << "Error opening the pcap file" << std::endl; return 1; } // read the first (and only) packet from the file pcpp::RawPacket rawPacket; if (!reader.getNextPacket(rawPacket)) { std::cerr << "Couldn't read the first packet in the file" << std::endl; return 1; } // parse the raw packet into a parsed packet pcpp::Packet parsedPacket(&rawPacket); // verify the packet is IPv4 if (parsedPacket.isPacketOfType(pcpp::IPv4)) { // extract source and dest IPs pcpp::IPv4Address srcIP = parsedPacket.getLayerOfType()->getSrcIPv4Address(); pcpp::IPv4Address destIP = parsedPacket.getLayerOfType()->getDstIPv4Address(); // print source and dest IPs std::cout << "Source IP is '" << srcIP << "'; Dest IP is '" << destIP << "'" << std::endl; } // close the file reader.close(); return 0; } ``` 您可以在 PcapPlusPlus 网站的[入门指南](https://pcapplusplus.github.io/docs/quickstart)页面中找到更多信息。该页面将通过几个简单的步骤引导您启动并运行一个应用程序。 ## API 文档 PcapPlusPlus 由 3 个库组成: 1. __Packet++__ - 用于解析、创建和编辑网络数据包的库 2. __Pcap++__ - 用于拦截和发送数据包的库,提供网络和 NIC 信息、统计等。它实际上是为 libpcap、WinPcap、Npcap、DPDK、AF_XDP、WinDivert 和 PF_RING 等数据包捕获引擎提供的 C++ 封装 3. __Common++__ - 包含 Packet++ 和 Pcap++ 共用的一些通用代码工具的库 您可以在 PcapPlusPlus 网站的 [API 文档部分](https://pcapplusplus.github.io/docs/api)中找到详尽的 API 文档。 如果您发现任何缺失的数据,请[联系我们](#provide-feedback)。 ## 多平台支持 PcapPlusPlus 目前支持以下平台: __Windows__ , __Linux__ , __MacOS__ , __Android__ 和 __FreeBSD__ . 请访问 PcapPlusPlus 网站查看所有[支持的平台](https://pcapplusplus.github.io/docs/platforms),并参考[下载](#download)部分以开始在您的平台上使用 PcapPlusPlus。 ## 支持的网络协议 PcapPlusPlus 目前支持解析、编辑和创建以下协议的数据包: ### 数据链路层 (L2) 1. Cisco HDLC 2. Ethernet II 3. IEEE 802.3 Ethernet 4. LLC (仅支持 BPDU) 5. Null/Loopback 6. Packet trailer (也称为 footer 或 padding) 7. PPPoE 8. SLL (Linux cooked capture) 9. SLL2 (Linux cooked capture v2) 10. STP 11. VLAN 12. VXLAN 13. Wake on LAN (WoL) 14. NFLOG (Linux Netfilter NFLOG) - 仅解析 (无编辑功能) ### 网络层 (L3) 15. ARP 16. GRE 17. ICMP 18. ICMPv6 19. IGMP (支持 IGMPv1, IGMPv2 和 IGMPv3) 20. IPv4 21. IPv6 22. MPLS 23. NDP 24. Raw IP (IPv4 & IPv6) 25. VRRP (IPv4 & IPv6) 26. WireGuard ### 传输层 (L4) 27. COTP 28. GTP (v1 & v2) 29. IPSec AH & ESP - 仅解析 (无编辑功能) 30. TCP 31. TPKT 32. UDP ### 会话层 (L5) 33. SDP 34. SIP ### 表示层 (L6) 35. SSL/TLS - 仅解析 (无编辑功能) ### 应用层 (L7) 36. ASN.1 decoder and encoder 37. BGP (v4) 38. Cryptographic key decoders 39. DHCP 40. DHCPv6 41. DNS 42. DoIP 43. FTP 44. HTTP headers (请求 & 响应) 45. LDAP 46. Modbus 47. NTP (v3, v4) 48. PEM decoder and encoder 49. Radius 50. S7 Communication (S7comm) 51. SMTP 52. SOME/IP 53. SSH - 仅解析 (无编辑功能) 54. Telnet - 仅解析 (无编辑功能) 55. X509 certificates - 仅解析 (无编辑功能) 56. Generic payload ## DPDK 和 PF_RING 支持 [The Data Plane Development Kit (DPDK)](https://www.dpdk.org/) 是一组数据平面库和网络接口控制器驱动程序,用于快速数据包处理。 [PF_RING™](https://www.ntop.org/products/packet-capture/pf_ring/) 是一种新型网络套接字,可显著提高数据包捕获速度。 这两个框架都提供了非常快速的数据包处理(高达线速),并用于许多网络应用程序,如路由器、防火墙、负载均衡器等。 PcapPlusPLus 在 DPDK 和 PF_RING 之上提供了一个 C++ 抽象层。该抽象层提供了一个易于使用的接口,消除了使用这些框架时涉及的大量样板代码。您可以访问 PcapPlusPlus 网站上的 [DPDK](https://pcapplusplus.github.io/docs/dpdk) 和 [PF_RING](https://pcapplusplus.github.io/docs/features#pf_ring-support) 支持页面了解更多信息。 ## 性能基准测试 我们使用 Matias Fontanini 的 [packet-capture-benchmarks](https://github.com/mfontanini/packet-capture-benchmarks) 项目将 PcapPlusPlus 与其他类似的 C++ 库(如 `libtins` 和 `libcrafter`)进行了性能比较。 您可以在 PcapPlusPlus 网站的[性能基准测试](https://pcapplusplus.github.io/docs/benchmark)页面中查看结果。 ## 提供反馈 我们非常乐意收到您的反馈,请随时通过以下任何方式联系我们: - 提交 GitHub issue - 在 PcapPlusPlus Google group 发布消息: - 在 Stack Overflow 上提问: - 发送邮件至: - 在 X 上关注我们: 请访问 [PcapPlusPlus 网站](https://pcapplusplus.github.io/community)了解更多信息。 ## 贡献 我们非常感谢对本项目的任何贡献。如果您有兴趣做出贡献,请访问 PcapPlusPlus 网站上的[贡献页面](https://pcapplusplus.github.io/community#contribute)。 ## 许可证 PcapPlusPlus 根据 [Unlicense license](https://choosealicense.com/licenses/unlicense/) 发布。 [![GitHub](https://img.shields.io/github/license/seladb/PcapPlusPlus?style=flat&color=blue&logo=unlicense)](https://choosealicense.com/licenses/unlicense/)
标签:AF_XDP, C++, DPDK, IP 地址批量处理, Npcap, PF_RING, Wildcard支持, WinDivert, WinPcap, 内核驱动, 协议解码, 嗅探, 开源库, 搜索引擎爬虫, 数据包构造, 数据包解析, 数据擦除, 系统分析, 网络分析, 网络协议, 网络安全, 网络库, 网络数据包, 网络编程, 防御绕过, 隐私保护