Module: Ronin::Nmap::CLI::FilteringOptions Private

Included in:
Commands::Dump, Commands::Print
Defined in:
lib/ronin/nmap/cli/filtering_options.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Mixin which adds nmap target filtering options to commands.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#with_domainsSet<String> (readonly)

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.

Returns:

  • (Set<String>)


118
119
120
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 118

def with_domains
  @with_domains
end

#with_ip_rangesSet<IPAddr> (readonly)

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.

Returns:

  • (Set<IPAddr>)


115
116
117
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 115

def with_ip_ranges
  @with_ip_ranges
end

#with_ipsSet<String> (readonly)

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.

Returns:

  • (Set<String>)


112
113
114
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 112

def with_ips
  @with_ips
end

#with_osesSet<String> (readonly)

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.

Returns:

  • (Set<String>)


121
122
123
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 121

def with_oses
  @with_oses
end

#with_portsSet<PortList> (readonly)

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.

Returns:



124
125
126
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 124

def with_ports
  @with_ports
end

#with_script_outputSet<String, Regexp> (readonly)

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.

Returns:

  • (Set<String, Regexp>)


133
134
135
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 133

def with_script_output
  @with_script_output
end

#with_scriptsSet<String> (readonly)

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.

Returns:

  • (Set<String>)


130
131
132
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 130

def with_scripts
  @with_scripts
end

#with_servicesSet<String> (readonly)

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.

Returns:

  • (Set<String>)


127
128
129
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 127

def with_services
  @with_services
end

Class Method Details

.included(command) ⇒ 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.

Adds filtering options to the command class including Ronin::Nmap::CLI::FilteringOptions.

Parameters:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 37

def self.included(command)
  command.option :ip, value: {
                        type:  String,
                        usage: 'IP'
                      },
                      desc: 'Filters the targets by IP' do |ip|
                        @with_ips << ip
                      end

  command.option :ip_range, value: {
                              type:  String,
                              usage: 'CIDR'
                            },
                            desc: 'Filter the targets by IP range' do |ip_range|
                              @with_ip_ranges << IPAddr.new(ip_range)
                            end

  command.option :domain, value: {
                            type:  String,
                            usage: 'DOMAIN'
                          },
                          desc: 'Filters the targets by domain' do |domain|
                            @with_domains << domain
                          end

  command.option :with_os, value: {
                             type:  String,
                             usage: 'OS'
                           },
                           desc: 'Filters the targets by OS' do |os|
                             @with_oses << os.to_sym
                           end

  command.option :with_ports, value: {
                                type: /\A(?:\d+|\d+-\d+)(?:,(?:\d+|\d+-\d+))*\z/,
                                usage: '{PORT | PORT1-PORT2},...'
                              },
                              desc: 'Filter targets by port numbers' do |ports|
                                @with_ports << PortList.parse(ports)
                              end

  command.option :with_service, value: {
                                  type:  /\A[a-z]+[a-z0-9_+-]*\z/,
                                  usage: 'SERVICE[,...]'
                                },
                                desc: 'Filters targets by service' do |service|
                                  @with_services << service
                                end

  command.option :with_script, value: {
                                 type:  /\A[a-z][a-z0-9-]*\z/,
                                 usage: 'SCRIPT[,...]'
                               },
                               desc: 'Filters targets with the script' do |script|
                                 @with_scripts << script
                               end

  command.option :with_script_output, value: {
                                        type:  String,
                                        usage: 'STRING'
                                      },
                                      desc: 'Filters targets containing the script output' do |string|
                                        @with_script_output << string
                                      end

  command.option :with_script_regex, value: {
                                       type:  Regexp,
                                       usage: '/REGEX/'
                                     },
                                     desc: 'Filters targets containing the script output' do |regexp|
                                       @with_script_output << regexp
                                     end
end

Instance Method Details

#filter_targets(xml) ⇒ 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.

Filters the nmap scan targets.

Parameters:

  • xml (::Nmap::XML)

    The parsed nmap xml data to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



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
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 163

