Advertisement
vinka

parsepcap.pl

Mar 4th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. use Net::TcpDumpLog;
  2. use NetPacket::Ethernet;
  3. use NetPacket::IP;
  4. use NetPacket::TCP;
  5. use strict;
  6. use warnings;
  7. use diagnostics;
  8. #http://hype-free.blogspot.com/2010/03/parsing-pcap-files-with-perl.html
  9.  
  10. my $log = Net::TcpDumpLog->new();
  11. $log->read("log.pcap");
  12.  
  13. foreach my $index ($log->indexes) {
  14. my ($length_orig, $length_incl, $drops, $secs, $msecs) = $log->header($index);
  15. my $data = $log->data($index);
  16.  
  17. my $eth_obj = NetPacket::Ethernet->decode($data);
  18. next unless $eth_obj->{type} == NetPacket::Ethernet::ETH_TYPE_IP;
  19.  
  20. my $ip_obj = NetPacket::IP->decode($eth_obj->{data});
  21. next unless $ip_obj->{proto} == NetPacket::IP::IP_PROTO_TCP;
  22.  
  23. my $tcp_obj = NetPacket::TCP->decode($ip_obj->{data});
  24. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($secs + $msecs/1000);
  25. print sprintf("Time: %02D-%02d %02d:%02d:%02d.%d\n",
  26. $mon, $mday, $hour, $min, $sec, $msecs);
  27. print "\tIP Address Source: ", $ip_obj->{src_ip}, "\n\t\tMac Address Source: ", $eth_obj->{src_mac},
  28. "\n\t\tPort Numbers: ", $tcp_obj->{src_port};
  29. print "\n\tIP Address Destination: ", $ip_obj->{dest_ip}, "\n\t\tMac Address Destination: ", $eth_obj->{dest_mac}, " \n",
  30. "\t\tPort Numbers: ", $tcp_obj->{dest_port}, "\n";
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement