Class: Search

Inherits:
Object
  • Object
show all
Defined in:
lib/search.rb

Overview

© 2024 Gerald Hilts License: MIT (github.com/gwhilts/cve_lookup/blob/main/LICENSE)

Instance Method Summary collapse

Constructor Details

#initialize(product, version, test_mode = false) ⇒ Search

Returns a new instance of Search.



8
9
10
11
12
13
14
# File 'lib/search.rb', line 8

def initialize(product, version, test_mode = false)
  @cpe_name = ""
  @cpe_title = ""
  @product = product
  @version = version
  @io = IOHelper.new(test_mode)
end

Instance Method Details

#present_cve_listObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/search.rb', line 58

def present_cve_list
  cve_list = NVDHelper.cve_list_for(@cpe_name)
  
  puts "\n--\nSearching Nist National Vulnerability Database for #{@cpe_title}\n\n"
  case cve_list.count
  when 0
    puts "Unable to located any CVEs for #{@product} v#{@version}."
  when 1
    puts "The following CVE is associated with #{@product} v#{@version}:\n\n"
    puts cve_list.map { |cve| "https://nvd.nist.gov/vuln/detail/#{cve}" }
  else
    puts "The following CVEs are associated with #{@product} v#{@version}:\n\n"
    puts cve_list.map { |cve| "https://nvd.nist.gov/vuln/detail/#{cve}" }
  end
end

#runObject



16
17
18
19
20
21
# File 'lib/search.rb', line 16

def run()
  set_product_name
  set_version
  select_cpe
  present_cve_list
end

#select_cpeObject



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
# File 'lib/search.rb', line 31

def select_cpe
  puts "Searching CPE Dictionary for : #{@product} v#{@version} ...\n--\n"
  
  cpe_list = NVDHelper.cpe_list_for(@product, @version)
  
  if cpe_list.count > 0
    puts "Please select a product:\n\n"
    cpe_list.each_with_index do |cve, i|
      puts "#{i}: #{cve[:title]} (#{cve[:cpe_name]}"
    end
    puts "X: eXit and try again.\n\n"
  else
    puts "Unable to locate any entries for #{@product} v#{@version} in the CPE Dictionary."
    puts 'Please try again.'
    exit 0
  end

  case index = @io.request_from_range(0..(cpe_list.count - 1))
  when "X"
    exit 0
  else
    @cpe_title = cpe_list[index.to_i][:title]
    @cpe_name = cpe_list[index.to_i][:cpe_name]
  end
  
end

#set_product_nameObject



23
24
25
# File 'lib/search.rb', line 23

def set_product_name
   @product = @product || @io.request('Product name:')
end

#set_versionObject



27
28
29
# File 'lib/search.rb', line 27

def set_version
  @version = @version || @io.request('Version number:')
end