@@ -145,7 +145,7 @@ def cleanup
145145 mod
146146 end
147147
148- let ( :aux_mod_with_option_validation ) do
148+ let ( :aux_mod_with_rhost_option_validation ) do
149149 mod_klass = Class . new ( Msf ::Auxiliary ) do
150150 def initialize ( info = { } )
151151 super (
@@ -157,6 +157,53 @@ def initialize(info = {})
157157
158158 register_options (
159159 [
160+ Msf ::Opt ::RHOST ,
161+ Msf ::OptString . new ( 'USERNAME' , [ true , 'Set me to be greeted' ] ) ,
162+ Msf ::OptString . new ( 'PASSWORD' , [ false , 'Secret value' ] )
163+ ]
164+ )
165+ end
166+
167+ def validate
168+ super
169+
170+ if datastore [ 'PASSWORD' ] != 'PleaseThrowTheBall'
171+ raise Msf ::OptionValidateError . new ( { 'PASSWORD' => 'Nuh uh uh, you didn\'t say the magic word.' } )
172+ end
173+ end
174+
175+ def check
176+ print_status ( 'Check completed!' )
177+ end
178+
179+ def run
180+ print ( "Hello #{ datastore [ 'USERNAME' ] } " )
181+ print_status ( 'Run completed!' )
182+ end
183+ end
184+
185+ mod = mod_klass . new
186+ datastore = Msf ::ModuleDataStore . new ( mod )
187+ allow ( mod ) . to receive ( :framework ) . and_return ( framework )
188+ mod . send ( :datastore= , datastore )
189+ datastore . import_options ( mod . options )
190+ Msf ::Simple ::Framework . simplify_module ( mod )
191+ mod
192+ end
193+
194+ let ( :aux_mod_with_file_format_option_validation ) do
195+ mod_klass = Class . new ( Msf ::Auxiliary ) do
196+ def initialize ( info = { } )
197+ super (
198+ 'Name' => 'mock file format module' ,
199+ 'Description' => 'mock file format module' ,
200+ 'Author' => [ 'Unknown' ] ,
201+ 'License' => MSF_LICENSE
202+ )
203+
204+ register_options (
205+ [
206+ Msf ::OptString . new ( 'FILENAME' , [ false , 'The file name' , 'foo.zip' ] ) ,
160207 Msf ::OptString . new ( 'USERNAME' , [ true , 'Set me to be greeted' ] ) ,
161208 Msf ::OptString . new ( 'PASSWORD' , [ false , 'Secret value' ] )
162209 ]
@@ -431,8 +478,46 @@ def run
431478 end
432479 end
433480
434- context 'when running an auxiliary module with option validation' do
435- let ( :current_mod ) { aux_mod_with_option_validation }
481+ context 'when running an auxiliary module with rhost option validation' do
482+ let ( :current_mod ) { aux_mod_with_rhost_option_validation }
483+
484+ it 'reports options that fail validation' do
485+ allow ( current_mod ) . to receive ( :check ) . and_call_original
486+ allow ( current_mod ) . to receive ( :validate ) . and_call_original
487+ current_mod . datastore [ 'RHOSTS' ] = '192.0.2.1'
488+ current_mod . datastore [ 'USERNAME' ] = 'Jackson'
489+ current_mod . datastore [ 'PASSWORD' ] = 'ThrowTheBall'
490+ subject . cmd_check
491+ expected_output = [
492+ 'Msf::OptionValidateError The following options failed to validate:' ,
493+ 'Invalid option PASSWORD: Nuh uh uh, you didn\'t say the magic word.'
494+ ]
495+
496+ expect ( @combined_output ) . to match_array ( expected_output )
497+ expect ( subject . mod ) . not_to have_received ( :check )
498+ expect ( subject . mod ) . to have_received ( :validate ) . at_least ( :once )
499+ end
500+
501+ it 'runs when validation passes' do
502+ allow ( current_mod ) . to receive ( :check ) . and_call_original
503+ allow ( current_mod ) . to receive ( :validate ) . and_call_original
504+ current_mod . datastore [ 'RHOSTS' ] = '192.0.2.1'
505+ current_mod . datastore [ 'USERNAME' ] = 'Jackson'
506+ current_mod . datastore [ 'PASSWORD' ] = 'PleaseThrowTheBall'
507+ subject . cmd_check
508+ expected_output = [
509+ 'Check completed!' ,
510+ '192.0.2.1 - Check failed: The state could not be determined.'
511+ ]
512+
513+ expect ( @combined_output ) . to match_array ( expected_output )
514+ expect ( subject . mod ) . to have_received ( :check )
515+ expect ( subject . mod ) . to have_received ( :validate ) . at_least ( :once )
516+ end
517+ end
518+
519+ context 'when running an auxiliary module with file format option validation' do
520+ let ( :current_mod ) { aux_mod_with_file_format_option_validation }
436521
437522 it 'reports options that fail validation' do
438523 allow ( current_mod ) . to receive ( :check ) . and_call_original
@@ -804,8 +889,8 @@ def run
804889 end
805890 end
806891
807- context 'when running an auxiliary module with option validation' do
808- let ( :current_mod ) { aux_mod_with_option_validation }
892+ context 'when running an auxiliary rhost module with option validation' do
893+ let ( :current_mod ) { aux_mod_with_rhost_option_validation }
809894
810895 it 'reports options that fail validation' do
811896 allow ( current_mod ) . to receive ( :run ) . and_call_original
@@ -843,6 +928,45 @@ def run
843928 expect ( subject . mod ) . to have_received ( :validate ) . at_least ( :once )
844929 end
845930 end
931+
932+ context 'when running an auxiliary file format module with option validation' do
933+ let ( :current_mod ) { aux_mod_with_file_format_option_validation }
934+
935+ it 'reports options that fail validation' do
936+ allow ( current_mod ) . to receive ( :run ) . and_call_original
937+ allow ( current_mod ) . to receive ( :validate ) . and_call_original
938+ current_mod . datastore [ 'RHOSTS' ] = '192.0.2.1'
939+ current_mod . datastore [ 'USERNAME' ] = 'Jackson'
940+ current_mod . datastore [ 'PASSWORD' ] = 'ThrowTheBall'
941+ subject . cmd_run
942+ expected_output = [
943+ 'Msf::OptionValidateError The following options failed to validate:' ,
944+ 'Invalid option PASSWORD: Nuh uh uh, you didn\'t say the magic word.'
945+ ]
946+
947+ expect ( @combined_output ) . to match_array ( expected_output )
948+ expect ( subject . mod ) . not_to have_received ( :run )
949+ expect ( subject . mod ) . to have_received ( :validate ) . at_least ( :once )
950+ end
951+
952+ it 'runs when validation passes' do
953+ allow ( current_mod ) . to receive ( :run ) . and_call_original
954+ allow ( current_mod ) . to receive ( :validate ) . and_call_original
955+ current_mod . datastore [ 'RHOSTS' ] = '192.0.2.1'
956+ current_mod . datastore [ 'USERNAME' ] = 'Jackson'
957+ current_mod . datastore [ 'PASSWORD' ] = 'PleaseThrowTheBall'
958+ subject . cmd_run
959+ expected_output = [
960+ 'Auxiliary module execution completed' ,
961+ 'Hello Jackson' ,
962+ 'Run completed!' ,
963+ ]
964+
965+ expect ( @combined_output ) . to match_array ( expected_output )
966+ expect ( subject . mod ) . to have_received ( :run )
967+ expect ( subject . mod ) . to have_received ( :validate ) . at_least ( :once )
968+ end
969+ end
846970 end
847971
848972 describe '#cmd_rerun' do
0 commit comments