Skip to content

Commit 8c0d5d6

Browse files
committed
Add spec file
1 parent 7810f3d commit 8c0d5d6

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

lib/metasploit/framework/login_scanner/nessus.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def check_setup
3030
#
3131
# @param username [String] The username to try
3232
# @param password [String] The password to try
33-
# @return [String]
33+
# @return [Hash]
3434
# * :status [Metasploit::Model::Login::Status]
3535
# * :proof [String] the HTTP response body
3636
def get_login_state(username, password)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
require 'spec_helper'
2+
require 'metasploit/framework/login_scanner/nessus'
3+
4+
describe Metasploit::Framework::LoginScanner::Nessus do
5+
6+
it_behaves_like 'Metasploit::Framework::LoginScanner::Base', has_realm_key: true, has_default_realm: false
7+
it_behaves_like 'Metasploit::Framework::LoginScanner::RexSocket'
8+
9+
let(:username) do
10+
'username'
11+
end
12+
13+
let(:good_password) do
14+
'good_password'
15+
end
16+
17+
let(:bad_password) do
18+
'bad_password'
19+
end
20+
21+
let(:successful_auth_response) do
22+
Rex::Proto::Http::Response.new(200, 'OK')
23+
end
24+
25+
let(:fail_auth_response) do
26+
Rex::Proto::Http::Response.new(401, 'Unauthorized')
27+
end
28+
29+
subject do
30+
described_class.new
31+
end
32+
33+
let(:response) do
34+
Rex::Proto::Http::Response.new(200, 'OK')
35+
end
36+
37+
before(:each) do
38+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:request_cgi).with(any_args)
39+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:send_recv).with(any_args).and_return(response)
40+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:set_config).with(any_args)
41+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:close)
42+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:connect)
43+
end
44+
45+
describe '#check_setup' do
46+
let(:msp_html_response) do
47+
res = Rex::Proto::Http::Response.new(200, 'OK')
48+
res.body = 'Nessus'
49+
res
50+
end
51+
52+
context 'when target is Nessus' do
53+
let(:response) { msp_html_response }
54+
it 'returns true' do
55+
expect(subject.check_setup).to be_truthy
56+
end
57+
end
58+
59+
context 'when target is not Nessus' do
60+
it 'returns false' do
61+
expect(subject.check_setup).to be_falsey
62+
end
63+
end
64+
end
65+
66+
describe '#get_login_state' do
67+
it 'sends a login request to /session' do
68+
expect(http_scanner).to receive(:send_request).with(hash_including('uri'=>'/session'))
69+
end
70+
71+
it 'sends a login request containing the username and password' do
72+
expect(http_scanner).to receive(:send_request).with(hash_including('data'=>"username=#{username}&password=#{password}"))
73+
end
74+
75+
context 'when the credential is valid' do
76+
let(:response) { successful_auth_response }
77+
it 'returns a hash indicating a successful login' do
78+
successful_status = Metasploit::Model::Login::Status::SUCCESSFUL
79+
expect(subject.get_login_state(username, good_password)[:status]).to eq(successful_status)
80+
end
81+
end
82+
83+
context 'when the creential is invalid' do
84+
let(:response) { fail_auth_response }
85+
it 'returns a hash indicating an incorrect cred' do
86+
incorrect_status = Metasploit::Model::Login::Status::INCORRECT
87+
expect(subject.get_login_state(username, good_password)[:status]).to eq(incorrect_status)
88+
end
89+
end
90+
end
91+
92+
describe '#attempt_login' do
93+
context 'when the credential is valid' do
94+
let(:response) { successful_auth_response }
95+
96+
it 'returns a Result object indicating a successful login' do
97+
cred_obj = Metasploit::Framework::Credential.new(public: username, private: good_password)
98+
result = subject.attempt_login(cred_obj)
99+
expect(result).to be_kind_of(::Metasploit::Framework::LoginScanner::Result)
100+
expect(result.status).to eq(Metasploit::Model::Login::Status::SUCCESSFUL)
101+
end
102+
end
103+
104+
context 'when the credential is invalid' do
105+
let(:response) { fail_auth_response }
106+
it 'returns a Result object indicating an incorrect cred' do
107+
cred_obj = Metasploit::Framework::Credential.new(public: username, private: bad_password)
108+
result = subject.attempt_login(cred_obj)
109+
expect(result).to be_kind_of(::Metasploit::Framework::LoginScanner::Result)
110+
expect(result.status).to eq(Metasploit::Model::Login::Status::INCORRECT)
111+
end
112+
end
113+
end
114+
end

0 commit comments

Comments
 (0)