Class: Ronin::Nmap::CLI::Commands::Grep Private

Inherits:
Ronin::Nmap::CLI::Command show all
Includes:
CommandKit::Colors, CommandKit::Printing::Indent
Defined in:
lib/ronin/nmap/cli/commands/grep.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.

Parses and searches nmap XML file(s) for the pattern.

Usage

ronin-nmap grep [options] PATTERN XML_FILE [...]

Options

-h, --help                       Print help information

Arguments

Instance Method Summary collapse

Instance Method Details

#grep_xml(xml, pattern) ⇒ Enumerator::Lazy<::Nmap::XML::Host>

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.

Searches the parsed nmap XML for the text pattern.

Parameters:

  • xml (::Nmap::XML)

    The parsed nmap XML object.

  • pattern (String)

    The text pattern to search for.

Returns:

  • (Enumerator::Lazy<::Nmap::XML::Host>)

    The nmap XML host objects that contain the text pattern.



97
98
99
100
101
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 97

def grep_xml(xml,pattern)
  xml.each_up_host.lazy.filter do |host|
    match_host(host,pattern)
  end
end

#highlight(text, pattern) ⇒ String

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.

Highlights the pattern in the text.

Parameters:

  • text (String)

    The text to modify.

  • pattern (String)

    The pattern to highlight.

Returns:

  • (String)

    The modified text.



370
371
372
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 370

def highlight(text,pattern)
  text.to_s.gsub(pattern,colors.bold(colors.red(pattern)))
end

#highlight_host(host, pattern) ⇒ 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.

Prints the nmap host with the pattern highlighted in the output.

Parameters:

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

    The nmap host to print.

  • pattern (String)

    The text pattern to highlight in the output.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 236

def highlight_host(host,pattern)
  addresses = host.addresses
  hostnames = host.hostnames

  unless hostnames.empty?
    puts "[ #{addresses.first} / #{highlight(hostnames.first,pattern)} ]"
  else
    puts "[ #{addresses.first} ]"
  end
  puts

  indent do
    if addresses.length > 1
      puts "[ addresses ]"
      puts

      indent do
        addresses.each do |address|
          puts address
        end
      end
      puts
    end

    if hostnames.length > 1
      puts "[ hostnames ]"
      puts

      indent do
        hostnames.each do |hostname|
          puts highlight(hostname,pattern)
        end
      end
      puts
    end

    if (host_script = host.host_script)
      puts "[ host scripts ]"
      puts

      indent do
        highlight_scripts(host_script)
      end
    end

    puts "[ ports ]"
    puts

    indent do
      host.each_open_port do |port|
        highlight_port(port,pattern)
      end
    end
  end
end

#highlight_hosts(hosts, pattern) ⇒ 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.

Prints the nmap hosts with the pattern highlighted in the output.

Parameters:

  • hosts (Enumerator::Lazy<::Nmap::XML::Host>)

    The nmap hosts to print.

  • pattern (String)

    The pattern to highlight in the output.



220
221
222
223
224
225
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 220

def highlight_hosts(hosts,pattern)
  hosts.each do |host|
    highlight_host(host,pattern)
    puts
  end
end

#highlight_port(port, pattern) ⇒ 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.

Prints the nmap port with the pattern highlighted in the output.

Parameters:

  • port (::Nmap::XML::Port)

    The nmap XML port object to print.

  • pattern (String)

    The text pattern to highlight in the output.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 301

def highlight_port(port,pattern)
  port_line = "#{port.number}/#{port.protocol}\t#{port.state}"

  if (service = port.service)
    port_line << "\t#{highlight(service,pattern)}"

    if (extra_info = service.extra_info)
      port_line << " #{highlight(extra_info,pattern)}"
    end
  end

  puts port_line

  unless port.scripts.empty?
    puts

    indent do
      highlight_scripts(port,pattern)
    end
  end
end

#highlight_script(script, pattern) ⇒ 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.

Prints the nmap script with the pattern highlighted in the output.

Parameters:

  • script (::Nmap::XML::Script)

    The nmap XML script object to print.

  • pattern (String)

    The text pattern to highlight in the output.



