| 1 | package IDS::Algorithm::Presence; | ||
| 2 | use base qw(IDS::Algorithm); | ||
| 3 | $IDS::Algorithm::Presence::VERSION = "1.0"; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | IDS::Algorithm::Presence - earn and.or test for the presence or absence | ||
| 8 | of attributes; | ||
| 9 | based on section 4.5 in the Kruegel and Vigna paper (L</SEE ALSO>). | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | A usage synopsis would go here. Since it is not here, read on. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | See IDS::Algorithm.pm docs for any functions not described here. | ||
| 18 | |||
| 19 | Note that this function does not properly implement the IDS::Algorithm | ||
| 20 | interface. | ||
| 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 | |||
| 30 | sub default_parameters { | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | %{$self->{"params"}} = ( | ||
| 34 | "verbose" => 0, | ||
| 35 | "state_file" => 0, | ||
| 36 | ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub param_options { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | return ( | ||
| 43 | "presence_verbose=i" => \${$self->{"params"}}{"verbose"}, | ||
| 44 | "ids_state=s" => \${$self->{"params"}}{"state_file"}, | ||
| 45 | ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub initialize { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | # nothing here | ||
| 52 | } | ||
| 53 | |||
| 54 | sub save { | ||
| 55 | my $self = shift; | ||
| 56 | my $fname = shift or confess "bug: missing fname to save"; | ||
| 57 | my ($fh, $path); | ||
| 58 | |||
| 59 | $fh = new IO::File("> $fname") or confess "Cannot open $fname: $!\n"; | ||
| 60 | for $path (keys %{$self->{"attpresence"}}) { | ||
| 61 | print $fh "$path\n"; | ||
| 62 | map {print $fh " $_\n"} keys %{${$self->{"attpresence"}}{$path}}; | ||
| 63 | } | ||
| 64 | undef $fh; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub load { | ||
| 68 | my $self = shift; | ||
| 69 | my $fname = shift or confess "bug: missing fname to load"; | ||
| 70 | my $fh = new IO::File("$fname") or confess "Unable to open $fname: $!\n"; | ||
| 71 | my ($path); | ||
| 72 | |||
| 73 | while (<$fh>) { | ||
| 74 | chomp; | ||
| 75 | if (/^[^\s]/) { # a path | ||
| 76 | $path = $_; | ||
| 77 | } else { # an attribute for the path | ||
| 78 | s/^ //; | ||
| 79 | ${$self->{"attpresence"}}{$path}{$_} = 1; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | undef $fh; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub add { | ||
| 86 | my $self = shift; | ||
| 87 | my $path = shift; | ||
| 88 | my $aref = shift; | ||
| 89 | my $instance = shift; | ||
| 90 | my $verbose = ${$self->{"params"}}{"verbose"}; | ||
| 91 | |||
| 92 | my ($v); | ||
| 93 | |||
| 94 | for $v (@{$aref}) { | ||
| 95 | print "path '$path' v '$v'\n" if $verbose; | ||
| 96 | if (exists(${$self->{"attpresence"}}{$path}{$v})) { | ||
| 97 | ${$self->{"attpresence"}}{$path}{$v}++; | ||
| 98 | } else { | ||
| 99 | ${$self->{"attpresence"}}{$path}{$v} = 1; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | # Simple, binary result. 1 only if all have been seen previously. | ||
| 105 | sub test { | ||
| 106 | my $self = shift; | ||
| 107 | my $path = shift; | ||
| 108 | my $aref = shift; | ||
| 109 | my $instance = shift; | ||
| 110 | my $verbose = ${$self->{"params"}}{"verbose"}; | ||
| 111 | |||
| 112 | my ($v); | ||
| 113 | |||
| 114 | for $v (@{$aref}) { | ||
| 115 | print "path '$path' v '$v'\n" if $verbose; | ||
| 116 | exists(${$self->{"attpresence"}}{$path}) && | ||
| 117 | exists(${$self->{"attpresence"}}{$path}{$v}) && | ||
| 118 | defined(${$self->{"attpresence"}}{$path}{$v}) && | ||
| 119 | ${$self->{"attpresence"}}{$path}{$v} > 0 or return 0; | ||
| 120 | } | ||
| 121 | return 1; | ||
| 122 | } | ||
| 123 | |||
| 124 | =head1 AUTHOR INFORMATION | ||
| 125 | |||
| 126 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 127 | |||
| 128 | This library is free software; you can redistribute it and/or modify | ||
| 129 | it under the same terms as Perl itself. | ||
| 130 | |||
| 131 | Address bug reports and comments to: ids_test at i-pi.com. When | ||
| 132 | sending bug reports, please provide the versions of IDS::Test.pm, | ||
| 133 | IDS::Algorithm.pm, IDS::DataSource.pm, the version of Perl, and the | ||
| 134 | name and version of the operating system you are using. Since Kenneth | ||
| 135 | is a PhD student, the speed of the response depends on how the research | ||
| 136 | is proceeding. | ||
| 137 | |||
| 138 | =head1 BUGS | ||
| 139 | |||
| 140 | Please report them. | ||
| 141 | |||
| 142 | =head1 SEE ALSO | ||
| 143 | |||
| 144 | L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm> | ||
| 145 | |||
| 146 | "Anomaly detection of web-based attacks" by Christopher Kruegel and | ||
| 147 | Giovanni Vigna, pages 251--261 in Proceedings of the 10th ACM conference | ||
| 148 | on computer and communications security, ACM Press, 2003, ISBN 1-58113-738-9. | ||
| 149 | http://doi.acm.org/10.1145/948109.948144 | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | 1; |