Module: Ronin::Nmap

Defined in:
lib/ronin/nmap.rb,
lib/ronin/nmap/cli.rb,
lib/ronin/nmap/root.rb,
lib/ronin/nmap/mixin.rb,
lib/ronin/nmap/version.rb,
lib/ronin/nmap/importer.rb,
lib/ronin/nmap/converter.rb,
lib/ronin/nmap/converters.rb,
lib/ronin/nmap/exceptions.rb,
lib/ronin/nmap/cli/command.rb,
lib/ronin/nmap/cli/port_list.rb,
lib/ronin/nmap/cli/importable.rb,
lib/ronin/nmap/converters/csv.rb,
lib/ronin/nmap/converters/json.rb,
lib/ronin/nmap/cli/commands/new.rb,
lib/ronin/nmap/cli/commands/dump.rb,
lib/ronin/nmap/cli/commands/grep.rb,
lib/ronin/nmap/cli/commands/scan.rb,
lib/ronin/nmap/cli/commands/print.rb,
lib/ronin/nmap/cli/commands/import.rb,
lib/ronin/nmap/cli/commands/convert.rb,
lib/ronin/nmap/cli/filtering_options.rb,
lib/ronin/nmap/cli/commands/completion.rb

Overview

Namespace for the ronin-nmap library.

Defined Under Namespace

Modules: Converter, Converters, Importer, Mixin Classes: CLI, Exception, NotInstalled, ScanFailed

Constant Summary collapse

CACHE_DIR =

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.

The ~/.cache/ronin-nmap cache directory.

Core::Home.cache_dir('ronin-nmap')
ROOT =

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.

Path to ronin-nmap root directory.

File.expand_path(File.join(__dir__,'..','..','..'))
VERSION =

ronin-nmap version

'0.1.0'

Class Method Summary collapse

Class Method Details

.parse(path) ⇒ ::Nmap::XML

Parses a nmap XML file.

Parameters:

  • path (String)

    The path to the nmap XML file.

Returns:

  • (::Nmap::XML)

    The parsed nmap XML file.

See Also:



219
220
221
# File 'lib/ronin/nmap.rb', line 219

def self.parse(path)
  ::Nmap::XML.open(path)
end

.scan(*targets, sudo: nil, **kwargs) {|nmap| ... } ⇒ ::Nmap::XML

Runs nmap and parses the XML output.

Examples:

xml = Nmap.scan('192.168.1.*', syn_scan: true, ports: [80, 443])
# => #<Nmap::XML: ...>
xml.up_hosts
# => [#<Nmap::XML::Host: 192.168.1.1>, ...]

with a block:

xml = Nmap.scan do |nmap|
  nmap.syn_scan = true
  nmap.ports    = [80, 443]
  nmap.targets  = '192.168.1.*'
end
# => #<Nmap::XML: ...>

Parameters:

  • targets (Array<#to_s>)

    The targets to scan.

  • sudo (Hash{Symbol => Object}, Boolean, nil) (defaults to: nil)

    Controls whether the nmap command should be ran under sudo. If the sudo: keyword argument is not given, then nmap will automatically be ran under sudo if sync_scan, ack_scan, window_scan, maimon_scan, null_scan, fin_scan, xmas_scan, scan_flags, os_fingerprint, or traceroute are enabled.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for nmap.

Options Hash (sudo:):

  • :askpass (Boolean)

    Enables the --askpass sudo option.

  • :background (Boolean)

    Enables the --background sudo option

  • :bell (Boolean)

    Enables the --bell sudo option

  • :close_from (Integer)

    Enables the --close-from=... sudo option

  • :chdir (String)

    Enables the --chdir=... sudo option

  • :preserve_env (String)

    Enables the --preseve-env=... sudo option

  • :group (String, Boolean)

    Enables the --preseve-env=... sudo option

  • :set_home (Boolean)

    Enables the --set-home sudo option

  • :host (String)

    Enables the --host=... sudo option

  • :login (Boolean)

    Enables the --login sudo option

  • :remove_timestamp (Boolean)

    Enables the --remove-timestamp sudo option

  • :reset_timestamp (Boolean)

    Enables the --reset-timestamp sudo option

  • :non_interactive (Boolean)

    Enables the --non-interactive sudo option

  • :preserve_groups (Boolean)

    Enables the --preserve-groups sudo option

  • :prompt (String)

    Enables the --prompt=... sudo option

  • :chroot (String)

    Enables the --chroot=... sudo option

  • :role (String)

    Enables the --role=... sudo option

  • :stdin (Boolean)

    Enables the --stdin sudo option

  • :shell (Boolean)

    Enables the --shell sudo option

  • :type (String)

    Enables the --type=... sudo option

  • :command_timeout (Integer)

    Enables the --command-timeout=... sudo option

  • :other_user (String)

    Enables the --other-user=... sudo option

  • :user (String)

    Enables the --user=... sudo option

Yields:

  • (nmap)

    If a block is given, it will be passed the new nmap command object for additional configuration.

Yield Parameters:

  • nmap (::Nmap::Command)

    The nmap command object.

Returns:

  • (::Nmap::XML)

    If the nmap command was successful, the parsed nmap XML data will be returned.

Raises:

See Also:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ronin/nmap.rb', line 161

def self.scan(*targets, sudo: nil, **kwargs,&block)
  nmap = ::Nmap::Command.new(**kwargs,&block)

  nmap.targets ||= targets

  unless nmap.output_xml
    FileUtils.mkdir_p(CACHE_DIR)
    tempfile = Tempfile.new(['nmap','.xml'], CACHE_DIR)

    nmap.output_xml = tempfile.path
  end

  sudo ||= nmap.syn_scan ||
           nmap.ack_scan ||
           nmap.window_scan ||
           nmap.maimon_scan ||
           nmap.null_scan ||
           nmap.fin_scan ||
           nmap.xmas_scan ||
           nmap.scan_flags ||
           nmap.ip_scan ||
           nmap.os_fingerprint ||
           nmap.traceroute

  # run the nmap command
  status = case sudo
           when Hash       then nmap.sudo_command(**sudo)
           when true       then nmap.sudo_command
           when false, nil then nmap.run_command
           else
             raise(ArgumentError,"sudo keyword must be a Hash, true, false, or nil")
           end

  # if the command was successful, return the parsed XML, otherwise raises
  # an exception.
  case status
  when nil
    raise(NotInstalled,"the nmap command is not installed")
  when false
    raise(ScanFailed,"nmap scan failed: #{nmap.command_argv.join(' ')}")
  else
    ::Nmap::XML.open(nmap.output_xml)
  end
end