Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CA and ADCS Template metadata to Pkcs12 #183

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ source "https://rubygems.org"
# development dependencies will be added by default to the :development group.
gemspec

gem 'metasploit-model', git: 'https://github.com/cdelafuente-r7/metasploit-model', branch: 'feat/model/search/operation/jsonb'

group :development do
# Entity-Relationship diagrams for developers that need to access database using SQL directly.
gem 'rails-erd'
Expand Down
53 changes: 51 additions & 2 deletions app/models/metasploit/credential/pkcs12.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# A private Pkcs12 file.
class Metasploit::Credential::Pkcs12 < Metasploit::Credential::Private

#
# Attributes
#
Expand All @@ -12,6 +13,14 @@ class Metasploit::Credential::Pkcs12 < Metasploit::Credential::Private
#
# @return [String]

# @!attribute metadata
# Metadata for this Pkcs12:
# adcs_ca: The Certificate Authority that issued the certificate
# adcs_template: The certificate template used to issue the certificate
# pkcs12_password: The password to decrypt the Pkcs12
#
# @return [JSONB]

#
#
# Validations
Expand All @@ -24,15 +33,49 @@ class Metasploit::Credential::Pkcs12 < Metasploit::Credential::Private

validates :data,
presence: true

#
# Method Validations
#

validate :readable

#
# Class methods
#

#
# Instance Methods
#
#

# The CA that issued the certificate
#
# @return [String]
def adcs_ca
metadata['adcs_ca']
end

# The certificate template used to issue the certificate
#
# @return [String]
def adcs_template
metadata['adcs_template']
end

# The password to decrypt the Pkcs12
#
# @return [String]
def pkcs12_password
metadata['pkcs12_password']
end

# The status if the certificate (active or inactive)
#
# @return [String]
def status
metadata['status']
end

# Converts the private pkcs12 data in {#data} to an `OpenSSL::PKCS12` instance.
#
Expand All @@ -41,7 +84,7 @@ class Metasploit::Credential::Pkcs12 < Metasploit::Credential::Private
def openssl_pkcs12
if data
begin
password = ''
password = metadata.fetch('pkcs12_password', '')
OpenSSL::PKCS12.new(Base64.strict_decode64(data), password)
rescue OpenSSL::PKCS12::PKCS12Error => error
raise ArgumentError.new(error)
Expand All @@ -50,7 +93,7 @@ def openssl_pkcs12
end

# The {#data key data}'s fingerprint, suitable for displaying to the
# user.
# user. The Pkcs12 password is voluntarily not included.
#
# @return [String]
def to_s
Expand All @@ -60,9 +103,12 @@ def to_s
result = []
result << "subject:#{cert.subject.to_s}"
result << "issuer:#{cert.issuer.to_s}"
result << "ADCS CA:#{metadata['adcs_ca']}" if metadata['adcs_ca']
result << "ADCS template:#{metadata['adcs_template']}" if metadata['adcs_template']
result.join(',')
end


private

#
Expand All @@ -80,5 +126,8 @@ def readable
end
end


public

Metasploit::Concern.run(self)
end
8 changes: 8 additions & 0 deletions app/models/metasploit/credential/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class Metasploit::Credential::Private < ApplicationRecord
#
# @return [DateTime]

# @!attribute metadata
# Metadata related to the private data. The data contained in this JSONB structure varies based on the subclass.
#
# @return [JSONB]

#
#
# Search
Expand All @@ -63,6 +68,9 @@ class Metasploit::Credential::Private < ApplicationRecord
search_attribute :data,
type: :string

search_attribute :metadata,
type: :jsonb

#
# Search Withs
#
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ en:
metasploit/credential/pkcs12:
attributes:
data:
format: "is not a Base64 encoded pkcs12 file without a password"
format: "is not a serialized data containing Base64 encoded pkcs12 file without a password and metadata"
metasploit/credential/ssh_key:
attributes:
data:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMetadataToMetasploitCredentialPrivates < ActiveRecord::Migration[7.0]
def change
add_column :metasploit_credential_privates, :metadata, :jsonb, null: false, default: {}
end
end
2 changes: 1 addition & 1 deletion lib/metasploit/credential/creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def create_credential_private(opts={})
when :ssh_key
private_object = Metasploit::Credential::SSHKey.where(data: private_data).first_or_create
when :pkcs12
private_object = Metasploit::Credential::Pkcs12.where(data: private_data).first_or_create
private_object = Metasploit::Credential::Pkcs12.where(data: private_data, metadata: opts.fetch(:private_metadata, {})).first_or_create
when :krb_enc_key
private_object = Metasploit::Credential::KrbEncKey.where(data: private_data).first_or_create
when :ntlm_hash
Expand Down
Loading