This is our old Q&A Site. Please post any new questions and answers at

Hi All,

In brief, what I have:

  • 50GB (100 x 512MB) pcap files captured with dumpcap containing multicast UDP data (mixed target ports).
  • Matlab scripts dealing with the raw binary payload data for a selection of ports.

What I want to do:

  • For each of the 100 pcap files, extract raw UDP binary PAYLOAD-ONLY data sent to a specific port X.
  • Repeat process for each port of interest (eg if I had 7 ports of interest, I'd end up with 700 files containing payload data)

Essentially it is the same conversion performed by Wiresharks "Follow UDP Stream", but the data volume is too high to use that feature.

I noticed a couple of other threads dealing with this or similar issues:

but I feel I am missing something, it seems the data has to go to ASCII first? Rather than straight to binary. With such large volumes of data, I was hoping there might be a perl or python trick out there to help batch this job. I'm not that familiar with PERL, but I have started looking into the Net::Pcap libraries.

I have no use for the additional packet info/stats collected by dumpcap, so in hindsight I probably would have been better off coding up a simple bit of software to receive and record the UDP data of interest directly, but it's a bit late for that now.

Any further tips greatly appreciated.

asked 03 Nov '15, 05:49

kevenofnine's gravatar image

accept rate: 0%

edited 03 Nov '15, 06:15

Jasper's gravatar image

Jasper ♦♦

Please take a look at my perl code in a similar question. Maybe you can use it as a starting point.

Instead of printing the hex representation of the UDP payload, you could write it to a binary file.


So, the code could look like this (works on my systems).

BTW: Net::Pcap will be able to read the pcap format by default. It will be able to alo read pcap-ng, if the version of libpacap on your system does support it, otherwise you will get an error message ("FATAL: cannot open 47183.pcapng -> ERROR: bad dump file format")


use warnings;
use strict;
use Net::Pcap;
use NetPacket::Ethernet qw(eth_strip);
use NetPacket::IP qw(:ALL);
use NetPacket::UDP;
use POSIX qw(strftime);

my $pcap_file = $ARGV[0];

my $port_list = {
    53 => 1,
    5353 => 1,
    1900 => 1

my $filehandles;

my $error;

my $pcap = Net::Pcap::open_offline($pcap_file, \$error) or die("FATAL: cannot open $pcap_file -> ERROR: $error\n");

Net::Pcap::loop($pcap, -1, \&process_packet, '');

sub process_packet {   
    my ($user_data,$header, $packet) = @_;

    my $ip = NetPacket::IP->decode(eth_strip($packet));
    my $src = $ip->{src_ip};
    my $dst = $ip->{dest_ip};

    if ($ip->{proto} == IP_PROTO_UDP) {

        my $udp = NetPacket::UDP->decode($ip->{data});
        my $udp_sport = $udp->{src_port};
        my $udp_dport = $udp->{dest_port};

        if (not exists $port_list->{$udp_dport}) {

        my $session = "${src}_${udp_sport}__${dst}_${udp_dport}";

        write_payload($session, $udp->{data});


sub write_payload {

    my $session = shift;
    my $data = shift;
    my $fh;

    if (exists $filehandles->{$session}) {
        $fh = $filehandles->{$session};
    } else { 
        my $filename = $pcap_file . "__" . $session . ".payload";
        open($fh, ">>:raw", $filename) || die("FATAL: cannot write to file $filename\n");
        $filehandles->{$session} = $fh;

    print $fh $data;

My script writes only the payload of one direction to the file (it looks for destination port), as it won't make much sense to mix data of both directions, packet by packet, into the binary output file. If you need something different, please adjust the script to your needs.


permanent link

answered 03 Nov '15, 07:13

Kurt%20Knochner's gravatar image

Kurt Knochner ♦
accept rate: 15%

edited 03 Nov '15, 12:03

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 03 Nov '15, 05:49

question was seen: 4,358 times

last updated: 03 Nov '15, 12:03

p​o​w​e​r​e​d by O​S​Q​A