Class: RubyNessus::Version2::Host

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby-nessus/version2/host.rb

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Host

Creates A New Host Object

Host.new(object)

Parameters:

  • Host (Object)

    Object



16
17
18
# File 'lib/ruby-nessus/version2/host.rb', line 16

def initialize(host)
  @host = host
end

Instance Method Details

#critical_severity_countInteger

Return the Critical severity count.

Examples:

scan.critical_severity_count #=> 10

Returns:

  • (Integer)

    The Critical Severity Count



379
380
381
# File 'lib/ruby-nessus/version2/host.rb', line 379

def critical_severity_count
  host_stats[:critical].to_i
end

#critical_severity_eventsEvent

Returns All critical severity Event Objects For A Given Host.

Examples:

host.critical_severity_events.each do |critical|
  puts critical.port
  puts critical.data if info.data
end

Returns:

  • (Event)

    Return The critical For A Given Host.



254
255
256
257
258
259
260
# File 'lib/ruby-nessus/version2/host.rb', line 254

def critical_severity_events
  return if @critical_events
  @critical_events = @host.xpath('ReportItem').select { |event| (event['severity'].to_i == 4) }.map do |event|
    Event.new(event)
  end
  @critical_events
end

#each_event {|prog| ... } ⇒ Object

Creates a new Event object to be parser

Examples:

host.each_event do |event|
  puts event.name if event.name
  puts event.port
end

Yields:

  • (prog)

    If a block is given, it will be passed the newly created Event object.

Yield Parameters:

  • prog (EVENT)

    The newly created Event object.



288
289
290
# File 'lib/ruby-nessus/version2/host.rb', line 288

def each_event(&block)
  events.each(&block)
end

#event_countInteger

Return the total event count for a given host.

Examples:

host.event_count #=> 3456

Returns:

  • (Integer)

    Return the total event count for a given host.



271
272
273
# File 'lib/ruby-nessus/version2/host.rb', line 271

def event_count
  low_severity_events.count + medium_severity_events.count + high_severity_events.count + critical_severity_events.count
end

#event_percentage_for(type, round_percentage = false) ⇒ Integer

Return the Total severity count.

Examples:

scan.event_percentage_for("low", true) #=> 11%

Parameters:

  • severity (String)

    the severity in which to calculate percentage for.

  • round (true, false)

    round the result to the nearest whole number.

Returns:

  • (Integer)

    The Percentage Of Events For A Passed Severity

Raises:

  • (ExceptionClass)

    One of the following severity options must be passed. [high, medium, low, informational, all]



454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/ruby-nessus/version2/host.rb', line 454

def event_percentage_for(type, round_percentage = false)
  @sc ||= host_stats
  if %w[high medium low tcp udp icmp all].include?(type)
    calc = ((@sc[:"#{type}"].to_f / @sc[:all].to_f) * 100)
    if round_percentage
      return calc.round.to_s
    else
      return calc.to_s
    end
  else
    raise "Error: #{type} is not an acceptable severity. Possible options include: all, tdp, udp, icmp, high, medium and low."
  end
end

#eventsArray<String>

Parses the events of the host.

Returns:

  • (Array<String>)

    The events of the host.



298
299
300
301
302
# File 'lib/ruby-nessus/version2/host.rb', line 298

def events
  @host.xpath('ReportItem').map do |event|
    Event.new(event)
  end
end

#high_severity_countInteger

Return the High severity count.

Examples:

scan.high_severity_count #=> 10

Returns:

  • (Integer)

    The High Severity Count



392
393
394
# File 'lib/ruby-nessus/version2/host.rb', line 392

def high_severity_count
  host_stats[:high].to_i
end

#high_severity_eventsEvent

Returns All high severity Event Objects For A Given Host.

Examples:

host.high_severity_events.each do |high|
  puts high.port
  puts high.data if high.data
end

Returns:

  • (Event)

    Return The high severity Event For A Given Host.



233
234
235
236
237
238
239
# File 'lib/ruby-nessus/version2/host.rb', line 233

def high_severity_events
  return if @high_severity_events
  @high_severity_events = @host.xpath('ReportItem').select { |event| (event['severity'].to_i == 3) }.map do |event|
    Event.new(event)
  end
  @high_severity_events
