Class: Ronin::Nmap::CLI::Commands::Scan Private

Inherits:
Ronin::Nmap::CLI::Command show all
Includes:
Core::CLI::Logging, Importable
Defined in:
lib/ronin/nmap/cli/commands/scan.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Runs nmap and outputs data as JSON or CSV or imports into the database.

Usage

ronin-nmap scan [options] -- [nmap_options]

Options

    --db NAME                    The database to connect to (Default: default)
    --db-uri URI                 The database URI to connect to
    --sudo                       Runs the nmap command under sudo
-o, --output FILE                The output file
-F, --output-format xml|json|csv The output format
    --import                     Imports the scan results into the database
-h, --help                       Print help information

Arguments

nmap_options ...                 Additional arguments for nmap

Examples

ronin-nmap scan -o scan.json -- -sV 192.168.1.1
ronin-nmap scan --import -- -sV 192.168.1.1

Constant Summary collapse

SUDO_OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

nmap options that require sudo.

OUTPUT_FORMATS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Supported output formats.

{
  '.xml'  => :xml,
  '.json' => :json,
  '.csv'  => :csv
}

Instance Method Summary collapse

Methods included from Importable

#import_file, included

Instance Method Details

#import_scan(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Imports a nmap XML scan file.

Parameters:

  • path (String)

    The path to the XML file.



203
204
205
206
# File 'lib/ronin/nmap/cli/commands/scan.rb', line 203

def import_scan(path)
  db_connect
  import_file(path)
end

#infer_output_format(path) ⇒ :xml, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Infers the output format from the given path's file extension.

Parameters:

  • path (String)

    The path to infer the output format from.

Returns:

  • (:xml, :json, :csv, nil)

    The output format or nil if the path's file extension is unknown.



225
226
227
# File 'lib/ronin/nmap/cli/commands/scan.rb', line 225

def infer_output_format(path)
  OUTPUT_FORMATS[File.extname(path)]
end

#run(*nmap_args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the ronin-nmap scan command.

Parameters:

  • nmap_args (Array<String>)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ronin/nmap/cli/commands/scan.rb', line 102

def run(*nmap_args)
  if (output = options[:output])
    output_format = options.fetch(:output_format) do
                      infer_output_format(output)
                    end

    if output_format.nil?
      print_error "cannot infer the output format of the output file (#{output.inspect}), please specify --output-format"
      exit(1)
    end
  end

  tempfile = Tempfile.new(['ronin-nmap', '.xml'])

  log_info "Running nmap #{nmap_args.join(' ')} ..."

  unless run_nmap(*nmap_args, output: tempfile.path)
    print_error "failed to run nmap"
    exit(1)
  end

  if output
    log_info "Saving #{output_format.upcase} output to #{output} ..."
    save_output(tempfile.path,output, format: output_format)
  end

  if options[:import]
    log_info "Importing scan XML ..."
    import_scan(tempfile.path)
  end
end

#run_nmap(*nmap_args, output:) ⇒ Boolean?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the nmap command.

Parameters:

  • nmap_args (Array<String>)

    Additional arguments for nmap.

  • output (String)

    The XML output file to save the scan data to.

Returns:

  • (Boolean, nil)

    Indicates whether the nmap command was successful.



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ronin/nmap/cli/commands/scan.rb', line 161

def run_nmap(*nmap_args, output: )
  sudo = options.fetch(:sudo) do
    nmap_args.any? do |arg|
      SUDO_OPTIONS.include?(arg)
    end
  end

  nmap_command = ['nmap', '-v', *nmap_args, '-oX', output]
  nmap_command.unshift('sudo') if sudo

  return system(*nmap_command)
end

#save_output(path, output, format:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Saves the nmap scan results to an output file in the given format.

Parameters:

  • path (String)

    The path to the nmap XML file.

  • output (String)

    The path to the desired output file.

  • format (:xml, :json, :csv)

    The desired output format.



186
187
188
189
190
191
192
193
194
195
# File 'lib/ronin/nmap/cli/commands/scan.rb', line 186

def save_output(path,output, format: )
  case format
  when :xml
    # copy the file if the output format is xml
    FileUtils.cp(path,output)
  else
    # the format has been explicitly specified
    Nmap::Converter.convert_file(path,output, format: format)
  end
end