openvas-cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ require 'vas_base'
2
+
3
+ # NOT IMPLEMENTED
4
+ class VasOverride
5
+ attr_accessor :task_id, :original_threat, :rule_id, :is_new
6
+
7
+ end
@@ -0,0 +1,59 @@
1
+ require 'vas_base'
2
+ require 'nokogiri'
3
+
4
+ # A name-value pair for OpenVAS configuration preferences. If inspecting a
5
+ # specific configuration set, +config_id+ will be populated. If +config_id+ is
6
+ # non-existant, then the preference is a system-wide default.
7
+ #--
8
+ # TODO: Implement write functions
9
+ #++
10
+ class VasPreference < VasBase
11
+ # Name of the configuration preference.
12
+ attr_accessor :name
13
+ # Value used for the preference.
14
+ attr_accessor :value
15
+ # Configuration Identifier. If +nil+, the preference is a system-wide
16
+ # default.
17
+ attr_accessor :config_id
18
+
19
+ validates :name, :presence => true, :length=>{:minimum => 1}
20
+
21
+ # Pulls Vas preferences.
22
+ # === Options:
23
+ # [:nvt_oid => [oid]] pulls preferences associated associated with the provided VasNVT.oid
24
+ # [:name => [name]] pulls the preference with the specified name
25
+ # [:sort_by => [field_name]] filters the results by the provided field name. Valid symbols are, :name, :value, :config_id.
26
+ def self.get_all(options={})
27
+ manual_filter = false
28
+ params = {}
29
+ params[:nvt_oid] = options[:nvt_oid] if options[:nvt_oid]
30
+ manual_filter = true if options[:name]
31
+
32
+ req = Nokogiri::XML::Builder.new { |xml|
33
+ if params.empty?
34
+ xml.get_preferences
35
+ else
36
+ xml.get_preferences(params)
37
+ end
38
+ }
39
+
40
+ prefs = client.send_receive(req.doc)
41
+ ret = []
42
+ prefs.xpath("//preference").each { |p|
43
+ pref = VasPreference.new
44
+ pref.name = extract_value_from("name", p)
45
+ pref.value = extract_value_from("value", p)
46
+ if manual_filter
47
+ ret << pref if options[:name] && options[:name] == pref.name
48
+ else
49
+ ret << pref
50
+ end
51
+ }
52
+
53
+ ret.sort!{ |a,b| a.name <=> b.name } if params[:sort_by] == :name
54
+ ret.sort!{ |a,b| a.value <=> b.value } if params[:sort_by] == :value
55
+ ret.sort!{ |a,b| a.config_id <=> b.config_id } if params[:sort_by] == :config_id
56
+
57
+ ret
58
+ end
59
+ end
@@ -0,0 +1,118 @@
1
+ require 'vas_base'
2
+
3
+ # Contains the details of a single OpenVAS report.
4
+ class VasReport < VasBase
5
+ attr_accessor :report_id
6
+ attr_accessor :task_id
7
+ attr_accessor :task_name
8
+ attr_accessor :started_at
9
+ # Overall Status Only
10
+ attr_accessor :status
11
+
12
+ validates :report_id, :presence => true, :UUID => true
13
+ validates :task_id, :presence => true, :UUID => true
14
+
15
+ # Pulls report details based off of the options passed. By default, pulls all
16
+ # reports.
17
+ # === Options:
18
+ # [:report_id => [report_id]] Pulls a specific +report_id+. If the id provided is bogus, an empty set is returned.
19
+ # [:filter => [array_of_filter_symbols]] Filters the report results by severity. Valid symbols are: [:high, :medium, :low, :log, :deubg].
20
+ def self.get_all(options={})
21
+ params = {}
22
+ params[:report_id] = options[:report_id] if options[:report_id]
23
+ if options[:filter]
24
+ params[:levels] = ""
25
+ options[:filter].each { |f|
26
+ case f
27
+ when :high
28
+ params[:levels] += 'h'
29
+ when :medium
30
+ params[:levels] += 'm'
31
+ when :low
32
+ params[:levels] += 'l'
33
+ when :log
34
+ params[:levels] += 'g'
35
+ when :debug
36
+ params[:levels] += 'd'
37
+ end
38
+ }
39
+ end
40
+
41
+ req = Nokogiri::XML::Builder.new { |xml|
42
+ if params.empty?
43
+ xml.get_reports
44
+ else
45
+ xml.get_reports(params)
46
+ end
47
+ }
48
+
49
+ begin
50
+ repts = client.send_receive(req.doc)
51
+ rescue VasExceptions::CommandException => e
52
+ raise e unless e.message =~ /Failed to find/i
53
+ return []
54
+ end
55
+
56
+ ret = []
57
+ repts.xpath('//report').each { |r|
58
+ r_id = VasReport.new
59
+ r_id.report_id = extract_value_from("@id", r)
60
+ r_id.task_id = extract_value_from("task/@id", r)
61
+ r_id.task_name = extract_value_from("task/name", r)
62
+ r_id.status = extract_value_from("scan_run_status", r)
63
+ r_id.started_at = extract_value_from("scan_start", r)
64
+
65
+ r_id.result_count[:total] = extract_value_from("result_count/full", r).to_i
66
+ r_id.result_count[:filtered] = extract_value_from("result_count/filtered", r).to_i
67
+ r_id.result_count[:debug][:total] = extract_value_from("result_count/debug/full", r).to_i
68
+ r_id.result_count[:debug][:filtered] = extract_value_from("result_count/debug/filtered", r).to_i
69
+ r_id.result_count[:high][:total] = extract_value_from("result_count/hole/full", r).to_i
70
+ r_id.result_count[:high][:filtered] = extract_value_from("result_count/hold/filtered", r).to_i
71
+ r_id.result_count[:low][:total] = extract_value_from("result_count/info/full", r).to_i
72
+ r_id.result_count[:low][:filtered] = extract_value_from("result_count/info/filtered", r).to_i
73
+ r_id.result_count[:log][:total] = extract_value_from("result_count/log/full", r).to_i
74
+ r_id.result_count[:log][:filtered] = extract_value_from("result_count/log/filtered", r).to_i
75
+ r_id.result_count[:medium][:total] = extract_value_from("result_count/warning/full", r).to_i
76
+ r_id.result_count[:medium][:filtered] = extract_value_from("result_count/warning/filtered", r).to_i
77
+
78
+ r.xpath("results/result").each { |result|
79
+ r_id.results << VasResult.parse_result_node(result)
80
+ }
81
+
82
+ ret << r_id
83
+ }
84
+
85
+ ret
86
+ end
87
+
88
+ def to_xml
89
+ req = Nokogiri::XML::Builder.new { |xml|
90
+ xml.get_reports(:report_id => @report_id)
91
+ }
92
+
93
+ report = VasBase.client.send_receive(req.doc)
94
+
95
+ report.at_xpath('//report').to_xml
96
+ end
97
+
98
+ def task
99
+ @task ||= VasTask.get_all(:task_id => @task_id)[0]
100
+ end
101
+
102
+ def results
103
+ @results ||= []
104
+ end
105
+
106
+ def result_count
107
+ unless @result_count
108
+ @result_count = {:total => 0, :filtered => 0,
109
+ :debug => {:total => 0, :filtered => 0},
110
+ :log => {:total => 0, :filtered => 0},
111
+ :low => {:total => 0, :filtered => 0},
112
+ :medium => {:total => 0, :filtered => 0},
113
+ :high => {:total => 0, :filtered => 0}
114
+ }
115
+ end
116
+ @result_count
117
+ end
118
+ end
@@ -0,0 +1,102 @@
1
+ require 'vas_base'
2
+ require 'vas_override'
3
+
4
+ class VasResult < VasBase
5
+ attr_accessor :result_id, :subnet, :host, :port, :rule_id, :threat,
6
+ :description, :notes, :overrides, :task_id
7
+
8
+ validates :result_id, :presence=>true, :UUID=>true
9
+
10
+ def self.get_all(options = {})
11
+ options[:sort_by] ||= :threat
12
+
13
+ params = {:overrides => 0, :notes => 0}
14
+ if options[:task_id]
15
+ params[:task_id] = options[:task_id]
16
+ params[:apply_overrides] = 1 if options[:apply_overrides]
17
+ end
18
+
19
+ levels = []
20
+ if options[:filter]
21
+ options[:filter].each { |ft|
22
+ case ft
23
+ when :high
24
+ levels << "High"
25
+ when :medium
26
+ levels << "Medium"
27
+ when :low
28
+ levels << "Low"
29
+ when :log
30
+ levels << "Log"
31
+ when :debug
32
+ levels << "Debug"
33
+ end
34
+ }
35
+ end
36
+ req = Nokogiri::XML::Builder.new { |xml|
37
+ xml.get_results(params)
38
+ }
39
+ xml = client.send_receive(req.doc)
40
+
41
+ results = {}
42
+ xml.xpath("//result").each { |xr|
43
+ id = extract_value_from("@id", xr)
44
+ threat = extract_value_from("threat", xr)
45
+ if (levels.empty? || levels.include?(threat)) && results.has_key?(id) == false
46
+ res = parse_result_node(xr, options[:task_id])
47
+ results[res.result_id] = res
48
+ end
49
+ }
50
+
51
+ ret = results.values
52
+
53
+ #Sort Results
54
+ ret.sort!{ |a,b| a.result_id <=> b.result_id } if options[:sort_by] == :result_id
55
+ ret.sort!{ |a,b| a.host <=> b.host } if options[:sort_by] == :host
56
+ ret.sort!{ |a,b| a.rule_id <=> b.rule_id } if options[:sort_by] == :rule_id
57
+ ret.sort!{ |a,b| a.subnet <=> b.subnet } if options[:sort_by] == :subnet
58
+ ret.sort!{ |a,b| b.threat_level <=> a.threat_level } if options[:sort_by] == :threat
59
+
60
+ #Fake Pagination
61
+ if options[:start]
62
+ if options[:count]
63
+ ret = ret[options[:start], options[:count]]
64
+ else
65
+ ret = ret[options[:start], ret.length - options[:start]]
66
+ end
67
+ end
68
+
69
+ ret
70
+ end
71
+
72
+ def self.parse_result_node(node, task_id = nil)
73
+ res = VasResult.new
74
+ res.result_id = extract_value_from("@id", node)
75
+ res.threat = extract_value_from("threat", node)
76
+ res.subnet = extract_value_from("subnet", node)
77
+ res.host = extract_value_from("host", node)
78
+ res.rule_id = extract_value_from("nvt/@oid", node)
79
+ res.description = extract_value_from("description", node)
80
+ res.task_id = task_id if task_id
81
+
82
+ res
83
+ end
84
+
85
+ def threat_level
86
+ case @threat
87
+ when "High"
88
+ 5
89
+ when "Medium"
90
+ 4
91
+ when "Low"
92
+ 3
93
+ when "Log"
94
+ 2
95
+ when "Debug"
96
+ 1
97
+ else
98
+ 0
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,63 @@
1
+ require 'vas_base'
2
+
3
+ class VasSchedule < VasBase
4
+ attr_accessor :schedule_id, :name, :comment, :first_time, :next_time,
5
+ :period, :period_months, :duration, :in_use, :task_ids
6
+
7
+ validates :schedule_id, :presence => true, :UUID => true
8
+
9
+ def self.get_all(options = {})
10
+
11
+ manual_sort = false
12
+ params = {:details => '1'}
13
+ params[:schedule_id] = options[:schedule_id] if options[:schedule_id]
14
+ case options[:sort_by]
15
+ when :schedule_id
16
+ params[:sort_field] = 'id'
17
+ when :next_time
18
+ manual_sort = true
19
+ when :first_time
20
+ params[:first_time] = 'first_time'
21
+ else
22
+ params[:sort_field] = 'name'
23
+ end
24
+
25
+ req = Nokogiri::XML::Builder.new { |xml|
26
+ xml.get_schedules(params)
27
+ }
28
+
29
+ schedules = client.send_receive(req.doc)
30
+
31
+ ret = []
32
+ schedules.xpath("//schedule").each { |s|
33
+ sch = VasSchedule.new
34
+ sch.schedule_id = extract_value_from("@id", s)
35
+ sch.name = extract_value_from("name", s)
36
+ sch.comment = extract_value_from("comment", s)
37
+ t_time = extract_value_from("first_time", s)
38
+ sch.first_time = Time.parse(t_time) unless t_time == ""
39
+ t_time = extract_value_from("next_time", s)
40
+ sch.next_time = Time.parse(t_time) unless t_time == "" or t_time == "done"
41
+ sch.period = extract_value_from("period", s).to_i
42
+ sch.period_months = extract_value_from("period_months", s).to_i
43
+ t_time = extract_value_from("duration", s)
44
+ unless t_time == ""
45
+ sch.duration = t_time.to_i unless t_time == 0
46
+ end
47
+ sch.in_use = extract_value_from("in_use", s).to_i > 0
48
+ sch.task_ids = []
49
+ s.xpath('tasks/task/@id') { |t|
50
+ sch.task_ids << t.value
51
+ }
52
+ ret << sch
53
+ }
54
+
55
+ if manual_sort
56
+ if options[:sort_by] == :next_time
57
+ ret.sort!{ |a,b| a.next_time <=> b.next_time }
58
+ end
59
+ end
60
+
61
+ ret
62
+ end
63
+ end
@@ -0,0 +1,28 @@
1
+ require 'nokogiri'
2
+
3
+ class VasTarget < VasBase
4
+ attr_accessor :vas_id, :name, :hosts, :comment, :credential
5
+
6
+ validates :vas_id, :UUID => true
7
+
8
+ def self.get_all
9
+ req = Nokogiri::XML::Builder.new { |xml|
10
+ xml.get_targets(:tasks=>0)
11
+ }
12
+
13
+ targets = client.send_receive(req.doc)
14
+
15
+ ret = []
16
+ targets.xpath('//target').each { |t|
17
+ targ = VasTarget.new
18
+ targ.vas_id = extract_value_from("@id", t)
19
+ targ.name = extract_value_from("name", t)
20
+ host_string = extract_value_from("hosts", t)
21
+ targ.hosts = host_string.split(", ")
22
+ targ.comment = extract_value_from("comment", t)
23
+ ret << targ
24
+ }
25
+
26
+ ret
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ require 'vas_base'
2
+ require 'vas_result'
3
+
4
+ class VasTask < VasBase
5
+ attr_accessor :task_id, :task_name, :comment, :status,
6
+ :progress, :times_run, :last_report_id
7
+
8
+ validates :task_id, :presence => true, :UUID => true
9
+
10
+ def self.get_all(options = {})
11
+ params = {:apply_overrides => 0, :sort_field => "name"}
12
+
13
+ params[:task_id] = options[:task_id] if options[:task_id]
14
+
15
+ req = Nokogiri::XML::Builder.new { |xml|
16
+ xml.get_tasks
17
+ }
18
+
19
+ tasks = client.send_receive(req.doc)
20
+
21
+ ret = []
22
+ tasks.xpath('//task').each { |t|
23
+ t_id = VasTask.new
24
+ t_id.task_id = extract_value_from("@id", t)
25
+ t_id.task_name = extract_value_from("name", t)
26
+ t_id.comment = extract_value_from("comment", t)
27
+ t_id.status = extract_value_from("status", t)
28
+ t_id.progress = extract_value_from("progress", t)
29
+ t_id.times_run = extract_value_from("report_count/finished", t).to_i
30
+ t_id.last_report_id = extract_value_from("last_report/report/@id", t)
31
+ ret << t_id
32
+ }
33
+
34
+ ret
35
+ end
36
+ end
@@ -0,0 +1,138 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{openvas-cli}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Reed Swenson"]
12
+ s.date = %q{2011-03-11}
13
+ s.description = %q{A full ruby implementation of the OpenVAS OMP (version 2.0) protocol.}
14
+ s.email = %q{fleureed@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "features/openvas-cli.feature",
29
+ "features/step_definitions/openvas-cli_steps.rb",
30
+ "features/support/env.rb",
31
+ "lib/openvas-cli/oid_validator.rb",
32
+ "lib/openvas-cli/openvas-cli.rb",
33
+ "lib/openvas-cli/uuid_validator.rb",
34
+ "lib/openvas-cli/vas_base.rb",
35
+ "lib/openvas-cli/vas_exceptions.rb",
36
+ "lib/openvas-cli/vas_nvt.rb",
37
+ "lib/openvas-cli/vas_nvt_family.rb",
38
+ "lib/openvas-cli/vas_override.rb",
39
+ "lib/openvas-cli/vas_preference.rb",
40
+ "lib/openvas-cli/vas_report.rb",
41
+ "lib/openvas-cli/vas_result.rb",
42
+ "lib/openvas-cli/vas_schedule.rb",
43
+ "lib/openvas-cli/vas_target.rb",
44
+ "lib/openvas-cli/vas_task.rb",
45
+ "openvas-cli.gemspec",
46
+ "spec/openvas-cli/openvas-cli_spec.rb",
47
+ "spec/openvas-cli/vas_nvt_family_spec.rb",
48
+ "spec/openvas-cli/vas_nvt_spec.rb",
49
+ "spec/openvas-cli/vas_preference_spec.rb",
50
+ "spec/openvas-cli/vas_report_spec.rb",
51
+ "spec/openvas-cli/vas_result_spec.rb",
52
+ "spec/openvas-cli/vas_schedule_spec.rb",
53
+ "spec/openvas-cli/vas_target_spec.rb",
54
+ "spec/openvas-cli/vas_task_spec.rb",
55
+ "spec/spec_helper.rb"
56
+ ]
57
+ s.homepage = %q{http://reedswenson.github.com/openvas-cli/}
58
+ s.licenses = ["MIT"]
59
+ s.require_paths = ["lib"]
60
+ s.rubygems_version = %q{1.6.0}
61
+ s.summary = %q{A full ruby implementation of the OpenVAS OMP (version 2.0) protocol.}
62
+ s.test_files = [
63
+ "spec/openvas-cli/openvas-cli_spec.rb",
64
+ "spec/openvas-cli/vas_nvt_family_spec.rb",
65
+ "spec/openvas-cli/vas_nvt_spec.rb",
66
+ "spec/openvas-cli/vas_preference_spec.rb",
67
+ "spec/openvas-cli/vas_report_spec.rb",
68
+ "spec/openvas-cli/vas_result_spec.rb",
69
+ "spec/openvas-cli/vas_schedule_spec.rb",
70
+ "spec/openvas-cli/vas_target_spec.rb",
71
+ "spec/openvas-cli/vas_task_spec.rb",
72
+ "spec/spec_helper.rb"
73
+ ]
74
+
75
+ if s.respond_to? :specification_version then
76
+ s.specification_version = 3
77
+
78
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
79
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.5"])
80
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.5"])
81
+ s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.4"])
82
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
83
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
84
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
85
+ s.add_development_dependency(%q<rcov>, [">= 0"])
86
+ s.add_development_dependency(%q<ZenTest>, [">= 0"])
87
+ s.add_development_dependency(%q<log4r>, [">= 0"])
88
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.5"])
89
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.5"])
90
+ s.add_runtime_dependency(%q<nokogiri>, [">= 1.4.4"])
91
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
92
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
93
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
94
+ s.add_development_dependency(%q<rcov>, [">= 0"])
95
+ s.add_development_dependency(%q<ZenTest>, [">= 0"])
96
+ s.add_development_dependency(%q<log4r>, [">= 0"])
97
+ else
98
+ s.add_dependency(%q<activesupport>, [">= 3.0.5"])
99
+ s.add_dependency(%q<activerecord>, [">= 3.0.5"])
100
+ s.add_dependency(%q<nokogiri>, [">= 1.4.4"])
101
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
102
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
103
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
104
+ s.add_dependency(%q<rcov>, [">= 0"])
105
+ s.add_dependency(%q<ZenTest>, [">= 0"])
106
+ s.add_dependency(%q<log4r>, [">= 0"])
107
+ s.add_dependency(%q<activesupport>, [">= 3.0.5"])
108
+ s.add_dependency(%q<activerecord>, [">= 3.0.5"])
109
+ s.add_dependency(%q<nokogiri>, [">= 1.4.4"])
110
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
111
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
112
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
113
+ s.add_dependency(%q<rcov>, [">= 0"])
114
+ s.add_dependency(%q<ZenTest>, [">= 0"])
115
+ s.add_dependency(%q<log4r>, [">= 0"])
116
+ end
117
+ else
118
+ s.add_dependency(%q<activesupport>, [">= 3.0.5"])
119
+ s.add_dependency(%q<activerecord>, [">= 3.0.5"])
120
+ s.add_dependency(%q<nokogiri>, [">= 1.4.4"])
121
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
122
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
123
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
124
+ s.add_dependency(%q<rcov>, [">= 0"])
125
+ s.add_dependency(%q<ZenTest>, [">= 0"])
126
+ s.add_dependency(%q<log4r>, [">= 0"])
127
+ s.add_dependency(%q<activesupport>, [">= 3.0.5"])
128
+ s.add_dependency(%q<activerecord>, [">= 3.0.5"])
129
+ s.add_dependency(%q<nokogiri>, [">= 1.4.4"])
130
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
131
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
132
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
133
+ s.add_dependency(%q<rcov>, [">= 0"])
134
+ s.add_dependency(%q<ZenTest>, [">= 0"])
135
+ s.add_dependency(%q<log4r>, [">= 0"])
136
+ end
137
+ end
138
+