Chi2ICD.pm untested

filter: Statementstmt 0.0% 0 / 181 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 61 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 346 CC 23 SCAR 54.6
CC
23
Cov
0%
test
420.0
summarize
6.0
load
6.0
CRAP 234.9
1 package IDS::Algorithm::Chi2ICD;
2 use base qw(IDS::Algorithm);
3 $IDS::Algorithm::Chi2ICD::VERSION = "1.0";
4  
5 =head1 NAME
6  
7 IDS::Algorithm::Chi2ICD - learn or test the character distribution of
8 a string, using the Chi2 of ICD developed by Kruegel and Vigna (See the
9 L</SEE ALSO> section for the reference).
10  
11 =head1 SYNOPSIS
12  
13 A usage synopsis would go here. Since it is not here, read on.
14  
15 The learning and testing are based on section 4.2 in kruegel2003anomaly.
16  
17 =head1 DESCRIPTION
18  
19 Someday more will be here.
20  
21 ASSUMPTION: characters are 0..255; need to change to allow unicode, etc
22  
23 =cut
24  
25 use strict;
26 use warnings;
27 use Carp qw(cluck carp confess);
28 use IDS::Utils qw(to_fh);
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 "chardist_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 $self->{"count"} = []; # character counts
52 map { ${$self->{"count"}}[$_] = 0; } 0..255;
53  
54 $self->{"cfreq"} = []; # character frequency
55 map { ${$self->{"cfreq"}}[$_] = 0; } 0..255;
56  
57 $self->{"bins"} = []; # binned frequencies
58 map { ${$self->{"bins"}}[$_] = 0; } 0..5;
59  
60 $self->{"summarized"} = 0;
61 }
62  
63 sub summarize {
64 my $self = shift;
65  
66 my ($n, $i);
67  
68 $n = 0;
69 map { $n += $_ } @{$self->{"count"}};
70  
71 if ($n > 0) {
72 for ($i=0; $i <= $#{$self->{"count"}}; $i++) {
73 ${$self->{"cfreq"}}[$i] = ${$self->{"count"}}[$i] / $n;
74 }
75 } else {
76 warn "Chi2ICD::summarize n is 0!\n";
77 }
78  
79 $self->{"summarized"} = 1;
80 }
81  
82 # note we write ord(c) rather than c to avoid problems with characters
83 # such as \r \n etc
84 sub save {
85 my $self = shift;
86 my $fname = $self->find_fname(shift);
87 defined($fname) && $fname or
88 confess *save{PACKAGE} . "::save missing filename";
89 my $fh = to_fh($fname, ">");
90  
91 my ($sum, $i);
92  
93 $self->{"summarized"} or $self->summarize;
94  
95 $sum = 0;
96 for ($i=0; $i <= $#{$self->{"cfreq"}}; $i++) {
97 print $fh "$i: ", ${$self->{"cfreq"}}[$i], "\n";
98 $sum += ${$self->{"cfreq"}}[$i];
99 }
100 print "Saved. sum is $sum (should be 1)\n";
101 }
102  
103 sub load {
104 my $self = shift;
105 my $fname = $self->find_fname(shift);
106 defined($fname) && $fname or
107 confess *load{PACKAGE} . "::load missing filename";
108 my $fh = to_fh($fname, "<");
109 my $verbose = ${$self->{"params"}}{"verbose"};
110  
111 my ($k, $v, $sum);
112  
113 $sum = 0;
114 map {chomp;
115 ($k, $v) = split(/:\s+/, $_, 2);
116 ${$self->{"cfreq"}}[$k] = $v;
117 $sum += $v;
118 } <$fh>;
119  
120 $self->{"bins"} = [ $self->bin(sort { $b <=> $a } @{$self->{"cfreq"}}) ];
121 print "Loaded. Sum is $sum (should be 1)\n" if $verbose;
122  
123 $self->{"summarized"} = 1;
124 }
125  
126 sub add {
127 my $self = shift;
128 my $tref = shift; # not used
129 my $data = shift; # zero-length OK
130 defined($data) or confess "Missing data to add\n";
131 my $verbose = ${$self->{"params"}}{"verbose"};
132  
133 print "Chi2ICD::add data '$data'\n" if $verbose;
134  
135 map { ${$self->{"count"}}[ord($_)]++ } split(//, $data);
136 }
137  
138 sub test {
139 my $self = shift;
140 my $tref = shift; # not used
141 my $data = shift;
142 defined($data) or confess "bug: missing data to test";
143  
144 my (@chars, $len, @observed, @expected, $chi2, $i, $sum, $weight);
145  
146 my $verbose = ${$self->{"params"}}{"verbose"};
147 my $msg_fh = ${$self->{"params"}}{"msg_fh"};
148  
149 $self->{"summarized"} or $self->summarize;
150  
151 $len = length($data);
152 $len > 0 or return 0; # 0-length means no dist
153 $weight = 1.0 / $len;
154 map { $chars[$_] = 0; } 0..255;
155 map { $chars[ord($_)] += $weight; } split(//, $data);
156 @chars = sort { $b <=> $a } @chars;
157  
158 # look at what we expected
159 @observed = $self->bin(@chars);
160 @expected = @{$self->{"bins"}};
161  
162 if ($verbose) {
163 $sum = 0;
164 map { $sum += $_ } @expected;
165 print $msg_fh "Expected sum $sum\n";
166 die "Bad sum\n" if $sum > 1.05; # should be 1, but allow fudge
167 }
168  
169 # calculate chi2
170 $chi2 = 0;
171 for ($i=0; $i<6; $i++) {
172 if ($expected[$i] == 0) {
173 # a really unusual distribution
174 warn "expected[$i] == 0";
175 $expected[$i] = 0.000000001;
176 }
177 $chi2 += ($observed[$i] - $expected[$i]) ** 2 / $expected[$i];
178 }
179  
180 print $msg_fh "chi2 $chi2\n" if $verbose;
181  
182 # Table lookup for 6 degrees of freedom
183 # table from http://www.statsoft.com/textbook/sttable.html#chi
184 # p = 0.05 => chi2 < 11.07
185 # p = 0.01 => chi2 < 15.09
186 # p = 0.001 => chi2 < 20.52
187 return 1.0 if $chi2 < 0.41174;
188 return 0.995 if $chi2 >= 0.41174 && $chi2 < 0.55430;
189 return 0.990 if $chi2 >= 0.55430 && $chi2 < 0.83121;
190 return 0.975 if $chi2 >= 0.83121 && $chi2 < 1.14548;
191 return 0.950 if $chi2 >= 1.14548 && $chi2 < 1.61031;
192 return 0.900 if $chi2 >= 1.61031 && $chi2 < 2.67460;
193 return 0.750 if $chi2 >= 2.67460 && $chi2 < 4.35146;
194 return 0.500 if $chi2 >= 4.35146 && $chi2 < 6.62568;
195 return 0.250 if $chi2 >= 6.62568 && $chi2 < 9.23636;
196 return 0.100 if $chi2 >= 9.23636 && $chi2 < 11.07050;
197 return 0.050 if $chi2 >= 11.07050 && $chi2 < 12.83250;
198 return 0.025 if $chi2 >= 12.83250 && $chi2 < 15.08627;
199 return 0.010 if $chi2 >= 15.08627 && $chi2 < 16.74960;
200 return 0.005 if $chi2 >= 16.74960 && $chi2 < 20.52;
201 return 0.001 if $chi2 >= 20.52;
202 # justification for direction of "p": chi2 is larger as the data
203 # gets further from expected. Therefore, larger chi2 means less
204 # normal data.
205 }
206  
207 ### for now, assume 6 bins per kruegel2003anomaly. Need to generalize
208 ### this code
209 sub bin {
210 my $self = shift;
211 my @freq = @_;
212 my (@bins);
213  
214 # bins from kruegel2003anomaly
215 @bins = (0, 0, 0, 0, 0, 0);
216 $bins[0] = $freq[0];
217 map { $bins[1] += $_ } @freq[1..3];
218 map { $bins[2] += $_ } @freq[4..6];
219 map { $bins[3] += $_ } @freq[7..11];
220 map { $bins[4] += $_ } @freq[12..15];
221 map { $bins[5] += $_ } @freq[16..255];
222  
223 return @bins;
224 }
225  
226 =head1 AUTHOR INFORMATION
227  
228 Copyright 2005-2007, Kenneth Ingham. All rights reserved.
229  
230 This library is free software; you can redistribute it and/or modify
231 it under the same terms as Perl itself.
232  
233 Address bug reports and comments to: ids_test at i-pi.com. When sending
234 bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm,
235 IDS::DataSource.pm, the version of Perl, and the name and version of the
236 operating system you are using. Since Kenneth is a PhD student, the
237 speed of the reponse depends on how the research is proceeding.
238  
239 =head1 BUGS
240  
241 Please report them.
242  
243 =head1 SEE ALSO
244  
245 L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm>
246  
247 "Anomaly detection of web-based attacks" by Christopher Kruegel and
248 Giovanni Vigna, published in Proceedings of the 10th ACM conference
249 on Computer and communications security 2003, pages 251--261,
250 http://doi.acm.org/10.1145/948109.948144.
251  
252 =cut
253  
254 1;