#!/usr/bin/perl -w # pmsac@toxyn.org, Sep 2005 (work in progress(?)) # For debug... #use Data::Dumper; #require "graftraf.cf"; # FILE entries # for knowing file names and directories # There's no concept of "base dir", you must # explicit full path entries all over #my $DATAFILEGLOB = 'ft-v05.2002-12-06.1310*'; my $DATAFILEGLOB = '*.cap.*'; # GLOBAL variables # accessible to subs, after each flow is read my $STEPSECONDS = 300; my $DEBUG_FILE=1; my $DEBUG_SAMPLE=0; my $DEBUG_PACKET=0; #struct pcap_file_header { # bpf_u_int32 magic; # u_short version_major; # u_short version_minor; # bpf_int32 thiszone; /* gmt to local correction */ # bpf_u_int32 sigfigs; /* accuracy of timestamps */ # bpf_u_int32 snaplen; /* max length saved portion of each pkt */ # bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */ #}; my $flhMagic; my $flhVMajor; my $flhVMinor; my $flhTimeZone; my $flhSigfigs; my $flhSnapLen; my $flhLinyType; my $flhTemplate = "LS2L4"; my $flhHeaderFixedSize = 24; #struct pcap_pkthdr { # struct timeval ts; /* time stamp */ # bpf_u_int32 caplen; /* length of portion present */ # bpf_u_int32 len; /* length this packet (off wire) */ #}; my $flphTimeStamp1; my $flphTimeStamp2; my $flphCapLen; my $flphLen; my $flphTemplate = "L4"; my $flphSize = "16"; # do not use complicated method of sorting by timestamp # as flowscan does (using FlowScan) my @files = sort <${DATAFILEGLOB}>; if (@files) { foreach my $file (@files) { my $buf; my $numPackets; open(FFILE, "<".$file) || die("can't open file $file\n"); binmode(FFILE); if (sysread(FFILE, $buf, $flhHeaderFixedSize) != $flhHeaderFixedSize) { print "Error, file header short read ?!?!\n"; exit(1); } ( $flhMagic, $flhVMajor, $flhVMinor, $flhTimeZone, $flhSigfigs, $flhSnapLen, $flhLinkType ) = unpack($flhTemplate, $buf); if ($flhMagic != 0xa1b2c3d4) { print "Error, not a tcpdump cap file ?!?!\n"; print "(read magic == $flhMagic)\n"; exit(2); } if (($flhVMajor != 0x0002) || ($flhVMinor != 0x0004)) { print "Error, not a tcpdump 2.4 cap file ?!?!\n"; exit(3); } if ($flhLinkType != 0x0001) { print "Error, not link type 0x0001!\n"; exit(4); } if ($DEBUG_FILE) { print "File:\t".$file."\n"; printf "Magic:\t0x%04x\n",$flhMagic; print "Version:\t$flhVMajor.$flhVMinor\n"; print "TZone:\t$flhTimeZone\n"; print "Sfigs:\t$flhSigfigs\n"; printf "SLen:\t0x%04x\n",$flhSnapLen; printf "LType:\t0x%04x\n",$flhLinkType; print "---\n"; } $numPackets = 0; $first = 1; $byteCount = 0; while (sysread(FFILE, $buf, $flphSize) == $flphSize) { ( $flphTimeStamp1, $flphTimeStamp2, $flphCapLen, $flphLen, ) = unpack($flphTemplate, $buf); if ($first) { $interval = $flphTimeStamp1 - ($flphTimeStamp1 % $STEPSECONDS); printf "Start:\t".localtime($interval)."\n"; print "---\n"; $first = 0; } $newInterval = $flphTimeStamp1 - ($flphTimeStamp1 % $STEPSECONDS); if ($newInterval != $interval) { do { if ($DEBUG_SAMPLE) { printf "Sample:\t".localtime($interval)."\n"; printf "sBytes:\t$byteCount\n"; print "---\n"; } print "$byteCount\n"; $byteCount = 0; $interval += $STEPSECONDS; } while ($interval != $newInterval); } if ($DEBUG_PACKET) { print "Time:\t".localtime($flphTimeStamp1)."\n"; printf "CapLen:\t0x%04x\n",$flphCapLen; printf "PktLenHex:\t0x%04x\n",$flphLen; printf "pBytes:\t".($flphLen-18)."\n"; print "---\n"; } $byteCount += ($flphLen-18); $numPackets++; if (!sysseek(FFILE, $flphCapLen, 1)) { print "Error, file too short ?!?!\n"; exit(5); } } close(FFILE); if ($DEBUG_SAMPLE) { printf "Sample:\t".localtime($interval)."\n"; printf "sBytes:\t$byteCount\n"; print "---\n"; } print "$byteCount\n"; } }