Core Interface¶
pcapkit.interface.core defines core user-oriented
interfaces, variables, and etc., which wraps around the
foundation classes from pcapkit.foundation.
- pcapkit.interface.core.extract(fin=None, fout=None, format=None, auto=True, extension=True, store=True, files=False, nofile=False, verbose=False, engine=None, layer=None, protocol=None, reassembly=False, reasm_strict=True, reasm_store=True, reasm_timeout=None, trace=False, trace_fout=None, trace_format=None, trace_byteorder='little', trace_nanosecond=False, trace_bidirectional=True, trace_analyse=False, ip=False, ipv4=False, ipv6=False, tcp=False, buffer_size=131072, buffer_save=False, buffer_path=None, no_eof=False, context=None)[source]¶
Extract a PCAP file.
- Parameters:
fin (
str|IO[bytes] |None) – file name to be read or a binary IO object; if file not exist, raiseFileNotFoundformat (
Literal['pcap','cap','json','tree','text','txt','plist','xml'] |None) – file format of outputauto (
bool) – if automatically run till EOFextension (
bool) – if check and append extensions to output filestore (
bool) – if store extracted packet infofiles (
bool) – if split each frame into different filesnofile (
bool) – if no output file is to be dumpedverbose (
bool|Callable[[Extractor,Frame|PCAPNG|Packet|Packet|Packet|pcap_packet|tuple[float,bytes]],Any]) – aboolvalue or a function takes theExtractorinstance and current parsed frame (depends on engine selected) as parameters to print verbose output informationengine (
Literal['default','pcapkit','dpkt','scapy','pyshark','pypcap','pcap_ct','pypcapfile'] |None) – extraction engine to be usedlayer (
Literal['link','internet','transport','application','none'] |None|Type[ProtocolBase]) – extract til which layerprotocol (
str|ProtocolBase|Type[ProtocolBase] |None) – extract til which protocolreassembly (
bool) – if perform reassemblyreasm_strict (
bool) – if set strict flag for reassemblyreasm_store (
bool) – if store reassembled datagramsreasm_timeout (
float|None) – reassembly timeout in seconds, on the capture’s own clock;Noneselects each protocol’s default (60 seconds for IPv4 and IPv6, disabled for TCP)trace (
bool) – if trace TCP traffic flowstrace_fout (
str|None) – path name for flow tracer if necessarytrace_format (
Literal['pcap','cap','json','tree','text','txt','plist','xml'] |None) – output file format of flow tracertrace_byteorder (
Literal['big','little']) – output file byte ordertrace_nanosecond (
bool) – output nanosecond-resolution file flagtrace_bidirectional (
bool) – whether both halves of a conversation are traced as one flow, which is the defaulttrace_analyse (
bool) – whether each traced flow reassembles its application layer, so that itspacketcan be read; off by defaultip (
bool) – if record data for IPv4 & IPv6 reassembly (must be used withreassembly=True)ipv4 (
bool) – if perform IPv4 reassembly (must be used withreassembly=True)ipv6 (
bool) – if perform IPv6 reassembly (must be used withreassembly=True)tcp (
bool) – if perform TCP reassembly and/or flow tracing (must be used withreassembly=Trueortrace=True)buffer_size (
int) – buffer size for reading input file (forSeekableReaderonly)buffer_save (
bool) – if save buffer to file (forSeekableReaderonly)buffer_path (
str|None) – path name for buffer file if necessary (forSeekableReaderonly)no_eof (
bool) – if not raiseEOFErrorwhen reach EOF – retry instead, which is what a live capture on a pipe or on standard input wants, since a read there blocks until the writer produces more or closes. Retrying stops as soon as one retry finds nothing new, so an exhausted input finishes rather than spinning; on a seekable input, where nothing blocks, that means a file still being appended to ends at the data present when the extraction reached itcontext (
ContextRegistry|ProtocolContext|Mapping[str,ProtocolContext] |Iterable[ProtocolContext] |None) –caller supplied parsing context for protocols that need information not carried on the wire, keyed by protocol index ID – c.f.
pcapkit.corekit.context. The motivating case isESP, which needs the Security Association to find the trailer and decrypt the payload:>>> from pcapkit.protocols.internet.esp import (Cipher, ESPContext, ... SecurityAssociation) >>> sa = SecurityAssociation(spi=0x4321, encryption=Cipher.AES_CBC, ... encryption_key=key) >>> extraction = pcapkit.extract('esp.pcap', context=ESPContext(sa))
- Return type:
- Returns:
An
Extractorobject.
- pcapkit.interface.core.reassemble(protocol, strict=False, timeout=None)[source]¶
Reassemble fragmented datagrams.
- Parameters:
- Return type:
- Returns:
A
Reassemblyobject of corresponding protocol.- Raises:
FormatError – If
protocolis NOT any of IPv4, IPv6 or TCP.
- pcapkit.interface.core.trace(protocol, fout, format, byteorder='little', nanosecond=False, bidirectional=True)[source]¶
Trace flows.
- Parameters:
- Return type:
- Returns:
A
TraceFlowobject.- Raises:
FormatError – If
protocolis NOT TCP.
Constants Defintion¶
Output File Formats¶
- pcapkit.interface.core.TREE = 'tree'¶
- pcapkit.interface.core.JSON = 'json'¶
- pcapkit.interface.core.PLIST = 'plist'¶
- pcapkit.interface.core.PCAP = 'pcap'¶
Layer Thresholds¶
- pcapkit.interface.core.RAW = 'none'¶
- pcapkit.interface.core.LINK = 'link'¶
- pcapkit.interface.core.INET = 'internet'¶
- pcapkit.interface.core.TRANS = 'transport'¶
- pcapkit.interface.core.APP = 'application'¶
Extration Engines¶
- pcapkit.interface.core.DPKT = 'dpkt'¶
- pcapkit.interface.core.Scapy = 'scapy'¶
- pcapkit.interface.core.PCAPKit = 'default'¶
- pcapkit.interface.core.PyShark = 'pyshark'¶
- pcapkit.interface.core.PyPCAP = 'pypcap'¶
- pcapkit.interface.core.PCAP_CT = 'pcap_ct'¶
- pcapkit.interface.core.PyPCAPFile = 'pypcapfile'¶
Note
Every engine pcapkit ships now has a constant here. The PyPCAP,
pcap-ct and PyPCAPFile ones were added after the first four, so code
written against an earlier release may still select them by their literal
engine= values – 'pypcap', 'pcap_ct' and 'pypcapfile'. That
keeps working: each constant is that string, so the two spellings are
interchangeable. An engine registered at runtime with
register_extractor_engine() has
no constant and is addressed by the name it was registered under.
See also
Engine Support for the full engine list, what each one supports, and the installation prerequisites the third-party ones carry.