Core Interface¶
pcapkit.interface.core defines the core user-facing functions and
constants, which wrap the classes in pcapkit.foundation.
- pcapkit.interface.core.extract(fin, *, dump=None, engine='default', layer=None, protocol=None, context=None, reassembly=None, traceflow=None, stream=None, auto=True, store=True, verbose=False)[source]¶
Extract a PCAP file.
- Parameters:
fin (
str|IO[bytes]) – file name to be read, as named, or a binary IO object; if the file does not exist, raiseFileNotFounddump (
DumpOptions|None) – output settings, aDumpOptions;Nonewrites no outputengine (
Literal['default','pcapkit','dpkt','scapy','pyshark','pypcap','pcap_ct','pypcapfile']) – extraction engine to be usedlayer (
Literal['link','internet','transport','application','none'] |Type[ProtocolBase] |None) – extract til which layer, by name or as a protocol classprotocol (
str|ProtocolBase|Type[ProtocolBase] |None) – extract til which protocolcontext (
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 – cf.
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.protocol import (Cipher, ESPContext, ... SecurityAssociation) >>> sa = SecurityAssociation(spi=0x4321, encryption=Cipher.AES_CBC, ... encryption_key=key) >>> extraction = pcapkit.extract('esp.pcap', context=ESPContext(sa))
reassembly (
ReassemblyOptions|Iterable[str] |None) – reassembly settings, aReassemblyOptions, or the names of the reassemblers to enable, each as registered inExtractor.__reassembly__;None, or no names, reassembles nothingtraceflow (
TraceFlowOptions|Iterable[str] |None) – flow tracing settings, aTraceFlowOptions, or the names of the flow tracers to enable, each as registered inExtractor.__traceflow__;None, or no names, traces nothingstream (
StreamOptions|None) – input stream settings, aStreamOptions;Nonetakes their defaultsauto (
bool) – if automatically run till EOFstore (
bool) – if store extracted packet infoverbose (
bool|Callable[[Extractor,Frame|PCAPNG|scapy.packet.Packet|dpkt.dpkt.Packet|pyshark.packet.packet.Packet|pcap_packet|Tuple[float,bytes]],Any]) – aboolvalue or a function that takes theExtractorinstance and current parsed frame (depends on engine selected) as parameters to print verbose output information
- Return type:
- Returns:
An
Extractorobject.
Changed in version 1.1.1.post2:
strictis renamedreasm_strict, with no alias, andreasm_storeis inserted after it, so a positional argument fromtraceonwards moves one place along.Changed in version 1.5.0:
reasm_timeout,trace_bidirectionalandtrace_analyseare inserted among the positional parameters andcontextis appended, so a positional argument afterreasm_storehas to move to a keyword. Flows are traced bidirectionally by default, and IPv4 and IPv6 reassembly buffers expire after 60 seconds of capture time.Changed in version 1.6.0:
dtlsis inserted aftertcp, so a positional argument frombuffer_sizeonwards moves one place along.
- pcapkit.interface.core.reassemble(protocol, *, strict=True, store=True, timeout=None)[source]¶
Reassemble fragmented datagrams.
- Parameters:
protocol (
str|Type[ProtocolBase]) – protocol to be reassembled, a name registered inReassembly.registry, matched lower-cased, or a protocol class, by the first of itsid()strict (
bool) – ifTrue, report a datagram that is not completely reassembled as the tuple of its received runs; otherwise as one contiguous payloadstore (
bool) – if store reassembled datagramstimeout (
float|None) – reassembly timeout in seconds, on the capture’s own clock;Noneselects the protocol’s own default
- Return type:
- Returns:
A
ReassemblyBaseobject of corresponding protocol.- Raises:
FormatError – If no reassembly is registered under
protocol.RegistryError – If the protocol name is not a
str.
Changed in version 1.5.0:
timeoutis added. IPv4 and IPv6 buffers expire after 60 seconds of capture time by default, where 1.4.x held them for the life of the object.Changed in version 1.7.0: Makes any registered reassembly, through
Extractor.make_reassembly, where it made IPv4, IPv6 and TCP only (#2814). Takes exactly the keyword-only parameters ofReassembly, sostoreis added andstrictdefaults toTrue, where it defaulted toFalse(#2837).
- pcapkit.interface.core.trace(protocol, *, fout='./tmp', format='pcap', byteorder=sys.byteorder, nanosecond=False, bidirectional=True, analyze=False)[source]¶
Trace flows.
- Parameters:
protocol (
str|Type[ProtocolBase]) – protocol whose flows are to be traced, a name registered inTraceFlow.registry, matched lower-cased, or a protocol class, by the first of itsid()fout (
str) – output root pathformat (
str) – output formatbyteorder (
Literal['little','big']) – output file byte ordernanosecond (
bool) – output nanosecond-resolution file flagbidirectional (
bool) – whether both halves of a conversation are traced as one flow, which is the defaultanalyze (
bool) – whether each traced flow reassembles its application layer
- Return type:
- Returns:
A
TraceFlowBaseobject.- Raises:
FormatError – If no flow tracer is registered under
protocol.RegistryError – If the protocol name is not a
str.
Changed in version 1.5.0:
bidirectionalis added, and by default both directions of a conversation are traced as one flow; 1.4.x traced each direction as a flow of its own.Changed in version 1.7.0: Makes any registered flow tracer, where it made TCP’s only, and takes exactly the keyword-only parameters of
TraceFlow, soanalyzeis added andfoutandformatdefault to'./tmp'and'pcap'(#2837). Makes it throughExtractor.make_traceflow, as the extractor does (#2843).
Constants Definition¶
Output File Formats¶
- pcapkit.interface.core.TREE = 'tree'¶
- pcapkit.interface.core.JSON = 'json'¶
- pcapkit.interface.core.PLIST = 'plist'¶
- pcapkit.interface.core.PCAP = 'pcap'¶
- pcapkit.interface.core.PCAP_NG = 'pcapng'¶
Added in version 1.7.0.
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'¶
Extraction 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= string pcapkit ships has a constant here, and the
constant is the string, so PyPCAP and 'pypcap' are
interchangeable; PCAPKit selects the built-in PCAP or PCAP-NG engine
by the file’s magic number. An engine registered at runtime with
register_extractor_engine() has
no constant, and is named by the string 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.