Module: Ronin::Masscan::CLI::FilteringOptions Private

Included in:
Commands::Dump, Commands::Grep, Commands::Print
Defined in:
lib/ronin/masscan/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

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

The IP ranges to filter the targets by.

Returns:

  • (Set<IPAddr>)


109
110
111
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 109

def ip_ranges
  @ip_ranges
end

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

The IPs to filter the targets by.

Returns:

  • (Set<String>)


104
105
106
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 104

def ips
  @ips
end

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

The ports to filter the targets by.

Returns:



114
115
116
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 114

def ports
  @ports
end

#protocolsSet<:tcp, :udp> (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.

The protocols to filter the targets by.

Returns:

  • (Set<:tcp, :udp>)


99
100
101
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 99

def protocols
  @protocols
end

#with_app_protocolsSet<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.

The app protocols to filter the targets by.

Returns:

  • (Set<String>)


119
120
121
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 119

def with_app_protocols
  @with_app_protocols
end

#with_payloadsSet<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.

The payload Strings or Regexps to filter the targets by.

Returns:

  • (Set<String, Regexp>)


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

def with_payloads
  @with_payloads
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::Masscan::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
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 37

def self.included(command)
  command.option :protocol, short: '-P',
                            value: {
                              type: [:tcp, :udp]
                            },
                            desc: 'Filters the targets by protocol' do |proto|
                              @protocols << proto
                            end

  command.option :ip, value: {
                        type:  String,
                        usage: 'IP'
                      },
                      desc: 'Filters the targets by IP' do |ip|
                        @ips << ip
                      end

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

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

  command.option :with_app_protocol, value: {
                                       type:  /\A[a-z][a-z0-9_-]*\z/,
                                       usage: 'APP_PROTOCOL[,...]'
                                     },
                                     desc: 'Filters targets with the app protocol' do |app_protocol|
                                       @with_app_protocols << app_protocol.to_sym
                                     end

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

  command.option :with_payload_regex, value: {
                                        type:  Regexp,
                                        usage: '/REGEX/'
                                      },
                                      desc: 'Filters targets with the matching payload' do |regexp|
                                        @with_payloads << regexp
                                      end
end

Instance Method Details

#filter_banner_records(records) ⇒ 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.

Filter Masscan::Banner records.

Parameters:

  • records (Enumerator::Lazy)

    The records to filter.

Returns:

  • (Enumerator::Lazy)

    The filtered records.



208
209
210
211
212
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 208

def filter_banner_records(records)
  records.filter do |record|
    record.kind_of?(::Masscan::Banner)
  end
end

#filter_records(output_file) ⇒ 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 masscan records.

Parameters:

  • output_file (::Masscan::OutputFile)

    The parsed nmap xml data to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered records.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 154

def filter_records(output_file)
  records = output_file.each.lazy

  unless @protocols.empty?
    records = filter_records_by_protocol(records)
  end

  unless @ips.empty?
    records = filter_records_by_ip(records)
  end

  unless @ip_ranges.empty?
    records = filter_records_by_ip_range(records)
  end

  unless @ports.empty?
    records = filter_records_by_port(records)
  end

  unless @with_app_protocols.empty?
    records = filter_records_by_app_protocol(records)
  end

  unless @with_payloads.empty?
    records = filter_records_by_payload(records)
  end

  return records
end

#filter_records_by_app_protocol(records) ⇒ 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 records by app-protocol IDs.

Parameters:

  • records (Enumerator::Lazy)

    The records to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered records.



285
286
287
288
289
290
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 285

def filter_records_by_app_protocol(records)
  records.filter do |record|
    record.kind_of?(::Masscan::Banner) &&
      @with_app_protocols.include?(record.app_protocol)
  end
end

#filter_records_by_ip(records) ⇒ 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 records by IP address.

Parameters:

  • records (Enumerator::Lazy)

    The records to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered records.



238
239
240
241
242
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 238

def filter_records_by_ip(records)
  records.filter do |record|
    @ips.include?(record.ip)
  end
end

#filter_records_by_ip_range(records) ⇒ 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 records by an IP rangeo.

Parameters:

  • records (Enumerator::Lazy)

    The records to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered records.



253
254
255
256
257
258
259
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 253

def filter_records_by_ip_range(records)
  records.filter do |record|
    @ip_ranges.any? do |ip_range|
      ip_range.include?(record.ip)
    end
  end
end

#filter_records_by_payload(records) ⇒ 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 records by payload contents.

Parameters:

  • records (Enumerator::Lazy)

    The records to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered records.



301
302
303
304
305
306
307
308
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 301

def filter_records_by_payload(records)
  regexp = Regexp.union(@with_payloads.to_a)

  records.filter do |record|
    record.kind_of?(::Masscan::Banner) &&
      record.payload =~ regexp
  end
end

#filter_records_by_port(records) ⇒ 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 records by port number.

Parameters:

  • records (Enumerator::Lazy)

    The records to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered records.



270
271
272
273
274
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 270

def filter_records_by_port(records)
  records.filter do |record|
    @ports.include?(record.port)
  end
end

#filter_records_by_protocol(records) ⇒ 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 records by protocol

Parameters:

  • records (Enumerator::Lazy)

    The records to filter.

Returns:

  • (Enumerator::Lazy)

    A lazy enumerator of the filtered records.



223
224
225
226
227
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 223

def filter_records_by_protocol(records)
  records.filter do |record|
    @protocols.include?(record.protocol)
  end
end

#filter_status_records(records) ⇒ 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.

Filter Masscan::Status records.

Parameters:

  • records (Enumerator::Lazy)

    The records to filter.

Returns:

  • (Enumerator::Lazy)

    The filtered records.



193
194
195
196
197
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 193

def filter_status_records(records)
  records.filter do |record|
    record.kind_of?(::Masscan::Status)
  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.



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ronin/masscan/cli/filtering_options.rb', line 132

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

  @protocols = Set.new
  @ips       = Set.new
  @ip_ranges = Set.new
  @ports     = Set.new

  # Masscan::Banner filtering options
  @with_app_protocols = Set.new
  @with_payloads      = Set.new
end