Skip to content

Commit 0cc6b53

Browse files
author
HD Moore
committed
Land rapid7#5905, support newer OpenVAS formats.
2 parents 98da192 + fba751a commit 0cc6b53

File tree

1 file changed

+70
-73
lines changed

1 file changed

+70
-73
lines changed

lib/rex/parser/openvas_nokogiri.rb

Lines changed: 70 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ def start_element(name=nil,attrs=[])
1515
attrs = normalize_attrs(attrs)
1616
block = @block
1717
@state[:current_tag][name] = true
18+
19+
unless @text.nil?
20+
@state[:text_backup] = @text
21+
@text = nil
22+
end
23+
1824
case name
1925
when "host"
2026
@state[:has_text] = true
@@ -25,97 +31,90 @@ def start_element(name=nil,attrs=[])
2531
def end_element(name=nil)
2632
block = @block
2733
case name
28-
when "name"
29-
return if not in_tag("result")
30-
@state[:has_text] = true
31-
@state[:vuln_name] = @text.strip if @text
32-
@text = nil
33-
when "description"
34+
when 'name'
35+
if in_tag('result')
36+
@state[:has_text] = true
37+
@state[:vuln_name] = @text.strip if @text
38+
end
39+
when 'description'
3440
@state[:has_text] = true
3541
@state[:vuln_desc] = @text.strip if @text
36-
@text = nil
37-
when "bid"
38-
return if not in_tag("result")
39-
return if not in_tag("nvt")
40-
@state[:has_text] = true
41-
@state[:bid] = @text.strip if @text
42-
@text = nil
43-
when "cve"
44-
return if not in_tag("result")
45-
return if not in_tag("nvt")
46-
@state[:has_text] = true
47-
@state[:cves] = @text.strip if @text
48-
@text = nil
49-
when "risk_factor"
50-
return if not in_tag("result")
51-
return if not in_tag("nvt")
52-
53-
#we do this to clean out the buffer so to speak
54-
#if we don't set text to nil now, the text will show up later
55-
@state[:has_text] = true
56-
@text = nil
57-
when "cvss_base"
58-
return if not in_tag("result")
59-
return if not in_tag("nvt")
60-
@state[:has_text] = true
61-
@text = nil
62-
when "subnet"
63-
@state[:has_text] = true
64-
@text = nil
65-
when "result"
66-
return if not in_tag("results")
67-
record_vuln
68-
when "threat"
69-
return if not in_tag("ports")
70-
return if not in_tag("port")
71-
@state[:has_text] = true
72-
73-
if not @text.index('(')
74-
@state[:name] = nil
75-
@state[:port] = nil
76-
@state[:proto] = nil
77-
@text = nil
78-
return
42+
when 'bid'
43+
if in_tag('result') && in_tag('nvt')
44+
@state[:has_text] = true
45+
@state[:bid] = @text.strip if @text
7946
end
80-
81-
@state[:name] = @text.split(' ')[0] if @text
82-
@state[:port] = @text.split('(')[1].split('/')[0] if @text
83-
@state[:proto] = @text.split('(')[1].split('/')[1].split(')')[0] if @text
84-
85-
@text = nil
86-
when "host"
47+
when 'cve'
48+
if in_tag('result') && in_tag('nvt')
49+
@state[:has_text] = true
50+
@state[:cves] = @text.strip if @text
51+
end
52+
when 'risk_factor'
53+
if in_tag('result') && in_tag('nvt')
54+
#we do this to clean out the buffer so to speak
55+
#if we don't set text to nil now, the text will show up later
56+
@state[:has_text] = true
57+
end
58+
when 'cvss_base'
59+
if in_tag('result') && in_tag('nvt')
60+
@state[:has_text] = true
61+
end
62+
when 'subnet'
63+
@state[:has_text] = true
64+
when 'result'
65+
record_vuln if in_tag('results')
66+
when 'threat'
67+
@state[:has_text] = true if in_tag('ports') && in_tag('port')
68+
when 'host'
8769
if in_tag('result')
8870
@state[:has_text] = true
8971
@state[:host] = @text.strip if @text
90-
@text = nil
91-
elsif in_tag('ports')
92-
return if not in_tag('port')
72+
elsif in_tag('ports') && in_tag('port')
9373
@state[:has_text] = true
9474
@state[:host] = @text.strip if @text
95-
@text = nil
9675
end
97-
when "port"
76+
when 'port'
9877
if in_tag('result')
9978
@state[:has_text] = true
100-
if not @text.index('(')
79+
if @text && @text.index('(')
80+
@state[:proto] = @text.split('(')[1].split('/')[1].gsub(/\)/, '')
81+
@state[:port] = @text.split('(')[1].split('/')[0].gsub(/\)/, '')
82+
elsif @text && @text.index('/')
83+
@state[:proto] = @text.split('/')[1].strip
84+
@state[:port] = @text.split('/')[0].strip
85+
else
86+
@state[:proto] = nil
87+
@state[:port] = nil
88+
end
89+
90+
if @state[:port] && @state[:port] == 'general'
10191
@state[:proto] = nil
10292
@state[:port] = nil
103-
@text = nil
104-
return
10593
end
106-
@state[:proto] = @text.split('(')[0].strip if @text
107-
@state[:port] = @text.split('(')[1].split('/')[0].gsub(/\)/, '') if @text
108-
@text = nil
10994
elsif in_tag('ports')
110-
record_service
95+
if @text && @text.index('(')
96+
@state[:name] = @text.split(' ')[0]
97+
@state[:port] = @text.split('(')[1].split('/')[0]
98+
@state[:proto] = @text.split('(')[1].split('/')[1].split(')')[0]
99+
record_service unless @state[:name].nil?
100+
elsif @text && @text.index('/')
101+
@state[:port] = @text.split('/')[0]
102+
@state[:proto] = @text.split('/')[1]
103+
record_service unless @state[:port] == 'general'
104+
end
111105
end
112-
when "name"
113-
return if not in_tag("result")
106+
when 'name'
107+
return if not in_tag('result')
114108
@state[:has_text] = true
115-
@text = nil
109+
end
110+
111+
if @state[:text_backup]
112+
@text = @state[:text_backup]
113+
@state[:text_backup] = nil
116114
else
117115
@text = nil
118116
end
117+
119118
@state[:current_tag].delete name
120119
end
121120

@@ -153,8 +152,6 @@ def record_vuln
153152
end
154153

155154
def record_service
156-
return if not @state[:name]
157-
158155
service_info = {}
159156
service_info[:host] = @state[:host]
160157
service_info[:name] = @state[:name]

0 commit comments

Comments
 (0)