-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuls_to_updateinfo.rb
329 lines (278 loc) · 8.48 KB
/
vuls_to_updateinfo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
require 'rexml/document'
require 'time'
########################################################################################################################
module IoHelper
module_function
def get_vuls_report_path
report_path = ARGV[0]
report_path ||= 'localhost.xml'
return report_path if FileTest.exist?(report_path)
puts 'vuls report XML file not found.'
exit
end
def get_installed_package_details
# get installed package arch and epoch
details = Hash.new
command_results = `repoquery --all --pkgnarrow=installed --qf="%{name}.%{arch}.%{epoch}"`
command_results.split(/\n/).each do |line|
detail = line.split('.')
pd = PackageDetail.new
pd.name = detail[0]
pd.arch = detail[1]
pd.epoch = detail[2]
details[pd.name] = pd
end
# update epoch by newest package
command_results = `repoquery --all --pkgnarrow=updates --qf="%{name}.%{epoch}"`
command_results.split(/\n/).each do |line|
new_detail = line.split('.')
details[new_detail[0]].set_new_epoch(new_detail[1])
end
details
end
def write_xml(doc, filePath)
File.open(filePath, 'w') do |file|
doc.write(file, indent=-1, transitive=true)
end
end
end
########################################################################################################################
class PackageDetail
attr_accessor :name, :arch, :epoch
def set_new_epoch(value)
if @epoch < value
@epoch = value
end
end
end
########################################################################################################################
class VulsReportAnalyzer
def initialize(doc)
@VulsReport = doc
end
def distribution
@VulsReport.root.get_text('ScanResult/Family').value
end
def version
@VulsReport.root.get_text('ScanResult/Release').value
end
def major
@VulsReport.root.get_text('ScanResult/Release').value.to_i
end
def create_update_package_list
hs = Hash.new
@VulsReport.elements.each('vulsreport/ScanResult/ScannedCves/Packages') do |package|
pi = PackageInfo.new(package)
next unless pi.installed_package?
hs[pi.full_name] = pi unless hs.has_key?(pi.full_name)
end
hs
end
def nvd_infos
if @nvd_infos.nil?
@nvd_infos = Array.new
@VulsReport.elements.each('vulsreport/ScanResult/KnownCves') do |known|
@nvd_infos << NvdInfo.new(known)
end
end
@nvd_infos
end
class NvdInfo
def initialize(elm_known)
@elm_known = elm_known
end
def summary
@elm_known.get_text('CveDetail/Nvd/Summary').value
end
def score
@elm_known.get_text('CveDetail/Nvd/Score').value.to_f
end
def published_date
Time.parse(@elm_known.get_text('CveDetail/Nvd/PublishedDate').value).localtime.strftime('%Y-%m-%d %H:%M:%S')
end
def references
if @references.nil?
@references = Array.new
@elm_known.elements.each('CveDetail/Nvd/References') do |ref|
@references << ReferrenceInfo.new(ref)
end
end
@references
end
class ReferrenceInfo
def initialize(ref)
@ref = ref
end
def source
@ref.get_text('Source').value
end
def link
@ref.get_text('Link').value
end
end
def packages
if @packages.nil?
@packages = Array.new
@elm_known.elements.each('Packages') do |package|
pi = PackageInfo.new(package)
next unless pi.installed_package?
@packages << pi
end
end
@packages
end
end
end
########################################################################################################################
class UpdateInfoBuilder
def initialize(package_infos, distribution, major)
@package_infos = package_infos
@distribution = distribution
@major = major
@update_nodes = create_update_nodes
@scores = create_package_scores(@package_infos.keys)
end
def set_nvd_info(nvd)
nvd.packages.each do |pkg|
if score_upper?(pkg.full_name, nvd.score)
get_node(pkg.full_name).get_elements('title')[0].text = get_title(nvd.score, pkg.name)
get_node(pkg.full_name).get_elements('issued')[0].add_attribute('date', nvd.published_date)
get_node(pkg.full_name).get_elements('severity')[0].text = get_severity(nvd.score)
get_node(pkg.full_name).get_elements('description')[0].text = nvd.summary
update_score(pkg.full_name, nvd.score)
end
nvd.references.each do |ref|
get_node(pkg.full_name).get_elements('references')[0].add_element(create_elm_reference(ref.link, ref.source))
end
end
end
def to_xml
root = create_elm_root
@update_nodes.keys.sort.each do |key|
root.add_element(@update_nodes[key])
end
root
end
private
def create_package_scores(package_keys)
hs = Hash.new
package_keys.each do |key|
hs[key] = 0.to_f
end
hs
end
def get_os_name
'centos'.eql?(@distribution) ? 'CentOS ' + @major.to_s : @distribution + @major.to_s
end
def create_update_nodes
hs = Hash.new
@package_infos.each_value do |pi|
elm_update = create_elm_update
elm_update.add_element('id').text = pi.full_name
elm_update.add_element('title').text = get_title(0, pi.name)
elm_update.add_element('release').text = get_os_name
elm_update.add_element('issued', {'date'=>''})
elm_update.add_element('severity').text = get_severity(0)
elm_update.add_element('description')
elm_update.add_element('references')
collection = elm_update.add_element('pkglist').add_element('collection', {'short'=>'EL-' + @major.to_s})
collection.add_element('name').text = get_os_name
collection.add_element(create_elm_package(pi))
hs[pi.full_name] = elm_update
end
hs
end
def create_elm_root
REXML::Element.new('updates')
end
def create_elm_update
upd = REXML::Element.new('update')
upd.add_attribute('from', 'you@your_domain.com')
upd.add_attribute('status', 'stable')
upd.add_attribute('type', 'security')
upd.add_attribute('version', '1.4')
upd
end
def create_elm_reference(url, type)
reference = REXML::Element.new('reference')
reference.add_attributes({'href' => url, 'type' => type})
reference
end
def create_elm_package(pi)
package = REXML::Element.new('package')
package.add_attributes({'arch'=>pi.arch, 'epoch'=>pi.epoch, 'name'=>pi.name, 'release'=>pi.release, 'src'=>'', 'version'=>pi.version})
package.add_element('filename').text = pi.file_name
package
end
def get_node(key)
@update_nodes[key]
end
def score_upper?(key, score)
@scores[key] < score
end
def update_score(key, score)
@scores[key] = score
end
def get_title(score, name)
get_severity(score) + ' ' + get_os_name + ' ' + name + ' Security Update'
end
def get_severity(value)
if 7.0 <= value
'Important'
elsif 4.0 <= value
'Moderate'
elsif 0.0 < value
'Low'
else
'Unknown'
end
end
end
########################################################################################################################
class PackageInfo
@@package_details = nil
def self.package_details=(value)
@@package_details = value
end
def initialize(elm_pkg)
@elm_pkg = elm_pkg
end
def name
@elm_pkg.get_text('Name').value
end
def version
@elm_pkg.get_text('NewVersion').value
end
def release
@elm_pkg.get_text('NewRelease').value
end
def arch
@@package_details[name].arch
end
def epoch
@@package_details[name].epoch
end
def full_name
name + '-' + version + '-' + release + '.' + arch
end
def file_name
full_name + '.rpm'
end
def installed_package?
@@package_details.has_key?(name)
end
end
########################################################################################################################
# init
PackageInfo.package_details = IoHelper.get_installed_package_details
analyzer = VulsReportAnalyzer.new(REXML::Document.new(open(IoHelper.get_vuls_report_path)))
builder = UpdateInfoBuilder.new(analyzer.create_update_package_list, analyzer.distribution, analyzer.major)
# set nvd information for update nodes
analyzer.nvd_infos.each do |nvd|
builder.set_nvd_info(nvd)
end
# write updateinfo to file
docUpdateInfo = REXML::Document.new
docUpdateInfo << REXML::XMLDecl.new('1.0', 'UTF-8')
docUpdateInfo.add_element(builder.to_xml)
IoHelper.write_xml(docUpdateInfo, 'updateinfo.xml')