Changeset 1201

Show
Ignore:
Timestamp:
08/12/08 22:03:02 (4 months ago)
Author:
mbr
Message:

merged -r 1181:1200 file:///home/mbr/svn/fwknop_repos/fwknop/branches/fwknop-redhat-integration into trunk for Mirek's patches

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fwknop/trunk/CREDITS

    r1170 r1201  
    204204    - Suggested that the "C" locale be set by default so that gpg process 
    205205      output would always be correctly interpreted. 
     206 
     207Mirek Trmac 
     208    - Contributed patches to allow fwknop to be bundled with Fedora.  These 
     209      patches included the following, and all were sponsored by Red Hat: 
     210 
     211        Updates to fwknopd to remove the NetPacket module as a dependency 
     212      (this is a particularly important update since it assists with getting 
     213      fwknop bundled with Debian as well).  The patch manually decodes the 
     214      network and transport layer headers. 
     215        A patch to make the fwknop init script not start fwknopd by default 
     216      on Red Hat systems.  This patch also supports Fedora init script 
     217      conventions better (i.e. fwknop instead of the fwknopd name for the lock 
     218      file in /var/lock/subsys). 
     219        Updated the fwknop Makefile to respect the OPTS variable which is used 
     220      in the RPM spec file. 
     221        Bugfix in fwknop_serv to support the variable expansion code from 
     222      fwknopd.  This was important for the TCPSERV_PID_FILE file which is 
     223      defined as $FWKNOP_RUN_DIR/fwknop_serv.pid. 
     224        Updated fwknopd to use the Net::Pcap API valid in Net::Pcap 0.14 for 
     225      the datalink() function (used to detect the datalink layer type). 
  • fwknop/trunk/ChangeLog

    r1173 r1201  
     1fwknop-1.9.7 (08//2008): 
     2    - Mirek Trmac from Red Hat contributed several patches so that fwknop can 
     3      be bundled within the Fedora Linux distribution.  These patches 
     4      implemented the following changes: 
     5 
     6        Updates to fwknopd to remove the NetPacket module as a dependency 
     7      (this is a particularly important update since it assists with getting 
     8      fwknop bundled with Debian as well).  The patch manually decodes the 
     9      network and transport layer headers. 
     10        A patch to make the fwknop init script not start fwknopd by default 
     11      on Red Hat systems.  This patch also supports Fedora init script 
     12      conventions better (i.e. fwknop instead of the fwknopd name for the lock 
     13      file in /var/lock/subsys). 
     14        Updated the fwknop Makefile to respect the OPTS variable which is used 
     15      in the RPM spec file. 
     16        Bugfix in fwknop_serv to support the variable expansion code from 
     17      fwknopd.  This was important for the TCPSERV_PID_FILE file which is 
     18      defined as $FWKNOP_RUN_DIR/fwknop_serv.pid. 
     19        Updated fwknopd to use the Net::Pcap API valid in Net::Pcap-0.14 for 
     20      the datalink() function (used to detect the datalink layer type). 
     21 
     22    - Updated fwknop, fwknopd, and knoptm to import perl modules out of the 
     23      /usr/lib/fwknop/ directory if it exists.  This allows the perl module 
     24      path to be manipulated via the --Lib-dir command line argument and 
     25      'require' statements instead of the old 'use module' strategy. 
     26 
    127fwknop-1.9.6 (07/18/2008): 
    228    - SPA packets are base64-encoded by the fwknop client, and this encoding 
  • fwknop/trunk/INSTALL

    r679 r1201  
    1010this case fwknop can only be used as an authentication client against an 
    1111fwknop server on a different system. 
     12 
     13DEPENDENCIES: 
     14    fwknop requires several perl modules that may or may not already be 
     15installed on your Linux system.  These modules are included in the deps/ 
     16directory in the fwknop sources (unless you have installed one of the -nodeps 
     17tarballs), and the list of modules is: 
     18 
     19Class-MethodMaker 
     20Crypt-CBC 
     21Crypt-Rijndael 
     22Digest-SHA 
     23GnuPG-Interface 
     24IPTables-ChainMgr 
     25IPTables-Parse 
     26Net-IPv4Addr 
     27Net-Pcap 
     28Net-RawIP 
     29TermReadKey 
     30Unix-Syslog 
  • fwknop/trunk/Makefile

    r512 r1201  
    2525# 
    2626 
     27OPTS = -Wall -O 
     28 
    2729### default 
    2830all : knopmd.c knopwatchd.c fwknop_funcs.c strlcpy.c strlcat.c fwknop.h 
    29         /usr/bin/gcc -Wall -O knopmd.c fwknop_funcs.c strlcpy.c strlcat.c -o knopmd 
    30         /usr/bin/gcc -Wall -O knopwatchd.c fwknop_funcs.c strlcpy.c strlcat.c -o knopwatchd 
     31        /usr/bin/gcc $(OPTS) knopmd.c fwknop_funcs.c strlcpy.c strlcat.c -o knopmd 
     32        /usr/bin/gcc $(OPTS) knopwatchd.c fwknop_funcs.c strlcpy.c strlcat.c -o knopwatchd 
    3133 
    3234### debug mode 
  • fwknop/trunk/fwknop

    r1174 r1201  
    55# File: fwknop 
    66# 
     7# URL: http://www.cipherdyne.org/fwknop 
     8# 
    79# Purpose: fwknop implements an authorization scheme known as Single Packet 
    8 #          Authorization (SPA) that requires only a single encrypted packet 
    9 #          to communicate various pieces of information including desired 
    10 #          access through an iptables policy and/or specific commands to 
    11 #          execute on the target system.  The main application of this 
    12 #          program is to protect services such as SSH with an additional 
    13 #          layer of security in order to make the exploitation of 
    14 #          vulnerabilities (both 0-day and unpatched code) much more 
    15 #          difficult.  For more information, see the fwknop(8) man page. 
     10#          Authorization (SPA) that requires only a single encrypted packet to 
     11#          communicate various pieces of information including desired access 
     12#          through an iptables/ipfw policy and/or specific commands to execute 
     13#          on the target system.  The main application of this program is to 
     14#          protect services such as SSH with an additional layer of security 
     15#          in order to make the exploitation of vulnerabilities (both 0-day 
     16#          and unpatched code) much more difficult.  fwknop also supports 
     17#          encrypted port knocking, but this is a legacy authentication mode 
     18#          when compared to SPA. 
     19
     20#          More information can be found in the fwknop(8) and fwknopd(8) man 
     21#          pages, and also online here: 
     22
     23#          http://www.cipherdyne.org/fwknop/docs/ 
    1624# 
    1725# Author: Michael Rash (mbr@cipherdyne.org) 
     
    2129# Copyright (C) 2004-2008 Michael Rash (mbr@cipherdyne.org) 
    2230# 
    23 # License (GNU Public License)
     31# License - GNU Public License version 2
    2432# 
    2533#    This program is distributed in the hope that it will be useful, 
     
    3846# 
    3947 
    40 use lib '/usr/lib/fwknop'; 
    41 use Crypt::CBC; 
    42 use Net::IPv4Addr qw(ipv4_in_network); 
    43 use Net::Ping::External qw(ping); 
    4448use IO::Socket; 
    4549use IO::Handle; 
     
    4751use Data::Dumper; 
    4852use POSIX; 
    49 use Term::ReadKey; 
    5053use Getopt::Long; 
    5154use strict; 
     
    5659($rev_num) = $revision_svn =~ m|\$Rev.*:\s+(\S+)|; 
    5760 
     61my $lib_dir = '/usr/lib/fwknop'; 
    5862my $print_version = 0; 
    5963my $print_help    = 0; 
     
    258262$digest_type = $SHA256_DIGEST if $use_sha256; 
    259263 
     264### import fwknop perl modules 
     265&import_perl_modules(); 
     266 
    260267### this is only necessary for older versions of perl (newer versions 
    261268### call srand() automatically at the first usage of rand() if srand() 
     
    812819sub pcap_Rijndael_encrypt_msg() { 
    813820    my $msg = shift; 
     821 
     822    require Crypt::CBC; 
    814823 
    815824    my $cipher = Crypt::CBC->new({ 
     
    10251034        ### the remote firewall is configued to not reply at all 
    10261035        if ($proto eq 'icmp') { 
     1036            require Net::Ping::External; 
     1037            Net::Ping::External->import(qw/ping/); 
    10271038            print "    icmp echo request -> $knock_dst\n"; 
    10281039            ping(hostname => "$knock_dst", count => 1, timeout => 1); 
     
    10571068    my @encrypted_seq = (); 
    10581069 
     1070    require Crypt::CBC; 
     1071 
    10591072    my $cipher = Crypt::CBC->new({ 
    10601073        'key'    => $enc_key, 
     
    12891302        my $try = 0; 
    12901303        my $max_tries = 20; 
    1291         ReadMode 'noecho'
     1304        ReadMode('noecho')
    12921305        KEY: while (1) { 
    12931306            $try++; 
     
    12991312                print "Encryption Key: "; 
    13001313            } 
    1301             my $ans = ReadLine 0
     1314            my $ans = ReadLine(0)
    13021315            next KEY unless defined $ans; 
    13031316            next KEY unless $ans =~ /\S/; 
     
    13161329            } 
    13171330        } 
    1318         ReadMode 'normal'
     1331        ReadMode('normal')
    13191332        print "\n"; 
    13201333 
     
    13331346    } 
    13341347    return; 
     1348} 
     1349 
     1350sub import_perl_modules() { 
     1351 
     1352    my $mod_paths_ar = &get_mod_paths(); 
     1353 
     1354    if ($#$mod_paths_ar > -1) {  ### /usr/lib/fwknop/ exists 
     1355        push @$mod_paths_ar, @INC; 
     1356        splice @INC, 0, $#$mod_paths_ar+1, @$mod_paths_ar; 
     1357    } 
     1358 
     1359    if ($debug) { 
     1360        print STDERR "[+] import_perl_modules(): The \@INC array:\n"; 
     1361        print STDERR "$_\n" for @INC; 
     1362    } 
     1363 
     1364    require Net::IPv4Addr; 
     1365    require Term::ReadKey; 
     1366 
     1367    Term::ReadKey->import(qw/ReadMode ReadLine/); 
     1368    Net::IPv4Addr->import(qw/ipv4_in_network/); 
     1369 
     1370    return; 
     1371} 
     1372 
     1373sub get_mod_paths() { 
     1374 
     1375    my @paths = (); 
     1376 
     1377    unless (-d $lib_dir) { 
     1378        my $dir_tmp = $lib_dir; 
     1379        $dir_tmp =~ s|lib/|lib64/|; 
     1380        if (-d $dir_tmp) { 
     1381            $lib_dir = $dir_tmp; 
     1382        } else { 
     1383            return []; 
     1384        } 
     1385    } 
     1386 
     1387    opendir D, $lib_dir or die "[*] Could not open $lib_dir: $!"; 
     1388    my @dirs = readdir D; 
     1389    closedir D; 
     1390 
     1391    push @paths, $lib_dir; 
     1392 
     1393    for my $dir (@dirs) { 
     1394        ### get directories like "/usr/lib/fwknop/x86_64-linux" 
     1395        next unless -d "$lib_dir/$dir"; 
     1396        push @paths, "$lib_dir/$dir" 
     1397            if $dir =~ m|linux| or $dir =~ m|thread|; 
     1398    } 
     1399    return \@paths; 
    13351400} 
    13361401 
     
    14321497sub handle_server_auth_method() { 
    14331498    if (lc($server_auth_method) eq 'crypt') { 
    1434         ReadMode 'noecho'
     1499        ReadMode('noecho')
    14351500        while (1) { 
    14361501            $quiet == 1 ? print "UNIX crypt() password: " 
    14371502                : print "    UNIX crypt() password: "; 
    1438             my $ans = ReadLine 0
     1503            my $ans = ReadLine(0)
    14391504            chomp $ans; 
    14401505            next unless $ans =~ /\S/; 
     
    14421507            last; 
    14431508        } 
    1444         ReadMode 'normal'
     1509        ReadMode('normal')
    14451510        print "\n"; 
    14461511        return; 
     
    15501615        'Include-gpg-prefix' => \$include_base64_gnupg_prefix, 
    15511616        'Test-mode'      => \$test_mode, 
     1617        'Lib-dir=s'      => \$lib_dir, 
    15521618        'LC_ALL=s'       => \$locale, 
    15531619        'locale=s'       => \$locale, 
     
    17951861                                 arguments as in the previous invocation. 
    17961862                                 The args are stored in ~/fwknop.run. 
    1797     -L, --Last-host <host>     - Run last command line arguments for <host>. 
     1863    --Last-host <host>         - Run last command line arguments for <host>. 
    17981864    --gpg-signing-key <key ID> - ID for key used to sign GnuPG encrypted 
    17991865                                 message (e.g. "0xABCD1234"). 
     
    19241990                                 knock sequence (synonym for -D). 
    19251991    -d, --debug                - Run fwknop in debugging mode. 
     1992    --Lib-dir <path>           - Path to the perl modules directory (not 
     1993                                 usually necessary). 
    19261994    --locale <locale>          - Manually define a locale setting. 
    19271995    --no-locale                - Don't set the locale to anything (the 
  • fwknop/trunk/fwknop_funcs.c

    r1144 r1201  
    6060 
    6161    /* read the first line of the pid_file, which will contain the 
    62      * process id of any running fwkmd process */ 
     62     * process id of any running fwknop process */ 
    6363    if (fgets(pid_line, MAX_PID_SIZE+1, pidfile_ptr) == NULL) { 
    6464        return; 
  • fwknop/trunk/fwknop_serv

    r1174 r1201  
    6262 
    6363### trivial loop; we just want the local TCP stack to accept connections; 
    64 ### fwknopd gets it data from pcap anyway 
     64### fwknopd gets data from pcap anyway 
    6565while (my $client = $server->accept()) { 
    6666 
     
    9494    ### import config 
    9595    &import_config(); 
     96 
     97    ### expand any embedded vars within config values 
     98    &expand_vars(); 
    9699 
    97100    ### make sure all the vars we need are actually in the config file. 
     
    170173    while (<C>) { 
    171174        next if /^\s*#/; 
    172         for my $var (@required_vars) { 
    173             if (/^\s*$var\s+(\S+);/) { 
    174                 $config{$var} = $1; 
    175             } 
     175        if (/^\s*(\S+)\s+(\S+);/) { 
     176            $config{$1} = $2; 
    176177        } 
    177178    } 
     
    179180    return; 
    180181} 
     182 
     183sub expand_vars() { 
     184 
     185    my $has_sub_var = 1; 
     186    my $resolve_ctr = 0; 
     187 
     188    while ($has_sub_var) { 
     189        $resolve_ctr++; 
     190        $has_sub_var = 0; 
     191        if ($resolve_ctr >= 20) { 
     192            die "[*] Exceeded maximum variable resolution counter."; 
     193        } 
     194        for my $var (keys %config) { 
     195            my $val = $config{$var}; 
     196            if ($val =~ m|\$(\w+)|) { 
     197                my $sub_var = $1; 
     198                die "[*] sub-ver $sub_var not allowed within same ", 
     199                    "variable $var" if $sub_var eq $var; 
     200                if (defined $config{$sub_var}) { 
     201                    $val =~ s|\$$sub_var|$config{$sub_var}|; 
     202                    $config{$var} = $val; 
     203                } else { 
     204                    die "[*] sub-var \"$sub_var\" not defined in ", 
     205                        "config for var: $var." 
     206                } 
     207                $has_sub_var = 1; 
     208            } 
     209        } 
     210    } 
     211    return; 
     212} 
  • fwknop/trunk/fwknopd

    r1174 r1201  
    1818#          page. 
    1919# 
     20#          More information can be found in the fwknop(8) and fwknopd(8) man 
     21#          pages, and also online here: 
     22# 
     23#          http://www.cipherdyne.org/fwknop/docs/ 
     24# 
    2025# Author: Michael Rash (mbr@cipherdyne.org) 
    2126# 
     
    2429# Copyright (C) 2004-2008 Michael Rash (mbr@cipherdyne.org) 
    2530# 
    26 # License (GNU Public License)
     31# License - GNU Public License version 2
    2732# 
    2833#    This program is distributed in the hope that it will be useful, 
     
    4146# 
    4247 
    43 use lib '/usr/lib/fwknop'; 
    44 use Crypt::CBC; 
    45 use Unix::Syslog qw(:subs :macros); 
    46 use Net::IPv4Addr qw(ipv4_in_network); 
    47 use 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; 
     
    9388my $packet_ctr     = 0; 
    9489my $packet_limit   = 0; 
     90my $lib_dir        = ''; 
    9591my $fw_list        = 0; 
    9692my $fw_type        = ''; 
     
    10096my $test_mode      = 0; 
    10197my $verbose        = 0; 
    102 my $use_gpg        = 0; 
     98my $imported_gpg   = 0; 
    10399my $os_ipt_log     = ''; 
    104100my $cmdline_intf   = ''; 
     
    110106my $build_ipt_config = 0; 
    111107my $skipped_first_loop = 0; 
     108my $imported_crypt_cbc = 0; 
    112109my $pcap_sleep_interval = 1;  ### seconds 
    113110my $imported_iptables_modules = 0; 
     
    214211    'T' => $tcp_timestamp_type 
    215212); 
     213 
     214my $ETH_HDR_LEN      = 14; 
     215my $MIN_IP_HDR_LEN   = 20; 
     216my $MIN_ICMP_HDR_LEN = 4; 
     217my $UDP_HDR_LEN      = 8; 
     218my $MIN_TCP_HDR_LEN  = 20; 
    216219 
    217220my %access_keys = ( 
     
    286289    ### we are parsing the pcap file created by the ulogd pcap 
    287290    ### writer, or in sniffing mode against an interface 
     291 
     292    require Net::Pcap; 
     293 
    288294    &pcap_loop(); 
    289295} 
     
    304310    eval { 
    305311        if (not $PCAP_COOKED_INTF and $Net::Pcap::VERSION > 0.05) { 
    306             if (Net::Pcap::pcap_datalink_val_to_name( 
    307                     Net::Pcap::pcap_datalink($pcap_t)) eq 'LINUX_SLL') { 
     312            if (Net::Pcap::datalink_val_to_name( 
     313                    Net::Pcap::datalink($pcap_t)) eq 'LINUX_SLL') { 
    308314                print STDERR "[+] Detected Linux Cooked Interface.\n" if $debug; 
    309315                $PCAP_COOKED_INTF = 1; 
     
    393399    my $src_ip     = ''; 
    394400    my $proto      = ''; 
    395     my $transport_obj = ''; 
     401    my $transport_data = ''; 
    396402 
    397403    if ($debug) { 
     
    421427    if ($config{'AUTH_MODE'} eq 'ULOG_PCAP') { 
    422428        ### The ulogd pcap writer does not include link layer information 
    423         $ip = NetPacket::IP->decode($pkt) or return; 
     429        $ip = &ip_decode($pkt) or return; 
    424430    } else { 
    425431        if ($config{'FIREWALL_TYPE'} eq 'ipfw' and $cmdline_intf eq 'lo0') { 
     
    429435            $pkt =~ s/^.{4}// if $pkt =~ /^[^\x45].{3}\x45/; 
    430436 
    431             $ip = NetPacket::IP->decode($pkt) or return; 
     437            $ip = &ip_decode($pkt) or return; 
    432438        } else { 
    433439            if ($PCAP_COOKED_INTF) { 
    434440                $ether_data = unpack("x[16]a*", $pkt); 
    435441            } else { 
    436                 $ether_data = NetPacket::Ethernet::strip($pkt) or return; 
    437             } 
    438             $ip = NetPacket::IP->decode($ether_data) or return; 
     442                $ether_data = &ethernet_strip($pkt) or return; 
     443            } 
     444            $ip = &ip_decode($ether_data) or return; 
    439445        } 
    440446    } 
     
    447453 
    448454    if ($proto == 1) { 
    449         $transport_obj = NetPacket::ICMP->decode($ip->{'data'}); 
     455        $transport_data = &icmp_decode_data($ip->{'data'}); 
    450456    } elsif ($proto == 6) { 
    451         $transport_obj = NetPacket::TCP->decode($ip->{'data'}); 
     457        $transport_data = &tcp_decode_data($ip->{'data'}); 
    452458    } elsif ($proto == 17) { 
    453         $transport_obj = NetPacket::UDP->decode($ip->{'data'}); 
     459        $transport_data = &udp_decode_data($ip->{'data'}); 
    454460    } else { 
    455461        return; 
     
    459465    ### any valid SPA message will be longer than 10 bytes, but this 
    460466    ### check is better than nothing 
    461     return unless defined $transport_obj->{'data'}
     467    return if $transport_data eq ""
    462468 
    463469    my $enc_msg_len = 0; 
    464     $enc_msg_len = length($transport_obj->{'data'}); 
     470    $enc_msg_len = length($transport_data); 
    465471    if (10 < $enc_msg_len and $enc_msg_len < 1500) { 
    466472        print STDERR localtime() . " [+] Data len: $enc_msg_len bytes\n" 
     
    474480    if ($debug) { 
    475481        ### make sure not to print non-printable stuff 
    476         my $data_tmp = $transport_obj->{'data'}
     482        my $data_tmp = $transport_data
    477483        $data_tmp =~ s/[^\x20-\x7e]/NA/g; 
    478484        print STDERR localtime() . 
     
    483489            print STDERR localtime() . 
    484490                "     Raw packet data (hex dump, minus packet headers):\n"; 
    485             &hex_dump($transport_obj->{'data'}); 
     491            &hex_dump($transport_data); 
    486492        } 
    487493    } 
     
    489495    ### see if this packet is worthy of being granted access through 
    490496    ### the firewall 
    491     &SPA_check_grant_access($src_ip, $enc_msg_len, $transport_obj->{'data'}); 
     497    &SPA_check_grant_access($src_ip, $enc_msg_len, $transport_data); 
    492498 
    493499    &collect_warn_die_msgs(); 
    494500 
    495501    return; 
     502} 
     503 
     504sub ethernet_strip() { 
     505    my $pkt = shift; 
     506 
     507    my $eth_data = ''; 
     508 
     509    if (length($pkt) >= $ETH_HDR_LEN) { 
     510        $eth_data = substr($pkt, $ETH_HDR_LEN); 
     511    } 
     512    ### Silently return '' for short frames 
     513    return $eth_data; 
     514} 
     515 
     516sub ip_addr_bytes_to_string() { 
     517    my $bytes = shift; 
     518 
     519    my ($a, $b, $c, $d) = unpack('C[4]', $bytes); 
     520    return "$a.$b.$c.$d"; 
     521} 
     522 
     523sub ip_decode() { 
     524    my $pkt = shift; 
     525 
     526    my $ip = {}; 
     527    if (length($pkt) >= $MIN_IP_HDR_LEN and $pkt =~ /^\x45/) { 
     528        (my $ver_ihl, $ip->{'tos'}, $ip->{'len'}, $ip->{'id'}, my $flags_frag, 
     529         $ip->{'ttl'}, $ip->{'proto'}, $ip->{'cksum'}, my $src_ip, my $dest_ip) 
     530            = unpack("CCnnnCCna[4]a[4]", $pkt); 
     531        $ip->{'ver'} = $ver_ihl >> 4; 
     532        $ip->{'hlen'} = $ver_ihl & 0x0F; 
     533        $ip->{'flags'} = $flags_frag >> 13; 
     534        $ip->{'foffset'} = ($flags_frag & 0x1FFF) * 8; 
     535        $ip->{'src_ip'} = &ip_addr_bytes_to_string($src_ip); 
     536        $ip->{'dest_ip'} = &ip_addr_bytes_to_string($dest_ip); 
     537        my $data_start = $ip->{'hlen'} * 4; 
     538        if ($data_start >= $MIN_IP_HDR_LEN) { 
     539            $ip->{'data'} = substr($pkt, $data_start); 
     540        } 
     541    } 
     542    return $ip; 
     543} 
     544 
     545sub icmp_decode_data() { 
     546    my $icmp = shift; 
     547 
     548    my $icmp_data = ''; 
     549    if (length($icmp) >= $MIN_ICMP_HDR_LEN) { 
     550        $icmp_data = substr($icmp, $MIN_ICMP_HDR_LEN); 
     551    } 
     552    ### Silently return '' for short packets 
     553    return $icmp_data; 
     554} 
     555 
     556sub tcp_decode_data() { 
     557    my $tcp = shift; 
     558 
     559    my $tcp_data = ''; 
     560 
     561    if (length($tcp) >= $MIN_TCP_HDR_LEN) { 
     562 
     563        my $data_start = 4 * (ord(substr($tcp, 12, 1)) >> 4); 
     564        if ($data_start >= $MIN_TCP_HDR_LEN) { 
     565            $tcp_data = substr($tcp, $data_start); 
     566        } 
     567    } 
     568    ### Silently return '' for short packets 
     569    return $tcp_data; 
     570} 
     571 
     572sub udp_decode_data() { 
     573    my $udp = shift; 
     574 
     575    my $udp_data = ''; 
     576    if (length($udp) >= $UDP_HDR_LEN) { 
     577        $udp_data = substr($udp, $UDP_HDR_LEN); 
     578    } 
     579    ### Silently return '' for short packets 
     580    return $udp_data; 
    496581} 
    497582 
     
    22992384        &hex_dump(decode_base64($msg)); 
    23002385    } 
     2386 
    23012387    my $cipher = Crypt::CBC->new({ 
    23022388        'key'    => $enc_key, 
     
    32743360                    $access_hsh{'DATA_COLLECT_MODE'} = $ENCRYPT_SEQUENCE; 
    32753361                } elsif ($line =~ /^\s*KEY:\s*(.*)\s*;/) { 
     3362                    require Crypt::CBC unless $imported_crypt_cbc; 
     3363                    $imported_crypt_cbc = 1; 
    32763364                    $access_hsh{'KEY'} = $1; 
    32773365                    ### pad with zeros to the key size 
     
    32803368                    } 
    32813369                } elsif ($line =~ /^\s*GPG_REMOTE_ID:\s*(.*)\s*;/) { 
    3282                     require GnuPG::Interface unless $use_gpg; 
    3283                     $use_gpg = 1; 
     3370                    require GnuPG::Interface unless $imported_gpg; 
     3371                    $imported_gpg = 1; 
    32843372                    my @arr = split /\s*\,\s*/, $1; 
    32853373                    for my $gpg_key_id (@arr) { 
     
    32873375                    } 
    32883376                } elsif ($line =~ /^\s*GPG_DECRYPT_ID:\s*(.*)\s*;/) { 
    3289                     require GnuPG::Interface unless $use_gpg; 
    3290                     $use_gpg = 1; 
     3377                    require GnuPG::Interface unless $imported_gpg; 
     3378                    $imported_gpg = 1; 
    32913379                    $access_hsh{'GPG_DECRYPT_ID'} = $1; 
    32923380                } elsif ($line =~ /^\s*GPG_DECRYPT_PW:\s*(.*)\s*;/) { 
    3293                     require GnuPG::Interface unless $use_gpg; 
    3294                     $use_gpg = 1; 
     3381                    require GnuPG::Interface unless $imported_gpg; 
     3382                    $imported_gpg = 1; 
    32953383                    $access_hsh{'GPG_DECRYPT_PW'} = $1; 
    32963384                } elsif ($line =~ /^\s*GPG_HOME_DIR:\s*(\S+)\s*;/) { 
    3297                     require GnuPG::Interface unless $use_gpg; 
    3298                     $use_gpg = 1; 
     3385                    require GnuPG::Interface unless $imported_gpg; 
     3386                    $imported_gpg = 1; 
    32993387                    $access_hsh{'GPG_HOME_DIR'} = $1; 
    33003388                } elsif ($line =~ /^\s*FILE_PCAP\s*;/) { 
     
    37763864        'LC_ALL=s'       => \$cmdline_locale, 
    37773865        'locale=s'       => \$cmdline_locale, 
     3866        'Lib-dir=s'      => \$lib_dir, 
    37783867        'no-LC_ALL'      => \$no_locale, 
    37793868        'no-locale'      => \$no_locale, 
     
    39564045    ### make sure command paths are correct 
    39574046    &check_commands() unless $os_fprint_only; 
     4047 
     4048    ### import fwknop perl modules 
     4049    &import_perl_modules(); 
    39584050 
    39594051    if ($fw_del_ip) { 
     
    46534745} 
    46544746 
     4747sub import_perl_modules() { 
     4748 
     4749    my $mod_paths_ar = &get_mod_paths(); 
     4750 
     4751    if ($#$mod_paths_ar > -1) {  ### /usr/lib/fwknop/ exists 
     4752        push @$mod_paths_ar, @INC; 
     4753        splice @INC, 0, $#$mod_paths_ar+1, @$mod_paths_ar; 
     4754    } 
     4755 
     4756    if ($debug) { 
     4757        print STDERR "[+] import_perl_modules(): The \@INC array:\n"; 
     4758        print STDERR "$_\n" for @INC; 
     4759    } 
     4760 
     4761    require Unix::Syslog unless $config{'ALERTING_METHODS'} =~ /no.?syslog/i; 
     4762 
     4763    Unix::Syslog->import(qw(:subs :macros)) 
     4764        unless $config{'ALERTING_METHODS'} =~ /no.?syslog/i; 
     4765 
     4766    require Net::IPv4Addr; 
     4767    Net::IPv4Addr->import(qw/ipv4_in_network/); 
     4768 
     4769    return; 
     4770} 
     4771 
     4772sub get_mod_paths() { 
     4773 
     4774    my @paths = (); 
     4775 
     4776    $config{'FWKNOP_MOD_DIR'} = $lib_dir if $lib_dir; 
     4777 
     4778    unless (-d $config{'FWKNOP_MOD_DIR'}) { 
     4779        my $dir_tmp = $config{'FWKNOP_MOD_DIR'}; 
     4780        $dir_tmp =~ s|lib/|lib64/|; 
     4781        if (-d $dir_tmp) { 
     4782            $config{'FWKNOP_MOD_DIR'} = $dir_tmp; 
     4783        } else { 
     4784            return []; 
     4785        } 
     4786    } 
     4787 
     4788    opendir D, $config{'FWKNOP_MOD_DIR'} 
     4789        or die "[*] Could not open $config{'FWKNOP_MOD_DIR'}: $!"; 
     4790    my @dirs = readdir D; 
     4791    closedir D; 
     4792 
     4793    push @paths, $config{'FWKNOP_MOD_DIR'}; 
     4794 
     4795    for my $dir (@dirs) { 
     4796        ### get directories like "/usr/lib/fwknop/x86_64-linux" 
     4797        next unless -d "$config{'FWKNOP_MOD_DIR'}/$dir"; 
     4798        push @paths, "$config{'FWKNOP_MOD_DIR'}/$dir" 
     4799            if $dir =~ m|linux| or $dir =~ m|thread|; 
     4800    } 
     4801    return \@paths; 
     4802} 
     4803 
    46554804sub import_digests() { 
    46564805 
     
    49385087            SNAT_TRANSLATE_IP PROC_IP_FORWARD_FILE ENABLE_PROC_IP_FORWARD 
    49395088            MIN_SPA_PKT_LEN ENABLE_IPT_LOCAL_NAT LOCALE ENABLE_SYSLOG_FILE 
    4940             IPT_SYSLOG_FILE 
     5089            IPT_SYSLOG_FILE FWKNOP_MOD_DIR 
    49415090    ) { 
    49425091        die "[*] Required variable $var is not defined in $config_file" 
     
    49895138                                 key information) when running in --debug 
    49905139                                 and --verbose mode. 
    4991     -L, --Linux-cooked-intf    - Force fwknopd to assume that the sniffing 
     5140    --Linux-cooked-intf        - Force fwknopd to assume that the sniffing 
    49925141                                 interface is a "Linux Cooked" interface. 
    49935142                                 This is useful when fwknopd uses a version 
     
    49955144                                 pcap_datalink_val_to_name() function or 
    49965145                                 have the pcap_datali.al file. 
     5146    --Lib-dir <path>           - Path to the perl modules directory (not 
     5147                                 usually necessary). 
    49975148    -d, --debug                - Run fwknopd in debugging mode. 
    49985149    --locale <locale>          - Manually define a locale setting. 
  • fwknop/trunk/init-scripts/fwknop-init.redhat

    r474 r1201  
    33# Startup script for fwknop 
    44# 
    5 # chkconfig: 345 99 05 
     5# chkconfig: - 99 05 
    66# description: The FireWall KNock OPerator (fwknop) 
    77# processname: fwknop 
     
    2727    echo 
    2828    if [ $RETVAL -eq 0 ]; then 
    29         touch /var/lock/subsys/fwknopd 
     29        touch /var/lock/subsys/fwknop 
    3030    fi 
    3131    ;; 
     
    3737        killproc fwknopd 
    3838    RETVAL=$? 
    39     [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/fwknopd 
     39    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/fwknop 
    4040    echo 
    4141    if [ -f /var/run/fwknop/knopmd.pid ]; then 
     
    6868    restart 
    6969    ;; 
    70 condrestart
    71     [ -f /var/lock/subsys/fwknopd ] && restart || : 
     70condrestart|try-restart
     71    [ -f /var/lock/subsys/fwknop ] && restart || : 
    7272    ;; 
    7373*) 
    7474    echo "Usage: fwknop {start|stop|status|restart|reload|condrestart}" 
    75     exit 1 
     75    exit 3 
    7676esac 
  • fwknop/trunk/install.pl

    r1150 r1201  
    6969my $data_method = ''; 
    7070my $runlevel = -1; 
     71my $deps_dir = 'deps'; 
    7172my $init_dir = '/etc/init.d'; 
    7273my $init_name = 'fwknop'; 
     
    240241&import_config(); 
    241242 
     243### see if the deps/ directory exists, and if not then we are installing 
     244### from the -nodeps sources so don't install any perl modules 
     245$skip_module_install = 1 unless -d $deps_dir; 
     246 
    242247### check to see if we are installing as a non-root user 
    243248&check_non_root_user() unless $client_install; 
     
    384389            &install_perl_module($mod_href); 
    385390        } 
    386     } 
    387  
    388     ### special case the NetPacket::<proto> modules since the NetPacket 
    389     ### directory is just for the base class, and we need to make sure 
    390     ### we have each of the NetPacket::IP, NetPacket::ICMP, NetPacket::UDP, 
    391     ### and NetPacket::TCP modules. 
    392     unless ($skip_module_install or $client_install) { 
    393         chdir 'NetPacket' or die "[*] Could not chdir NetPacket directory: $!"; 
    394         unless (-e 'Makefile.PL') { 
    395             die "[*] Your NetPacket source directory appears to be incomplete!\n", 
    396                 "    Download the latest sources from ", 
    397                 "http://www.cipherdyne.org\n"; 
    398         } 
    399         system "$cmds{'make'} clean" if -e 'Makefile'; 
    400         system "$cmds{'perl'} Makefile.PL PREFIX=$config{'FWKNOP_MOD_DIR'} " . 
    401             "LIB=$config{'FWKNOP_MOD_DIR'}"; 
    402         system $cmds{'make'}; 
    403 #        system "$cmds{'make'} test"; 
    404         system "$cmds{'make'} install"; 
    405         chdir $src_dir or die "[*] Could not chdir $src_dir: $!"; 
    406391    } 
    407392 
     
    11691154sub install_perl_module() { 
    11701155    my $mod_hr = shift; 
     1156 
     1157    chdir $src_dir or die "[*] Could not chdir $src_dir: $!"; 
     1158    chdir $deps_dir or die "[*] Could not chdir($deps_dir): $!"; 
    11711159 
    11721160    for my $key qw/module force-install client-mode-install mod-dir/ { 
  • fwknop/trunk/knoptm

    r1174 r1201  
    4040# 
    4141 
    42 use lib '/usr/lib/fwknop'; 
    43 use Unix::Syslog qw(:subs :macros); 
    44 use Net::IPv4Addr qw(ipv4_in_network); 
    4542use IO::Socket; 
    4643use IO::Handle; 
     
    6259my $print_ver  = 0; 
    6360my $debug      = 0; 
     61my $lib_dir    = ''; 
    6462my $die_msg    = ''; 
    6563my $warn_msg   = ''; 
     
    9593    'no-voluntary-exits' => \$no_voluntary_exits, 
    9694    'no-logs'   => \$no_logs, 
     95    'Lib-dir=s' => \$lib_dir, 
    9796    'LC_ALL=s'  => \$cmdline_locale, 
    9897    'locale=s'  => \$cmdline_locale, 
     
    536535    ### make sure all the vars we need are actually in the config file. 
    537536    &required_vars(); 
     537 
     538    ### import all necessary perl modules 
     539    &import_perl_modules(); 
    538540 
    539541    ### validate config 
     
    744746            KNOPTM_SYSLOG_FACILITY KNOPTM_SYSLOG_PRIORITY 
    745747            ENABLE_VOLUNTARY_EXITS EXIT_INTERVAL FWKNOP_PID_FILE 
    746             LOCALE 
     748            LOCALE FWKNOP_MOD_DIR 
    747749    ) { 
    748750 
     
    844846    } 
    845847    return; 
     848} 
     849 
     850sub import_perl_modules() { 
     851 
     852    my $mod_paths_ar = &get_mod_paths(); 
     853 
     854    if ($#$mod_paths_ar > -1) {  ### /usr/lib/fwknop/ exists 
     855        push @$mod_paths_ar, @INC; 
     856        splice @INC, 0, $#$mod_paths_ar+1, @$mod_paths_ar; 
     857    } 
     858 
     859    if ($debug) { 
     860        print STDERR "[+] import_perl_modules(): The \@INC array:\n"; 
     861        print STDERR "$_\n" for @INC; 
     862    } 
     863 
     864    require Unix::Syslog unless $config{'ALERTING_METHODS'} =~ /no.?syslog/i; 
     865 
     866    Unix::Syslog->import(qw(:subs :macros)) 
     867        unless $config{'ALERTING_METHODS'} =~ /no.?syslog/i; 
     868 
     869    require Net::IPv4Addr; 
     870    Net::IPv4Addr->import(qw/ipv4_in_network/);