Class: Ronin::IPAddress

Inherits:
Address show all
Includes:
Model::Importable
Defined in:
lib/ronin/ip_address.rb

Overview

Represents IP addresses that can be stored in the Database.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model::Importable

included

Methods inherited from Address

[], #inspect, parse, #to_s

Methods included from Model

included

Class Method Details

.extract(text, version = nil) {|ip| ... } ⇒ Object

Extracts and parses IP addresses from text.

Parameters:

  • text (String)

    The text to parse.

  • version (Symbol, Integer) (defaults to: nil)

    Specifies whether to parse IPv4 or IPv6 addresses.

Yields:

  • (ip)

    The given block will be passed each extracted IP address.

Yield Parameters:

  • ip (IPAddress)

    An extracted IP Address from the text.

See Also:

Since:

  • 1.3.0



103
104
105
106
107
108
109
110
111
# File 'lib/ronin/ip_address.rb', line 103

def self.extract(text,version=nil)
  return enum_for(:extract,text,version).to_a unless block_given?

  IPAddr.extract(text,version) do |ip|
    yield parse(ip)
  end

  return nil
end

.lookup(name, nameserver = nil) ⇒ Array<IPAddress>

Looks up the host name to multiple IP addresses.

Parameters:

  • name (String)

    The host name to look up.

  • nameserver (String) (defaults to: nil)

    Optional nameserver to query.

Returns:

  • (Array<IPAddress>)

    The new or previously saved IP Addresses for the host name.

Since:

  • 1.0.0



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ronin/ip_address.rb', line 208

def self.lookup(name,nameserver=nil)
  host = HostName.first_or_new(:address => name)
  resolver = Resolv.resolver(nameserver)

  ips = begin
          resolver.getaddresses(name)
        rescue
          []
        end
    
  ips.map! do |addr|
    IPAddress.first_or_create(
      :address => addr,
      :host_names => [host]
    )
  end

  return ips
end

.v4Array<IPAddress>

Searches for all IPv4 addresses.

Returns:

Since:

  • 1.0.0



123
124
125
# File 'lib/ronin/ip_address.rb', line 123

def self.v4
  all(:version => 4)
end

.v6Array<IPAddress>

Searches for all IPv6 addresses.

Returns:

Since:

  • 1.0.0



137
138
139
# File 'lib/ronin/ip_address.rb', line 137

def self.v6
  all(:version => 6)
end

.with_hosts(names) ⇒ Array<IPAddress>

Searches for IP addresses associated with the given host names.

Parameters:

  • names (Array<String>, String)

    The host name(s) to search for.

Returns:

  • (Array<IPAddress>)

    The matching IP addresses.

Since:

  • 1.0.0



171
172
173
# File 'lib/ronin/ip_address.rb', line 171

def self.with_hosts(names)
  all('host_names.address' => names)
end

.with_macs(macs) ⇒ Array<IPAddress>

Searches for all IP addresses associated with specific MAC addresses.

Parameters:

  • macs (Array<String>, String)

    The MAC address(es) to search for.

Returns:

  • (Array<IPAddress>)

    The matching IP addresses.

Since:

  • 1.0.0



154
155
156
# File 'lib/ronin/ip_address.rb', line 154

def self.with_macs(macs)
  all('mac_addresses.address' => macs)
end

.with_ports(numbers) ⇒ Array<IPAddress>

Searches for IP addresses with the given open ports.

Parameters:

  • numbers (Array<Integer>, Integer)

    The port number(s) to search for.

Returns:

  • (Array<IPAddress>)

    The matching IP addresses.

Since:

  • 1.0.0



188
189
190
# File 'lib/ronin/ip_address.rb', line 188

def self.with_ports(numbers)
  all('ports.number' => numbers)
end

Instance Method Details

#last_scanned_atTime?

Determines when the IP address was last scanned.

Returns:

  • (Time, nil)

    The time the IP address was last scanned at.

Since:

  • 1.0.0



316
317
318
319
320
321
322
# File 'lib/ronin/ip_address.rb', line 316

def last_scanned_at
  last_scanned_port = self.open_ports.first(
    :order => [:last_scanned_at.desc]
  )

  return last_scanned_port.last_scanned_at if last_scanned_port
end

#lookup!(nameserver = nil) ⇒ Array<HostName>

Performs a reverse lookup on the IP address.

Parameters:

  • nameserver (String) (defaults to: nil)

    Optional nameserver to query.

Returns:

  • (Array<HostName>)

    The host-names associated with the IP Address.

Since:

  • 1.0.0



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

def lookup!(nameserver=nil)
  resolver = Resolv.resolver(nameserver)
  hosts = begin
            resolver.getnames(self.address.to_s)
          rescue
            []
          end

  hosts.map! do |name|
    HostName.first_or_create(
      :address => name,
      :ip_addresses => [self]
    )
  end

  return hosts
end

#recent_host_nameHostName

The host-name that was most recently used by the IP Address.

Returns:

  • (HostName)

    The host-name that most recently used by the IP Address.

Since:

  • 1.0.0



285
286
287
288
289
# File 'lib/ronin/ip_address.rb', line 285

def recent_host_name
  self.host_name_ip_addresses.all(
    :order => [:created_at.desc]
  ).host_names.first
end

#recent_mac_addressMacAddress

The MAC Address that was most recently used by the IP Address.

Returns:

  • (MacAddress)

    The MAC Address that most recently used the IP Address.

Since:

  • 1.0.0



269
270
271
272
273
# File 'lib/ronin/ip_address.rb', line 269

def recent_mac_address
  self.ip_address_mac_addresses.all(
    :order => [:created_at.desc]
  ).mac_addresses.first
end

#recent_os_guessOS

The Operating System that was most recently guessed for the IP Address.

Returns:

  • (OS)

    The Operating System that most recently was guessed.

Since:

  • 1.0.0



302
303
304
# File 'lib/ronin/ip_address.rb', line 302

def recent_os_guess
  self.os_guesses.all(:order => [:created_at.desc]).oses.first
end

#to_iInteger

Converts the address to an Integer.

Returns:

  • (Integer)

    The network representation of the IP address.

Since:

  • 1.0.0



348
349
350
# File 'lib/ronin/ip_address.rb', line 348

def to_i
  self.address.to_i
end

#to_ipIPAddr

Converts the address to an IP address object.

Returns:

  • (IPAddr)

    The IPAddr object representing either the IPv4 or IPv6 address.

Since:

  • 1.0.0



334
335
336
# File 'lib/ronin/ip_address.rb', line 334

def to_ip
  self.address
end