Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/lib/metasploit/framework/credential_collection_spec.rb
Views: 11784
require 'spec_helper'1require 'metasploit/framework/credential_collection'23RSpec.describe Metasploit::Framework::CredentialCollection do45subject(:collection) do6described_class.new(7nil_passwords: nil_passwords,8blank_passwords: blank_passwords,9pass_file: pass_file,10password: password,11user_as_pass: user_as_pass,12user_file: user_file,13username: username,14userpass_file: userpass_file,15prepended_creds: prepended_creds,16additional_privates: additional_privates,17additional_publics: additional_publics,18password_spray: password_spray19)20end2122before(:each) do23# The test suite overrides File.open(...) calls; fall back to the normal behavior for any File.open calls that aren't explicitly mocked24allow(File).to receive(:open).with(anything).and_call_original25allow(File).to receive(:open).with(anything, anything).and_call_original26allow(File).to receive(:open).with(anything, anything, anything).and_call_original27end2829let(:nil_passwords) { nil }30let(:blank_passwords) { nil }31let(:username) { "user" }32let(:password) { "pass" }33let(:user_file) { nil }34let(:pass_file) { nil }35let(:user_as_pass) { nil }36let(:userpass_file) { nil }37let(:prepended_creds) { [] }38let(:additional_privates) { [] }39let(:additional_publics) { [] }40let(:password_spray) { false }4142describe "#each" do43specify do44expect { |b| collection.each(&b) }.to yield_with_args(Metasploit::Framework::Credential)45end4647context "when given a user_file and password" do48let(:username) { nil }49let(:user_file) do50filename = "foo"51stub_file = StringIO.new("asdf\njkl\n")52allow(File).to receive(:open).with(filename,/^r/).and_yield stub_file5354filename55end5657specify do58expect { |b| collection.each(&b) }.to yield_successive_args(59Metasploit::Framework::Credential.new(public: "asdf", private: password),60Metasploit::Framework::Credential.new(public: "jkl", private: password),61)62end63end6465context "when given a pass_file and username" do66let(:password) { nil }67let(:pass_file) do68filename = "foo"69stub_file = StringIO.new("asdf\njkl\n")70allow(File).to receive(:open).with(filename,/^r/).and_return stub_file7172filename73end7475specify do76expect { |b| collection.each(&b) }.to yield_successive_args(77Metasploit::Framework::Credential.new(public: username, private: "asdf"),78Metasploit::Framework::Credential.new(public: username, private: "jkl"),79)80end81end8283context "when given a userspass_file" do84let(:username) { nil }85let(:password) { nil }86let(:userpass_file) do87filename = "foo"88stub_file = StringIO.new("asdf jkl\nfoo bar\n")89allow(File).to receive(:open).with(filename,/^r/).and_yield stub_file9091filename92end9394specify do95expect { |b| collection.each(&b) }.to yield_successive_args(96Metasploit::Framework::Credential.new(public: "asdf", private: "jkl"),97Metasploit::Framework::Credential.new(public: "foo", private: "bar"),98)99end100end101102context "when given a pass_file and user_file" do103let(:password) { nil }104let(:username) { nil }105let(:user_file) do106filename = "user_file"107stub_file = StringIO.new("asdf\njkl\n")108allow(File).to receive(:open).with(filename,/^r/).and_yield stub_file109110filename111end112let(:pass_file) do113filename = "pass_file"114stub_file = StringIO.new("asdf\njkl\n")115allow(File).to receive(:open).with(filename,/^r/).and_return stub_file116117filename118end119120specify do121expect { |b| collection.each(&b) }.to yield_successive_args(122Metasploit::Framework::Credential.new(public: "asdf", private: "asdf"),123Metasploit::Framework::Credential.new(public: "asdf", private: "jkl"),124Metasploit::Framework::Credential.new(public: "jkl", private: "asdf"),125Metasploit::Framework::Credential.new(public: "jkl", private: "jkl"),126)127end128end129130context "when given a pass_file and user_file and password spray" do131let(:password) { nil }132let(:username) { nil }133let(:password_spray) { true }134let(:pass_file) do135filename = "pass_file"136stub_file = StringIO.new("password1\npassword2\n")137allow(File).to receive(:open).with(filename,/^r/).and_yield stub_file138139filename140end141let(:user_file) do142filename = "user_file"143stub_file = StringIO.new("user1\nuser2\nuser3\n")144allow(File).to receive(:open).with(filename,/^r/).and_return stub_file145146filename147end148149specify do150expect { |b| collection.each(&b) }.to yield_successive_args(151Metasploit::Framework::Credential.new(public: "user1", private: "password1"),152Metasploit::Framework::Credential.new(public: "user2", private: "password1"),153Metasploit::Framework::Credential.new(public: "user3", private: "password1"),154Metasploit::Framework::Credential.new(public: "user1", private: "password2"),155Metasploit::Framework::Credential.new(public: "user2", private: "password2"),156Metasploit::Framework::Credential.new(public: "user3", private: "password2"),157)158end159160context 'when :user_as_pass is true' do161let(:user_as_pass) { true }162163specify do164expect { |b| collection.each(&b) }.to yield_successive_args(165Metasploit::Framework::Credential.new(public: "user1", private: "user1"),166Metasploit::Framework::Credential.new(public: "user2", private: "user2"),167Metasploit::Framework::Credential.new(public: "user3", private: "user3"),168Metasploit::Framework::Credential.new(public: "user1", private: "password1"),169Metasploit::Framework::Credential.new(public: "user2", private: "password1"),170Metasploit::Framework::Credential.new(public: "user3", private: "password1"),171Metasploit::Framework::Credential.new(public: "user1", private: "password2"),172Metasploit::Framework::Credential.new(public: "user2", private: "password2"),173Metasploit::Framework::Credential.new(public: "user3", private: "password2"),174)175end176end177end178179context 'when given a username and password' do180let(:password) { 'password' }181let(:username) { 'root' }182183specify do184expected = [185Metasploit::Framework::Credential.new(public: 'root', private: 'password'),186]187expect { |b| collection.each(&b) }.to yield_successive_args(*expected)188end189end190191context 'when given a pass_file, user_file, password spray and a default username' do192let(:password) { nil }193let(:username) { 'root' }194let(:password_spray) { true }195let(:pass_file) do196filename = "pass_file"197stub_file = StringIO.new("password1\npassword2\n")198allow(File).to receive(:open).with(filename,/^r/).and_yield stub_file199200filename201end202let(:user_file) do203filename = "user_file"204stub_file = StringIO.new("user1\nuser2\nuser3\n")205allow(File).to receive(:open).with(filename,/^r/).and_return stub_file206207filename208end209210specify do211expected = [212Metasploit::Framework::Credential.new(public: "root", private: "password1"),213Metasploit::Framework::Credential.new(public: "user1", private: "password1"),214Metasploit::Framework::Credential.new(public: "user2", private: "password1"),215Metasploit::Framework::Credential.new(public: "user3", private: "password1"),216Metasploit::Framework::Credential.new(public: "root", private: "password2"),217Metasploit::Framework::Credential.new(public: "user1", private: "password2"),218Metasploit::Framework::Credential.new(public: "user2", private: "password2"),219Metasploit::Framework::Credential.new(public: "user3", private: "password2"),220]221expect { |b| collection.each(&b) }.to yield_successive_args(*expected)222end223end224225context 'when given a pass_file, user_file, password spray and additional privates' do226let(:password) { nil }227let(:username) { 'root' }228let(:password_spray) { true }229let(:additional_privates) { ['foo'] }230let(:pass_file) do231filename = "pass_file"232stub_file = StringIO.new("password1\npassword2\n")233allow(File).to receive(:open).with(filename,/^r/).and_yield stub_file234235filename236end237let(:user_file) do238filename = "user_file"239stub_file = StringIO.new("user1\nuser2\nuser3\n")240allow(File).to receive(:open).with(filename,/^r/).and_return stub_file241242filename243end244245specify do246expected = [247Metasploit::Framework::Credential.new(public: "root", private: "password1"),248Metasploit::Framework::Credential.new(public: "user1", private: "password1"),249Metasploit::Framework::Credential.new(public: "user2", private: "password1"),250Metasploit::Framework::Credential.new(public: "user3", private: "password1"),251Metasploit::Framework::Credential.new(public: "root", private: "password2"),252Metasploit::Framework::Credential.new(public: "user1", private: "password2"),253Metasploit::Framework::Credential.new(public: "user2", private: "password2"),254Metasploit::Framework::Credential.new(public: "user3", private: "password2"),255Metasploit::Framework::Credential.new(public: "root", private: "foo"),256Metasploit::Framework::Credential.new(public: "user1", private: "foo"),257Metasploit::Framework::Credential.new(public: "user2", private: "foo"),258Metasploit::Framework::Credential.new(public: "user3", private: "foo"),259]260expect { |b| collection.each(&b) }.to yield_successive_args(*expected)261end262end263264context 'when given a username, user_file and pass_file' do265let(:password) { nil }266let(:username) { 'my_username' }267let(:user_file) do268filename = "user_file"269stub_file = StringIO.new("asdf\njkl\n")270allow(File).to receive(:open).with(filename, /^r/).and_yield stub_file271272filename273end274275let(:pass_file) do276filename = "pass_file"277stub_file = StringIO.new("asdf\njkl\n")278allow(File).to receive(:open).with(filename, /^r/).and_return stub_file279280filename281end282283it do284expect { |b| collection.each(&b) }.to yield_successive_args(285Metasploit::Framework::Credential.new(public: "my_username", private: "asdf"),286Metasploit::Framework::Credential.new(public: "my_username", private: "jkl"),287Metasploit::Framework::Credential.new(public: "asdf", private: "asdf"),288Metasploit::Framework::Credential.new(public: "asdf", private: "jkl"),289Metasploit::Framework::Credential.new(public: "jkl", private: "asdf"),290Metasploit::Framework::Credential.new(public: "jkl", private: "jkl")291)292end293end294295context "when :user_as_pass is true" do296let(:user_as_pass) { true }297specify do298expect { |b| collection.each(&b) }.to yield_successive_args(299Metasploit::Framework::Credential.new(public: username, private: password),300Metasploit::Framework::Credential.new(public: username, private: username),301)302end303end304305context "when :nil_passwords is true" do306let(:nil_passwords) { true }307specify do308expect { |b| collection.each(&b) }.to yield_successive_args(309Metasploit::Framework::Credential.new(public: username, private: nil),310Metasploit::Framework::Credential.new(public: username, private: password),311)312end313end314315context "when :blank_passwords is true" do316let(:blank_passwords) { true }317specify do318expect { |b| collection.each(&b) }.to yield_successive_args(319Metasploit::Framework::Credential.new(public: username, private: password),320Metasploit::Framework::Credential.new(public: username, private: ""),321)322end323end324325end326327describe "#empty?" do328context "when only :userpass_file is set" do329let(:username) { nil }330let(:password) { nil }331let(:userpass_file) { "test_file" }332specify do333expect(collection.empty?).to eq false334end335end336337context "when :username is set" do338context "and :password is set" do339specify do340expect(collection.empty?).to eq false341end342end343344context "and :password is not set" do345let(:password) { nil }346specify do347expect(collection.empty?).to eq true348end349350context "and :nil_passwords is true" do351let(:nil_passwords) { true }352specify do353expect(collection.empty?).to eq false354end355end356357context "and :blank_passwords is true" do358let(:blank_passwords) { true }359specify do360expect(collection.empty?).to eq false361end362end363end364end365366context "when :username is not set" do367context "and :password is not set" do368let(:username) { nil }369let(:password) { nil }370specify do371expect(collection.empty?).to eq true372end373374context "and :prepended_creds is not empty" do375let(:prepended_creds) { [ "test" ] }376specify do377expect(collection.empty?).to eq false378end379end380381context "and :additional_privates is not empty" do382let(:additional_privates) { [ "test_private" ] }383specify do384expect(collection.empty?).to eq true385end386end387388context "and :additional_publics is not empty" do389let(:additional_publics) { [ "test_public" ] }390specify do391expect(collection.empty?).to eq true392end393end394end395end396end397398describe "#prepend_cred" do399specify do400prep = Metasploit::Framework::Credential.new(public: "foo", private: "bar")401collection.prepend_cred(prep)402expect { |b| collection.each(&b) }.to yield_successive_args(403prep,404Metasploit::Framework::Credential.new(public: username, private: password),405)406end407end408409end410411412