Class: WPScan::Controllers

Inherits:
Array
  • Object
show all
Defined in:
lib/wpscan/controllers.rb

Overview

Controllers container. Summary width is 45 (wpscan-specific; upstream default was 40).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_parser = OptParseValidator::OptParser.new(nil, 45)) ⇒ Controllers

Returns a new instance of Controllers.

Parameters:



9
10
11
12
13
14
15
# File 'lib/wpscan/controllers.rb', line 9

def initialize(option_parser = OptParseValidator::OptParser.new(nil, 45))
  @option_parser = option_parser

  register_config_files

  option_parser.config_files.result_key = 'cli_options'
end

Instance Attribute Details

#option_parserObject (readonly)

Returns the value of attribute option_parser.



6
7
8
# File 'lib/wpscan/controllers.rb', line 6

def option_parser
  @option_parser
end

#runningObject (readonly)

Returns the value of attribute running.



6
7
8
# File 'lib/wpscan/controllers.rb', line 6

def running
  @running
end

Instance Method Details

#<<(controller) ⇒ Controllers

Returns self.

Parameters:

Returns:



36
37
38
39
40
41
42
43
44
# File 'lib/wpscan/controllers.rb', line 36

def <<(controller)
  options = controller.cli_options

  unless include?(controller)
    option_parser.add(*options) if options
    super
  end
  self
end

#apply_no_colour_defaultObject

Force the non-colored CLI formatter when ANSI escapes would be unwanted: writing to a file, piping to another process, or when the caller has set NO_COLOR (see no-color.org). Explicit –format choices are preserved.



50
51
52
53
54
55
56
57
# File 'lib/wpscan/controllers.rb', line 50

def apply_no_colour_default
  return if WPScan::ParsedCli.options[:format]

  no_color = ENV.fetch('NO_COLOR', nil)
  return unless WPScan::ParsedCli.output || !$stdout.tty? || (no_color && !no_color.empty?)

  WPScan::ParsedCli.options[:format] = 'cli-no-colour'
end

#register_config_filesObject

Registers the potential option-file paths with the option_parser.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wpscan/controllers.rb', line 18

def register_config_files
  # XDG Base Directory support for configuration
  # https://specifications.freedesktop.org/basedir/latest/
  xdg = ENV.fetch('XDG_CONFIG_HOME', nil)
  xdg = Pathname.new(Dir.home).join('.config') if xdg.nil? || xdg.empty?
  app = WPScan.app_name

  dirs = [[xdg, app], [Dir.home, ".#{app}"], [Dir.pwd, ".#{app}"]]
  exts = option_parser.config_files.class.supported_extensions

  dirs.product(exts).each do |(dir, sub), ext|
    option_parser.config_files << Pathname.new(dir).join(sub, "scan.#{ext}").to_s
  end
end

#runObject



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
# File 'lib/wpscan/controllers.rb', line 59

def run
  WPScan::ParsedCli.options = option_parser.results
  first.class.option_parser = option_parser # needed to output help on -h/--hh

  apply_no_colour_default
  redirect_output_to_file(WPScan::ParsedCli.output) if WPScan::ParsedCli.output

  Timeout.timeout(WPScan::ParsedCli.max_scan_duration, WPScan::Error::MaxScanDurationReached) do
    each(&:before_scan)

    @running = true

    each(&:run)
  end
ensure
  # The rescue prevents unfinished requests from raising, which would stop reverse_each from running.
  # rubocop:disable Style/RescueModifier
  WPScan::Browser.instance.hydra.abort rescue nil
  # rubocop:enable Style/RescueModifier

  # Reverse order: app/controllers/core#after_scan finishes the output and must be last.
  # Guarantees stats are output even on error. after_scan runs only if scan was actually running
  # (skipped on CLI error, -h/--hh/--version).
  reverse_each(&:after_scan) if running
end