| This adds a script to create matches for each signature against every other sig… | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 97843c47d173a7437e2329017fbc6ae5bab8f836 | |
| parent 78f8af7b8c4dc5c0f0ad6fa3683702c1bd01f8f6 | |
| Author: HD Moore <[email protected]> | |
| Date: Sun, 27 Mar 2011 18:30:32 +0000 | |
| This adds a script to create matches for each signature against every other sig… | |
| Diffstat: | |
| A bin/create_matches.rb | 80 +++++++++++++++++++++++++++++… | |
| 1 file changed, 80 insertions(+), 0 deletions(-) | |
| --- | |
| diff --git a/bin/create_matches.rb b/bin/create_matches.rb | |
| @@ -0,0 +1,80 @@ | |
| +#!/usr/bin/env ruby | |
| + | |
| +# | |
| +# Given the path to a signature file, determine the closests matching signatur… | |
| +# within the same directory, creating a .match file. | |
| +# | |
| + | |
| + | |
| +def is_bad_sig?(sig) | |
| + return true if sig == [0,0,0,0] | |
| + false | |
| +end | |
| + | |
| +def load_signature(data) | |
| + data.split("\n").map { |line| | |
| + line.strip.split(",").map{|bits| bits.to_i } | |
| + }.reject {|sig| is_bad_sig?(sig) } | |
| +end | |
| + | |
| +inp = ARGV.shift || exit(1) | |
| +ind = ARGV.shift | |
| +dir = File.expand_path(inp) + "/" | |
| +set = {} | |
| + | |
| +d = Dir.new(dir) | |
| +d.entries.each do |ent| | |
| + next if ent !~ /\.sig$/ | |
| + name,trash = ent.split('.', 2) | |
| + data = File.read(File.join(dir, ent)) | |
| + set[name] = load_signature(data) | |
| + | |
| + if set.keys.length % 500 == 0 | |
| + puts "[*] Loaded #{set.keys.length} signatures..." … | |
| + end | |
| +end | |
| +d.close | |
| + | |
| +puts "[*] Finished loading #{set.keys.length} signatures..." | |
| + | |
| +max = 1 | |
| +cnt = 0 | |
| +stime = Time.now.to_f | |
| + | |
| +targs = ind ? [ind] : set.keys.sort | |
| + | |
| +while targs.length > 0 | |
| + jobs = [] | |
| + | |
| + while jobs.length < max | |
| + targ = targs.shift | |
| + break if not targ | |
| + pid = fork | |
| + | |
| + if pid | |
| + jobs << pid | |
| + next | |
| + end | |
| + | |
| + mine = targ | |
| + msig = set[targ] | |
| + | |
| + res = [] | |
| + set.each_pair do |n,sig| | |
| + next if n == mine | |
| + hits = (msig & sig).length | |
| + res << [ ( hits / msig.length.to_f ) * 100.0, hits, n ] | |
| + end | |
| + | |
| + File.open(File.join(dir, mine + ".matches"), "w") do |fd| | |
| + res.sort{|a,b| b[0] <=> a[0] }.each do |r| | |
| + fd.puts "#{"%.2f" % r[0]}\t#{r[2]}" | |
| + end | |
| + end | |
| + | |
| + exit(0) | |
| + end | |
| + jobs.each {|j| Process.waitpid(j) } | |
| + cnt += jobs.length | |
| + puts "[*] Processed #{cnt}/#{set.keys.length} in #{Time.now.to_f - sti… | |
| +end |