From 9c6528f13f611c973e3ec6d1922b24454c8b7274 Mon Sep 17 00:00:00 2001 From: David Maloney Date: Mon, 21 Apr 2014 18:58:23 -0500 Subject: [PATCH] use the CredDetail class now that we have the new CredDetail class, use it instead of hashes --- lib/metasploit/framework/login_scanner/ssh.rb | 38 ++++------ .../framework/login_scanner/ssh_spec.rb | 75 +++++++++++-------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/lib/metasploit/framework/login_scanner/ssh.rb b/lib/metasploit/framework/login_scanner/ssh.rb index 600c326d04..d3cb1a67d7 100644 --- a/lib/metasploit/framework/login_scanner/ssh.rb +++ b/lib/metasploit/framework/login_scanner/ssh.rb @@ -1,5 +1,6 @@ require 'metasploit/framework/login_scanner/invalid' require 'metasploit/framework/login_scanner/result' +require 'metasploit/framework/login_scanner/cred_detail' require 'net/ssh' module Metasploit @@ -14,7 +15,7 @@ module Metasploit # @return [Fixnum] The timeout in seconds for a single SSH connection attr_accessor :connection_timeout # @!attribute cred_details - # @return [Array] An array of hashes containing the cred + # @return [Array] An array of CredDetail objects attr_accessor :cred_details # @!attribute successes # @return [Array] Array of of result objects that failed @@ -66,7 +67,7 @@ module Metasploit validate :host_address_must_be_valid - validate :cred_details_must_be_array_of_hashes + validate :validate_cred_details # @param attributes [Hash{Symbol => String,nil}] def initialize(attributes={}) @@ -138,7 +139,7 @@ module Metasploit def scan! valid! cred_details.each do |credential| - result = attempt_login(credential[:public], credential[:private]) + result = attempt_login(credential.public, credential.private) if result.success? successes << result break if stop_on_success @@ -194,36 +195,23 @@ module Metasploit end end - def cred_details_must_be_array_of_hashes + def validate_cred_details if cred_details.kind_of? Array cred_details.each do |detail| - validate_cred_detail(detail) + unless detail.kind_of? Metasploit::Framework::LoginScanner::CredDetail + errors.add(:cred_details, "has invalid element #{detail.inspect}") + next + end + unless detail.valid? + errors.add(:cred_details, "has invalid element #{detail.inspect}") + end end else errors.add(:cred_details, "must be an array") end end - def validate_cred_detail(detail) - if detail.kind_of? Hash - if detail.has_key? :public - unless detail[:public].kind_of? String - errors.add(:cred_details, "has invalid element, invalid public component #{detail.inspect}") - end - else - errors.add(:cred_details, "has invalid element, missing public component #{detail.inspect}") - end - if detail.has_key? :private - unless detail[:private].kind_of? String - errors.add(:cred_details, "has invalid element, invalid private component #{detail.inspect}") - end - else - errors.add(:cred_details, "has invalid element, missing private component #{detail.inspect}") - end - else - errors.add(:cred_details, "has invalid element #{detail.inspect}") - end - end + end diff --git a/spec/lib/metasploit/framework/login_scanner/ssh_spec.rb b/spec/lib/metasploit/framework/login_scanner/ssh_spec.rb index 779400fb1f..c66ff6af0e 100644 --- a/spec/lib/metasploit/framework/login_scanner/ssh_spec.rb +++ b/spec/lib/metasploit/framework/login_scanner/ssh_spec.rb @@ -4,6 +4,43 @@ require 'metasploit/framework/login_scanner/ssh' describe Metasploit::Framework::LoginScanner::SSH do let(:public) { 'root' } let(:private) { 'toor' } + + let(:pub_blank) { + Metasploit::Framework::LoginScanner::CredDetail.new( + paired: true, + public: public, + private: '' + ) + } + + let(:pub_pub) { + Metasploit::Framework::LoginScanner::CredDetail.new( + paired: true, + public: public, + private: public + ) + } + + let(:pub_pri) { + Metasploit::Framework::LoginScanner::CredDetail.new( + paired: true, + public: public, + private: private + ) + } + + let(:invalid_detail) { + Metasploit::Framework::LoginScanner::CredDetail.new( + paired: true, + public: nil, + private: nil + ) + } + + let(:detail_group) { + [ pub_blank, pub_pub, pub_pri] + } + subject(:ssh_scanner) { described_class.new } @@ -122,43 +159,19 @@ describe Metasploit::Framework::LoginScanner::SSH do expect(ssh_scanner.errors[:cred_details]).to include "must be an array" end - it 'is not valid if any of the elements are not a hash' do + it 'is not valid if any of the elements are not a CredDetail' do ssh_scanner.cred_details = [1,2] expect(ssh_scanner).to_not be_valid expect(ssh_scanner.errors[:cred_details]).to include "has invalid element 1" end - it 'is not valid if any of the elements are missing a public component' do - detail = { private: private} - ssh_scanner.cred_details = [detail] + it 'is not valid if any of the CredDetails are invalid' do + ssh_scanner.cred_details = [pub_blank, invalid_detail] expect(ssh_scanner).to_not be_valid - expect(ssh_scanner.errors[:cred_details]).to include "has invalid element, missing public component #{detail}" end - it 'is not valid if any of the elements have an invalid public component' do - detail = { public: 5, private: private} - ssh_scanner.cred_details = [detail] - expect(ssh_scanner).to_not be_valid - expect(ssh_scanner.errors[:cred_details]).to include "has invalid element, invalid public component #{detail}" - end - - it 'is not valid if any of the elements are missing a public component' do - detail = { public: public} - ssh_scanner.cred_details = [detail] - expect(ssh_scanner).to_not be_valid - expect(ssh_scanner.errors[:cred_details]).to include "has invalid element, missing private component #{detail}" - end - - it 'is not valid if any of the elements have an invalid public component' do - detail = { public: public, private: []} - ssh_scanner.cred_details = [detail] - expect(ssh_scanner).to_not be_valid - expect(ssh_scanner.errors[:cred_details]).to include "has invalid element, invalid private component #{detail}" - end - - it 'is valid if all of the lements are properly formed hashes' do - detail = { public: public, private: private} - ssh_scanner.cred_details = [detail] + it 'is valid if all of the elements are valid' do + ssh_scanner.cred_details = [pub_blank, pub_pub, pub_pri] expect(ssh_scanner.errors[:cred_details]).to be_empty end end @@ -402,7 +415,7 @@ describe Metasploit::Framework::LoginScanner::SSH do ssh_scanner.connection_timeout = 30 ssh_scanner.verbosity = :fatal ssh_scanner.stop_on_success = false - ssh_scanner.cred_details = [ { public: public, private: '' }, { public: public, private: public}, { public: public, private: private} ] + ssh_scanner.cred_details = detail_group end it 'calls valid! before running' do @@ -445,7 +458,7 @@ describe Metasploit::Framework::LoginScanner::SSH do ssh_scanner.connection_timeout = 30 ssh_scanner.verbosity = :fatal ssh_scanner.stop_on_success = true - ssh_scanner.cred_details = [ { public: public, private: '' }, { public: public, private: public}, { public: public, private: private} ] + ssh_scanner.cred_details = detail_group end it 'stops after the first successful login' do