Class: Ronin::Nmap::CLI::Commands::Dump Private

Inherits:
Ronin::Nmap::CLI::Command show all
Includes:
FilteringOptions
Defined in:
lib/ronin/nmap/cli/commands/dump.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.

Dumps the scanned ports from nmap XML file(s).

Usage

ronin-nmap dump [options] XML_FILE [...]

Options

    --print-ips                  Print all IP addresses
    --print-hosts                Print all hostnames
    --print-ip-ports             Print IP:PORT pairs. (Default)
    --print-host-ports           Print HOST:PORT pairs
    --print-uris                 Print URIs
    --ip IP                      Filters the targets by IP
    --ip-range CIDR              Filter the targets by IP range
    --domain DOMAIN              Filters the targets by domain
    --with-os OS                 Filters the targets by OS
    --with-ports {PORT | PORT1-PORT2},...
                                 Filter targets by port numbers
    --with-service SERVICE[,...] Filters targets by service
    --with-script SCRIPT[,...]   Filters targets with the script
    --with-script-output STRING  Filters targets containing the script output
    --with-script-regex /REGEX/  Filters targets containing the script output
    -p, --ports {PORT | PORT1-PORT2},...
                                 Filter targets by port numbers
    --services SERVICE[,...]     Filters targets by service
-h, --help                       Print help information

Arguments

XML_FILE ...                     The nmap XML file(s) to parse

Examples

ronin-nmap dump --print-ip-ports scan.xml
ronin-nmap dump --print-ip-ports --ports 22,80,443 scan.xml
ronin-nmap dump --print-host-ports scan.xml
ronin-nmap dump --print-hosts --with-port 22 scan.xml
ronin-nmap dump --print-uris scan.xml

Instance Attribute Summary

Attributes included from FilteringOptions

#with_domains, #with_ip_ranges, #with_ips, #with_oses, #with_ports, #with_script_output, #with_scripts, #with_services

Instance Method Summary collapse

Methods included from FilteringOptions

#filter_targets, #filter_targets_by_domain, #filter_targets_by_ip, #filter_targets_by_ip_range, #filter_targets_by_os, #filter_targets_by_port, #filter_targets_by_script, #filter_targets_by_script_output, #filter_targets_by_service, included

Constructor Details

#initialize(**kwargs) ⇒ Dump

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.

Initializes the command.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keywords for the command.



137
138
139
140
141
142
143
144
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 137

def initialize(**kwargs)
  super(**kwargs)

  @mode = :ip_ports

  @ports    = Set.new
  @services = Set.new
end

Instance Method Details

#filter_ports(host) ⇒ Enumerator::Lazy

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.

Parameters:

  • host (::Nmap::XML::Host)

Returns:

  • (Enumerator::Lazy)


249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 249

def filter_ports(host)
  ports = host.each_open_port.lazy

  unless @ports.empty?
    ports = filter_ports_by_number(ports)
  end

  unless @services.empty?
    ports = filter_ports_by_service(ports)
  end

  return ports
end

#filter_ports_by_number(ports) ⇒ Enumerator::Lazy

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.

Parameters:

  • ports (Enumerator::Lazy)

Returns:

  • (Enumerator::Lazy)


268
269
270
271
272
273
274
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 268

def filter_ports_by_number(ports)
  ports.filter do |port|
    @ports.any? do |port_list|
      port_list.include?(port.number)
    end
  end
end

#filter_ports_by_service(ports) ⇒ Enumerator::Lazy

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.

Parameters:

  • ports (Enumerator::Lazy)

Returns:

  • (Enumerator::Lazy)


281
282
283
284
285
286
287
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 281

def filter_ports_by_service(ports)
  ports.filter do |port|
    if (service = port.service)
      @services.include?(service.name)
    end
  end
end

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.

Prints the HOST:PORT pair for the target.

Parameters:

  • host (::Nmap::XML::Host)


213
214
215
216
217
218
219
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 213

def print_host_ports(host)
  filter_ports(host).each do |port|
    if (hostname = host.hostname)
      puts "#{hostname}:#{port.number}"
    end
  end
end

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.

Prints the host names for the target.

Parameters:

  • host (::Nmap::XML::Host)


191
192
193
194
195
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 191

def print_hostnames(host)
  if (hostname = host.hostname)
    puts hostname
  end
end

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.

Prints the IPs for the target.

Parameters:

  • host (::Nmap::XML::Host)


182
183
184
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 182

def print_ip(host)
  puts host.address
end

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.

Prints the IP:PORT pair for the target.

Parameters:

  • host (::Nmap::XML::Host)


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

def print_ip_ports(host)
  filter_ports(host).each do |port|
    puts "#{host.address}:#{port.number}"
  end
end

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.

Prints the targets.

Parameters:

  • host (::Nmap::XML::Host)


167
168
169
170
171
172
173
174
175
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 167

def print_target(host)
  case @mode
  when :ips        then print_ip(host)
  when :hostnames  then print_hostname(host)
  when :ip_ports   then print_ip_ports(host)
  when :host_ports then print_host_ports(host)
  when :uris       then print_uris(host)
  end
end

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.

Prints the URIs for the target.

Parameters:

  • host (::Nmap::XML::Host)


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 226

def print_uris(host)
  filter_ports(host).each do |port|
    if (port.service && port.service.name == 'http') ||
       (port.number == 80)
      puts URI::HTTP.build(
        host: host.to_s,
        port: port.number
      )
    elsif (port.service && port.service.name == 'https') ||
          (port.number == 443)
      puts URI::HTTPS.build(
        host: host.to_s,
        port: port.number
      )
    end
  end
end

#run(*xml_files) ⇒ 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 dump command.

Parameters:

  • xml_files (Array<String>)

    The nmap XML files to parse.



152
153
154
155
156
157
158
159
160
# File 'lib/ronin/nmap/cli/commands/dump.rb', line 152

def run(*xml_files)
  xml_files.each do |xml_file|
    xml = ::Nmap::XML.open(xml_file)

    filter_targets(xml).each do |host|
      print_target(host)
    end
  end
end