| 1 | package IDS::Algorithm::MM; | ||
| 2 | use base qw(IDS::Algorithm); | ||
| 3 | $IDS::Algorithm::MM::VERSION = "1.0"; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | IDS::Algorithm::MM - Learn or test using a first-order Markov Model | ||
| 8 | (MM). | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | A usage synopsis would go here. Since it is not here, read on. | ||
| 13 | |||
| 14 | In section 4.2 in Kruegel and Vigna's paper, they ignored the | ||
| 15 | probability information that the MM provided, and produced a binary | ||
| 16 | result. In effect, they were using the constructed MM as a {N,D}FA. | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Someday more will be here. | ||
| 21 | |||
| 22 | Ideally, we would be using the algorithm from stolcke94bestfirst. | ||
| 23 | Constructing a DFA rather than a NFA in effect has performed most of the | ||
| 24 | state merging that stolcke93hidden do. | ||
| 25 | |||
| 26 | Consider also a java or C/C++ implementaion: | ||
| 27 | http://www.ghmm.org/ | ||
| 28 | http://www.run.montefiore.ulg.ac.be/~francois/software/jahmm/ | ||
| 29 | |||
| 30 | Useful information: | ||
| 31 | http://www.cs.brown.edu/research/ai/dynamics/tutorial/Documents/HiddenMarkovModels.html | ||
| 32 | http://www.comp.leeds.ac.uk/roger/HiddenMarkovModels/html_dev/main.html | ||
| 33 | L R Rabiner and B H Juang, `An introduction to HMMs', IEEE ASSP | ||
| 34 | Magazine, 3, 4-16. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | use Carp qw(cluck carp confess); | ||
| 41 | use IDS::DFAState; | ||
| 42 | use IDS::Utils qw(to_fh); | ||
| 43 | |||
| 44 | sub default_parameters { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | %{$self->{"params"}} = ( | ||
| 48 | "verbose" => 0, | ||
| 49 | "state_file" => 0, | ||
| 50 | ); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub param_options { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | return ( | ||
| 57 | "markov_verbose=i" => \${$self->{"params"}}{"verbose"}, | ||
| 58 | "ids_state=s" => \${$self->{"params"}}{"state_file"}, | ||
| 59 | ); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub initialize { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | $self->{"states"} = []; # a set of states | ||
| 66 | $self->{"tokens"} = {}; # token to a list of states that the token will | ||
| 67 | # cause a transition into | ||
| 68 | |||
| 69 | # set up start and accept states | ||
| 70 | $self->{"states"}[0] = new IDS::DFAState($self->{"params"}{"verbose"}); # Start state | ||
| 71 | $self->{"start"} = $self->{"states"}[0]; | ||
| 72 | $self->{"states"}[1] = new IDS::DFAState($self->{"params"}{"verbose"}); # Accept state | ||
| 73 | $self->{"accept"} = $self->{"states"}[1]; | ||
| 74 | ${$self->{"tokens"}}{'(ACCEPT)'} = $self->{"accept"}; | ||
| 75 | |||
| 76 | # whether the MM has been reduced (generalized) via state merging | ||
| 77 | $self->{"reduced"} = 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub save { | ||
| 81 | my $self = shift; | ||
| 82 | my $fname = $self->find_fname(shift); | ||
| 83 | defined($fname) && $fname or | ||
| 84 | confess *save{PACKAGE} . "::save missing filename"; | ||
| 85 | my $fh = to_fh($fname, ">"); | ||
| 86 | |||
| 87 | my ($i, $s, $node_map); | ||
| 88 | my $verbose = ${$self->{"params"}}{"verbose"}; | ||
| 89 | |||
| 90 | # generalization is not required, but controlled by IDS::Test | ||
| 91 | # $self->{"reduced"} or $self->generalize; | ||
| 92 | |||
| 93 | $i = 0; | ||
| 94 | for $s (@{$self->{"states"}}) { | ||
| 95 | $node_map->{$s} = $i++; | ||
| 96 | } | ||
| 97 | |||
| 98 | for $s (@{$self->{"states"}}) { | ||
| 99 | $s->print_edges($node_map, $fh); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | =over | ||
| 104 | |||
| 105 | =item printvcg | ||
| 106 | |||
| 107 | =item printvcg(filehandle) | ||
| 108 | |||
| 109 | Print in a form usable by VCG for printing the DFA. | ||
| 110 | |||
| 111 | If the filehandle is specified, print there; otherwise, print to STDOUT. | ||
| 112 | |||
| 113 | This code was stolen from DFA, and does not know about the | ||
| 114 | probabilities. | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub printvcg { | ||
| 121 | my $self = shift; | ||
| 122 | my $fh = $self->get_fh(shift); | ||
| 123 | my ($i, $node_map, $s); | ||
| 124 | |||
| 125 | print $fh "graph: { layoutalgorithm: minbackward\n" . | ||
| 126 | " manhattan_edges: yes\n" . | ||
| 127 | " splines: yes\n" . | ||
| 128 | " display_edge_labels: yes\n" . | ||
| 129 | " port_sharing: no\n" . | ||
| 130 | ' title: "HTTP DFA"' . "\n\n"; | ||
| 131 | |||
| 132 | # The nodes | ||
| 133 | $i = 0; | ||
| 134 | for $s (@{$self->{"states"}}) { | ||
| 135 | $s->print_vcg_node($i, $fh); | ||
| 136 | $node_map->{$s} = $i; | ||
| 137 | $i++; | ||
| 138 | } | ||
| 139 | print $fh "\n"; | ||
| 140 | |||
| 141 | # The transitions | ||
| 142 | # This loop cannot be combined with the one above because nodes have | ||
| 143 | # to be defined for vcg before they are referenced. | ||
| 144 | for $s (@{$self->{"states"}}) { | ||
| 145 | $s->print_vcg_edges($node_map, $fh); | ||
| 146 | } | ||
| 147 | print $fh "}\n"; | ||
| 148 | } | ||
| 149 | |||
| 150 | =over | ||
| 151 | |||
| 152 | =item load(filehandle) | ||
| 153 | |||
| 154 | Load a MM from a file; this is the inverse of "print", and the format | ||
| 155 | we expect is that used in $self->print. | ||
| 156 | |||
| 157 | =back | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub load { | ||
| 162 | my $self = shift; | ||
| 163 | my $fname = $self->find_fname(shift); | ||
| 164 | $fname or | ||
| 165 | confess *load{PACKAGE} . "::load missing filename"; | ||
| 166 | my $fh = to_fh($fname, "<"); | ||
| 167 | |||
| 168 | my ($cnt, $from, $to, $token, $l); | ||
| 169 | my $verbose = ${$self->{"params"}}{"verbose"}; | ||
| 170 | |||
| 171 | $l = 0; | ||
| 172 | while (<$fh>) { | ||
| 173 | chomp; | ||
| 174 | $l++; | ||
| 175 | ($from, $to, $cnt) = /^State (\d+) To (\d+) count (\d+) Token:$/; | ||
| 176 | defined($from) or confess *load{PACKAGE} . "::load missing 'from' on line $l"; | ||
| 177 | defined($to) or confess *load{PACKAGE} . "::load missing 'to' on line $l"; | ||
| 178 | defined($cnt) or confess *load{PACKAGE} . "::load missing 'cnt' on line $l"; | ||
| 179 | $token = <$fh>; | ||
| 180 | defined($token) or confess *load{PACKAGE} . "::load missing 'token' on line $l"; | ||
| 181 | $token =~ s/^ //; | ||
| 182 | chomp $token; | ||
| 183 | defined($self->{"states"}[$from]) or | ||
| 184 | $self->{"states"}[$from] = new IDS::DFAState($verbose); | ||
| 185 | defined($self->{"states"}[$to]) or | ||
| 186 | $self->{"states"}[$to] = new IDS::DFAState($verbose); | ||
| 187 | $self->{"states"}[$from]->add_outbound($token, $self->{"states"}[$to]); | ||
| 188 | $self->{"states"}[$to]->add_inbound($self->{"states"}[$from], $token); | ||
| 189 | ### set count for the node from $cnt | ||
| 190 | ${$self->{"tokens"}}{$token} = $self->{"states"}[$to]; | ||
| 191 | } | ||
| 192 | $self->verify if $verbose; # paranoia | ||
| 193 | } | ||
| 194 | |||
| 195 | =over | ||
| 196 | |||
| 197 | =item test(tokensref, string, instance) | ||
| 198 | |||
| 199 | Test the string of tokens and calculate the probability of the string | ||
| 200 | being seen. At each stage, we get a p in [0,1]. The result is the | ||
| 201 | product of these probabilities. | ||
| 202 | |||
| 203 | Note that if a transition cannot be made, we return a 0 probability. | ||
| 204 | |||
| 205 | =back | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub test { | ||
| 210 | my $self = shift; | ||
| 211 | my $tokensref = shift or | ||
| 212 | confess "bug: missing tokensref to ", *test{PACKAGE} . "::test"; | ||
| 213 | my $string = shift; # not used | ||
| 214 | my $instance = shift or | ||
| 215 | confess "bug: missing instance to ", *test{PACKAGE} . "::test"; | ||
| 216 | |||
| 217 | my ($result, $t, $current_state, $next, $prob); | ||
| 218 | |||
| 219 | $current_state = $self->{"start"}; | ||
| 220 | defined($current_state) or | ||
| 221 | confess *test{PACKAGE} . "::test missing start state in MM"; | ||
| 222 | |||
| 223 | $result = 1.0; | ||
| 224 | foreach $t (@{$tokensref}, "(ACCEPT)") { # note included eof token | ||
| 225 | $next = $current_state->next($t); | ||
| 226 | defined($next) or return 0; | ||
| 227 | $prob = $current_state->probability($t); | ||
| 228 | $result *= $prob; | ||
| 229 | $current_state = $next; | ||
| 230 | } | ||
| 231 | return $result; | ||
| 232 | } | ||
| 233 | |||
| 234 | =over | ||
| 235 | |||
| 236 | =item add(tokensref, string, instance) | ||
| 237 | |||
| 238 | The collection of tokens (in the list referenced by tokensref) | ||
| 239 | is a complete example of a list that should be accepted by the DFA. | ||
| 240 | |||
| 241 | string and instance are IDS::Test framework arguments that we ignore | ||
| 242 | because we do not need them. | ||
| 243 | |||
| 244 | WE add the transition from the last token to the '(ACCEPT)' state. | ||
| 245 | |||
| 246 | =back | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | sub add { | ||
| 251 | my $self = shift; | ||
| 252 | my $tokensref = shift or | ||
| 253 | confess "bug: missing tokensref to ", *add{PACKAGE} . "::add"; | ||
| 254 | my $string = shift; # not used | ||
| 255 | my $instance = shift or | ||
| 256 | confess "bug: missing instance to ", *add{PACKAGE} . "::add"; | ||
| 257 | my ($current_state, $t); | ||
| 258 | my $verbose = ${$self->{"params"}}{"verbose"}; | ||
| 259 | |||
| 260 | $current_state = $self->{"start"}; | ||
| 261 | print "Start $current_state\n" if $verbose; | ||
| 262 | foreach $t (@{$tokensref}) { | ||
| 263 | defined($t) or cluck("undefined token in add. List: '" . | ||
| 264 | join("' '", @{$tokensref}) . "'"); | ||
| 265 | print "Adding a transition from $current_state for $t\n" if $verbose; | ||
| 266 | ### MM: Need to increment the counter for the transition usage | ||
| 267 | $current_state = $self->add_transition($current_state, $t); | ||
| 268 | print " New state $current_state\n" if $verbose; | ||
| 269 | } | ||
| 270 | print "Adding a transition from $current_state for (ACCEPT)\n" if $verbose; | ||
| 271 | $current_state = $self->add_transition($current_state, "(ACCEPT)"); | ||
| 272 | } | ||
| 273 | |||
| 274 | =over | ||
| 275 | |||
| 276 | =item add_transition(from, token) | ||
| 277 | |||
| 278 | Add a transition from one state to another when the specified token is | ||
| 279 | received. It is not an error to try to add an existing transition. | ||
| 280 | In that event, this function quietly returns. If no such transition | ||
| 281 | exists, we look for a transition on the token; if so, we add an edge to | ||
| 282 | the destination node for the existing edge. Finally, if there is no | ||
| 283 | other choice, we create a new state and add the edge. | ||
| 284 | |||
| 285 | =back | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub add_transition { | ||
| 290 | my $self = shift; | ||
| 291 | my $from = shift; | ||
| 292 | defined($from) or | ||
| 293 | confess *add_transition{PACKAGE} . "::add_transition missing 'from'"; | ||
| 294 | $from->isa("IDS::DFAState") or | ||
| 295 | confess *add_transition{PACKAGE} . "::add_transition 'from' is wrong type"; | ||
| 296 | my $token = shift; # the token causing the transition | ||
| 297 | defined($token) or | ||
| 298 | confess *add_transition{PACKAGE} . "::add_transition missing 'token'"; | ||
| 299 | my $verbose = ${$self->{"params"}}{"verbose"}; | ||
| 300 | my ($next, $end, $s, $next_state, $n); | ||
| 301 | |||
| 302 | # First, see if a transition on this token already exists; if so, | ||
| 303 | # we will assume (valid?) that it is what we want. | ||
| 304 | $next = $from->next($token); | ||
| 305 | print " existing transition $next\n" if defined($next) && $verbose; | ||
| 306 | defined($next) and return $next; | ||
| 307 | |||
| 308 | print " No existing transition\n" if $verbose; | ||
| 309 | # OK, we know the transition does not exist. Does a *single* | ||
| 310 | # state exist that this token has caused a transition to in | ||
| 311 | # the past? If so, we assume (valid?) that we want to follow it. | ||
| 312 | # In this DFA, a token can cause only one transition. | ||
| 313 | if (exists ${$self->{"tokens"}}{$token}) { | ||
| 314 | $s = ${$self->{"tokens"}}{$token}; | ||
| 315 | $from->add_outbound($token, $s); | ||
| 316 | print " going to existing state $s\n" if $verbose; | ||
| 317 | return $s; | ||
| 318 | } | ||
| 319 | |||
| 320 | print " No existing state\n" if $verbose; | ||
| 321 | # We have run out of re-use options. Create a new state and | ||
| 322 | # set up the transition into it. | ||
| 323 | $next_state = new IDS::DFAState($verbose); | ||
| 324 | $from->add_outbound($token, $next_state); | ||
| 325 | # done in add_outbound: $next_state->add_inbound($from, $token); | ||
| 326 | ${$self->{"tokens"}}{$token} = $next_state; | ||
| 327 | push @{$self->{"states"}}, $next_state; | ||
| 328 | $n = $#{$self->{"states"}}; | ||
| 329 | print " State $n $next_state\n" if $verbose; | ||
| 330 | return $next_state; | ||
| 331 | } | ||
| 332 | |||
| 333 | =over | ||
| 334 | |||
| 335 | =item generalize() | ||
| 336 | |||
| 337 | Reduce the number of states in the model. | ||
| 338 | |||
| 339 | Our building a DFA rather than a NFA has in effect performed most of the | ||
| 340 | state merging that would have occurred. | ||
| 341 | |||
| 342 | XXX We should still be doing some checks for additional merge possibilities. | ||
| 343 | |||
| 344 | XXX A proof that the DFA is effectively the NFA with merged states would | ||
| 345 | be useful. | ||
| 346 | |||
| 347 | =back | ||
| 348 | |||
| 349 | =cut | ||
| 350 | |||
| 351 | sub generalize { | ||
| 352 | my $self = shift; | ||
| 353 | |||
| 354 | $self->{"reduced"} = 1; | ||
| 355 | } | ||
| 356 | |||
| 357 | # model prior, section 3.3 from stolcke93hidden | ||
| 358 | sub prior { | ||
| 359 | my $self = shift; | ||
| 360 | my ($m_s); | ||
| 361 | |||
| 362 | # e^-sizeof(M) | ||
| 363 | $m_s = exp(- scalar(@{$self->{"states"}})); | ||
| 364 | } | ||
| 365 | |||
| 366 | =head1 AUTHOR INFORMATION | ||
| 367 | |||
| 368 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 369 | |||
| 370 | This library is free software; you can redistribute it and/or modify | ||
| 371 | it under the same terms as Perl itself. | ||
| 372 | |||
| 373 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 374 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 375 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 376 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 377 | speed of the response depends on how the research is proceeding. | ||
| 378 | |||
| 379 | =head1 BUGS | ||
| 380 | |||
| 381 | Please report them. | ||
| 382 | |||
| 383 | =head1 SEE ALSO | ||
| 384 | |||
| 385 | L<IDS::Test>, L<IDS::DataSource>, L<IDS::Algorithm> | ||
| 386 | |||
| 387 | Best-first Model Merging for Hidden Markov Model Induction by A. Stolcke | ||
| 388 | and S. M. Omohundro, Technical Report TR-94-003, 1994. | ||
| 389 | http://citeseer.ist.psu.edu/stolcke94bestfirst.html | ||
| 390 | |||
| 391 | Anomaly detection of web-based attacks by Christopher Kruegel and | ||
| 392 | Giovanni Vigna in Proceedings of the 10th ACM conference on Computer and | ||
| 393 | Communications Security, 2003, pages 251--261, ISBN 1-58113-738-9. | ||
| 394 | http://doi.acm.org/10.1145/948109.948144 | ||
| 395 | |||
| 396 | =cut | ||
| 397 | |||
| 398 | 1; |