Class: Rhessus::ScanRunner

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

Overview

Encapulates scan targetting and running

Instance Method Summary collapse

Methods inherited from Scan

#checkin, checkout, #failure, #should_fire=, #should_fire?

Instance Method Details

#fire!Object

waits until it can safely get a fork, then initiates a new scan at its target The forked process attempts to notify the central authority upon being killed that the scan was a failure



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/scan_runner.rb', line 9

def fire!
  # safely get a fork and fire the scan
  return nil unless should_fire?
  tryagain = true
  pid = nil
  @path = lock(self.targets)
  while tryagain
    begin
      if pid = fork(&self)
        tryagain = false
      end
    rescue Errno::EWOULDBLOCK
      sleep 5
      tryagain = true
    end
  end
  Process.detach(pid)
end

#lock(targets) ⇒ Object

Creates a tempfile containing the scans



56
57
58
59
60
61
62
# File 'lib/scan_runner.rb', line 56

def lock(targets)
  @results = Tempfile.new("rhessus-scan-#{self.id}-results").path
  (Tempfile.new("rhessus-scan-#{self.id}-targets").tap do |x|
    x.puts targets
    x.flush
  end).path
end

#scan_stringObject

the second half of the scanner’s data (after host, login, password)



65
66
67
# File 'lib/scan_runner.rb', line 65

def scan_string
  " #{@path} #{@results}"
end

#to_procObject

The procedure that should be executed when the scan actually runs, including signal handling



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/scan_runner.rb', line 30

def to_proc
  if should_fire?
    Proc.new do
	  puts "Attempting to fire scan... #{scan_string}"
	  puts "#{::Rhessus.scanner}"
      begin
        Signal.trap("KILL") do 
          self.failure
          exit
        end
        Signal.trap("TERM") do 
          self.failure
          exit
        end
      checkin(system(::Rhessus.scanner + scan_string) && File.read(@results))
      rescue
        self.failure
        exit
      end
    end
  else
    Proc.new { nil }
  end
end