Skip to content

Impacket split.py

Back to the category index / Back to the command index

Reads a pcap file and splits IPv4 TCP traffic into separate files by source and destination.

ItemDetails
Implementationexamples/split.py
CategoryPackets / ICMP / capture
Size147 lines
DistributionInstalled as a script through scripts=examples/*.py in setup.py

This older sample does not handle -h. Check its usage message and top-level sys.argv handling.

python examples/split.py <capture.pcap>

It uses only the first positional argument as the input pcap. There is no output path option. It creates one file per connection in the current working directory, using a filename based on the two endpoints: <src-ip>.<src-port>-<dst-ip>.<dst-port>.pcap.

pcapy.open_offline() reads the capture in order. A decoder for its datalink type extracts TCP/IP. The script builds a directional string key and filename from each source/destination endpoint pair, then appends complete capture records for that direction to one pcap file.

split does not reconstruct TCP byte streams. It distributes packets from a pcap into separate pcap files by connection key. It sets a BPF filter on the input handle: ip proto \tcp. Ethernet and Linux SLL decoders extract IPv4 and TCP headers. The filter excludes non-IPv4 and non-TCP packets.

A connection key is a string joining the source (IP, port) and destination (IP, port) in that order. The Connection class has a __cmp__() intended to treat both directions as equal, but the dictionary actually uses directional strings, not Connection objects, as keys. Thus, on the current Python 3 implementation, outbound and return traffic go to separate files. For a new key, it opens a pcapdumper with an endpoint-ordered filename and writes the original capture header and entire packet unchanged.

It does not reorder sequence numbers, remove retransmissions, reassemble IP fragments or TCP segments, or distinguish generations of the same 4-tuple by session timeout. The output is therefore a collection of packets seen in one direction of the capture, not an application payload file. To combine both directions, create an explicitly order-independent key. Many flows also increase the number of open dumpers, so handle closure and resource limits matter when improving the implementation.

SymbolLocationWhere to trace its responsibility
Connection35__init__(), getFilename()
Decoder69__init__(), start(), packetHandler()
main()127Module function
ModuleMain names used
impacketversion
impacket.ImpactDecoderEthDecoder, LinuxSLLDecoder
Import nameDistribution packageImport path and declaration status
pcapypcapyImported directly; not declared as a direct dependency

Output, side effects, and required privileges

Section titled “Output, side effects, and required privileges”
  • The main operation parses local files. Treat input as untrusted: validate offsets, lengths, and format identifiers before accessing data.
  • Options may create or update local files. Check how existing files are handled and the permissions on output containing secrets.
  • Distinguish a missing input file, a damaged file, an incomplete read, and an unsupported format.

The code depends on pcapy, EthDecoder, and LinuxSLLDecoder. pcapy is not declared as a direct dependency. Decoder.packetHandler() sorts packets by directional flow; it does not reconstruct TCP streams or resolve retransmissions, missing segments, or out-of-order delivery.

  • Syntax: python -m py_compile examples/split.py
  • CLI: Use a subprocess to check the usage message and exit code with no or too few arguments.
  • No test specific to this command was found. Choose shared-module tests and, if needed, a run in an isolated environment.