python多引擎pcap分析工具包。
pypcapkit的Python项目详细描述
Pypcapkit
pcapkit项目是一个开放源码的python程序,主要用于解析和分析pcap,它是一个流pcap文件提取器。支持dictdumper
,支持多种输出报告格式。
请注意,整个项目支持python 3.4或更高版本。
关于
pcapkit
是一个独立的开放源码库,仅使用dictdumper
作为其格式化输出转储程序。
有一个名为jspcapy
的项目可以在pcapkit
上工作,这是一个用于提取pcap的命令行工具,但现在不推荐使用。
与流行的PCAP文件提取程序(如scapy
、dpkt
、pyshark
等)不同,PCAPKit
使用流式处理策略读取输入文件。即逐帧读取,减少内存占用,并以某种方式提高效率。
模块结构
在pcapkit
中,所有文件可以描述为以下六个部分。
- interface(
pcapkit.interface
)--pcapkit库的用户界面,它标准化并简化了该库的使用 - 基金会(<代码> PCAKIT.BASIC < /代码>)-综合文件I/O和协议分析,协调所有网络中的信息交换K层
- 重新组装(
pcapkit.rejomby
)——基于rfc815
中描述的算法,实现IP和TCP数据包的数据报重新组装 - ipsuite(
pcapkit.ipsuite
)——internet协议套件的构造函数集合 - 协议(
pcapkit.protocols
)——所有协议系列的集合,包括详细的实现和方法 - 实用程序(
pcapkit.utilities
)——四个实用程序函数和类的集合 - coreKit(
pcapkit.coreKit
)--用于pcapkit
实现的核心实用程序 - 工具包(
pcapkit.toolkit
)——用于实现的功能工具 - dumpkit(
pcapkit.dumpkit
)--用于pcapkit
实现的转储实用程序
发动机比较
此外,由于pcapkit的复杂性,它的提取过程每包大约需要0.01秒,这还不够理想。因此,pcapkit
引入了替代的提取引擎来加速此过程。到目前为止,pcapkit
支持scapy
,dpkt
,以及pyshark
。另外,pcapkit
支持两种多处理策略(server
&;pipeline
)。有关详细信息,请参阅文档。
dpkt
0.0003609057267506917
scapy
0.0024434357844035
默认值
0.017523006995519
管道
0.0145042414863079
服务器
pyshark
0.0792640733718872
< /P>
安装
< Buff行情>注意pcapkit
支持3.4以后的python版本
只需运行以下命令即可从pypi安装当前版本:
pip install pypcapkit
或从Git存储库安装最新版本:
git clone https://github.com/JarryShaw/PyPCAPKit.git cd pypcapkit pip install -e . # and to update at any time git pull
而且由于PCAPKit支持各种提取引擎和广泛的插件功能,因此您可能需要安装可选引擎:
# for DPKT only pip install pypcapkit[DPKT]# for Scapy only pip install pypcapkit[Scapy]# for PyShark only pip install pypcapkit[PyShark]# and to install all the optional packages pip install pypcapkit[all]# or to do this explicitly pip install pypcapkit dpkt scapy pyshark
< /P>
用法
文档
接口
<表><广告>跟踪
宏
格式
<表><广告>json
plist
树
pcap
层
<表><广告>raw
链接
应用程序
发动机
<表><广告>名称 说明
< /广告><正文>默认引擎
mpserver
具有服务器处理策略的多处理引擎
具有流水线策略的多处理引擎
引擎
引擎
引擎
协议
<表><广告>名称 说明
<泰德><正文>无有效载荷 原始数据包数据 arp
地址解析协议
以太网
以太网协议
第二层隧道协议
首先打开最短路径 rarp
反向地址解析协议
vlan
802.1q客户VLAN标记类型
验证标题 主机标识协议
IPv6逐跳选项 ip
互联网协议
ipsec
互联网协议安全性
ipv4
互联网协议版本4
ipv6
互联网协议版本6
ipv6框架
IPv6的片段头
IPv6的目标选项
ipv6路由
IPv6的路由头 网络数据包交换
mh
机动性标题 tcp
传输控制协议
用户数据报协议
http
超文本传输协议
mpserver
arp
以太网
rarp
vlan
ip
ipsec
ipv4
ipv6
ipv6框架
ipv6路由
mh
tcp
http
文档可以在pcapkit
的子模块中找到。或者,您可以在test
文件夹中找到使用示例。有关详细信息,请参阅源代码--docstrings应该可以帮助您:)
ps:help
python中的函数应该总能帮助您解决问题。
cli用法
< Buff行情>以下部分最初在jspcapy
中描述,该部分现已弃用并合并到此存储库中。
如帮助手册中所示,它非常易于使用:
$ pcapkit --help
usage: pcapkit [-h] [-V] [-o file-name] [-f format] [-j] [-p] [-t] [-a] [-v]
[-F] [-E PKG] [-P PROTOCOL] [-L LAYER]
input-file-name
PCAP file extractor and formatted exporter
positional arguments:
input-file-name The name of input pcap file. If ".pcap" omits, it will
be automatically appended.
optional arguments:
-h, --help show this help message and exit
-V, --version show program's version number and exit
-o file-name, --output file-name
The name of input pcap file. If format extension
omits, it will be automatically appended.
-f format, --format format
Print a extraction report in the specified output
format. Available are all formats supported by
dictdumper, e.g.: json, plist, and tree.
-j, --json Display extraction report as json. This will yield
"raw" output that may be used by external tools. This
option overrides all other options.
-p, --plist Display extraction report as macOS Property List
(plist). This will yield "raw" output that may be used
by external tools. This option overrides all other
options.
-t, --tree Display extraction report as tree view text. This will
yield "raw" output that may be used by external tools.
This option overrides all other options.
-a, --auto-extension If output file extension omits, append automatically.
-v, --verbose Show more information.
-F, --files Split each frame into different files.
-E PKG, --engine PKG Indicate extraction engine. Note that except default
engine, all other engines need support of corresponding
packages.
-P PROTOCOL, --protocol PROTOCOL
Indicate extraction stops after which protocol.
-L LAYER, --layer LAYER
Indicate extract frames until which layer.
在大多数情况下,您应该指明输入PCAP文件的名称(扩展名可能省略),并且至少应指明输出格式(json
、plist
或tree
)。一旦未指定格式,输出文件的名称必须具有正确的扩展名(*.json
,*.plist
,或*.txt
),否则将引发格式错误
。
对于verbose
模式,提取时将打印详细信息(如下示例)。和自动扩展名
标志对输出文件有效,指示是否应附加扩展名。
< /P>
样品
使用示例
如test
文件夹中所述,pcapkit
非常容易使用,只需三个动词作为其主界面。下面显示了几种情况。
提取一个pcap文件并将结果转储到一个特定的文件(不需要重新组合)
importpcapkit# dump to a PLIST file with no frame storage (property frame disabled)plist=pcapkit.extract(fin='in.pcap',fout='out.plist',format='plist',store=False)# dump to a JSON file with no extension auto-completejson=pcapkit.extract(fin='in.cap',fout='out.json',format='json',extension=False)# dump to a folder with each tree-view text file per frametree=pcapkit.extract(fin='in.pcap',fout='out',format='tree',files=True)
提取PCAP文件并从帧中提取IP数据包(IPv4和IPv6)(无输出文件)
>>>importpcapkit>>>extraction=pcapkit.extract(fin='in.pcap',nofile=True)>>>frame0=extraction.frame[0]# check if IP in this frame, otherwise ProtocolNotFound will be raised>>>flag=pcapkit.IPinframe0>>>tcp=frame0[pcapkit.IP]ifflagelseNone
提取一个pcap文件并重新组装tcp负载(没有输出文件或帧存储)
importpcapkit# set strict to make sure full reassemblyextraction=pcapkit.extract(fin='in.pcap',store=False,nofile=True,tcp=True,strict=True)# print extracted packet if HTTP in reassembled payloadsforpacketinextraction.reassembly.tcp:forreassemblyinpacket.packets:ifpcapkit.HTTPinreassembly.protochain:print(reassembly.info)
cli示例
pcapkit
的cli(命令行界面)有两种不同的访问方式。
- 通过控制台脚本--直接使用命令名
pcapkit[…]
(如示例所示) - 通过python模块--
python-m pypcapkit[…]
的工作原理与上面完全相同
下面是一些使用示例:
- 导出到MacOS属性列表(
xcode
对此格式有特殊支持)
$ pcapkit in --format plist --verbose
?Loading file 'in.pcap'
- Frame 1: Ethernet:IPv6:ICMPv6
- Frame 2: Ethernet:IPv6:ICMPv6
- Frame 3: Ethernet:IPv4:TCP
- Frame 4: Ethernet:IPv4:TCP
- Frame 5: Ethernet:IPv4:TCP
- Frame 6: Ethernet:IPv4:UDP
?Report file stored in 'out.plist'
- 导出到JSON文件(未指定格式)
$ pcapkit in --output out.json --verbose
?Loading file 'in.pcap'
- Frame 1: Ethernet:IPv6:ICMPv6
- Frame 2: Ethernet:IPv6:ICMPv6
- Frame 3: Ethernet:IPv4:TCP
- Frame 4: Ethernet:IPv4:TCP
- Frame 5: Ethernet:IPv4:TCP
- Frame 6: Ethernet:IPv4:UDP
?Report file stored in 'out.json'
- 导出到文本树视图文件(不带扩展名自动更正)
pip install pypcapkit0
< /P>
待办事项
- [X]指定
raw
packet - [X]介面动词
- [X]查看文档字符串
- [X]合并
jspcapy
- []编写文档
- []实现IP和MAC地址容器
- []实现选项列表提取器
- []实施更多协议