使用Perl修改PCAP文件并使用校验和更新

时间:2013-03-01 00:06:26

标签: perl pcap

我有250MB的pcap。我能够使用tcpreplay更改MAC和IP地址,但我还需要修改数据包有效负载内的一些字段。如果我通过读取二进制文件来更改字段,则ip有效负载的校验和将失败。是否有一个允许我修改某些字段的模块,校验和会更新吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Net :: PCap来解析此类文件:http://search.cpan.org/~kcarnut/Net-Pcap-0.05/Pcap.pm

以下是一个示例:http://hype-free.blogspot.co.uk/2010/03/parsing-pcap-files-with-perl.html

use Net::TcpDumpLog;
use NetPacket::Ethernet;
use NetPacket::IP;
use NetPacket::TCP;
use strict;
use warnings;

my $log = Net::TcpDumpLog->new(); 
$log->read("foo.pcap");

foreach my $index ($log->indexes) { 
  my ($length_orig, $length_incl, $drops, $secs, $msecs) = $log->header($index); 
  my $data = $log->data($index);

  my $eth_obj = NetPacket::Ethernet->decode($data);    
  next unless $eth_obj->{type} == NetPacket::Ethernet::ETH_TYPE_IP;

  my $ip_obj = NetPacket::IP->decode($eth_obj->{data});
  next unless $ip_obj->{proto} == NetPacket::IP::IP_PROTO_TCP;

  my $tcp_obj = NetPacket::TCP->decode($ip_obj->{data});
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($secs + $msecs/1000);
  print sprintf("%02d-%02d %02d:%02d:%02d.%d", 
    $mon, $mday, $hour, $min, $sec, $msecs), 
    " ", $eth_obj->{src_mac}, " -> ", 
    $eth_obj->{dest_mac}, "\n";    
  print "\t", $ip_obj->{src_ip}, ":", $tcp_obj->{src_port}, 
    " -> ", 
    $ip_obj->{dest_ip}, ":", $tcp_obj->{dest_port}, "\n";
}