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.



269
270
271
272
273
274
275
276
277
# File 'lib/ronin/db/ip_address.rb', line 269

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

#advisoriesArray<Advisory>

The advisories that the IP Address is vulnerable to.

Returns:

Since:

  • 0.2.0



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

has_many :advisories, through: :vulnerabilities

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

#notesArray<Note>

The associated notes.

Returns:

Since:

  • 0.2.0



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

has_many :notes, dependent: :destroy

#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

#organization_ip_addressesArray<OrganizationIPAddress>

The association of IP addresses and organizations.

Returns:

Since:

  • 0.2.0



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

has_many :organization_ip_addresses, class_name: 'OrganizationIPAddress',
dependent:  :destroy

#organizationsArray<Organization>

The organizations that claim ownership of the IP address.

Returns:

Since:

  • 0.2.0



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

has_many :organizations, through: :organization_ip_addresses

#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

#vulnerabilitiesArray<Vulnerability>

The vulnerabilities which reference the IP Address.

Returns:

Since:

  • 0.2.0



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

has_many :vulnerabilities, dependent: :destroy

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:



192
193
194
195
196
197
198
199
# File 'lib/ronin/db/ip_address.rb', line 192

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:



209
210
211
# File 'lib/ronin/db/ip_address.rb', line 209

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

.v4Array<IPAddress>

Searches for all IPv4 addresses.

Returns:



164
165
166
# File 'lib/ronin/db/ip_address.rb', line 164

def self.v4
  where(version: 4)
end

.v6Array<IPAddress>

Searches for all IPv6 addresses.

Returns:



176
177
178
# File 'lib/ronin/db/ip_address.rb', line 176

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.



239
240
241
# File 'lib/ronin/db/ip_address.rb', line 239

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.



224
225
226
# File 'lib/ronin/db/ip_address.rb', line 224

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.



254
255
256
# File 'lib/ronin/db/ip_address.rb', line 254

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:



316
317
318
# File 'lib/ronin/db/ip_address.rb', line 316

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.



301
302
303
304
305
306
307
308
309
# File 'lib/ronin/db/ip_address.rb', line 301

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.



340
341
342
# File 'lib/ronin/db/ip_address.rb', line 340

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.



328
329
330
# File 'lib/ronin/db/ip_address.rb', line 328

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.



353
354
355
# File 'lib/ronin/db/ip_address.rb', line 353

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.



367
368
369
# File 'lib/ronin/db/ip_address.rb', line 367

def to_i
  ipaddr.to_i
end