Class: Ronin::Nmap::CLI::Commands::Scan Private
- Inherits:
-
Ronin::Nmap::CLI::Command
- Object
- Core::CLI::Command
- Ronin::Nmap::CLI::Command
- Ronin::Nmap::CLI::Commands::Scan
- 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 requiresudo
. - 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
-
#import_scan(path) ⇒ Object
private
Imports a nmap XML scan file.
-
#infer_output_format(path) ⇒ :xml, ...
private
Infers the output format from the given path's file extension.
-
#run(*nmap_args) ⇒ Object
private
Runs the
ronin-nmap scan
command. -
#run_nmap(*nmap_args, output:) ⇒ Boolean?
private
Runs the
nmap
command. -
#save_output(path, output, format:) ⇒ Object
private
Saves the nmap scan results to an output file in the given format.
Methods included from Importable
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.
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.
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.
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 = [:output]) output_format = .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 [: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.
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 = .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.
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 |