1

我一直在尝试编写一个脚本来过滤设备中的数据包以及该设备上的特定 IP 地址。

当您选择特定设备并使用 ip.src==xx.xx.xx.xx 时,我希望数据类似于我从 wireshark 获得的输出

到目前为止我的程序是这样的

    #!/usr/bin/perl -w
my $interface='eth1';

sub process_pkt #Packet processing routine 
{
 my ($user_data,$header, $packet) = @_;
 my $minipacket = substr($packet,0,54); 
 print ("\n## raw: ###\n");
 print ($minipacket);
print ("\n==Byte# / Hex / Dec / Bin==\n");
 for ($i=0;$i<55;$i++)
 {
 $hexval = unpack('H2',substr($packet,$i,1)); 
 $decval = hex(unpack('H2',substr($packet,$i,1)));
 printf ("%03s-%02s-%03s-%08b\n", $i, $hexval, $decval, $decval);
 }
}
# ###################################################################### 

# Here we are invoking the NetPcap module and looping through forever.
Net::PcapUtils::loop(\&process_pkt, 
 SNAPLEN => 65536, #Size of data to get from packet
 PROMISC => 1, #Put in promiscuous mode
FILTER => 'tcp', #only pass TCP packets
 DEV => $interface, ); 

我得到了输出 输出

现在我想过滤掉在eth1设备和 xx.xx.xx.xx 的源 IP 上接收到的数据包,我们可以使用 Net::PcapUtils::loop 中的过滤器选项来做到这一点吗?然后我想要数据长度为 xx 的数据包……我尝试浏览 cpan.org 中的文档,但我发现的只是可用的选项……我找不到任何示例……

有人可以帮帮我吗?

改进:

我可以使用类似的东西吗

FILTER => 'ip src xx.xx.xx.xx'

之后

FILTER => 'tcp'

代码中的行?我可以以某种方式包含数据包的数据长度以过滤数据长度= 86的数据包吗?


我用来获取数据包有效负载的替代程序:

#!/usr/bin/perl -w
# #########################
#
use Net::PcapUtils;
use NetPacket::Ethernet qw(:strip);
use NetPacket::IP;
use NetPacket::TCP;
use NetPacket::IP qw(:strip);

my $interface= 'eth1';
my $snaplen= 65536;
my $filter='tcp';
my $promisc = 1;
my $timeout = 10000 ;
my $err;

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

$ip= NetPacket::IP->decode(eth_strip($packet));
    $tcp= NetPacket::TCP->decode($ip->{data});
    $payload = $tcp->{data};
    print ("payload: \n ".$payload." \n----end-----\n");
    for($i=0;$i<55;$i++){   
    $hexval = unpack('H2',substr($payload,$i,1));
    open (MYFILE, '>>perldata1.txt');
    print MYFILE ($i." :hex: ". $hexval."\n");
    close (MYFILE);
    }
}

Net::PcapUtils::loop(\&process_pkt,
            SNAPLEN => 65536,
            PROMISC => 1,
            FILTER => 'tcp',
            FILTER => 'ip src 129.7.236.40',
            DEV => $interface, );

但我仍然无法弄清楚如何获取数据字段的长度。:( 谢谢。

4

1 回答 1

0
#!/usr/bin/perl -w
# #########################
#
use Net::PcapUtils;
use NetPacket::Ethernet qw(:strip);
use NetPacket::IP;
use NetPacket::TCP;
use NetPacket::IP qw(:strip);
use strict;
use Data::Dumper;
#use warnings;

my $interface= 'eth1';
my $snaplen= 65536;
my $filter='tcp';
my $promisc = 1;
my $timeout = 10000 ;
my $err;
my @array;
sub process_pkt
{
    my ($user_data,$header,$packet) = @_;

my $ip= NetPacket::IP->decode(eth_strip($packet));
    my $tcp= NetPacket::TCP->decode($ip->{data});
    my $payload = $tcp->{data}; 
    if(length($payload)==32)
    {
    for(my $decode=0;$decode<32;$decode++)
    {   
    $array[$decode] = unpack('H2',substr($payload,$decode,1));
    }
    my $length= scalar(@array);
    open (MYFILE, '>doorstatus.tab');
    if($array[22] eq '0c')
        {
            print MYFILE ( " Decision: Granted\n");
        }
    elsif($array[22] eq '04')
    {
    print MYFILE ("Decision: Denied\n");
    }
    elsif($array[22] eq '0d')
    {
    print MYFILE ("Decision: Locked\n");
    }
    else
    {
    print MYFILE ("Decision: unknown \n");
    }

    #print MYFILE ( " Data: \n".Dumper(\@array)." \n");

    close (MYFILE);
    }
}

Net::PcapUtils::loop(\&process_pkt,
            SNAPLEN => 65536,
            PROMISC => 1,
            FILTER => 'tcp',
            FILTER => 'ip src xx.xx.xx.xx',
            DEV => $interface, );

该代码将来自特定源的数据过滤到一个数组中,您可以用它做任何事情,

于 2013-05-20T22:06:06.203 回答