D.8. text2pcap: Converting ASCII hexdumps to network captures

There may be some occasions when you wish to convert a hex dump of some network traffic into a libpcap file.

Text2pcap is a program that reads in an ASCII hex dump and writes the data described into a libpcap-style capture file. text2pcap can read hexdumps with multiple packets in them, and build a capture file of multiple packets. text2pcap is also capable of generating dummy Ethernet, IP and UDP headers, in order to build fully processable packet dumps from hexdumps of application-level data only.

Text2pcap understands a hexdump of the form generated by od -A x -t x1. In other words, each byte is individually displayed and surrounded with a space. Each line begins with an offset describing the position in the file. The offset is a hex number (can also be octal - see -o), of more than two hex digits. Here is a sample dump that text2pcap can recognize:

000000 00 e0 1e a7 05 6f 00 10 ........
000008 5a a0 b9 12 08 00 46 00 ........
000010 03 68 00 00 00 00 0a 2e ........
000018 ee 33 0f 19 08 7f 0f 19 ........
000020 03 80 94 04 00 00 10 01 ........
000028 16 a2 0a 00 03 50 00 0c ........
000030 01 01 0f 19 03 80 11 01 ........
    

There is no limit on the width or number of bytes per line. Also the text dump at the end of the line is ignored. Bytes/hex numbers can be uppercase or lowercase. Any text before the offset is ignored, including email forwarding characters '>'. Any lines of text between the bytestring lines is ignored. The offsets are used to track the bytes, so offsets must be correct. Any line which has only bytes without a leading offset is ignored. An offset is recognized as being a hex number longer than two characters. Any text after the bytes is ignored (e.g. the character dump). Any hex numbers in this text are also ignored. An offset of zero is indicative of starting a new packet, so a single text file with a series of hexdumps can be converted into a packet capture with multiple packets. Multiple packets are read in with timestamps differing by one second each. In general, short of these restrictions, text2pcap is pretty liberal about reading in hexdumps and has been tested with a variety of mangled outputs (including being forwarded through email multiple times, with limited line wrap etc.)

There are a couple of other special features to note. Any line where the first non-whitespace character is '#' will be ignored as a comment. Any line beginning with #TEXT2PCAP is a directive and options can be inserted after this command to be processed by text2pcap. Currently there are no directives implemented; in the future, these may be used to give more fine grained control on the dump and the way it should be processed e.g. timestamps, encapsulation type etc.

Text2pcap also allows the user to read in dumps of application-level data, by inserting dummy L2, L3 and L4 headers before each packet. The user can elect to insert Ethernet headers, Ethernet and IP, or Ethernet, IP and UDP headers before each packet. This allows Wireshark or any other full-packet decoder to handle these dumps.

Example D.6. Help information available for text2pcap

$ text2pcap -h
Text2pcap 0.99.6
Generate a capture file from an ASCII hexdump of packets.
See http://www.wireshark.org for more information.

Usage: text2pcap [-h] [-d] [-q] [-o h|o] [-l typenum] [-e l3pid] [-i proto]
          [-m max-packet] [-u srcp,destp] [-T srcp,destp] [-s srcp,destp,tag]
          [-S srcp,destp,tag] [-t timefmt] <input-filename> <output-filename>

where <input-filename> specifies input filename (use - for standard input)
      <output-filename> specifies output filename (use - for standard output)

[options] are one or more of the following

 -h              : Display this help message
 -d              : Generate detailed debug of parser states
 -o hex|oct      : Parse offsets as (h)ex or (o)ctal. Default is hex
 -l typenum      : Specify link-layer type number. Default is 1 (Ethernet).
                   See net/bpf.h for list of numbers.
 -q              : Generate no output at all (automatically turns off -d)
 -e l3pid        : Prepend dummy Ethernet II header with specified L3PID (in
                   HEX)
                   Example: -e 0x800
 -i proto        : Prepend dummy IP header with specified IP protocol (in
                   DECIMAL).
                   Automatically prepends Ethernet header as well.
                   Example: -i 46
 -m max-packet   : Max packet length in output, default is 64000
 -u srcp,destp   : Prepend dummy UDP header with specified dest and source ports
                   (in DECIMAL).
                   Automatically prepends Ethernet and IP headers as well
                   Example: -u 30,40
 -T srcp,destp   : Prepend dummy TCP header with specified dest and source ports
                   (in DECIMAL).
                   Automatically prepends Ethernet and IP headers as well
                   Example: -T 50,60
 -s srcp,dstp,tag: Prepend dummy SCTP header with specified dest/source ports
                   and verification tag (in DECIMAL).
                   Automatically prepends Ethernet and IP headers as well
                   Example: -s 30,40,34
 -S srcp,dstp,ppi: Prepend dummy SCTP header with specified dest/source ports
                   and verification tag 0. It also prepends a dummy SCTP DATA
                   chunk header with payload protocol identifier ppi.
                   Example: -S 30,40,34
 -t timefmt      : Treats the text before the packet as a date/time code; the
                   specified argument is a format string of the sort supported
                   by strptime.
                   Example: The time "10:15:14.5476" has the format code
                   "%H:%M:%S."
                   NOTE:    The subsecond component delimiter must be specified
                            (.) but no pattern is required; the remaining number
                            is assumed to be fractions of a second.
      
-w <filename>

Write the capture file generated by text2pcap to <filename>. The default is to write to standard output.

-h

Display the help message

-d

Displays debugging information during the process. Can be used multiple times to generate more debugging information.

-q

Be completely quiet during the process.

-o hex|oct

Specify the radix for the offsets (hex or octal). Defaults to hex. This corresponds to the -A option for od.

-l

Specify the link-layer type of this packet. Default is Ethernet(1). See net/bpf.h for the complete list of possible encapsulations. Note that this option should be used if your dump is a complete hex dump of an encapsulated packet and you wish to specify the exact type of encapsulation. Example: -l 7 for ARCNet packets.

-e l3pid

Include a dummy Ethernet header before each packet. Specify the L3PID for the Ethernet header in hex. Use this option if your dump has Layer 3 header and payload (e.g. IP header), but no Layer 2 encapsulation. Example: -e 0x806 to specify an ARP packet.

For IP packets, instead of generating a fake Ethernet header you can also use -l 12 to indicate a raw IP packet to Wireshark. Note that -l 12 does not work for any non-IP Layer 3 packet (e.g. ARP), whereas generating a dummy Ethernet header with -e works for any sort of L3 packet.

-u srcport destport

Include dummy UDP headers before each packet. Specify the source and destination UDP ports for the packet in decimal. Use this option if your dump is the UDP payload of a packet but does not include any UDP, IP or Ethernet headers. Note that this automatically includes appropriate Ethernet and IP headers with each packet. Example: -u 1000 69 to make the packets look like TFTP/UDP packets.