Class: OpenvasCli::VasResult

Inherits:
VasBase
  • Object
show all
Defined in:
lib/openvas-cli/vas_result.rb

Instance Attribute Summary collapse

Attributes inherited from VasBase

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VasBase

#create_or_update, #delete_record, #destroy, #destroy!, #initialize, #new_record?, #reset_changes, #save, #save!, #to_key, #to_param, #update_attributes

Methods included from ConnAddin

included

Methods included from XmlAddin

included

Constructor Details

This class inherits a constructor from OpenvasCli::VasBase

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def description
  @description
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def host
  @host
end

#notesObject

Returns the value of attribute notes.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def notes
  @notes
end

#overridesObject

Returns the value of attribute overrides.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def overrides
  @overrides
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def port
  @port
end

#result_idObject

Returns the value of attribute result_id.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def result_id
  @result_id
end

#rule_idObject

Returns the value of attribute rule_id.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def rule_id
  @rule_id
end

#subnetObject

Returns the value of attribute subnet.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def subnet
  @subnet
end

#task_idObject

Returns the value of attribute task_id.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def task_id
  @task_id
end

#threatObject

Returns the value of attribute threat.



6
7
8
# File 'lib/openvas-cli/vas_result.rb', line 6

def threat
  @threat
end

Class Method Details

.get_all(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/openvas-cli/vas_result.rb', line 15

def self.get_all(options = {})
  options[:sort_by] ||= :threat
  
  params = {:overrides => 0, :notes => 0}
  
  params[:result_id] = options[:id] if options[:id]
  if options[:task_id]
    params[:task_id] = options[:task_id]
    params[:apply_overrides] = 1 if options[:apply_overrides]
  end

  levels = []    
  if options[:filter]
    options[:filter].each { |ft|
      case ft
      when :high
        levels << "High"
      when :medium
        levels << "Medium"
      when :low
        levels << "Low"
      when :log
        levels << "Log"
      when :debug
        levels << "Debug"
      end
    }
  end
  req = Nokogiri::XML::Builder.new { |xml|
    xml.get_results(params)
  }
  
  results = {}
  
  begin
    xml = connection.send_receive(req.doc)
    xml.xpath("//result").each { |xr|
      id     = extract_value_from("@id", xr)
      threat = extract_value_from("threat", xr)
      if (levels.empty? || levels.include?(threat)) && results.has_key?(id) == false
        res = parse_result_node(xr, options[:task_id])
        results[res.result_id] = res
      end
    }    
  rescue VasExceptions::CommandException => e
  end
  
  ret = results.values

  #Sort Results
  ret.sort!{ |a,b| a.result_id <=> b.result_id }       if options[:sort_by] == :result_id
  ret.sort!{ |a,b| a.host <=> b.host }                 if options[:sort_by] == :host
  ret.sort!{ |a,b| a.rule_id <=> b.rule_id }           if options[:sort_by] == :rule_id
  ret.sort!{ |a,b| a.subnet <=> b.subnet }             if options[:sort_by] == :subnet
  ret.sort!{ |a,b| b.threat_level <=> a.threat_level } if options[:sort_by] == :threat
  
  #Fake Pagination
  if options[:start]
    if options[:count]
      ret = ret[options[:start], options[:count]]
    else
      ret = ret[options[:start], ret.length - options[:start]]
    end
  end
  
  ret
end

.get_by_id(id) ⇒ Object



11
12
13
# File 'lib/openvas-cli/vas_result.rb', line 11

def self.get_by_id(id)
  get_all(:id => id)[0]
end

.parse_result_node(node, task_id = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/openvas-cli/vas_result.rb', line 83

def self.parse_result_node(node, task_id = nil)
  res             = VasResult.new
  res.id          = extract_value_from("@id", node)
  res.threat      = extract_value_from("threat", node)
  res.subnet      = extract_value_from("subnet", node)
  res.host        = extract_value_from("host", node)
  res.rule_id     = extract_value_from("nvt/@oid", node)
  res.description = extract_value_from("description", node)
  res.task_id     = task_id if task_id
  
  res
end

Instance Method Details

#threat_levelObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/openvas-cli/vas_result.rb', line 96

def threat_level
  case @threat
  when "High"
    5
  when "Medium"
    4
  when "Low"
    3
  when "Log"
    2
  when "Debug"
    1
  else
    0
  end
end