Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

(Guildenstern) Re: Finding patterns (prelim code)

by Guildenstern (Deacon)
on Aug 07, 2000 at 18:58 UTC ( [id://26571] : note . print w/replies, xml ) Need Help??


in reply to Finding patterns in packet data?

Here's what I've come up with so far. Basically I iterate over each packet and generate substrings from size 2 to the size of the packet. I then compare that against all of the other packets. It needs some tweaking in that area, but I get output that makes sense. Only problem is that on my machine, running it with large packets kills the OS after an hour or so. Apparently I need to make some memory optimizations somewhere. All comments are appreciated! Windows environ - no #!
die "No file name" unless @ARGV; my @packets = (); while (<>) { push(@packets,$_); } chop(@packets); my $maxlen =0; my $index = 0; my $curpack; my %all; # iterate over all packets foreach $curpack (@packets) { $maxlen = length($curpack); # substring size for (my $fsize = 2; $fsize < $maxlen; $fsize ++) { # packet to compare against for (my $pnum = 0; $pnum < @packets; $pnum ++) { # start substr'ing at index 0 for (0..$maxlen-$fsize) { # don't compare against self if ($index != $pnum) { my $str = substr($curpack,$_,$fsize); my @temparr = ($packets[$pnum] =~ /$str/g); my $nmatch = @temparr; if (defined($all{$str})) { $all{$str} += $nmatch; } else { $all{$str} = $nmatch; } undef @temparr; } } } } $index ++; # print results for current packet print "Comparing packet ",$index," to all others:\n"; my $value; my @sort = sort {length($b) <=> length($a)} keys %all; foreach $value (@sort) { if ($all{$value} > 0) { print "$value: $all{$value} times\n"; } delete($all{$value}); } undef @sort; } exit;