EnumOrRandom.pm untested

filter: Statementstmt 0.0% 0 / 178 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 292 CC 20 SCAR 36.2
CC
20
Cov
0%
add
90.0
save
42.0
test
30.0
CRAP 37.4
1 package IDS::Algorithm::EnumOrRandom;
2 use base qw(IDS::Algorithm);
3 $IDS::Algorithm::EnumOrRandom::VERSION = "1.0";
4  
5 =head1 NAME
6  
7 IDS::Algorithm::EnumOrRandom - is a value enumurated or random;
8 based on section 4.4 in the Kruegel and Vigna paper (L</SEE ALSO>).
9  
10 =head1 SYNOPSIS
11  
12 A usage synopsis would go here. Since it is not here, read on.
13  
14 =head1 DESCRIPTION
15  
16 BUG: This algorithm cannot go straight from training to testing without
17 saving; the save function also performs the necessary calculations.
18  
19 See IDS::Algorithm.pm docs for any functions not described here.
20  
21 notation from kruegel2003anomaly
22  
23 =cut
24  
25 use strict;
26 use warnings;
27 use IO::File;
28 use Statistics::Basic::CoVariance;
29 use Statistics::Basic::Variance;
30 use Carp qw(cluck carp confess);
31 use IDS::Utils qw(to_fh);
32  
33 sub param_options {
34 my $self = shift;
35  
36 return (
37 "eor_verbose=i" => \${$self->{"params"}}{"verbose"},
38 "ids_state=s" => \${$self->{"params"}}{"state_file"},
39 "separator=s" => \${$self->{"params"}}{"separator"},
40 );
41 }
42  
43 sub default_parameters {
44 my $self = shift;
45  
46 %{$self->{"params"}} = (
47 "verbose" => 0,
48 "state_file" => 0,
49 "separator" => '=',
50 );
51 }
52  
53  
54 sub initialize {
55 my $self = shift;
56  
57 $self->{"f"} = {}; # count of occurrences of each "a" (attribute)
58 $self->{"g"} = {}; # count of new versus seen values for each "a"
59 $self->{"value"} = {}; # values seen for each "a"; probably needs to be tied
60 }
61  
62 sub save {
63 my $self = shift;
64 my $fname = $self->find_fname(shift);
65 defined($fname) && $fname or
66 confess *save{PACKAGE} . "::save missing filename";
67 my $fh = to_fh($fname, ">");
68 my $verbose = ${$self->{"params"}}{"verbose"};
69  
70 my ($a, @f, $covar, $fvar, $gvar, $rho, %value);
71  
72 %value = %{$self->{"value"}};
73 for $a (keys %{$self->{"g"}}) {
74 print "Looking at $a\n" if $verbose;
75 @f = (1 .. ${$self->{"f"}}{$a});
76 $covar = new Statistics::Basic::CoVariance(\@f, \@{${$self->{"g"}}{$a}})->query;
77 $fvar = new Statistics::Basic::Variance(\@f)->query;
78 $gvar = new Statistics::Basic::Variance(\@{${$self->{"g"}}{$a}})->query;
79 print "fvar $fvar gvar $gvar\n" if $verbose;
80 $rho = $fvar != 0 && $gvar != 0 ? $covar / sqrt($fvar * $gvar)
81 : 1;
82 print "rho $rho\n" if $verbose;
83 if ($rho < 0) {
84 my $n = scalar(keys (%{$value{$a}}));
85 print $fh "--------------- $n '$a' ---------------\n";
86 print $fh map {"$_\n"} keys (%{$value{$a}});
87 } else {
88 print $fh "--------------- random '$a' ---------------\n";
89 }
90 }
91 undef $fh;
92 }
93  
94 sub load {
95 my $self = shift;
96 my $fname = $self->find_fname(shift);
97 $fname or
98 confess *load{PACKAGE} . "::load missing filename";
99 my $fh = to_fh($fname, "<");
100  
101 my ($k, $v, $i, $j, $line, %value, $count, $end);
102  
103 $self->{"value"} = {};
104 $self->{"f"} = {};
105 $self->{"g"} = {};
106 %value = ();
107  
108 $i=0;
109 while (<$fh>) {
110 chomp;
111 if (/^--------------- (\d+) '([^']*)' ---------------$/) {
112 $count = $1;
113 $a = $2;
114 for ($j=0; $j < $count; $j++) {
115 $line = <$fh>;
116 chomp($line);
117 $value{$a}{$line} = 1;
118 }
119 $i = $end;
120 } elsif (/^--------------- random '([^']*)' ---------------$/) {
121 $a = $1;
122 $value{$a} = "random";
123 } else {
124 die "Unexpected line ($i) '$_'\n";
125 }
126 $i++;
127 }
128 $self->{"value"} = \%value;
129 }
130  
131 sub add {
132 my $self = shift;
133 my $tokensref = shift or
134 confess "bug: missing tokensref to ", *add{PACKAGE} . "::add";
135 my $string = shift; # not used
136 my $instance = shift or
137 confess "bug: missing instance to ", *add{PACKAGE} . "::add";
138 my $verbose = ${$self->{"params"}}{"verbose"};
139 my $sep = ${$self->{"params"}}{"separator"};
140  
141 my ($a, $v, $pair, $new, $gend);
142  
143 for $pair (@{$tokensref}) {
144 print "pair '$pair'\n" if $verbose;
145  
146 # only try this if we can get a key+value
147 ($a, $v) = split(/$sep/, $pair, 2);
148 if (defined($a) && defined($v)) {
149 print "a '$a' v '$v'\n" if $verbose;
150  
151 exists(${$self->{"value"}}{$a}) or ${$self->{"value"}}{$a} = {};
152  
153 # f(x) = x; # since this function is strictly incrementing, we
154 # keep track of the top, and generate the array of numbers when
155 # needed later.
156 ${$self->{"f"}}{$a}++;
157  
158 # g(x)
159 # Is the value new?
160 $new = undef;
161 # If statement checks include checks for continuing to learn
162 ### continued learning may not be valid for this algorithm
163 if (exists(${$self->{"value"}}{$a}) && ${$self->{"value"}}{$a} ne "random") {
164 # we have seen this $a; how about the $v?
165 if (exists(${$self->{"value"}}{$a}{$v})) {
166 # yes
167 $new = 0;
168 ${$self->{"value"}}{$a}{$v}++;
169 } else {
170 # no
171 $new = 1;
172 ${$self->{"value"}}{$a}{$v} = 1;
173 }
174 } else {
175 # we have never seen this $a before
176 ${$self->{"value"}}{$a} = {};
177 $new = 1;
178 }
179 if (exists(${$self->{"g"}}{$a})) {
180 $gend = $#{${$self->{"g"}}{$a}};
181 # gend must be >= 0 since when we create it, we put a value in.
182 push @{${$self->{"g"}}{$a}}, $new
183 ? (${${$self->{"g"}}{$a}}[$gend] - 1)
184 : (${${$self->{"g"}}{$a}}[$gend] + 1);
185 } else {
186 ${$self->{"g"}}{$a} = [ 0 ];
187 }
188 } else {
189 # This is not serious, at least with the HTTP parser;
190 # it has some tokens that are validated and deleted.
191 # There is no need for us to try to memorize IP addrs,
192 # qvalues, etc.
193 print "Could not split '$pair' at '$sep'\n" if $verbose;
194 }
195 }
196 }
197  
198  
199 sub test {
200 my $self = shift;
201 my $tokensref = shift or
202 confess "bug: missing tokensref to ", *test{PACKAGE} . "::test";
203 my $string = shift; # not used
204 my $instance = shift or
205 confess "bug: missing instance to ", *test{PACKAGE} . "::test";
206  
207 my ($a, $v, $n, $pair, $result);
208  
209 $n = scalar(@{$tokensref});
210 $result = 0;
211 for $pair (@{$tokensref}) {
212 ($a, $v) = split(/${$self->{"params"}}{"separator"}/, $pair, 2);
213  
214 defined($a) or confess "a undefined; pair '$pair'";
215  
216 if (exists(${$self->{"value"}}{$a})) {
217 if (${$self->{"value"}}{$a} eq "random") {
218 # random means any value is OK
219 $result++;
220 } elsif (exists(${$self->{"value"}}{$a}{$v}) &&
221 defined(${$self->{"value"}}{$a}{$v}) &&
222 ${$self->{"value"}}{$a}{$v}) {
223 # enumerated is only OK if we have seen it.
224 $result++;
225 }
226 }
227 }
228  
229 ### Should this be very strict or weighted?
230 ### Currently choose strict
231 return $result == $n ? 1 : 0; # strict
232 # return $result / $n; # proportional
233 }
234  
235 =head1 AUTHOR INFORMATION
236  
237 Copyright 2005-2007, Kenneth Ingham. All rights reserved.
238  
239 This library is free software; you can redistribute it and/or modify
240 it under the same terms as Perl itself.
241  
242 Address bug reports and comments to: ids_test at i-pi.com. When
243 sending bug reports, please provide the versions of IDS::Test.pm,
244 IDS::Algorithm.pm, IDS::DataSource.pm, the version of Perl, and the
245 name and version of the operating system you are using. Since Kenneth
246 is a PhD student, the speed of the response depends on how the research
247 is proceeding.
248  
249 =head1 BUGS
250  
251 Please report them.
252  
253 =head1 SEE ALSO
254  
255 L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm>
256  
257 "Anomaly detection of web-based attacks" by Christopher Kruegel and
258 Giovanni Vigna, pages 251--261 in Proceedings of the 10th ACM conference
259 on computer and communications security, ACM Press, 2003, ISBN 1-58113-738-9.
260 http://doi.acm.org/10.1145/948109.948144
261  
262 =cut
263  
264 1;