Module: Ronin::Masscan::Converter

Defined in:
lib/ronin/masscan/converter.rb

Overview

Handles converting masscan scan file into other formats.

Supports the following formats:

  • JSON
  • CSV

Constant Summary collapse

FILE_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.

Mapping of file extension names to formats.

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

Class Method Summary collapse

Class Method Details

.convert(masscan_file, output = nil, format:) ⇒ IO, String

Converts parsed masscan scan file into the desired format.

Parameters:

  • masscan_file (::Masscan::OutputFile)

    The masscan scan file to convert.

  • output (IO, nil) (defaults to: nil)

    Optional output to write the converted output to.

  • format (:json, :csv)

    The desired convert to convert the parsed masscan scan file to.

Returns:

  • (IO, String)

    The converted masscan scan file.



98
99
100
101
102
103
104
105
106
# File 'lib/ronin/masscan/converter.rb', line 98

def self.convert(masscan_file,output=nil, format: )
  if output
    Converters[format].convert(masscan_file,output)
  else
    output = StringIO.new
    convert(masscan_file,output, format: format)
    output.string
  end
end

.convert_file(src, dest, input_format: nil, format: infer_format_for(dest)) ⇒ Object

Converts an masscan scan file into another format.

Parameters:

  • src (String)

    The input masscan scan file path.

  • dest (String)

    The output file path.

  • input_format (:binary, :list, :json, :ndjson, nil) (defaults to: nil)

    The explicit format of the input masscan scan file. If not specified the input format will be inferred from the file's extension.

  • format (:json, :csv) (defaults to: infer_format_for(dest))

    The format to convert the masscan scan file into. If not specified it will be inferred from the output file's extension.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ronin/masscan/converter.rb', line 66

def self.convert_file(src,dest, input_format: nil,
                                format:       infer_format_for(dest))
  scan_file = if input_format
                ::Masscan::OutputFile.new(src, format: input_format)
              else
                ::Masscan::OutputFile.new(src)
              end

  converter = Converters[format]

  File.open(dest,'w') do |output|
    converter.convert(scan_file,output)
  end
end

.infer_format_for(path) ⇒ :json, :csv

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 output file's extension.

Parameters:

  • path (String)

    The output file name.

Returns:

  • (:json, :csv)

    The conversion format.

Raises:

  • (ArgumentError)

    The format could not be inferred from the path's file extension.



122
123
124
125
126
# File 'lib/ronin/masscan/converter.rb', line 122

def self.infer_format_for(path)
  FILE_FORMATS.fetch(File.extname(path)) do
    raise(ArgumentError,"cannot infer output format from path: #{path.inspect}")
  end
end