KruegelVigna.pm untested

filter: Statementstmt 0.0% 0 / 359 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 591 CC 49 SCAR 49.3
CC
49
Cov
0%
test
380.0
parameters
110.0
next_pass
110.0
CRAP 138.4
1 package IDS::Algorithm::KruegelVigna;
2 use base qw(IDS::Algorithm);
3 $IDS::Algorithm::KruegelVigna::VERSION = "1.0";
4  
5 =head1 NAME
6  
7 IDS::Algorithm::KruegelVigna - an IDS algorithm based on the Kruegel and
8 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 See IDS::Algorithm.pm docs for any functions not described here.
17  
18 This algorithm is HTTP-specific and will not work with any other data
19 source.
20  
21 This algorithm requires two passes over the training data to function
22 properly.
23  
24 =cut
25  
26 use strict;
27 use warnings;
28 use IDS::Algorithm::Length;
29 use IDS::Algorithm::KVCharDist;
30 use IDS::Algorithm::Order;
31 use IDS::Algorithm::Presence;
32 use IDS::Algorithm::EnumOrRandom;
33 use IDS::Algorithm::MM;
34 use Carp qw(cluck carp confess);
35  
36 =over
37  
38 =item new()
39  
40 =item new(params)
41  
42 =item new(filehandle, params)
43  
44 Create the object for the algorithm. If the parameters are supplied,
45 they are used; otherwise everything is defaults (unsurprisingly).
46 If a filehandle is supplied, the filehandle is taken as the source for
47 a load operation.
48  
49 =back
50  
51 =cut
52  
53 # Part of the logic here seems kind of backwards, but we cannot load
54 # until parameters have been loaded. Some of the parameters may affect
55 # how we load.
56 sub new {
57 my $invocant = shift;
58 my $class = ref($invocant) || $invocant;
59 my $self = { };
60 my $source;
61 my $state = 0;
62  
63 # necessary before we call handle_parameters.
64 bless($self, $class);
65  
66 $self->default_parameters;
67 $source = $self->handle_parameters(@_);
68  
69 $self->load($source) if defined($source); # unlikely to occur
70 # due to IDS::Test framework
71  
72 $self->{"length"} = new IDS::Algorithm::Length; # 4.1
73 $self->{"chardist"} = new IDS::Algorithm::KVCharDist; # 4.2
74 $self->{"markov"} = new IDS::Algorithm::MM; # 4.3
75 $self->{"enumorrandom"} = new IDS::Algorithm::EnumOrRandom; # 4.4
76 $self->{"presence"} = new IDS::Algorithm::Presence; # 4.5
77 $self->{"order"} = new IDS::Algorithm::Order; # 4.6
78  
79 # weights are per algorithm. We store the minimum similarity, since
80 # the test framework has everything in [0,1] with 0 being anomalous
81 $self->{"worst"} = {};
82 ${$self->{"worst"}}{"length"} = 1.0;
83 ${$self->{"worst"}}{"chardist"} = 1.0;
84 ${$self->{"worst"}}{"markov"} = 1.0;
85 ${$self->{"worst"}}{"enumorrandom"} = 1.0;
86 ${$self->{"worst"}}{"presence"} = 1.0;
87 ${$self->{"worst"}}{"order"} = 1.0;
88  
89 return $self;
90 }
91  
92 sub param_options {
93 my $self = shift;
94  
95 # Local parameters last, to override the state file from the
96 # sub-objects
97 return (
98 $self->{"length"}->param_options,
99 $self->{"chardist"}->param_options,
100 $self->{"markov"}->param_options,
101 $self->{"enumorrandom"}->param_options,
102 $self->{"presence"}->param_options,
103 $self->{"order"}->param_options,
104 "kv_verbose=i" => \${$self->{"params"}}{"verbose"},
105 "ids_state=s" => \${$self->{"params"}}{"state_file"},
106 "kvfudge=f" => \${$self->{"params"}}{"fudge"},
107 "MMmap_chars" => \${$self->{"params"}}{"map_chars"},
108 );
109 }
110  
111 sub default_parameters {
112 my $self = shift;
113  
114 %{$self->{"params"}} = (
115 "verbose" => 0,
116 "state_file" => 0,
117 "fudge" => 0.10,
118 "map_chars" => 0,
119 );
120 }
121  
122 sub parameters {
123 my $self = shift;
124 my @params = ( %{$self->{"params"}} );
125  
126 push @params, $self->{"length"}->parameters if defined($self->{"length"});
127 push @params, $self->{"chardist"}->parameters if defined($self->{"chardist"});
128 push @params, $self->{"markov"}->parameters if defined($self->{"markov"});
129 push @params, $self->{"enumorrandom"}->parameters if defined($self->{"enumorrandom"});
130 push @params, $self->{"presence"}->parameters if defined($self->{"presence"});
131 push @params, $self->{"order"}->parameters if defined($self->{"order"});
132  
133 if ($#_ == -1) {
134 return wantarray ? @params : \@params;
135 }
136  
137 # If we are setting, set in all of the sub-objects
138 # For value, return the last
139 if ($#_ == 1) {
140 my $old = ${$self->{"params"}}{$_[0]};
141 ${$self->{"params"}}{$_[0]} = $_[1];
142 $old = $self->{"length"}->parameters($_[0], $_[1]);
143 $old = $self->{"chardist"}->parameters($_[0], $_[1]);
144 $old = $self->{"markov"}->parameters($_[0], $_[1]);
145 $old = $self->{"enumorrandom"}->parameters($_[0], $_[1]);
146 $old = $self->{"presence"}->parameters($_[0], $_[1]);
147 $old = $self->{"order"}->parameters($_[0], $_[1]);
148 return $old;
149 }
150  
151 scalar(@_) % 2 != 0 and confess "odd > 1 number of parameters passed to ",
152 *parameters{PACKAGE}, ". See documentation for proper usage.\n";
153  
154 # If we are setting, set in all of the sub-objects
155 for (my $i = 0; $i < $#_; $i+=2) {
156 ${$self->{"params"}}{$_[$i]} = $_[$i+1];
157 $self->{"length"}->parameters($_[$i], $_[$i+1]);
158 $self->{"chardist"}->parameters($_[$i], $_[$i+1]);
159 $self->{"markov"}->parameters($_[$i], $_[$i+1]);
160 $self->{"enumorrandom"}->parameters($_[$i], $_[$i+1]);
161 $self->{"presence"}->parameters($_[$i], $_[$i+1]);
162 $self->{"order"}->parameters($_[$i], $_[$i+1]);
163 }
164  
165 return 1;
166 }
167  
168  
169 # Instead of file to save in, our argument will be a dir to save in.
170 sub save {
171 my $self = shift;
172 my $dir = $self->find_fname(shift);
173 defined($dir) && $dir or
174 confess *save{PACKAGE} . "::save missing dir";
175  
176 unless (-d $dir) {
177 warn "$dir does not exist; creating.\n";
178 mkdir $dir or confess "mkdir '$dir' failed: $!\n";
179 }
180 $dir =~ m!/$! or $dir .= "/";
181
182 $self->{"length"}->save($dir . "length");
183 $self->{"chardist"}->save($dir . "chardist");
184 $self->{"markov"}->save($dir . "markov");
185 $self->{"enumorrandom"}->save($dir . "enumorrandom");
186 $self->{"presence"}->save($dir . "presence");
187 $self->{"order"}->save($dir . "order");
188  
189 $self->calc_weights unless defined($self->{"weight"});
190 my $wfname = $dir . "weights";
191 open(WF, ">$wfname") or confess "Cannot open $wfname for writing: $!\n";
192 for my $m (keys %{$self->{"weight"}}) {
193 print WF "$m: ", ${$self->{"weight"}}{$m}, "\n";
194 }
195 close WF;
196 my $bfname = $dir . "bottoms";
197 open(WF, ">$bfname") or confess "Cannot open $bfname for writing: $!\n";
198 for my $m (keys %{$self->{"bottom"}}) {
199 print WF "$m: ", ${$self->{"bottom"}}{$m}, "\n";
200 }
201 close WF;
202 }
203  
204 # Instead of file to load from, our argument will be a dir to load from.
205 sub load {
206 my $self = shift;
207 my $dir = $self->find_fname(shift);
208 defined($dir) && $dir or
209 confess *load{PACKAGE} . "::load missing dir";
210  
211 $dir =~ m!/$! or $dir .= "/";
212  
213 $self->{"length"}->load($dir . "length");
214 $self->{"chardist"}->load($dir . "chardist");
215 $self->{"markov"}->load($dir . "markov");
216 $self->{"enumorrandom"}->load($dir . "enumorrandom");
217 $self->{"presence"}->load($dir . "presence");
218 $self->{"order"}->load($dir . "order");
219  
220 my $wfname = $dir . "weights";
221 open(WF, "$wfname") or confess "Cannot open $wfname for reading: $!\n";
222 while (<WF>) {
223 chomp;
224 my ($m, $w) = split(/: /, $_, 2);
225 ${$self->{"weight"}}{$m} = $w;
226 }
227 close WF;
228  
229 my $bfname = $dir . "bottoms";
230 open(WF, "$bfname") or confess "Cannot open $bfname for reading: $!\n";
231 while (<WF>) {
232 chomp;
233 my ($m, $w) = split(/: /, $_, 2);
234 ${$self->{"bottom"}}{$m} = $w;
235 }
236 close WF;
237 }
238  
239 sub method {
240 my $self = shift;
241 my $string = shift or
242 confess "bug: missing string to ", *method{PACKAGE} . "::method";
243
244 # The method is the first "word" in the string.
245 $string =~ /([^\s]+)\s/;
246 return $1;
247 }
248  
249 sub path {
250 my $self = shift;
251 my $string = shift or
252 confess "bug: missing string to ", *method{PACKAGE} . "::method";
253
254 # The path is the second "word" in the string. Assume greedy
255 # pattern matching
256 $string =~ /[^\s]+\s+([^\s]+)/;
257 return $1;
258 }
259  
260 sub test {
261 my $self = shift;
262 my $tokensref = shift or
263 confess "bug: missing tokensref to ", *test{PACKAGE} . "::test";
264 my $string = shift or
265 confess "bug: missing string to ", *test{PACKAGE} . "::test";
266 my $instance = shift or
267 confess "bug: missing instance to ", *test{PACKAGE} . "::test";
268 my $verbose = ${$self->{"params"}}{"verbose"};
269  
270 # This test is only applied to GET URIs
271 # All others are considered normal
272 return 1 unless $self->method($string) eq "GET";
273  
274 my $uri = $self->path($string);
275 my $q = $self->extract_query($uri);
276 my $path = $self->extract_qpath($uri);
277  
278 # This test is only applied to CGI queries
279 # All others are considered normal
280 return 1 unless $q;
281  
282 if (defined($q)) {
283 my @Sq = split(/\&/, $q);
284 my @Sa = @Sq;
285 my @Sv = @Sq;
286
287 map { s/=.*// } @Sa;
288 map { s/.*=// } @Sv;
289  
290 # For Markov model; KV mapped characters to a class of their case
291 my $mm_q = $q;
292 if (${$self->{"params"}}{"map_chars"}) {
293 $mm_q =~ tr/a-z/xxxxxxxxxxxxxxxxxxxxxxxxxx/;
294 $mm_q =~ tr/A-Z/XXXXXXXXXXXXXXXXXXXXXXXXXX/;
295 }
296 my @Sc = split(//, $mm_q);
297  
298 my ($n, $m, $r, $l, $c, $mk, $er, $pr, $o, $result);
299  
300 defined($self->{"weight"}) or
301 $self->calc_weights;
302  
303 $n = 0;
304  
305 # attribute lengths (4.1 in kruegel2003anomaly)
306 $m = 0;
307 $r = 0;
308 map {
309 $l = $self->{"length"}->test(undef, $_, $instance);
310 $m++;
311 $r += $l > ${$self->{"bottom"}}{"length"} ? 1 : 0;
312 } @Sv;
313 $result = $r / $m;
314 print "Attr len: $result n $n\n" if $verbose;
315  
316 # character distribution (4.2 in kruegel2003anomaly)
317 $m = 0;
318 $r = 0;
319 map {
320 $c = $self->{"chardist"}->test(undef, $_, $instance);
321 $m++;
322 $r += $c > ${$self->{"bottom"}}{"chardist"} ? 1 : 0;
323 } @Sv;
324 $result += $r / $m;
325 print "chardist: $result n $n\n" if $verbose;
326  
327 # Markov model (4.3 in kruegel2003anomaly)
328 $mk = $self->{"markov"}->test(\@Sc, undef, $instance);
329 $mk = $mk > 0 ? 1 : 0; # from sec 4.3.2 in K&V
330 $n++;
331 $result += $mk > ${$self->{"bottom"}}{"markov"} ? 1 : 0;
332 print "MM: $result n $n\n" if $verbose;
333  
334 # enumeration or random values (4.4 in kruegel2003anomaly)
335 $er = $self->{"enumorrandom"}->test(\@Sv, undef, $instance);
336 $n++;
337 $result += $er > ${$self->{"bottom"}}{"enumorrandom"} ? 1 : 0;
338 print "enumorrandom: $result n $n\n" if $verbose;
339  
340 # attribute presence or absence (4.5 in kruegel2003anomaly)
341 # Note non-standard IDS::Algorithm interface
342 $pr = $self->{"presence"}->test($path, \@Sa, $instance);
343 $n++;
344 $result += $pr > ${$self->{"bottom"}}{"presence"} ? 1 : 0;
345 print "presence: $result n $n\n" if $verbose;
346  
347 # attribute order (4.6 in kruegel2003anomaly)
348 $o = $self->{"order"}->test(\@Sv, undef, $instance);
349 $n++;
350 $result += $o > ${$self->{"bottom"}}{"order"} ? 1 : 0;
351 print "attrorder: $result n $n\n" if $verbose;
352  
353 print "return = ", $result / $n, "\n\n" if $verbose;
354  
355 return $result / $n;
356 } else {
357 # non-CGI with parameters are considered normal.
358 return 1;
359 }
360 }
361  
362 sub calc_weights {
363 my $self = shift;
364 my $fudge = 1.0 - ${$self->{"params"}}{"fudge"};
365  
366 my @models = qw(length chardist markov enumorrandom presence order);
367  
368 for my $m (@models) {
369 ${$self->{"bottom"}}{$m} = ${$self->{"worst"}}{$m} * $fudge;
370 ${$self->{"weight"}}{$m} = 1.0 / (1.0 - ${$self->{"bottom"}}{$m});
371 }
372 }
373  
374 sub add {
375 my $self = shift;
376 my $tokensref = shift or
377 confess "bug: missing tokensref to ", *test{PACKAGE} . "::test";
378 my $string = shift or
379 confess "bug: missing string to ", *test{PACKAGE} . "::test";
380 my $instance = shift or
381 confess "bug: missing instance to ", *test{PACKAGE} . "::test";
382  
383 my $verbose = ${$self->{"params"}}{"verbose"};
384  
385 # This algorithm only applies to GET URIs
386 # All others are considered normal
387 my $method = $self->method($string);
388 return 1 unless defined($method) && $method eq "GET";
389  
390 my $uri = $self->path($string);
391 my $q = $self->extract_query($uri);
392 my $path = $self->extract_qpath($uri);
393  
394 return 1 unless $q;
395  
396 print "Query: '$q'\n" if $verbose;
397  
398 if (defined($q)) {
399 my @Sq = split(/\&/, $q);
400 my @Sa = @Sq;
401 my @Sv = @Sq;
402
403 map { s/=.*// } @Sa;
404 map { s/.*=// } @Sv;
405  
406 print "Sa @Sa\n" if $verbose;
407 print "Sv @Sv\n" if $verbose;
408  
409 # For Markov model; KV mapped characters to a class of their case
410 my $mm_q = $q;
411 if (${$self->{"params"}}{"map_chars"}) {
412 $mm_q =~ tr/a-z/xxxxxxxxxxxxxxxxxxxxxxxxxx/;
413 $mm_q =~ tr/A-Z/XXXXXXXXXXXXXXXXXXXXXXXXXX/;
414 }
415 my @Sc = split(//, $mm_q);
416  
417 # attribute lengths (4.1 in kruegel2003anomaly)
418 map { $self->{"length"}->add(undef, $_, $instance) } @Sv;
419  
420 # character distribution (4.2 in kruegel2003anomaly)
421 map { $self->{"chardist"}->add(undef, $_, $instance) } @Sv;
422  
423 # Markov model (4.3 in kruegel2003anomaly)
424 $self->{"markov"}->add(\@Sc, undef, $instance);
425  
426 # enumeration or random values (4.4 in kruegel2003anomaly)
427 $self->{"enumorrandom"}->add(\@Sq, undef, $instance);
428  
429 # attribute presence or absence (4.5 in kruegel2003anomaly)
430 # Note non-standard IDS::Algorithm interface
431 $self->{"presence"}->add($path, \@Sa, $instance);
432  
433 # attribute order (4.6 in kruegel2003anomaly)
434 $self->{"order"}->add(\@Sv, undef, $instance);
435 }
436 }
437  
438 sub next_pass {
439 my $self = shift;
440 my $tokensref = shift or
441 confess "bug: missing tokensref to ", *test{PACKAGE} . "::test";
442 my $string = shift or
443 confess "bug: missing string to ", *test{PACKAGE} . "::test";
444 my $instance = shift or
445 confess "bug: missing instance to ", *test{PACKAGE} . "::test";
446  
447 # This algorithm only applies to GET URIs
448 # All others are considered normal
449 return 1 unless $self->method($string) eq "GET";
450  
451 my $uri = $self->path($string);
452 my $q = $self->extract_query($uri);
453 my $path = $self->extract_qpath($uri);
454  
455 if (defined($q)) {
456 my @Sq = split(/\&/, $q);
457 my @Sa = @Sq;
458 my @Sv = @Sq;
459
460 map { s/=.*// } @Sa;
461 map { s/.*=// } @Sv;
462  
463 my ($l, $c, $mk, $er, $pr, $o);
464  
465 # attribute lengths (4.1 in kruegel2003anomaly)
466 map {
467 $l = $self->{"length"}->test(undef, $_, $instance);
468 ${$self->{"weight"}}{"length"} = $l
469 if $l < ${$self->{"weight"}}{"length"};
470 } @Sv;
471  
472 # character distribution (4.2 in kruegel2003anomaly)
473 map {
474 $c = $self->{"chardist"}->test(undef, $_, $instance);
475 ${$self->{"weight"}}{"chardist"} = $c
476 if $c < ${$self->{"weight"}}{"chardist"};
477 } @Sv;
478  
479 # Markov model (4.3 in kruegel2003anomaly)
480 ### Do the Markov models work with characters?
481 $mk = $self->{"markov"}->test(\@Sv, undef, $instance);
482 $mk = $mk > 0 ? 1 : 0; # from sec 4.3.2 in K&V
483 ${$self->{"weight"}}{"markov"} = $mk
484 if $mk < ${$self->{"weight"}}{"markov"};
485  
486 # enumeration or random values (4.4 in kruegel2003anomaly)
487 $er = $self->{"enumorrandom"}->test(\@Sv, undef, $instance);
488 ${$self->{"weight"}}{"enumorrandom"} = $er
489 if $er < ${$self->{"weight"}}{"enumorrandom"};
490  
491 # attribute presence or absence (4.5 in kruegel2003anomaly)
492 # Note non-standard IDS::Algorithm interface
493 $pr = $self->{"presence"}->test($path, \@Sv, $instance);
494 ${$self->{"weight"}}{"presence"} = $pr
495 if $pr < ${$self->{"weight"}}{"presence"};
496  
497 # attribute order (4.6 in kruegel2003anomaly)
498 $o = $self->{"order"}->test(\@Sv, undef, $instance);
499 ${$self->{"weight"}}{"order"} = $o
500 if $o < ${$self->{"weight"}}{"order"};
501 }
502 }
503  
504 sub extract_query {
505 my $self = shift;
506 my $uri = shift or
507 confess "bug: missing uri to ", *extract_query{PACKAGE} . "::extract_query";
508  
509 if ($uri =~ /\?/) {
510 $uri =~ s/^.*\?//;
511  
512 return $uri;
513 } else {
514 return undef; # no query
515 }
516 }
517  
518 sub extract_qpath {
519 my $self = shift;
520 my $uri = shift or
521 confess "bug: missing uri to ", *extract_qpath{PACKAGE} . "::extract_qpath";
522  
523 if ($uri =~ /\?/) {
524 $uri =~ s/\?.*$//;
525  
526 return $uri;
527 } else {
528 return undef; # no query
529 }
530 }
531  
532 sub generalize {
533 my $self = shift;
534 }
535  
536 =head1 AUTHOR INFORMATION
537  
538 Copyright 2005-2007, Kenneth Ingham. All rights reserved.
539  
540 This library is free software; you can redistribute it and/or modify
541 it under the same terms as Perl itself.
542  
543 Address bug reports and comments to: ids_test at i-pi.com. When
544 sending bug reports, please provide the versions of IDS::Test.pm,
545 IDS::Algorithm.pm, IDS::DataSource.pm, the version of Perl, and the
546 name and version of the operating system you are using. Since Kenneth
547 is a PhD student, the speed of the response depends on how the research
548 is proceeding.
549  
550 =head1 BUGS
551  
552 Please report them.
553  
554 =head1 SEE ALSO
555  
556 L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm>
557  
558 "Anomaly detection of web-based attacks" by Christopher Kruegel and
559 Giovanni Vigna, pages 251--261 in Proceedings of the 10th ACM conference
560 on computer and communications security, ACM Press, 2003, ISBN 1-58113-738-9.
561 http://doi.acm.org/10.1145/948109.948144
562  
563 libAnomaly, by Darren Mutz, Wil Robertson, Fredrik Valeur,
564 Christopher Kruegel, Giovanni Vigna, and Richard Kemmerer.
565 http://www.cs.ucsb.edu/~rsg/libAnomaly/index.html
566  
567 =cut
568  
569 1;