348
349
350
351
352
353
354
355
356
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 348

def highlight_script(script,pattern)
  puts "#{highlight(script.id,pattern)}:"

  indent do
    script.output.strip.each_line do |line|
      puts highlight(line,pattern)
    end
  end
end

#highlight_scripts(has_scripts, pattern) ⇒ 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.

Prints the nmap scripts with the pattern highlighted in the output.

Parameters:

  • has_scripts (::Nmap::XML::Scripts)

    The nmap XML object that has scripts.

  • pattern (String)

    The text pattern to highlight in the output.



332
333
334
335
336
337
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 332

def highlight_scripts(has_scripts,pattern)
  has_scripts.scripts.each_value do |script|
    highlight_script(script,pattern)
    puts
  end
end

#match_host(host, pattern) ⇒ 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.

Determines if the nmap XML host object contains the text pattern.

Parameters:

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

    The nmap XML host object to search.

  • pattern (String)

    The text pattern to search for.

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 114

def match_host(host,pattern)
  hostnames   = host.each_hostname
  open_ports  = host.each_open_port
  host_script = host.host_script

  hostnames.any? { |hostname| match_hostname(hostname,pattern) } ||
    open_ports.any? { |port| match_port(port,pattern) } ||
    (host_script && match_scripts(host_script,pattern))
end

#match_hostname(hostname, pattern) ⇒ 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.

Determines if the nmap XML hostname object contains the text pattern.

Parameters:

  • hostname (::Nmap::XML::Hostname)

    The nmap XML hostname object to search.

  • pattern (String)

    The text pattern to search for.

Returns:

  • (Boolean)


136
137
138
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 136

def match_hostname(hostname,pattern)
  hostname.name.match(pattern)
end

#match_port(port, pattern) ⇒ 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.

Determines if the nmap XML port object contains the text pattern.

Parameters:

  • port (::Nmap::XML::Port)

    The nmap XML port object to search.

  • pattern (String)

    The text pattern to search for.

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 151

def match_port(port,pattern)
  match_scripts(port,pattern) || if (service = port.service)
                                   match_service(service,pattern)
                                 end
end

#match_script(script, pattern) ⇒ 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.

Determines if the nmap XML script object contains the text pattern.

Parameters:

  • script (::Nmap::XML::Script)

    The nmap XML script object to search.

  • pattern (String)

    The text pattern to search for.

Returns:

  • (Boolean)


207
208
209
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 207

def match_script(script,pattern)
  script.id.match(pattern) || script.output.match(pattern)
end

#match_scripts(has_scripts, pattern) ⇒ 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.

Determines if the nmap XML scripts object contains the text pattern.

Parameters:

  • has_scripts (::Nmap::XML::Scripts)

    The nmap XML object that includes Nmap::XML::Scripts.

  • pattern (String)

    The text pattern to search for.

Returns:

  • (Boolean)


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

def match_scripts(has_scripts,pattern)
  has_scripts.scripts.any? do |id,script|
    match_script(script,pattern)
  end
end

#match_service(service, pattern) ⇒ 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.

Determines if the nmap XML service object contains the text pattern.

Parameters:

  • service (::Nmap::XML::Service)

    The nmap XML service object to search.

  • pattern (String)

    The text pattern to search for.

Returns:

  • (Boolean)


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

def match_service(service,pattern)
  product    = service.product
  version    = service.version
  extra_info = service.extra_info

  service.name.match(pattern) ||
    (product && product.match(pattern)) ||
    (version && version.match(pattern)) ||
    (extra_info && extra_info.match(pattern))
end

#run(pattern, *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 grep command.

Parameters:

  • pattern (String)

    The pattern to search for.

  • xml_files (Array<String>)

    The nmap .xml file(s) to grep.



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ronin/nmap/cli/commands/grep.rb', line 71

def run(pattern,*xml_files)
  xml_files.each do |xml_file|
    unless File.file?(xml_file)
      print_error "no such file or directory: #{xml_file}"
      next
    end

    xml   = ::Nmap::XML.open(xml_file)
    hosts = grep_xml(xml,pattern)

    highlight_hosts(hosts,pattern)
  end
end