#!/usr/bin/perl # # extremely basic sniffer with Net::Pcap - Tikiman # use Net::Pcap; $err = ''; $device = Net::Pcap::lookupdev(\$err); $cap_descrip = Net::Pcap::open_live($device,2000,0,1000,\$err); Net::Pcap::loop($cap_descrip, 100, \&read_func , "test" ); sub read_func { my($data, $header, $packet) = @_; # while(($key,$val) = each %$header) { # print "$key - $val\n"; # } ($smac,$tmac,$type) = unpack('H12 H12 H4',$packet); if ($type == "0806") { print "ARP packet: Source - $smac | Target - $tmac\n"; } elsif ($type == "0800") { $packet = substr($packet,14,9999); ($verihl,$tos,$len, $id, $flags, $ttl, $proto, $checksum, $sourceip, $destip) = unpack('H2 B8 n n n C C H4 A4 A4',$packet); $data = substr($packet,40,999); print "IP Protocol - $proto\n"; print "Source IP - " . join('.',unpack('C4',$sourceip)) . "\n"; print "Dest IP - " . join('.',unpack('C4',$destip)) . "\n"; print "Data - $data\n"; } # binDebug($packet); # hexDebug($packet); } sub binDebug { my $data = shift; my $counter = 1; print "Binary Octet Dump:\n"; for (my $i = 0;$i < length($data);$i += 4) { print "$counter: " . join(' ',unpack('B8 B8 B8 B8',substr($data,$i,4))) . "\n"; $counter++; } } sub hexDebug { my $data = shift; my $counter = 1; print "Hex Dump:\n"; for (my $i = 0;$i < length($data);$i += 4) { print "$counter: " . join(' ',unpack('H2 H2 H2 H2',substr($data,$i,4))) . "\n"; $counter++; } }