Skip to content
Merged
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
9 changes: 8 additions & 1 deletion lib/metasploit/credential/creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ def create_credential_origin_manual(opts={})
# If there is not a matching `Mdm::Host` it will create it. If there is not a matching
# `Mdm::Service` it will create that too.
#
# @option opts [Mdm::Service] :service The service to use instead of creating one
# @option opts [Fixnum] :service_id The ID of the `Mdm::Service` to link this Origin to
# @option opts [String] :address The address of the `Mdm::Host` to link this Origin to
# @option opts [Fixnum] :port The port number of the `Mdm::Service` to link this Origin to
# @option opts [String] :service_name The service name to use for the `Mdm::Service`
Expand All @@ -423,8 +425,13 @@ def create_credential_origin_manual(opts={})
# @return [Metasploit::Credential::Origin::Service] The created {Metasploit::Credential::Origin::Service} object
def create_credential_origin_service(opts={})
return nil unless active_db?

module_fullname = opts.fetch(:module_fullname)
service_object = create_credential_service(opts)
if (service_id = opts[:service_id] || opts[:service].try(:id))
service_object = Mdm::Service.where(id: service_id).first
else
service_object = create_credential_service(opts)
end
Comment thread
zeroSteiner marked this conversation as resolved.
return nil if service_object.nil?

retry_transaction do
Expand Down
43 changes: 43 additions & 0 deletions spec/lib/metasploit/credential/creation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,49 @@
expect{ test_object.create_credential_origin_service(opts)}.to raise_error KeyError
end
end

context 'when :service is provided' do
it 'uses the given service object and does not create a new Mdm::Service' do
host = FactoryBot.create(:mdm_host, workspace: workspace)
existing_service = FactoryBot.create(:mdm_service, host: host)
opts = {
service: existing_service,
module_fullname: 'auxiliary/scanner/smb/smb_login',
origin_type: :service
}
expect { test_object.create_credential_origin_service(opts) }.to_not change { Mdm::Service.count }
origin = test_object.create_credential_origin_service(opts)
expect(origin.service_id).to eq(existing_service.id)
end
end

context 'when :service_id is provided' do
context 'and the ID corresponds to an existing Mdm::Service' do
it 'uses that service and does not create a new Mdm::Service' do
host = FactoryBot.create(:mdm_host, workspace: workspace)
existing_service = FactoryBot.create(:mdm_service, host: host)
opts = {
service_id: existing_service.id,
module_fullname: 'auxiliary/scanner/smb/smb_login',
origin_type: :service
}
expect { test_object.create_credential_origin_service(opts) }.to_not change { Mdm::Service.count }
origin = test_object.create_credential_origin_service(opts)
expect(origin.service_id).to eq(existing_service.id)
end
end

context 'and the ID does not correspond to an existing Mdm::Service' do
it 'returns nil' do
opts = {
service_id: 0,
module_fullname: 'auxiliary/scanner/smb/smb_login',
origin_type: :service
}
expect(test_object.create_credential_origin_service(opts)).to be_nil
end
end
end
end

context '#create_credential_origin_session' do
Expand Down
Loading