Skip to content

Commit d4eba39

Browse files
authored
Merge pull request #20800 from adfoster-r7/add-autocheck-vulnerability-logic
Add autocheck report_vuln logic
2 parents f743b42 + 34ceae4 commit d4eba39

File tree

2 files changed

+120
-8
lines changed

2 files changed

+120
-8
lines changed

lib/msf/core/exploit/remote/auto_check.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,35 @@ def with_prepended_auto_check
4040

4141
warning_msg = 'ForceExploit is enabled, proceeding with exploitation.'
4242
error_msg = '"set ForceExploit true" to override check result.'
43-
4443
check_code = check
44+
4545
case check_code
4646
when Exploit::CheckCode::Vulnerable, Exploit::CheckCode::Appears
4747
print_good(check_code.message)
48+
49+
if respond_to?(:report_vuln)
50+
report_vuln_opts = {
51+
name: fullname,
52+
username: respond_to?(:owner) ? owner : nil,
53+
refs: references,
54+
info: description.strip
55+
}
56+
57+
if respond_to?(:session) && session.respond_to?(:session_host)
58+
report_vuln(
59+
**report_vuln_opts,
60+
host: session.session_host
61+
)
62+
elsif respond_to?(:rhost)
63+
report_vuln(
64+
**report_vuln_opts,
65+
host: rhost,
66+
port: respond_to?(:rport) ? rport : nil,
67+
proto: Msf::DBManager::DEFAULT_SERVICE_PROTO
68+
)
69+
end
70+
end
71+
4872
return yield
4973
when Exploit::CheckCode::Detected
5074
print_warning(check_code.message)

spec/lib/msf/core/exploit/remote/auto_check_spec.rb

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,78 @@
6868
context 'when the check method returns vulnerable' do
6969
let(:check_result) { ::Msf::Exploit::CheckCode::Vulnerable }
7070

71-
before(:each) do
72-
subject.send(opts[:method])
71+
context 'when there is no session or rhost details' do
72+
before(:each) do
73+
subject.send(opts[:method])
74+
end
75+
76+
it "calls the check method" do
77+
expect(subject).to have_received(:check)
78+
end
79+
80+
it "calls the original #{opts[:method]} method" do
81+
expect(subject).to have_received(:"original_#{opts[:method]}_call")
82+
end
7383
end
7484

75-
it "calls the check method" do
76-
expect(subject).to have_received(:check)
85+
context 'when a session is present' do
86+
subject do
87+
mock_module_with_session.new
88+
end
89+
90+
before(:each) do
91+
mock_session = instance_double(Msf::Sessions::Meterpreter_x64_Linux, session_host: '192.0.2.2')
92+
allow(subject).to receive(:session).and_return(mock_session)
93+
allow(subject).to receive(:report_vuln).and_call_original
94+
subject.send(opts[:method])
95+
end
96+
97+
it "calls the check method" do
98+
expect(subject).to have_received(:check)
99+
end
100+
101+
it "calls the original #{opts[:method]} method" do
102+
expect(subject).to have_received(:"original_#{opts[:method]}_call")
103+
end
104+
105+
it "registers the vulnerability" do
106+
expect(subject).to have_received(:report_vuln).with(hash_including(
107+
name: a_kind_of(String),
108+
info: a_kind_of(String),
109+
refs: a_kind_of(Array),
110+
host: '192.0.2.2'
111+
))
112+
end
77113
end
78114

79-
it "calls the original #{opts[:method]} method" do
80-
expect(subject).to have_received(:"original_#{opts[:method]}_call")
115+
context 'when rhost is present' do
116+
subject do
117+
mock_module_with_rhost.new
118+
end
119+
120+
before(:each) do
121+
allow(subject).to receive(:report_vuln).and_call_original
122+
subject.send(opts[:method])
123+
end
124+
125+
it "calls the check method" do
126+
expect(subject).to have_received(:check)
127+
end
128+
129+
it "calls the original #{opts[:method]} method" do
130+
expect(subject).to have_received(:"original_#{opts[:method]}_call")
131+
end
132+
133+
it "registers the vulnerability" do
134+
expect(subject).to have_received(:report_vuln).with(hash_including(
135+
name: a_kind_of(String),
136+
info: a_kind_of(String),
137+
refs: a_kind_of(Array),
138+
host: '192.0.2.2',
139+
port: 8080,
140+
proto: 'tcp'
141+
))
142+
end
81143
end
82144
end
83145

@@ -121,7 +183,7 @@
121183
prepend context_described_class
122184

123185
def check
124-
# mocked
186+
raise 'should be mocked'
125187
end
126188

127189
def run
@@ -139,6 +201,32 @@ def exploit
139201
def original_exploit_call
140202
# Helper for verifying the original exploit function was called
141203
end
204+
205+
def report_vuln(opts)
206+
original_report_vuln(opts)
207+
end
208+
209+
def original_report_vuln(opts)
210+
# Helper for verifying the original exploit function was called
211+
end
212+
end
213+
end
214+
let(:mock_module_with_session) do
215+
Class.new(mock_module_with_prepend_autocheck) do
216+
def session
217+
raise 'should be mocked'
218+
end
219+
end
220+
end
221+
let(:mock_module_with_rhost) do
222+
Class.new(mock_module_with_prepend_autocheck) do
223+
def rhost
224+
'192.0.2.2'
225+
end
226+
227+
def rport
228+
8080
229+
end
142230
end
143231
end
144232
let(:mock_module_with_include_autocheck) do

0 commit comments

Comments
 (0)