Skip to content

Commit f925793

Browse files
committed
Land rapid7#7894, refactor empty test on CredentialCollection
2 parents be170ab + 0dcf000 commit f925793

File tree

12 files changed

+80
-5
lines changed

12 files changed

+80
-5
lines changed

lib/metasploit/framework/credential_collection.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ def each
205205
pass_fd.close if pass_fd && !pass_fd.closed?
206206
end
207207

208+
# Returns true when #each will have no results to iterate
209+
def empty?
210+
hasUser = username.present? || user_file.present? || !additional_publics.empty?
211+
hasPass = password.present? || pass_file.present? || !additional_privates.empty? || blank_passwords
212+
prepended_creds.empty? && !hasUser || (hasUser && !hasPass)
213+
end
214+
208215
private
209216

210217
def private_type(private)

lib/metasploit/framework/login_scanner/base.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,7 @@ def validate_cred_details
305305
errors.add(:cred_details, "must respond to :each")
306306
end
307307

308-
if cred_details.prepended_creds.empty? &&
309-
cred_details.additional_publics.empty? &&
310-
cred_details.additional_privates.empty? &&
311-
!cred_details.username.present? &&
312-
!cred_details.password.present?
308+
if cred_details.empty?
313309
errors.add(:cred_details, "can't be blank")
314310
end
315311
end

spec/lib/metasploit/framework/credential_collection_spec.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
user_file: user_file,
1313
username: username,
1414
userpass_file: userpass_file,
15+
prepended_creds: prepended_creds,
16+
additional_privates: additional_privates,
17+
additional_publics: additional_publics
1518
)
1619
end
1720

@@ -22,6 +25,9 @@
2225
let(:pass_file) { nil }
2326
let(:user_as_pass) { nil }
2427
let(:userpass_file) { nil }
28+
let(:prepended_creds) { [] }
29+
let(:additional_privates) { [] }
30+
let(:additional_publics) { [] }
2531

2632
describe "#each" do
2733
specify do
@@ -134,6 +140,61 @@
134140

135141
end
136142

143+
describe "#empty?" do
144+
context "when :username is set" do
145+
context "and :password is set" do
146+
specify do
147+
expect(collection.empty?).to eq false
148+
end
149+
end
150+
151+
context "and :password is not set" do
152+
let(:password) { nil }
153+
specify do
154+
expect(collection.empty?).to eq true
155+
end
156+
157+
context "and :blank_passwords is true" do
158+
let(:blank_passwords) { true }
159+
specify do
160+
expect(collection.empty?).to eq false
161+
end
162+
end
163+
end
164+
end
165+
166+
context "when :username is not set" do
167+
context "and :password is not set" do
168+
let(:username) { nil }
169+
let(:password) { nil }
170+
specify do
171+
expect(collection.empty?).to eq true
172+
end
173+
174+
context "and :prepended_creds is not empty" do
175+
let(:prepended_creds) { [ "test" ] }
176+
specify do
177+
expect(collection.empty?).to eq false
178+
end
179+
end
180+
181+
context "and :additional_privates is not empty" do
182+
let(:additional_privates) { [ "test_private" ] }
183+
specify do
184+
expect(collection.empty?).to eq true
185+
end
186+
end
187+
188+
context "and :additional_publics is not empty" do
189+
let(:additional_publics) { [ "test_public" ] }
190+
specify do
191+
expect(collection.empty?).to eq true
192+
end
193+
end
194+
end
195+
end
196+
end
197+
137198
describe "#prepend_cred" do
138199
specify do
139200
prep = Metasploit::Framework::Credential.new(public: "foo", private: "bar")

spec/lib/metasploit/framework/login_scanner/base_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def self.model_name
2424
allow(creds).to receive(:additional_publics).and_return(['user'])
2525
allow(creds).to receive(:each).and_return(['user', 'pass'])
2626
allow(creds).to receive(:additional_publics).and_return([])
27+
allow(creds).to receive(:empty?).and_return(false)
2728
creds
2829
}
2930

spec/lib/metasploit/framework/login_scanner/ftp_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
allow(creds).to receive(:prepended_creds).and_return([])
6060
allow(creds).to receive(:additional_privates).and_return([])
6161
allow(creds).to receive(:additional_publics).and_return([])
62+
allow(creds).to receive(:empty?).and_return(true)
6263
ftp_scanner.cred_details = creds
6364
end
6465

spec/lib/metasploit/framework/login_scanner/mssql_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
allow(creds).to receive(:prepended_creds).and_return([])
5050
allow(creds).to receive(:additional_privates).and_return([])
5151
allow(creds).to receive(:additional_publics).and_return([])
52+
allow(creds).to receive(:empty?).and_return(true)
5253
login_scanner.cred_details = creds
5354
end
5455

spec/lib/metasploit/framework/login_scanner/smb_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
allow(creds).to receive(:prepended_creds).and_return([])
5858
allow(creds).to receive(:additional_privates).and_return([])
5959
allow(creds).to receive(:additional_publics).and_return([])
60+
allow(creds).to receive(:empty?).and_return(true)
6061
login_scanner.cred_details = creds
6162
end
6263

spec/lib/metasploit/framework/login_scanner/ssh_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
allow(creds).to receive(:prepended_creds).and_return([])
7171
allow(creds).to receive(:additional_privates).and_return([])
7272
allow(creds).to receive(:additional_publics).and_return([])
73+
allow(creds).to receive(:empty?).and_return(true)
7374
ssh_scanner.cred_details = creds
7475
end
7576

spec/lib/metasploit/framework/login_scanner/telnet_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
allow(creds).to receive(:prepended_creds).and_return([])
2323
allow(creds).to receive(:additional_privates).and_return([])
2424
allow(creds).to receive(:additional_publics).and_return([])
25+
allow(creds).to receive(:empty?).and_return(true)
2526
login_scanner.cred_details = creds
2627
end
2728

spec/support/shared/examples/metasploit/framework/login_scanner/login_scanner_base.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
allow(creds).to receive(:prepended_creds).and_return([])
7676
allow(creds).to receive(:additional_privates).and_return([])
7777
allow(creds).to receive(:additional_publics).and_return(['user'])
78+
allow(creds).to receive(:empty?).and_return(true)
7879
login_scanner.cred_details = creds
7980
end
8081

@@ -182,6 +183,7 @@
182183
allow(creds).to receive(:prepended_creds).and_return([])
183184
allow(creds).to receive(:additional_privates).and_return([])
184185
allow(creds).to receive(:additional_publics).and_return([])
186+
allow(creds).to receive(:empty?).and_return(true)
185187
login_scanner.cred_details = creds
186188
expect(login_scanner).to_not be_valid
187189
expect(login_scanner.errors[:cred_details]).to include "can't be blank"
@@ -198,6 +200,7 @@
198200
allow(creds).to receive(:prepended_creds).and_return([])
199201
allow(creds).to receive(:additional_privates).and_return([])
200202
allow(creds).to receive(:additional_publics).and_return(['user'])
203+
allow(creds).to receive(:empty?).and_return(true)
201204
login_scanner.cred_details = creds
202205
expect(login_scanner).to_not be_valid
203206
expect(login_scanner.errors[:cred_details]).to include "must respond to :each"

0 commit comments

Comments
 (0)