end

#hostnameString Also known as: fqdn, dns_name

Return the Host Object hostname.

Examples:

host.hostname #=> "example.com"

Returns:

  • (String)

    The Host Object Hostname



37
38
39
# File 'lib/ruby-nessus/version2/host.rb', line 37

def hostname
  @host.at('tag[name=host-fqdn]')&.inner_text
end

#icmp_countInteger

Return the ICMP Event Count.

Examples:

scan.icmp_count #=> 3

Returns:

  • (Integer)

    The ICMP Event Count



353
354
355
# File 'lib/ruby-nessus/version2/host.rb', line 353

def icmp_count
  host_stats[:icmp].to_i
end

#informational_severity_countInteger

Return the informational severity count.

Examples:

scan.informational_severity_count #=> 1203

Returns:

  • (Integer)

    The Informational Severity Count



366
367
368
# File 'lib/ruby-nessus/version2/host.rb', line 366

def informational_severity_count
  host_stats[:informational].to_i
end

#informational_severity_eventsEvent

Returns All Informational Event Objects For A Given Host.

Examples:

host.informational_severity_events.each do |info|
  puts info.port
  puts info.data if info.data
end

Returns:

  • (Event)

    Return The Informational Event For A Given Host.



170
171
172
173
174
175
176
# File 'lib/ruby-nessus/version2/host.rb', line 170

def informational_severity_events
  return if @informational_events
  @informational_events = @host.xpath('ReportItem').select { |event| event['severity'].to_i.zero? }.map do |event|
    Event.new(event)
  end
  @informational_events
end

#ipString

Return the Host Object IP.

Examples:

host.ip #=> "127.0.0.1"

Returns:

  • (String)

    The Host Object IP



52
53
54
# File 'lib/ruby-nessus/version2/host.rb', line 52

def ip
  @host.at('tag[name=host-ip]')&.inner_text
end

#low_severity_countInteger

Return the Low severity count.

Examples:

scan.low_severity_count #=> 114

Returns:

  • (Integer)

    The Low Severity Count



418
419
420
# File 'lib/ruby-nessus/version2/host.rb', line 418

def low_severity_count
  host_stats[:low].to_i
end

#low_severity_eventsEvent

Returns All low_severity Event Objects For A Given Host.

Examples:

host.low_severity_events.each do |low|
  puts low.port
  puts low.data if low.data
end

Returns:

  • (Event)

    Return The low_severity Event For A Given Host.



191
192
193
194
195
196
197
# File 'lib/ruby-nessus/version2/host.rb', line 191

def low_severity_events
  return if @low_severity_events
  @low_severity_events = @host.xpath('ReportItem').select { |event| (event['severity'].to_i == 1) }.map do |event|
    Event.new(event)
  end
  @low_severity_events
end

#mac_addrString Also known as: mac_address

Return the Host Mac Address.

Examples:

host.mac_addr #=> "00:11:22:33:44:55"

Returns:

  • (String)

    Return the Host Mac Address



124
125
126
# File 'lib/ruby-nessus/version2/host.rb', line 124

def mac_addr
  @host.at('tag[name=mac-address]')&.inner_text
end

#medium_severity_countInteger

Return the Medium severity count.

Examples:

scan.medium_severity_count #=> 234

Returns:

  • (Integer)

    The Medium Severity Count



405
406
407
# File 'lib/ruby-nessus/version2/host.rb', line 405

def medium_severity_count
  host_stats[:medium].to_i
end

#medium_severity_eventsEvent

Returns All medium severity Event Objects For A Given Host.

Examples:

host.medium_severity_events.each do |medium|
  puts medium.port
  puts medium.data if medium.data
end

Returns:

  • (Event)

    Return The medium severity Event For A Given Host.



212
213
214
215
216
217
218
# File 'lib/ruby-nessus/version2/host.rb', line 212

def medium_severity_events
  return if @medium_severity_events
  @medium_severity_events = @host.xpath('ReportItem').select { |event| (event['severity'].to_i == 2) }.map do |event|
    Event.new(event)
  end
  @medium_severity_events
end

#nameObject



