class Mdm::Cred

@deprecated Use metasploit-credential's `Metasploit::Credential::Core`.

A credential captured from a {#service}.

Constants

KEY_ID_REGEX

Checks if {#proof} is an SSH Key in {#ssh_key_id}.

PTYPES

Maps {#ptype_human} to {#ptype}.

Public Instance Methods

ptype_human() click to toggle source

Humanized {#ptype}.

@return [String, nil]

# File app/models/mdm/cred.rb, line 109
def ptype_human
  humanized = PTYPES.select do |k, v|
    v == ptype
  end.keys[0]

  humanized ? humanized : ptype
end
ssh_key_id() click to toggle source

Returns SSH Key ID.

@return [String] SSH Key Id if ssh-type key and {#proof} matches {KEY_ID_REGEX}. @return [nil] otherwise

# File app/models/mdm/cred.rb, line 121
def ssh_key_id
  return nil unless self.ptype =~ /^ssh_/
  return nil unless self.proof =~ KEY_ID_REGEX
  $1.downcase # Can't run into NilClass problems.
end
ssh_key_matches?(other_cred) click to toggle source

Returns whether `other`'s SSH private key or public key matches.

@return [false] if `other` is not same class as `self`. @return [false] if {#ptype} does not match. @return [false] if {#ptype} is neither `“ssh_key”` nor `“ssh_pubkey”`. @return [false] if {#ssh_key_id} is `nil`. @return [false] if {#ssh_key_id} does not match. @return [true] if {#ssh_key_id} matches.

# File app/models/mdm/cred.rb, line 135
def ssh_key_matches?(other_cred)
  return false unless other_cred.kind_of? self.class
  return false unless self.ptype == other_cred.ptype
  case self.ptype
    when "ssh_key"
      matches = self.ssh_private_keys
    when "ssh_pubkey"
      matches = self.ssh_public_keys
    else
      return false
  end
  matches.include?(self) and matches.include?(other_cred)
end
ssh_keys() click to toggle source

Returns all keys with matching key ids, including itself.

@return [ActiveRecord::Relation<Mdm::Cred>] ssh_key and ssh_pubkey creds with matching {#ssh_key_id}.

# File app/models/mdm/cred.rb, line 152
def ssh_keys
  (self.ssh_private_keys | self.ssh_public_keys)
end
ssh_private_keys() click to toggle source

Returns all private keys with matching {#ssh_key_id}, including itself.

@return [ActiveRecord::Relation<Mdm::Cred>] ssh_key creds with matching {#ssh_key_id}.

# File app/models/mdm/cred.rb, line 159
def ssh_private_keys
  return [] unless self.ssh_key_id
  matches = Mdm::Cred.where(
      "ptype = ? AND proof ILIKE ?", "ssh_key", "%#{self.ssh_key_id}%"
  ).to_a
  matches.select {|c| c.workspace == self.workspace}
end
ssh_public_keys() click to toggle source

Returns all public keys with matching {#ssh_key_id}, including itself.

@return [ActiveRecord::Relation<Mdm::Cred>] ssh_pubkey creds with matching {#ssh_key_id}.

# File app/models/mdm/cred.rb, line 170
def ssh_public_keys
  return [] unless self.ssh_key_id
  matches = Mdm::Cred.where(
      "ptype = ? AND proof ILIKE ?", "ssh_pubkey", "%#{self.ssh_key_id}%"
  ).to_a
  matches.select {|c| c.workspace == self.workspace}
end
workspace() click to toggle source

Returns its workspace

@return [Mdm::Workspace]

# File app/models/mdm/cred.rb, line 181
def workspace
  self.service.host.workspace
end

Private Instance Methods

decrement_host_counter_cache() click to toggle source

Decrements {Mdm::Host#cred_count}.

@return [void]

# File app/models/mdm/cred.rb, line 190
def decrement_host_counter_cache
  Mdm::Host.decrement_counter("cred_count", self.service.host_id)
end
increment_host_counter_cache() click to toggle source

Increments {Mdm::Host#cred_count}.

@return [void]

# File app/models/mdm/cred.rb, line 197
def increment_host_counter_cache
  Mdm::Host.increment_counter("cred_count", self.service.host_id)
end