Class: Snortor::RulefileCollection

Inherits:
Array
  • Object
show all
Includes:
RuleFinder, RuleLoader
Defined in:
lib/rulefile_collection.rb

Instance Method Summary collapse

Methods included from RuleFinder

#find_all_by_msg, #find_by_msg

Methods included from RuleLoader

#read_rules_from_dir, #read_rules_from_file

Instance Method Details

#<<(a) ⇒ Object



12
13
14
15
# File 'lib/rulefile_collection.rb', line 12

def <<(a)
  raise "only instances of Rulefile allowed" unless a.class == Rulefile
  old_push(a)
end

#[](index) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/rulefile_collection.rb', line 17

def [](index)
  offset = 0
  self.old_each do |rule_file|
    if index < offset+rule_file.size
      return rule_file[index-offset]
      break
    end
    offset += rule_file.size
  end
end

#each(&block) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rulefile_collection.rb', line 36

def each(&block)
  self.old_each do |rulefile|
    rulefile.each do |rule|
      block.call(rule)
    end
  end
  nil
end

#import_rules(path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rulefile_collection.rb', line 45

def import_rules(path)
  rulefile = nil
  if File.directory?(path)
    read_rules_from_dir(path) do |filepath,line|
      if rulefile == nil || rulefile.filepath != filepath
        rulefile = Rulefile.new(filepath)
        rulefile.calc_relative_path(path)
        self << rulefile
      end
      begin
        # only parse lines that seem to be a rule
        # maybe handle includes and comments as well
        if line["alert"]
          if line.strip[0] == "#"
            line[line.index("#")] = ""
            rule = Snort::Rule.parse_rule(line.strip)
            rule.active = false
            rulefile << rule if rule
          else
            rulefile << Snort::Rule.parse_rule(line.strip)
          end
        end
      rescue
        puts "Problem parsing line #{line} in #{filepath}"
      end
    end
  else

    read_rules_from_file(path) do |filepath,line|
      if rulefile == nil || rulefile.filepath != filepath
        rulefile = Rulefile.new(filepath)
        rulefile.calc_relative_path(path)
        self << rulefile
      end
      begin
        # only parse lines that seem to be a rule
        # maybe handle includes and comments as well
        if line["alert"]
          if line.strip[0] == "#"
            line[line.index("#")] = ""
            rule = Snort::Rule.parse_rule(line.strip)
            rule.active = false
            rulefile << rule if rule
          else
            rulefile << Snort::Rule.parse_rule(line.strip)
          end
        end
      rescue
        puts "Problem parsing line #{line} in #{filepath}"
      end
    end
  end
end

#old_eachObject



10
# File 'lib/rulefile_collection.rb', line 10

alias_method :old_each, :each

#old_pushObject



9
# File 'lib/rulefile_collection.rb', line 9

alias_method :old_push, :<<

#sizeObject



28
29
30
31
32
33
34
# File 'lib/rulefile_collection.rb', line 28

def size
  res = 0
  self.old_each do |rule_file|
    res += rule_file.size
  end
  res
end

#write_rules(path) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rulefile_collection.rb', line 99

def write_rules(path)
  Dir.mkdir(path) if !File.exists?(path)
  self.old_each do |rulefile|
    begin
      dest = File.join(path,rulefile.relative_path,rulefile.filename)
      file = File.new(dest,"w")
    rescue Errno::ENOENT
      Dir.mkdir(File.join(path,rulefile.relative_path))
      file = File.new(dest,"w")
    end
    rulefile.each do |rf|
      if rf.active
        file.write(rf.to_line.gsub("\n","")+"\n")
      else
        file.write("# "+rf.to_line.gsub("\n","")+"\n")
      end
    end
    file.close
  end
end