def filter_targets(xml)
  targets = xml.each_up_host.lazy

  unless @with_ips.empty?
    targets = filter_targets_by_ip(targets)
  end

  unless @with_ip_ranges.empty?
    targets = filter_targets_by_ip_range(targets)
  end

  unless @with_domains.empty?
    targets = filter_targets_by_domain(targets)
  end

  unless @with_oses.empty?
    targets = filter_targets_by_os(targets)
  end

  unless @with_ports.empty?
    targets = filter_targets_by_port(targets)
  end

  unless @with_services.empty?
    targets = filter_targets_by_scripts(targets)
  end

  unless @with_scripts.empty?
    targets = filter_targets_by_script(targets)
  end

  unless @with_script_output.empty?
    targets = filter_targets_by_script_output(targets)
  end

  return targets
end

#filter_targets_by_domain(targets) ⇒ 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.

Filters the targets by a domain.

Parameters:

  • targets (Enumerator::Lazy)

    The targets to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 242

def filter_targets_by_domain(targets)
  regexp = Regexp.union(
    @with_domains.map { |domain|
      escaped_domain = Regexp.escape(domain)

      /\.#{escaped_domain}\z|\A#{escaped_domain}\z/
    }
  )

  targets.filter do |host|
    if (hostname = host.hostname)
      hostname.name =~ regexp
    end
  end
end

#filter_targets_by_ip(targets) ⇒ 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.

Filters the targets by IP address.

Parameters:

  • targets (Enumerator::Lazy)

    The targets to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



210
211
212
213
214
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 210

def filter_targets_by_ip(targets)
  targets.filter do |host|
    @with_ips.include?(host.address)
  end
end

#filter_targets_by_ip_range(targets) ⇒ 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.

Filters the targets by an IP rangeo.

Parameters:

  • targets (Enumerator::Lazy)

    The targets to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



225
226
227
228
229
230
231
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 225

def filter_targets_by_ip_range(targets)
  targets.filter do |host|
    @with_ip_ranges.any? do |ip_range|
      ip_range.include?(host.address)
    end
  end
end

#filter_targets_by_os(targets) ⇒ 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.

Filters the targets by OS.

Parameters:

  • targets (Enumerator::Lazy)

    The targets to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



267
268
269
270
271
272
273
274
275
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 267

def filter_targets_by_os(targets)
  targets.filter do |host|
    if (os = host.os)
      os.each_class.any? do |os_class|
        @with_oses.include?(os_class.family)
      end
    end
  end
end

#filter_targets_by_port(targets) ⇒ 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.

Filters the targets by port number.

Parameters:

  • targets (Enumerator::Lazy)

    The targets to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



286
287
288
289
290
291
292
293
294
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 286

def filter_targets_by_port(targets)
  targets.filter do |host|
    host.each_open_port.any? do |port|
      @with_ports.any? do |port_list|
        port_list.include?(port.number)
      end
    end
  end
end

#filter_targets_by_script(targets) ⇒ 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.

Filters the targets by script IDs.

Parameters:

  • targets (Enumerator::Lazy)

    The targets to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



324
325
326
327
328
329
330
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 324

def filter_targets_by_script(targets)
  targets.filter do |host|
    host.each_open_port.any? do |port|
      @with_scripts.intersect?(port.scripts.keys)
    end
  end
end

#filter_targets_by_script_output(targets) ⇒ 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.

Filters the targets by script output.

Parameters:

  • targets (Enumerator::Lazy)

    The targets to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



341
342
343
344
345
346
347
348
349
350
351
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 341

def filter_targets_by_script_output(targets)
  regexp = Regexp.union(@with_script_output.to_a)

  targets.filter do |host|
    host.each_open_port.any? do |port|
      port.scripts.each_value.any? do |script|
        script.output =~ regexp
      end
    end
  end
end

#filter_targets_by_service(targets) ⇒ 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.

Filters the targets by service name.

Parameters:

  • targets (Enumerator::Lazy)

    The targets to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered targets.



305
306
307
308
309
310
311
312
313
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 305

def filter_targets_by_service(targets)
  targets.filter do |host|
    host.each_open_port.any? do |port|
      if (service = port.service)
        @with_services.include?(service.name)
      end
    end
  end
end

#initialize(**kwargs) ⇒ 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.

Initializes the command.

Parameters:

  • kwargs (Hash{Symbol => String})

    Additional keywords for the command.



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ronin/nmap/cli/filtering_options.rb', line 141

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

  @with_ips           = Set.new
  @with_ip_ranges     = Set.new
  @with_domains       = Set.new
  @with_oses          = Set.new
  @with_ports         = Set.new
  @with_services      = Set.new
  @with_scripts       = Set.new
  @with_script_output = Set.new
end