Class: Ronin::DB::IPAddress

Inherits:
Address
  • Object
show all
Defined in:
lib/ronin/db/ip_address.rb

Overview

Represents IP addresses and their associated host names and MAC addresses.

Instance Attribute Summary collapse

Attributes inherited from Address

#created_at, #id

Attributes included from Model::LastScannedAt

#last_scanned_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Address

import, lookup, #to_s

Methods included from Model::LastScannedAt

included

Methods included from Model::Importable

included

Methods included from Model

included

Constructor Details

#initialize(*arguments, **kwargs) ⇒ IPAddress

Note:

Also assigns a default value to version based on the address.

Initializes the IP address record.

Parameters:

  • arguments (Array)

    Additional attribute arguments.

  • kwargs (Hash{Symbol => Object})

    Additional attribute values.



228
229
230
231
232
233
234
235
236
# File 'lib/ronin/db/ip_address.rb', line 228

def initialize(*arguments,**kwargs)
  super(*arguments,**kwargs)

  self.version ||= if ipaddr
                     if ipaddr.ipv6? then 6
                     else                 4
                     end
                   end
end

Instance Attribute Details

#addressString

The IP Address.

Returns:

  • (String)


39
# File 'lib/ronin/db/ip_address.rb', line 39

attribute :address, :string

#host_name_ip_addressesArray<HostNameIPAddress>

The host-names that the IP Address serves.

Returns:



80
81
# File 'lib/ronin/db/ip_address.rb', line 80

has_many :host_name_ip_addresses, dependent: :destroy,
class_name: 'HostNameIPAddress'

#host_namesArray<HostName>

The host-names associated with the IP Address.

Returns:



87
# File 'lib/ronin/db/ip_address.rb', line 87

has_many :host_names, through: :host_name_ip_addresses

#htonString

The IP address, but in network byte-order.

Returns:

  • (String)


52
# File 'lib/ronin/db/ip_address.rb', line 52

attribute :hton, :binary

#ip_address_mac_addressesArray<IPAddressMACAddress>

The MAC Addresses associations.

Returns:



66
67
# File 'lib/ronin/db/ip_address.rb', line 66

has_many :ip_address_mac_addresses, dependent:  :destroy,
class_name: 'IPAddressMACAddress'

#mac_addressesArray<MACAddress>

The MAC Addresses associated with the IP Address.

Returns:



73
74
# File 'lib/ronin/db/ip_address.rb', line 73

has_many :mac_addresses, through: :ip_address_mac_addresses,
class_name: 'MACAddress'

#open_portsArray<OpenPort>

The open ports of the host.

Returns:



93
# File 'lib/ronin/db/ip_address.rb', line 93

has_many :open_ports, dependent: :destroy

#os_guessesArray<OSGuess>

Any OS guesses against the IP Address.

Returns:



105
106
# File 'lib/ronin/db/ip_address.rb', line 105

has_many :os_guesses, dependent: :destroy,
class_name: 'OSGuess'

#osesArray<OS>

Any OSes that the IP Address might be running

Returns:

  • (Array<OS>)


112
113
# File 'lib/ronin/db/ip_address.rb', line 112

has_many :oses, through: :os_guesses,
class_name: 'OS'

#portsArray<Port>

The ports of the host.

Returns:



99
# File 'lib/ronin/db/ip_address.rb', line 99

has_many :ports, through: :open_ports

#versionInteger

Type of the address.

Returns:

  • (Integer)


59
# File 'lib/ronin/db/ip_address.rb', line 59

attribute :version, :integer

Class Method Details

.between(first_ip, last_ip) ⇒ Array<IPAddress>

Queries all IP address that are between the first IP address and last IP address.

Parameters:

  • first_ip (String)

    The first IP of the IP range.

  • last_ip (String)

    The last IP of the IP range.

Returns:



151
152
153
154
155
156
157
158
# File 'lib/ronin/db/ip_address.rb', line 151

def self.between(first_ip,last_ip)
  first_ip_hton = IPAddr.new(first_ip).hton
  last_ip_hton  = IPAddr.new(last_ip).hton

  hton = arel_table[:hton]

  where(hton.gteq(first_ip_hton).and(hton.lteq(last_ip_hton)))
end

.in_range(range) ⇒ Array<IPAddress>

Queries all IP addresses that exist in the range of IP addresses.

Parameters:

  • range (Range, #begin, #end)

    The IP range to query.

Returns:



168
169
170
# File 'lib/ronin/db/ip_address.rb', line 168

def self.in_range(range)
  between(range.begin,range.end)
end

.v4Array<IPAddress>

Searches for all IPv4 addresses.

Returns:



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

def self.v4
  where(version: 4)
end

.v6Array<IPAddress>

Searches for all IPv6 addresses.

Returns:



135
136
137
# File 'lib/ronin/db/ip_address.rb', line 135

def self.v6
  where(version: 6)
end

.with_host_name(name) ⇒ Array<IPAddress>

Searches for IP addresses associated with the given host name(s).

Parameters:

  • name (Array<String>, String)

    The host name(s) to search for.

Returns:

  • (Array<IPAddress>)

    The matching IP addresses.



198
199
200
# File 'lib/ronin/db/ip_address.rb', line 198

def self.with_host_name(name)
  joins(:host_names).where(host_names: {name: name})
end

.with_mac_address(mac) ⇒ Array<IPAddress>

Searches for all IP addresses associated with specific MAC address(es).

Parameters:

  • mac (Array<String>, String)

    The MAC address(es) to search for.

Returns:

  • (Array<IPAddress>)

    The matching IP addresses.



183
184
185
# File 'lib/ronin/db/ip_address.rb', line 183

def self.with_mac_address(mac)
  joins(:mac_addresses).where(mac_addresses: {address: mac})
end

.with_port_number(number) ⇒ Array<IPAddress>

Searches for IP addresses with the given open port(s).

Parameters:

  • number (Array<Integer>, Integer)

    The port number(s) to search for.

Returns:

  • (Array<IPAddress>)

    The matching IP addresses.



213
214
215
# File 'lib/ronin/db/ip_address.rb', line 213

def self.with_port_number(number)
  joins(:ports).where(ports: {number: number})
end

Instance Method Details

#asnASN?

Queries the ASN record for the IP address.

Returns:



275
276
277
# File 'lib/ronin/db/ip_address.rb', line 275

def asn
  ASN.containing_ip(ipaddr)
end

#ipaddrIPAddr Also known as: to_ip

Returns an IPAddr object for the IP address.

Returns:

  • (IPAddr)

    The IPAddr object representing either the IPv4 or IPv6 address.



260
261
262
263
264
265
266
267
268
# File 'lib/ronin/db/ip_address.rb', line 260

def ipaddr
  @ipaddr ||= if self.address
                begin
                  IPAddr.new(self.address)
                rescue IPAddr::InvalidAddressError
                  nil
                end
              end
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.



299
300
301
# File 'lib/ronin/db/ip_address.rb', line 299

def recent_host_name
  self.host_name_ip_addresses.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.



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

def recent_mac_address
  self.ip_address_mac_addresses.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.



312
313
314
# File 'lib/ronin/db/ip_address.rb', line 312

def recent_os_guess
  self.os_guesses.order('created_at DESC').oses.first
end

#to_iInteger

Converts the address to an Integer.

Returns:

  • (Integer)

    The network representation of the IP address.



326
327
328
# File 'lib/ronin/db/ip_address.rb', line 326

def to_i
  ipaddr.to_i
end