Skip to content

Commit 7b4fd2f

Browse files
committed
Land rapid7#4642, Allow 'creds -u "" ' to return blank usernames
2 parents cb2bef8 + 63c3832 commit 7b4fd2f

File tree

2 files changed

+113
-0
lines changed
  • lib/msf/ui/console/command_dispatcher
  • spec/lib/msf/ui/console/command_dispatcher

2 files changed

+113
-0
lines changed

lib/msf/ui/console/command_dispatcher/db.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,16 @@ def creds_search(*args)
868868
# Exclude creds that don't match the given type
869869
next if type.present? && !core.private.kind_of?(type)
870870

871+
# Exclude non-blank username creds if that's what we're after
872+
if user_regex.present? && user_regex == // && !core.public.username.blank?
873+
next
874+
end
875+
876+
# Exclude non-blank password creds if that's what we're after
877+
if pass_regex.present? && pass_regex == // && !core.private.data.blank?
878+
next
879+
end
880+
871881
# Exclude creds that don't match the given user
872882
if user_regex.present? && !core.public.username.match(user_regex)
873883
next

spec/lib/msf/ui/console/command_dispatcher/db_spec.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,109 @@
6565
it { is_expected.to respond_to :set_rhosts_from_addrs }
6666

6767
describe "#cmd_creds" do
68+
69+
describe "-u" do
70+
let(:username) { "thisuser" }
71+
let(:password) { "thispass" }
72+
let(:nomatch_username) { "thatuser" }
73+
let(:nomatch_password) { "thatpass" }
74+
let(:blank_username) { "" }
75+
let(:blank_password) { "" }
76+
let(:nonblank_username) { "nonblank_user" }
77+
let(:nonblank_password) { "nonblank_pass" }
78+
before(:each) do
79+
priv = FactoryGirl.create(:metasploit_credential_password, data: password)
80+
pub = FactoryGirl.create(:metasploit_credential_username, username: username)
81+
core = FactoryGirl.create(:metasploit_credential_core,
82+
origin: FactoryGirl.create(:metasploit_credential_origin_import),
83+
private: priv,
84+
public: pub,
85+
realm: nil,
86+
workspace: framework.db.workspace)
87+
nonblank_priv = FactoryGirl.create(:metasploit_credential_password, data: nonblank_password)
88+
blank_pub = FactoryGirl.create(:metasploit_credential_blank_username)
89+
core = FactoryGirl.create(:metasploit_credential_core,
90+
origin: FactoryGirl.create(:metasploit_credential_origin_import),
91+
private: nonblank_priv,
92+
public: blank_pub,
93+
realm: nil,
94+
workspace: framework.db.workspace)
95+
nonblank_pub = FactoryGirl.create(:metasploit_credential_username, username: nonblank_username)
96+
blank_priv = FactoryGirl.create(:metasploit_credential_password, data: blank_password)
97+
core = FactoryGirl.create(:metasploit_credential_core,
98+
origin: FactoryGirl.create(:metasploit_credential_origin_import),
99+
private: blank_priv,
100+
public: nonblank_pub,
101+
realm: nil,
102+
workspace: framework.db.workspace)
103+
end
104+
context "when the credential is present" do
105+
it "should show a user that matches the given expression" do
106+
db.cmd_creds("-u", username)
107+
@output.should =~ [
108+
"Credentials",
109+
"===========",
110+
"",
111+
"host service public private realm private_type",
112+
"---- ------- ------ ------- ----- ------------",
113+
" thisuser thispass Password",
114+
]
115+
end
116+
context "and when the username is blank" do
117+
it "should show a user that matches the given expression" do
118+
db.cmd_creds("-u", blank_username )
119+
@output.should =~ [
120+
"Credentials",
121+
"===========",
122+
"",
123+
"host service public private realm private_type",
124+
"---- ------- ------ ------- ----- ------------",
125+
" nonblank_pass Password"
126+
]
127+
end
128+
end
129+
context "and when the password is blank" do
130+
it "should show a user that matches the given expression" do
131+
db.cmd_creds("-P", blank_password )
132+
@output.should =~ [
133+
"Credentials",
134+
"===========",
135+
"",
136+
"host service public private realm private_type",
137+
"---- ------- ------ ------- ----- ------------",
138+
" nonblank_user Password"
139+
]
140+
end
141+
end
142+
end
143+
context "when the credential is absent" do
144+
context "due to a nonmatching username" do
145+
it "should return a blank set" do
146+
db.cmd_creds("-u", nomatch_username)
147+
@output.should =~ [
148+
"===========",
149+
"Credentials",
150+
"",
151+
"---- ------- ------ ------- ----- ------------",
152+
"host service public private realm private_type"
153+
]
154+
end
155+
end
156+
context "due to a nonmatching password" do
157+
it "should return a blank set" do
158+
db.cmd_creds("-P", nomatch_password)
159+
@output.should =~ [
160+
"===========",
161+
"Credentials",
162+
"",
163+
"---- ------- ------ ------- ----- ------------",
164+
"host service public private realm private_type"
165+
]
166+
end
167+
end
168+
end
169+
end
170+
68171
describe "add-password" do
69172
let(:username) { "username" }
70173
let(:password) { "password" }

0 commit comments

Comments
 (0)