Changeset 1183

Show
Ignore:
Timestamp:
08/04/08 08:35:33 (1 year ago)
Author:
mbr
Message:

applied fwknop-1.9.6-no-NetPacket?.patch from Mirek

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fwknop/branches/fwknop-redhat-integration/fwknopd

    r1174 r1183  
    4646use Net::IPv4Addr qw(ipv4_in_network); 
    4747use Net::Pcap; 
    48 use NetPacket::IP; 
    49 use NetPacket::UDP; 
    50 use NetPacket::TCP; 
    51 use NetPacket::ICMP; 
    52 use NetPacket::Ethernet; 
    5348use IO::Socket; 
    5449use IO::Handle; 
     
    393388    my $src_ip     = ''; 
    394389    my $proto      = ''; 
    395     my $transport_obj = ''; 
     390    my $transport_data = ''; 
    396391 
    397392    if ($debug) { 
     
    421416    if ($config{'AUTH_MODE'} eq 'ULOG_PCAP') { 
    422417        ### The ulogd pcap writer does not include link layer information 
    423         $ip = NetPacket::IP->decode($pkt) or return; 
     418        $ip = &ip_decode($pkt) or return; 
    424419    } else { 
    425420        if ($config{'FIREWALL_TYPE'} eq 'ipfw' and $cmdline_intf eq 'lo0') { 
     
    429424            $pkt =~ s/^.{4}// if $pkt =~ /^[^\x45].{3}\x45/; 
    430425 
    431             $ip = NetPacket::IP->decode($pkt) or return; 
     426            $ip = &ip_decode($pkt) or return; 
    432427        } else { 
    433428            if ($PCAP_COOKED_INTF) { 
    434429                $ether_data = unpack("x[16]a*", $pkt); 
    435430            } else { 
    436                 $ether_data = NetPacket::Ethernet::strip($pkt) or return; 
    437             } 
    438             $ip = NetPacket::IP->decode($ether_data) or return; 
     431                $ether_data = &ethernet_strip($pkt) or return; 
     432            } 
     433            $ip = &ip_decode($ether_data) or return; 
    439434        } 
    440435    } 
     
    447442 
    448443    if ($proto == 1) { 
    449         $transport_obj = NetPacket::ICMP->decode($ip->{'data'}); 
     444        $transport_data = &icmp_decode_data($ip->{'data'}); 
    450445    } elsif ($proto == 6) { 
    451         $transport_obj = NetPacket::TCP->decode($ip->{'data'}); 
     446        $transport_data = &tcp_decode_data($ip->{'data'}); 
    452447    } elsif ($proto == 17) { 
    453         $transport_obj = NetPacket::UDP->decode($ip->{'data'}); 
     448        $transport_data = &udp_decode_data($ip->{'data'}); 
    454449    } else { 
    455450        return; 
     
    459454    ### any valid SPA message will be longer than 10 bytes, but this 
    460455    ### check is better than nothing 
    461     return unless defined $transport_obj->{'data'}
     456    return if $transport_data eq ""
    462457 
    463458    my $enc_msg_len = 0; 
    464     $enc_msg_len = length($transport_obj->{'data'}); 
     459    $enc_msg_len = length($transport_data); 
    465460    if (10 < $enc_msg_len and $enc_msg_len < 1500) { 
    466461        print STDERR localtime() . " [+] Data len: $enc_msg_len bytes\n" 
     
    474469    if ($debug) { 
    475470        ### make sure not to print non-printable stuff 
    476         my $data_tmp = $transport_obj->{'data'}
     471        my $data_tmp = $transport_data
    477472        $data_tmp =~ s/[^\x20-\x7e]/NA/g; 
    478473        print STDERR localtime() . 
     
    483478            print STDERR localtime() . 
    484479                "     Raw packet data (hex dump, minus packet headers):\n"; 
    485             &hex_dump($transport_obj->{'data'}); 
     480            &hex_dump($transport_data); 
    486481        } 
    487482    } 
     
    489484    ### see if this packet is worthy of being granted access through 
    490485    ### the firewall 
    491     &SPA_check_grant_access($src_ip, $enc_msg_len, $transport_obj->{'data'}); 
     486    &SPA_check_grant_access($src_ip, $enc_msg_len, $transport_data); 
    492487 
    493488    &collect_warn_die_msgs(); 
    494489 
    495490    return; 
     491} 
     492 
     493sub ethernet_strip() { 
     494    my $pkt = shift; 
     495 
     496    ### Silently return '' for short frames 
     497    return substr($pkt, 14); 
     498} 
     499 
     500sub ip_addr_bytes_to_string() { 
     501    my $bytes = shift; 
     502 
     503    my ($a, $b, $c, $d) = unpack('C[4]', $bytes); 
     504    return "$a.$b.$c.$d"; 
     505} 
     506 
     507sub ip_decode() { 
     508    my $pkt = shift; 
     509 
     510    my $ip = {}; 
     511    (my $ver_ihl, $ip->{'tos'}, $ip->{'len'}, $ip->{'id'}, my $flags_frag, 
     512     $ip->{'ttl'}, $ip->{'proto'}, $ip->{'cksum'}, my $src_ip, my $dest_ip) 
     513        = unpack("CCnnnCCna[4]a[4]", $pkt); 
     514    $ip->{'ver'} = $ver_ihl >> 4; 
     515    $ip->{'hlen'} = $ver_ihl & 0x0F; 
     516    $ip->{'flags'} = $flags_frag >> 13; 
     517    $ip->{'foffset'} = ($flags_frag & 0x1FFF) * 8; 
     518    $ip->{'src_ip'} = &ip_addr_bytes_to_string($src_ip); 
     519    $ip->{'dest_ip'} = &ip_addr_bytes_to_string($dest_ip); 
     520    my $data_start = $ip->{'hlen'} * 4; 
     521    if ($data_start < 20) { 
     522        $data_start = 20; 
     523    } 
     524    $ip->{'data'} = substr($pkt, $data_start); 
     525    return $ip; 
     526} 
     527 
     528sub icmp_decode_data() { 
     529    my $icmp = shift; 
     530 
     531    ### Silently return '' for short packets 
     532    return substr($icmp, 4); 
     533} 
     534 
     535sub tcp_decode_data() { 
     536    my $tcp = shift; 
     537 
     538    ### Silently return '' for short packets 
     539    if (length($tcp) < 20) { 
     540        return ''; 
     541    } 
     542    my $data_start = 4 * (ord(substr($tcp, 12, 1)) >> 4); 
     543    if ($data_start < 20) { 
     544        $data_start = 20; 
     545    } 
     546    return substr($tcp, $data_start); 
     547} 
     548 
     549sub udp_decode_data() { 
     550    my $udp = shift; 
     551 
     552    ### Silently return '' for short packets 
     553    return substr($udp, 8); 
    496554} 
    497555