| 1 | package IDS::Algorithm::Order; | ||
| 2 | $IDS::Algorithm::Order::VERSION = "1.0"; | ||
| 3 | use base qw(IDS::Algorithm); | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | IDS::Algorithm::Order - learn and/or test the order of attributes; | ||
| 8 | based on section 4.6 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 | See IDS::Algorithm.pm docs for any functions not described here. | ||
| 17 | |||
| 18 | Note when using this with a full HTTP request, using tokens without | ||
| 19 | values might be appropriate. This class was written to test as a part | ||
| 20 | of a complete re-implementation of the kruegel2003anomaly approach. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use IO::File; | ||
| 27 | use Statistics::Lite qw(:all); | ||
| 28 | use Carp qw(cluck carp confess); | ||
| 29 | use IDS::Utils qw(to_fh); | ||
| 30 | |||
| 31 | sub param_options { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | return ( | ||
| 35 | "order_verbose=i" => \${$self->{"params"}}{"verbose"}, | ||
| 36 | "ids_state=s" => \${$self->{"params"}}{"state_file"}, | ||
| 37 | ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub default_parameters { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | %{$self->{"params"}} = ( | ||
| 44 | "verbose" => 0, | ||
| 45 | "state_file" => 0, | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub initialize { | ||
| 50 | my $self = shift; | ||
| 51 | |||
| 52 | # %before{$a}{$b} means $a occurs before $b in training data | ||
| 53 | $self->{"before"} = {}; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub save { | ||
| 57 | my $self = shift; | ||
| 58 | my $fname = $self->find_fname(shift); | ||
| 59 | defined($fname) && $fname or | ||
| 60 | confess *save{PACKAGE} . "::save missing filename"; | ||
| 61 | my $fh = to_fh($fname, ">"); | ||
| 62 | |||
| 63 | my ($t, $v); | ||
| 64 | |||
| 65 | for $t (keys %{$self->{"before"}}) { | ||
| 66 | print $fh "$t\n"; | ||
| 67 | for $v (keys %{${$self->{"before"}}{$t}}) { | ||
| 68 | print $fh " $v\n"; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub load { | ||
| 74 | my $self = shift; | ||
| 75 | my $fname = $self->find_fname(shift); | ||
| 76 | $fname or | ||
| 77 | confess *load{PACKAGE} . "::load missing filename"; | ||
| 78 | my $fh = to_fh($fname, "<"); | ||
| 79 | |||
| 80 | my ($t, $v); | ||
| 81 | |||
| 82 | while (<$fh>) { | ||
| 83 | chomp; | ||
| 84 | if (/^ /) { # an "after" | ||
| 85 | ${$self->{"before"}}{$t}{$_} = 1; | ||
| 86 | } else { | ||
| 87 | $t = $_; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub add { | ||
| 93 | my $self = shift; | ||
| 94 | my $tokensref = shift or | ||
| 95 | confess "bug: missing tokensref to ", *add{PACKAGE} . "::add"; | ||
| 96 | my $string = shift; # not used | ||
| 97 | my $instance = shift or | ||
| 98 | confess "bug: missing instance to ", *add{PACKAGE} . "::add"; | ||
| 99 | |||
| 100 | my ($i, $j, $before); | ||
| 101 | |||
| 102 | # %before{$a}{$b} means $a occurs before $b in training data | ||
| 103 | $before = $self->{"before"}; | ||
| 104 | for ($i=0; $i < $#{$tokensref}; $i++) { | ||
| 105 | for ($j=$i+1; $j < $#{$tokensref}; $j++) { | ||
| 106 | $a = ${$tokensref}[$i]; | ||
| 107 | $b = ${$tokensref}[$j]; | ||
| 108 | ${$before}{$a}{$b} = 1 unless exists(${$before}{$a}{$b}); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | sub test { | ||
| 114 | my $self = shift; | ||
| 115 | my $tokensref = shift or | ||
| 116 | confess "bug: missing tokensref to ", *test{PACKAGE} . "::test"; | ||
| 117 | my $string = shift; # not used; | ||
| 118 | my $instance = shift or | ||
| 119 | confess "bug: missing instance to ", *test{PACKAGE} . "::test"; | ||
| 120 | |||
| 121 | my ($i, $j, $before); | ||
| 122 | |||
| 123 | # %before{$a}{$b} means $a occurs before $b in training data | ||
| 124 | $before = $self->{"before"}; | ||
| 125 | for ($i=0; $i < $#{$tokensref}; $i++) { | ||
| 126 | for ($j=$i+1; $j < $#{$tokensref}; $j++) { | ||
| 127 | $a = ${$tokensref}[$i]; | ||
| 128 | $b = ${$tokensref}[$j]; | ||
| 129 | return 0 if exists(${$before}{$b}{$a}) && ${$before}{$b}{$a} && !exists(${$before}{$a}{$b}); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head1 AUTHOR INFORMATION | ||
| 136 | |||
| 137 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 138 | |||
| 139 | This library is free software; you can redistribute it and/or modify | ||
| 140 | it under the same terms as Perl itself. | ||
| 141 | |||
| 142 | Address bug reports and comments to: ids_test at i-pi.com. When | ||
| 143 | sending bug reports, please provide the versions of IDS::Test.pm, | ||
| 144 | IDS::Algorithm.pm, IDS::DataSource.pm, the version of Perl, and the | ||
| 145 | name and version of the operating system you are using. Since Kenneth | ||
| 146 | is a PhD student, the speed of the response depends on how the research | ||
| 147 | is proceeding. | ||
| 148 | |||
| 149 | =head1 BUGS | ||
| 150 | |||
| 151 | Please report them. | ||
| 152 | |||
| 153 | =head1 SEE ALSO | ||
| 154 | |||
| 155 | L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm> | ||
| 156 | |||
| 157 | "Anomaly detection of web-based attacks" by Christopher Kruegel and | ||
| 158 | Giovanni Vigna, pages 251--261 in Proceedings of the 10th ACM conference | ||
| 159 | on computer and communications security, ACM Press, 2003, ISBN 1-58113-738-9. | ||
| 160 | http://doi.acm.org/10.1145/948109.948144 | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | 1; |