Class: Ronin::DB::Port

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Model
Defined in:
lib/ronin/db/port.rb

Overview

Represents a TCP or UDP port.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

included

Instance Attribute Details

#created_atTime (readonly)

Defines the created_at timestamp

Returns:

  • (Time)

Since:

  • 0.2.0



65
# File 'lib/ronin/db/port.rb', line 65

attribute :created_at, :datetime

#idInteger

The primary key of the port.

Returns:

  • (Integer)


38
# File 'lib/ronin/db/port.rb', line 38

attribute :id, :integer

#ip_addressesArray<IPAddress>

The IP Addresses that that have this port open.

Returns:

Since:

  • 0.2.0



79
# File 'lib/ronin/db/port.rb', line 79

has_many :ip_addresses, through: :open_ports

#notesArray<Note>

The associated notes.

Returns:

Since:

  • 0.2.0



95
# File 'lib/ronin/db/port.rb', line 95

has_many :notes, dependent: :destroy

#numberInteger

The port number.

Returns:

  • (Integer)


51
# File 'lib/ronin/db/port.rb', line 51

attribute :number, :integer

#open_portsArray<OpenPort>

The open ports.

Returns:



71
# File 'lib/ronin/db/port.rb', line 71

has_many :open_ports, dependent: :destroy

#protocol"tcp", "udp"

The protocol of the port (either 'tcp' / 'udp').

Returns:

  • ("tcp", "udp")


44
# File 'lib/ronin/db/port.rb', line 44

enum :protocol, {tcp: 'tcp', udp: 'udp'}, default: :tcp

#servicesArray<Service>

The services that that use this port.

Returns:

Since:

  • 0.2.0



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

has_many :services, through: :open_ports

Class Method Details

.import(number) ⇒ Port

Creates a new Port.

Parameters:

  • number (String, Integer)

    The port number.

Returns:

  • (Port)

    The new or previously saved port.



199
200
201
# File 'lib/ronin/db/port.rb', line 199

def self.import(number)
  create(number: number)
end

.lookup(number) ⇒ Port?

Looks up a port by it's number.

Parameters:

  • number (String, Integer)

    The port number to query.

Returns:

  • (Port, nil)

    The found port number.



184
185
186
# File 'lib/ronin/db/port.rb', line 184

def self.lookup(number)
  find_by(number: number)
end

.with_ip_address(address) ⇒ Array<Port>

Queries all ports associated with the IP address.

Parameters:

  • address (String)

    The IP address to search by.

Returns:

  • (Array<Port>)

    The ports associated with the IP address.

Since:

  • 0.2.0



165
166
167
168
169
170
171
# File 'lib/ronin/db/port.rb', line 165

def self.with_ip_address(address)
  joins(open_ports: :ip_address).where(
    open_ports: {
      ronin_ip_addresses: {address: address}
    }
  )
end

.with_number(number) ⇒ Array<Port>

Queries all ports with the port number or in the port range.

Parameters:

  • number (Integer, Range<Integer>)

    The port number or range.

Returns:

  • (Array<Port>)

    The ports with the port number or in the port range.

Since:

  • 0.2.0



110
111
112
# File 'lib/ronin/db/port.rb', line 110

def self.with_number(number)
  where(number: number)
end

.with_protocol(protocol) ⇒ Array<Port>

Queries all ports with the protocol.

Parameters:

  • protocol (:tcp, ;udp)

    The protocol to search for.

Returns:

  • (Array<Port>)

    The ports that use the protocol.

Since:

  • 0.2.0



127
128
129
# File 'lib/ronin/db/port.rb', line 127

def self.with_protocol(protocol)
  where(protocol: protocol)
end

.with_service_name(name) ⇒ Array<Port>

Queries all ports associated with the service name.

Parameters:

  • name (String)

    The service name to search for.

Returns:

  • (Array<Port>)

    The ports associated with the service name.

Since:

  • 0.2.0



144
145
146
147
148
149
150
# File 'lib/ronin/db/port.rb', line 144

def self.with_service_name(name)
  joins(open_ports: :service).where(
    open_ports: {
      ronin_services: {name: name}
    }
  )
end

Instance Method Details

#to_iInteger

Converts the port to an integer.

Returns:

  • (Integer)

    The port number.



211
212
213
# File 'lib/ronin/db/port.rb', line 211

def to_i
  self.number.to_i
end

#to_sString

Converts the port to a string.

Returns:

  • (String)

    The port number and protocol.



223
224
225
# File 'lib/ronin/db/port.rb', line 223

def to_s
  "#{self.number}/#{self.protocol}"
end