Class: Mdm::Cred Deprecated

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/mdm/cred.rb

Overview

Deprecated.

Use metasploit-credential's Metasploit::Credential::Core.

A credential captured from a #service.

Constant Summary collapse

KEY_ID_REGEX =

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

/([0-9a-fA-F:]{47})/
PTYPES =

Maps #ptype_human to #ptype.

{
    'read/write password' => 'password_rw',
    'read-only password' => 'password_ro',
    'SMB hash' => 'smb_hash',
    'SSH private key' => 'ssh_key',
    'SSH public key' => 'ssh_pubkey'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#activefalse, true

Whether the credential is active.

Returns:

  • (false)

    if a captured credential cannot be used to log into #service.

  • (true)

    otherwise



# File 'app/models/mdm/cred.rb', line 49

#created_atDateTime

When this credential was created.

Returns:

  • (DateTime)


# File 'app/models/mdm/cred.rb', line 55

#passString?

Pass of credential.

Returns:

  • (String, nil)


# File 'app/models/mdm/cred.rb', line 60

#proofString

Proof of credential capture.

Returns:

  • (String)


# File 'app/models/mdm/cred.rb', line 65

#ptypeString

Type of #pass.

Returns:

  • (String)


# File 'app/models/mdm/cred.rb', line 70

#source_idInteger?

Id of source of this credential.

Returns:

  • (Integer, nil)


# File 'app/models/mdm/cred.rb', line 75

#source_typeString?

Type of source with #source_id.

Returns:

  • (String, nil)


# File 'app/models/mdm/cred.rb', line 80

#updated_atDateTime

The last time this credential was updated.

Returns:

  • (DateTime)


# File 'app/models/mdm/cred.rb', line 85

#userString?

User name of credential.

Returns:

  • (String, nil)


# File 'app/models/mdm/cred.rb', line 90

Instance Method Details

#ptype_humanString?

Humanized #ptype.

Returns:

  • (String, nil)


109
110
111
112
113
114
115
# 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_idString?

Returns SSH Key ID.

Returns:

  • (String)

    SSH Key Id if ssh-type key and #proof matches KEY_ID_REGEX.

  • (nil)

    otherwise



121
122
123
124
125
# 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) ⇒ false, true

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

Returns:

  • (false)

    if other is not same class as self.

  • (false)

    if #ptype does not match.

  • (false)

    if #ptype is neither "ssh_key" nor "ssh_pubkey".

  • (false)

    if #ssh_key_id is nil.

  • (false)

    if #ssh_key_id does not match.

  • (true)

    if #ssh_key_id matches.



135
136
137
138
139
140
141
142
143
144
145
146
147
# 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_keysActiveRecord::Relation<Mdm::Cred>

Returns all keys with matching key ids, including itself.

Returns:



152
153
154
# File 'app/models/mdm/cred.rb', line 152

def ssh_keys
  (self.ssh_private_keys | self.ssh_public_keys)
end

#ssh_private_keysActiveRecord::Relation<Mdm::Cred>

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

Returns:



159
160
161
162
163
164
165
# 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_keysActiveRecord::Relation<Mdm::Cred>

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

Returns:



170
171
172
173
174
175
176
# 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

#workspaceMdm::Workspace

Returns its workspace

Returns:



181
182
183
# File 'app/models/mdm/cred.rb', line 181

def workspace
  self.service.host.workspace
end