Changeset 1664

Show
Ignore:
Timestamp:
11/16/06 20:59:00 (2 years ago)
Author:
mbr
Message:

added code to differentiate between fwsnort sig matches vs. psad sig matches

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • psad/branches/sigdevel/psad

    r1662 r1664  
    229229### (http://www.cipherdyne.org/fwsnort/) will automatically 
    230230### build such a ruleset from snort signatures. 
    231 my %snort_msgs = (); 
     231my %fwsnort_sigs = (); 
    232232 
    233233### Cache snort classification.config file for class priorities 
     
    12451245        unless ($no_snort_sids) { 
    12461246            if ($pkt{'fwsnort_sid'}) { 
     1247 
    12471248                ### found a snort sid in the packet log message 
    12481249                my $dl = &add_snort_sid(\%pkt); 
     1250 
    12491251                $curr_sids_dl{$pkt{'src'}} = $dl if $dl; 
     1252 
    12501253            } else { 
    12511254                ### attempt to match any tcp/udp/icmp signatures in the 
     
    14101413                ($pkt_hr->{'flags'} =~ /ACK/ || $pkt_hr->{'flags'} =~ /RST/)) { 
    14111414 
    1412 ###                 $dp > 1024 && ($pkt_hr->{'flags'} =~ /ACK/ || 
     1415            ###                 $dp > 1024 && ($pkt_hr->{'flags'} =~ /ACK/ || 
    14131416 
    14141417            ### FIXME: ignore TCP packets that have the ACK or RST 
     
    14221425            return $PKT_IGNORE; 
    14231426        } 
     1427 
    14241428        ### per page 595 of the Camel book, "if /blah1|blah2/" 
    14251429        ### can be slower than "if /blah1/ || /blah2/ 
     
    17041708                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'pkts'}++; 
    17051709 
     1710                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'tcp'} 
     1711                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 0; 
     1712 
    17061713            } 
    17071714        } elsif ($sig_hr->{'proto'} eq 'udp') { 
     
    17141721                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'dp'} 
    17151722                         = $pkt_hr->{'dp'}; 
     1723 
    17161724                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'udp'} 
    17171725                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'pkts'}++; 
     1726 
     1727                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'udp'} 
     1728                    {'sid'}{$sid}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 0; 
    17181729            } 
    17191730        } elsif ($sig_hr->{'proto'} eq 'icmp') { 
     
    17481759                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'icmp'}{'sid'} 
    17491760                    {$sid}{$pkt_hr->{'chain'}}{'pkts'}++; 
     1761 
     1762                $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'icmp'}{'sid'} 
     1763                    {$sid}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 0; 
    17501764            } 
    17511765        } 
     
    17811795            "$sig_hr->{'sid'} (psad_id: $sig_hr->{'psad_id'})\n"; 
    17821796    } 
    1783     return $sig_hr->{'dl'}; 
     1797 
     1798    return &get_largest_sid_dl($sig_hr->{'sid'}, 
     1799            $sig_hr->{'dl'}, $sig_hr->{'classtype'}); 
    17841800} 
    17851801 
     
    18021818            "$sig_hr->{'sid'} (psad_id: $sig_hr->{'psad_id'})\n"; 
    18031819    } 
    1804     return $sig_hr->{'dl'}; 
     1820 
     1821    return &get_largest_sid_dl($sig_hr->{'sid'}, 
     1822            $sig_hr->{'dl'}, $sig_hr->{'classtype'}); 
    18051823} 
    18061824 
     
    18281846            "$sig_hr->{'sid'} (psad_id: $sig_hr->{'psad_id'})\n"; 
    18291847    } 
    1830     return $sig_hr->{'dl'}; 
     1848 
     1849    return &get_largest_sid_dl($sig_hr->{'sid'}, 
     1850            $sig_hr->{'dl'}, $sig_hr->{'classtype'}); 
    18311851} 
    18321852 
     
    18551875            {$sig_hr->{'sid'}}{$pkt_hr->{'chain'}}{'pkts'}++; 
    18561876 
    1857         return $sig_hr->{'dl'} 
     1877        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{'ip'}{'sid'} 
     1878            {$sig_hr->{'sid'}}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 0; 
     1879 
     1880        return &get_largest_sid_dl($sig_hr->{'sid'}, 
     1881                $sig_hr->{'dl'}, $sig_hr->{'classtype'}); 
    18581882    } 
    18591883    return 1; 
     
    21712195} 
    21722196 
     2197sub get_largest_sid_dl() { 
     2198    my ($sid, $sig_dl, $classtype) = @_; 
     2199 
     2200    my $dl = $sig_dl; 
     2201 
     2202    ### see if /etc/psad/snort_rule_dl gives a higher DL 
     2203    if (defined $snort_rule_dl{$sid}) { 
     2204        if ($snort_rule_dl{$sid} > $dl) { 
     2205            print STDERR "[+] get_largest_sid_dl() snort_rule_dl ", 
     2206                "assigning SID $sid a danger level of ", 
     2207                "$snort_rule_dl{$sid}\n" if $debug; 
     2208            $dl = $snort_rule_dl{$sid}; 
     2209        } 
     2210    } 
     2211 
     2212    ### see if /etc/psad/snort_rules/classification.config gives 
     2213    ### a higher DL 
     2214    if (defined $snort_class_dl{$classtype}) { 
     2215        if ($snort_class_dl{$classtype} > $dl) { 
     2216            print STDERR "[+] get_largest_sid_dl() classification.config ", 
     2217                "assigning SID $sid a danger level of ", 
     2218                "$snort_class_dl{$classtype}\n" if $debug; 
     2219            $dl = $snort_class_dl{$classtype}; 
     2220        } 
     2221    } 
     2222 
     2223    return $dl; 
     2224} 
     2225 
    21732226sub add_snort_sid() { 
    21742227    my $pkt_hr = shift; 
     
    21762229    my $sid = $pkt_hr->{'fwsnort_sid'}; 
    21772230 
    2178     if (defined $snort_msgs{$sid}) { 
     2231    if (defined $fwsnort_sigs{$sid}) { 
    21792232 
    21802233        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}} 
    21812234            {'sid'}{$sid}{$pkt_hr->{'chain'}}{'pkts'}++; 
    21822235 
    2183         my $classtype = $snort_msgs{$sid}{'classtype'}; 
    2184  
    2185         ### first see if the /etc/psad/snort_rule_dl file has defined 
    2186         ### the danger level 
    2187         if (defined $snort_rule_dl{$sid}) { 
    2188             print STDERR "[+] snort_rule_dl assigning SID $sid a danger ", 
    2189                 "level of $snort_rule_dl{$sid}\n" if $debug; 
    2190             return $snort_rule_dl{$sid}; 
    2191         ### now see if the rule itself has a priority defined (note that 
    2192         ### this value has already been converted into a valid psad 
    2193         ### danger level. 
    2194         } elsif (defined $snort_msgs{$sid}{'priority'}) { 
    2195             print STDERR "[+] Snort rule contains priority field, ", 
    2196                 "assigning SID $sid a danger level of ", 
    2197                 "$snort_msgs{$sid}{'priority'}\n" if $debug; 
    2198             return $snort_msgs{$sid}{'priority'}; 
    2199         } elsif (defined $snort_class_dl{$classtype}) { 
    2200             print STDERR "[+] classification.config assigning SID $sid ", 
    2201                 "a danger level of $snort_class_dl{$classtype}\n" if $debug; 
    2202             return $snort_class_dl{$classtype}; 
    2203         } else { 
    2204             print STDERR "[-] No classification or rule dl defined for ", 
    2205                 "SID $sid\n" if $debug; 
    2206             return 2; 
    2207         } 
     2236        $scan{$pkt_hr->{'src'}}{$pkt_hr->{'dst'}}{$pkt_hr->{'proto'}} 
     2237            {'sid'}{$sid}{$pkt_hr->{'chain'}}{'is_fwsnort'} = 1; 
     2238 
     2239 
     2240        return &get_largest_sid_dl($sid, 2, $fwsnort_sigs{$sid}{'classtype'}); 
     2241 
    22082242    } 
    22092243    return 0; 
     
    27772811            } 
    27782812 
    2779             $snort_msgs{$sid}{'msg'} = $1 
     2813            $fwsnort_sigs{$sid}{'msg'} = $1 
    27802814                if $line =~ /msg:\s*\"(.*?)\"\s*;/; 
    27812815 
    2782             $snort_msgs{$sid}{'classtype'} = $1 
    2783                 if $line =~ /[\s;]classtype:\s*(.*?)\s*;/; 
    2784  
    2785             $snort_msgs{$sid}{'priority'} = &convert_snort_priority($1) 
     2816            if ($line =~ /[\s;]classtype:\s*(.*?)\s*;/) { 
     2817                $fwsnort_sigs{$sid}{'classtype'} = $1; 
     2818            } else { 
     2819                $fwsnort_sigs{$sid}{'classtype'} = ''; 
     2820            } 
     2821 
     2822            $fwsnort_sigs{$sid}{'priority'} = &convert_snort_priority($1) 
    27862823                if $line =~ /[\s;]priority:\s*(\d+)\s*;/; 
    27872824 
     
    27902827            ### multiple content strings. 
    27912828            while ($line =~ /[\s;](?:uri)?content:\s*\"(.*?)\"\s*;/g) { 
    2792                 push @{$snort_msgs{$sid}{'content'}}, $1; 
     2829                push @{$fwsnort_sigs{$sid}{'content'}}, $1; 
    27932830            } 
    27942831 
     
    27972834                if ($ref =~ /^(\w+),(\S+)/) { 
    27982835                    ### reference:bugtraq,9732; 
    2799                     push @{$snort_msgs{$sid}{'reference'}{lc($1)}}, $2; 
    2800                 } 
    2801             } 
    2802  
    2803             next RULE unless defined $snort_msgs{$sid}{'msg'} 
    2804                     and defined $snort_msgs{$sid}{'classtype'} 
    2805                     and defined $snort_msgs{$sid}{'content'}; 
     2836                    push @{$fwsnort_sigs{$sid}{'reference'}{lc($1)}}, $2; 
     2837                } 
     2838            } 
     2839 
     2840            next RULE unless defined $fwsnort_sigs{$sid}{'msg'} 
     2841                    and defined $fwsnort_sigs{$sid}{'classtype'} 
     2842                    and defined $fwsnort_sigs{$sid}{'content'}; 
    28062843        } 
    28072844    } 
     
    28172854    &import_snort_rule_dl(); 
    28182855 
    2819     print STDERR Dumper %snort_msgs if $debug and $verbose; 
     2856    print STDERR Dumper %fwsnort_sigs if $debug and $verbose; 
    28202857    &Psad::psyslog('psad', 'imported Snort rules') 
    28212858        unless $no_syslog_alerts; 
     
    30233060        } 
    30243061 
     3062        ### classtype field 
     3063        if ($rule_options =~ /[\s;]classtype:\s*(.+?)\s*;/) { 
     3064            $sig{'classtype'} = $1; 
     3065        } else { 
     3066            $sig{'classtype'} = ''; 
     3067        } 
     3068 
     3069        ### reference field 
     3070 
     3071        while ($rule_options =~ /[\s;]reference:\s*(.*?)\s*;/g) { 
     3072            my $ref = $1; 
     3073            if ($ref =~ /^(\w+),(\S+)/) { 
     3074                ### reference:bugtraq,9732; 
     3075                push @{$sig{'reference'}{lc($1)}}, $2; 
     3076            } 
     3077        } 
     3078 
    30253079        ### psad danger level 
    30263080        $sig{'dl'} = 2;  ### default danger level 
    3027         if ($rule_options =~ /psad_dl:\s*(\d+)/) { 
     3081        if ($rule_options =~ /[\s;]psad_dl:\s*(\d+)/) { 
    30283082            $sig{'dl'} = $1; 
    30293083        } 
     
    42874341            for my $sid (keys %{$href->{'sid'}}) { 
    42884342                for my $chain (keys %{$href->{'sid'}->{$sid}}) { 
    4289                     next unless defined $snort_msgs{$sid}; 
    4290                     my $sm_hr = $snort_msgs{$sid}; 
     4343 
     4344                    my $sig_hr = ''; 
     4345                    my $is_fwsnort = $href->{'sid'}->{$sid} 
     4346                            ->{$chain}->{'is_fwsnort'}; 
     4347 
     4348                    if ($is_fwsnort) { 
     4349                        next unless defined $fwsnort_sigs{$sid}; 
     4350                        $sig_hr = $fwsnort_sigs{$sid}; 
     4351                    } else { 
     4352                        next unless defined $sigs{$sid}; 
     4353                        $sig_hr = $sigs{$sid}; 
     4354                    } 
    42914355 
    42924356                    my $dp    = -1; 
     
    42984362                    my $pkts = $href->{'sid'}->{$sid}->{$chain}->{'pkts'}; 
    42994363 
    4300                     print $fh qq|   "$sm_hr->{'msg'}"\n|; 
     4364                    print $fh qq|   "$sig_hr->{'msg'}"\n|; 
    43014365                    if ($proto eq 'tcp' or $proto eq 'udp') { 
    43024366                        if ($chain eq 'INPUT') { 
     
    43144378                        } 
    43154379                    } 
    4316                     for my $content (@{$sm_hr->{'content'}}) { 
    4317                         print $fh qq(       content:   "$content"\n); 
     4380 
     4381                    if ($is_fwsnort) { 
     4382                        for my $content (@{$sig_hr->{'content'}}) { 
     4383                            print $fh qq(       content:   "$content"\n); 
     4384                        } 
    43184385                    } 
    4319                     print $fh "       sid:       $sid\n", 
    4320                         "       chain:     $chain\n", 
    4321                         "       packets:   $pkts\n", 
    4322                         "       classtype: $sm_hr->{'classtype'}\n"; 
    4323                     if (defined $sm_hr->{'reference'}) { 
    4324                         for my $reftype (keys %{$sm_hr->{'reference'}}) { 
     4386                    print $fh "       sid:       $sid\n"; 
     4387                    if (defined $sig_hr->{'psad_derived_sids'}) { 
     4388                        my $sid_str = ''; 
     4389                        $sid_str .= "$_ " 
     4390                            for @{$sig_hr->{'psad_derived_sids'}}; 
     4391                        $sid_str =~ s|\s*$||; 
     4392                        print $fh "derived sids:     $sid_str\n"; 
     4393                    } 
     4394                    print $fh "       chain:     $chain\n", 
     4395                        "       packets:   $pkts\n"; 
     4396 
     4397                    if ($sig_hr->{'classtype'}) { 
     4398                        print $fh "       classtype: $sig_hr->{'classtype'}\n"; 
     4399                    } 
     4400                    if (defined $sig_hr->{'reference'} 
     4401                            and $sig_hr->{'reference'}) { 
     4402                        for my $reftype (keys %{$sig_hr->{'reference'}}) { 
    43254403                            my $baseurl = ''; 
    43264404                            if (defined $snort_ref_baseurl{$reftype}) { 
     
    43294407                                next; 
    43304408                            } 
    4331                             for my $ref (@{$sm_hr->{'reference'}->{$reftype}}) { 
     4409                            for my $ref (@{$sig_hr->{'reference'}->{$reftype}}) { 
    43324410                                print $fh "       reference: ($reftype) ", 
    43334411                                    "${baseurl}$ref\n";