Class: Ronin::Masscan::CLI::Commands::Scan Private

Inherits:
Ronin::Masscan::CLI::Command show all
Includes:
Core::CLI::Logging, Importable
Defined in:
lib/ronin/masscan/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 masscan and outputs data as JSON or CSV or imports into the database.

Usage

ronin-masscan scan [options] -- [masscan_options]

Options

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

Arguments

masscan_options ...              Additional arguments for masscan

Examples

ronin-masscan scan -o scan.json -- 192.168.1.1
ronin-masscan scan --import -- 192.168.1.1

Constant Summary collapse

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.

{
  '.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 masscan output file.

Parameters:

  • path (String)

    The path to the output file.



177
178
179
180
# File 'lib/ronin/masscan/cli/commands/scan.rb', line 177

def import_scan(path)
  db_connect
  import_file(path)
end

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

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:

  • (:json, :csv, nil)

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



198
199
200
# File 'lib/ronin/masscan/cli/commands/scan.rb', line 198

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

#run(*masscan_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-masscan scan command.

Parameters:

  • masscan_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/masscan/cli/commands/scan.rb', line 102

def run(*masscan_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-masscan', '.bin'])

  log_info "Running masscan #{masscan_args.join(' ')} ..."

  unless run_masscan(*masscan_args, output: tempfile.path)
    print_error "failed to run masscan"
    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 masscan output ..."
    import_scan(tempfile.path)
  end
end

#run_masscan(*masscan_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 masscan command.

Parameters:

  • masscan_args (Array<String>)

    Additional arguments for masscan.

  • output (String)

    The output file to save the scan data to.

Returns:

  • (Boolean, nil)

    Indicates whether the masscan command was successful.



146
147
148
149
150
151
# File 'lib/ronin/masscan/cli/commands/scan.rb', line 146

def run_masscan(*masscan_args, output: )
  masscan_command = ['masscan', '-v', *masscan_args, '-oB', output]
  masscan_command.unshift('sudo') if options[:sudo]

  return system(*masscan_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 masscan scan results to an output file in the given format.

Parameters:

  • path (String)

    The path to the masscan output file.

  • output (String)

    The path to the desired output file.

  • format (:json, :csv)

    The desired output format.



166
167
168
169
# File 'lib/ronin/masscan/cli/commands/scan.rb', line 166

def save_output(path,output, format: )
  # the format has been explicitly specified
  Converter.convert_file(path,output, format: format)
end