Class: CMSScanner::Scan

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

Overview

Scan

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Scan

Returns a new instance of Scan.

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
13
14
15
16
# File 'lib/cms_scanner/scan.rb', line 8

def initialize
  NS.start_memory = GetProcessMem.new.bytes

  controllers << NS::Controller::Core.new

  exit_hook

  yield self if block_given?
end

Instance Attribute Details

#run_errorObject (readonly)

Returns the value of attribute run_error.



6
7
8
# File 'lib/cms_scanner/scan.rb', line 6

def run_error
  @run_error
end

Instance Method Details

#controllersControllers

Returns:



19
20
21
# File 'lib/cms_scanner/scan.rb', line 19

def controllers
  @controllers ||= NS::Controllers.new
end

#datastoreHash

Returns:

  • (Hash)


48
49
50
# File 'lib/cms_scanner/scan.rb', line 48

def datastore
  controllers.first.datastore
end

#exit_hookObject

Hook to be able to have an exit code returned depending on the findings / errors :nocov:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cms_scanner/scan.rb', line 55

def exit_hook
  # Avoid hooking the exit when rspec is running, otherwise it will always return 0
  # and Travis won't detect failed builds. Couldn't find a better way, even though
  # some people managed to https://github.com/rspec/rspec-core/pull/410
  return if defined?(RSpec)

  at_exit do
    exit(run_error_exit_code) if run_error

    # The parsed_option[:url] must be checked to avoid raising erros when only -h/-v are given
    exit(NS::ExitCode::VULNERABLE) if NS::ParsedCli.url && controllers.first.target.vulnerable?
    exit(NS::ExitCode::OK)
  end
end

#formatterObject

Used for convenience



43
44
45
# File 'lib/cms_scanner/scan.rb', line 43

def formatter
  controllers.first.formatter
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cms_scanner/scan.rb', line 23

def run
  controllers.run
rescue OptParseValidator::NoRequiredOption => e
  @run_error = e

  formatter.output('@usage', msg: e.message)
rescue NoMemoryError, ScriptError, SecurityError, SignalException, StandardError, SystemStackError => e
  @run_error = e

  formatter.output('@scan_aborted',
                   reason: e.is_a?(Interrupt) ? 'Canceled by User' : e.message,
                   trace: e.backtrace,
                   verbose: NS::ParsedCli.verbose ||
                            run_error_exit_code == NS::ExitCode::EXCEPTION)
ensure
  formatter.beautify
end

#run_error_exit_codeInteger

Returns The exit code related to the run_error.

Returns:

  • (Integer)

    The exit code related to the run_error



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cms_scanner/scan.rb', line 72

def run_error_exit_code
  return NS::ExitCode::CLI_OPTION_ERROR if run_error.is_a?(OptParseValidator::Error) ||
                                           run_error.is_a?(OptionParser::ParseError)

  return NS::ExitCode::INTERRUPTED if run_error.is_a?(Interrupt)

  return NS::ExitCode::ERROR if run_error.is_a?(NS::Error::Standard) ||
                                run_error.is_a?(CMSScanner::Error::Standard)

  NS::ExitCode::EXCEPTION
end