Class: Ronin::HostName

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

Overview

Represents host names 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

.domain(name) ⇒ Array<HostName>

Searches for all host names sharing a common domain name.

Parameters:

  • name (String)

    The common domain name to search for.

Returns:

  • (Array<HostName>)

    The matching host names.

Since:

  • 1.0.0



163
164
165
166
# File 'lib/ronin/host_name.rb', line 163

def self.domain(name)
  all(:address.like => "#{name}.%") |
  all(:address.like => "%.#{name}.%")
end

.extract(text) {|host| ... } ⇒ Array<HostName>

Extracts host-names from the given text.

Parameters:

  • text (String)

    The text to parse.

Yields:

  • (host)

    The given block will be passed each extracted host-name.

Yield Parameters:

  • host (HostName)

    An extracted host-name.

Returns:

  • (Array<HostName>)

    If no block is given, an Array of host-names will be returned.

See Also:

  • 11.31.3.0


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ronin/host_name.rb', line 87

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

  scanner = StringScanner.new(text)

  while scanner.skip_until(Regexp::HOST_NAME)
    yield parse(scanner.matched)
  end

  return nil
end

.lookup(addr, nameserver = nil) ⇒ Array<HostName>

Looks up all host names associated with an IP address.

Parameters:

  • addr (IPAddr, String)

    The IP address to lookup.

  • nameserver (String) (defaults to: nil)

    The optional nameserver to query.

Returns:

  • (Array<HostName>)

    The host names associated with the IP address.

Since:

  • 1.0.0



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ronin/host_name.rb', line 184

def self.lookup(addr,nameserver=nil)
  addr = addr.to_s
  ip = IPAddress.first_or_new(:address => addr)

  resolver = Resolv.resolver(nameserver)
  hosts = begin
            resolver.getnames(addr)
          rescue
            []
          end

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

  return hosts
end

.tld(name) ⇒ Array<HostName>

Searches for all host names under the Top-Level Domain (TLD).

Parameters:

  • name (String)

    The Top-Level Domain (TLD).

Returns:

  • (Array<HostName>)

    The matching host names.

Since:

  • 1.0.0



146
147
148
# File 'lib/ronin/host_name.rb', line 146

def self.tld(name)
  all(:address.like => "%.#{name}")
end

.with_ips(ips) ⇒ Array<HostName>

Searches for host names associated with the given IP address(es).

Parameters:

  • ips (Array<String>, String)

    The IP address(es) to search for.

Returns:

  • (Array<HostName>)

    The matching host names.

Since:

  • 1.0.0



112
113
114
# File 'lib/ronin/host_name.rb', line 112

def self.with_ips(ips)
  all('ip_addresses.address' => ips)
end

.with_ports(numbers) ⇒ Array<HostName>

Searches for host names with the given open port(s).

Parameters:

  • numbers (Array<Integer>, Integer)

    The open port(s) to search for.

Returns:

  • (Array<HostName>)

    The matching host names.

Since:

  • 1.0.0



129
130
131
# File 'lib/ronin/host_name.rb', line 129

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

Instance Method Details

#last_scanned_atTime?

Determines when the host was last scanned.

Returns:

  • (Time, nil)

    The time the host was last scanned at.

Since:

  • 1.0.0



264
265
266
267
268
269
270
# File 'lib/ronin/host_name.rb', line 264

def last_scanned_at
  last_scanned_url = self.urls.first(
    :order => [:last_scanned_at.desc]
  )

  return last_scanned_url.last_scanned_at if last_scanned_url
end

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

Looks up all IP Addresses for the host name.

Parameters:

  • nameserver (String) (defaults to: nil)

    The optional nameserver to query.

Returns:

  • (Array<IPAddress>)

    The IP Addresses for the host name.

Since:

  • 1.0.0



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/ronin/host_name.rb', line 220

def lookup!(nameserver=nil)
  resolver = Resolv.resolver(nameserver)
  ips = begin
          resolver.getaddresses(self.address)
        rescue
          []
        end

  ips.map! do |addr|
    IPAddress.first_or_create(
      :address => addr,
      :host_names => [self]
    )
  end

  return ips
end

#recent_ip_addressIpAddress

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

Returns:

  • (IpAddress)

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

Since:

  • 1.0.0



248
249
250
251
252
# File 'lib/ronin/host_name.rb', line 248

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