24
25
26
# File 'lib/ruby-nessus/version2/host.rb', line 24

def name
  @host["name"]
end

#netbios_nameString?

Return the Host Netbios Name.

Examples:

host.netbios_name #=> "SOMENAME4243"

Returns:

  • (String, nil)

    The Host Netbios Name



111
112
113
# File 'lib/ruby-nessus/version2/host.rb', line 111

def netbios_name
  @host.at('tag[name=netbios-name]')&.inner_text
end

#open_portsInteger

Return the open ports for a given host object.

Examples:

host.open_ports #=> 213

Returns:

  • (Integer)

    Return the open ports for a given host object.



153
154
155
# File 'lib/ruby-nessus/version2/host.rb', line 153

def open_ports
  @scanned_ports ||= host_stats[:open_ports].to_i
end

#os_nameString Also known as: os, operating_system

Return the Host OS Name.

Examples:

host.dns_name #=> "Microsoft Windows 2000, Microsoft Windows Server 2003"

Returns:

  • (String)

    Return the Host OS Name



138
139
140
# File 'lib/ruby-nessus/version2/host.rb', line 138

def os_name
  @host.at('tag[name=operating-system]')&.inner_text
end

#portsArray

Return an Array of open ports.

Examples:

scan.ports #=> ['22', '80', '443']

Returns:

  • (Array)

    The open ports



313
314
315
316
# File 'lib/ruby-nessus/version2/host.rb', line 313

def ports
  return if @ports
  @ports = @host.xpath('ReportItem').map { |port| port['port'] }.uniq.sort
end

#runtimeString Also known as: scan_runtime, get_runtime

Return the host run time.

Examples:

scan.scan_run_time #=> '2 hours 5 minutes and 16 seconds'

Returns:

  • (String)

    The Host Scan Run Time



95
96
97
98
# File 'lib/ruby-nessus/version2/host.rb', line 95

def runtime
  return unless stop_time && start_time
  Time.at(stop_time - start_time).utc.strftime('%H hours %M minutes and %S seconds')
end

#start_timeTime

Return the host scan start time.

Examples:

scan.scan_start_time #=> 'Fri Nov 11 23:36:54 1985'

Returns:

  • (Time)

    The Host Scan Start Time



65
66
67
68
69
# File 'lib/ruby-nessus/version2/host.rb', line 65

def start_time
  if (start_time = @host.at('tag[name=HOST_START]'))
    Time.parse(start_time.inner_text + ' UTC')
  end
end

#stop_timeTime

Return the host scan stop time.

Examples:

scan.scan_start_time #=> 'Fri Nov 11 23:36:54 1985'

Returns:

  • (Time)

    The Host Scan Stop Time



80
81
82
83
84
# File 'lib/ruby-nessus/version2/host.rb', line 80

def stop_time
  if (stop_time = @host.at('tag[name=HOST_END]'))
    Time.parse(stop_time.inner_text + ' UTC')
  end
end

#tcp_countInteger

Return the TCP Event Count.

Examples:

scan.tcp_count #=> 3

Returns:

  • (Integer)

    The TCP Event Count



327
328
329
# File 'lib/ruby-nessus/version2/host.rb', line 327

def tcp_count
  host_stats[:tcp].to_i
end

#to_sObject



20
21
22
# File 'lib/ruby-nessus/version2/host.rb', line 20

def to_s
  ip.to_s
end

#total_event_count(count_informational = nil) ⇒ Integer

Return the Total severity count. [high, medium, low, informational]

Examples:

scan.total_event_count #=> 1561

Returns:

  • (Integer)

    The Total Severity Count



431
432
433
434
435
436
437
# File 'lib/ruby-nessus/version2/host.rb', line 431

def total_event_count(count_informational = nil)
  if count_informational
    host_stats[:all].to_i + informational_severity_count
  else
    host_stats[:all].to_i
  end
end

#udp_countInteger

Return the UDP Event Count.

Examples:

scan.udp_count #=> 3

Returns:

  • (Integer)

    The UDP Event Count



340
341
342
# File 'lib/ruby-nessus/version2/host.rb', line 340

def udp_count
  host_stats[:udp].to_i
end