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
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