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:
Return type:

Extractor

Returns:

An Extractor object.

Changed in version 1.1.1.post2: strict is renamed reasm_strict, with no alias, and reasm_store is inserted after it, so a positional argument from trace onwards moves one place along.

Changed in version 1.5.0: reasm_timeout, trace_bidirectional and trace_analyse are inserted among the positional parameters and context is appended, so a positional argument after reasm_store has 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: dtls is inserted after tcp, so a positional argument from buffer_size onwards moves one place along.

Changed in version 1.7.0: Takes exactly the parameters of Extractor, whose 1.7.0 note lists what is grouped and what is removed (#2836).

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 in Reassembly.registry, matched lower-cased, or a protocol class, by the first of its id()

  • strict (bool) – if True, report a datagram that is not completely reassembled as the tuple of its received runs; otherwise as one contiguous payload

  • store (bool) – if store reassembled datagrams

  • timeout (float | None) – reassembly timeout in seconds, on the capture’s own clock; None selects the protocol’s own default

Return type:

ReassemblyBase

Returns:

A ReassemblyBase object of corresponding protocol.

Raises:

Changed in version 1.5.0: timeout is 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 of Reassembly, so store is added and strict defaults to True, where it defaulted to False (#2837).

pcapkit.interface.core.trace(protocol, *, fout='./tmp', format='pcap', byteorder=sys.byteorder, nanosecond=False, bidirectional=True, analyze=False, timeout=None)[source]

Trace flows.

Parameters:
  • protocol (str | Type[ProtocolBase]) – protocol whose flows are to be traced, a name registered in TraceFlow.registry, matched lower-cased, or a protocol class, by the first of its id(), or else by the protocol of a registered tracer it derives from (see _traceflow_name())

  • fout (str) – output root path

  • format (str) – output format

  • byteorder (Literal['little', 'big']) – output file byte order

  • nanosecond (bool) – output nanosecond-resolution file flag

  • bidirectional (bool) – whether both halves of a conversation are traced as one flow, which is the default

  • analyze (bool) – whether each traced flow reassembles its application layer

  • timeout (float | None) – idle timeout in seconds, on the capture’s clock; None keeps every flow until finish()

Return type:

TraceFlowBase

Returns:

A TraceFlowBase object.

Raises:

Changed in version 1.5.0: bidirectional is 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, so analyze is added and fout and format default to './tmp' and 'pcap' (#2837). Makes it through Extractor.make_traceflow, as the extractor does (#2843).

Changed in version 1.10.0: Takes timeout, as the flow tracing classes do, and a protocol class whose first id names no tracer makes the tracer of a protocol it derives from, so IPv4 makes the ip one (#2956).

pcapkit.interface.core._traceflow_name(protocol)[source]

Name the flow tracer trace() makes for a protocol class.

Parameters:

protocol (Type[ProtocolBase]) – protocol class

Return type:

str

Returns:

The first of its id() if a flow tracer is registered under it; otherwise the name of the first registered flow tracer whose protocol the class derives from, as IPv4 and IPv6 derive from the ip tracer’s IP; and failing both, that first id, which Extractor.make_traceflow refuses.

Added in version 1.10.0.

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.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.