Skip to content

Commit 26a145e

Browse files
committed
Always overwrite the old module even when ambiguous
1 parent 3a57262 commit 26a145e

File tree

3 files changed

+64
-139
lines changed

3 files changed

+64
-139
lines changed

lib/msf/core/module_set.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ def add_module(mod, name, modinfo = nil)
219219

220220
# TODO this isn't terribly helpful since the refnames will always match, that's why they are ambiguous.
221221
wlog("The module #{mod.refname} is ambiguous with #{self[name].refname}.")
222-
else
223-
self[name] = mod
224222
end
225223

224+
self[name] = mod
225+
226226
mod
227227
end
228228

lib/msf/core/modules/loader/base.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def load_module(parent_path, type, module_reference_name, options={})
125125
return false
126126
end
127127

128-
namespace_module_transaction(type + "/" + module_reference_name, :reload => reload) { |namespace_module|
128+
try_eval_module = lambda { |namespace_module|
129129
# set the parent_path so that the module can be reloaded with #load_module
130130
namespace_module.parent_path = parent_path
131131

@@ -173,23 +173,20 @@ def load_module(parent_path, type, module_reference_name, options={})
173173
return false
174174
end
175175

176-
ilog("Loaded #{type} module #{module_reference_name} under #{parent_path}", 'core', LEV_2)
176+
if reload
177+
ilog("Reloading #{type} module #{module_reference_name}. Ambiguous module warnings are safe to ignore", 'core', LEV_2)
178+
else
179+
ilog("Loaded #{type} module #{module_reference_name} under #{parent_path}", 'core', LEV_2)
180+
end
177181

178182
module_manager.module_load_error_by_path.delete(module_path)
179183

180184
true
181185
}
182186

183-
if reload
184-
# Delete the original copy of the module so that module_manager.on_load_module called from inside load_module does
185-
# not trigger an ambiguous name warning, which would cause the reloaded module to not be stored in the
186-
# ModuleManager.
187-
module_manager.delete(module_reference_name)
188-
189-
# Delete the original copy of the module in the type-specific module set stores the reloaded module and doesn't
190-
# trigger an ambiguous name warning
191-
module_set = module_manager.module_set(type)
192-
module_set.delete(module_reference_name)
187+
loaded = namespace_module_transaction(type + "/" + module_reference_name, :reload => reload, &try_eval_module)
188+
unless loaded
189+
return false
193190
end
194191

195192
# Do some processing on the loaded module to get it into the right associations

spec/lib/msf/core/modules/loader/base_spec.rb

Lines changed: 53 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Metasploit3
2020

2121
let(:module_content) do
2222
<<-EOS
23-
class Metasploit3 < Msf::Auxiliary
23+
class Metasploit3 < Msf::Auxiliary
2424
# fully-qualified name is Msf::GoodRanking, so this will failing if lexical scope is not captured
2525
Rank = GoodRanking
2626
end
@@ -106,10 +106,10 @@ class Metasploit3 < Msf::Auxiliary
106106
let(:namespace_module) do
107107
Object.module_eval(
108108
<<-EOS
109-
module #{namespace_module_names[0]}
109+
module #{namespace_module_names[0]}
110110
module #{namespace_module_names[1]}
111111
module #{namespace_module_names[2]}
112-
#{described_class::NAMESPACE_MODULE_CONTENT}
112+
#{described_class::NAMESPACE_MODULE_CONTENT}
113113
end
114114
end
115115
end
@@ -255,18 +255,27 @@ module #{namespace_module_names[2]}
255255
subject.stub(:module_path => module_path)
256256
end
257257

258-
it 'should return false if :force is false and the file has not been changed' do
259-
module_manager.stub(:file_changed? => false)
260-
261-
subject.load_module(parent_path, type, module_reference_name, :force => false).should be_false
262-
end
263-
264258
it 'should call file_changed? with the module_path' do
265259
module_manager.should_receive(:file_changed?).with(module_path).and_return(false)
266260

267261
subject.load_module(parent_path, type, module_reference_name, :force => false)
268262
end
269263

264+
context 'without file changed' do
265+
before(:each) do
266+
module_manager.stub(:file_changed? => false)
267+
end
268+
269+
it 'should return false if :force is false' do
270+
subject.load_module(parent_path, type, module_reference_name, :force => false).should be_false
271+
end
272+
273+
it 'should not call #read_module_content' do
274+
subject.should_not_receive(:read_module_content)
275+
subject.load_module(parent_path, type, module_reference_name)
276+
end
277+
end
278+
270279
context 'with file changed' do
271280
let(:module_full_name) do
272281
File.join('auxiliary', module_reference_name)
@@ -355,7 +364,7 @@ class Metasploit3 < Msf::Auxiliary
355364
end
356365

357366
it 'should not attempt to make a new namespace_module' do
358-
subject.should_not_receive(:namespace_module_transaction)
367+
subject.should_not_receive(:namespace_module_transaction)
359368
subject.load_module(parent_path, type, module_reference_name).should be_false
360369
end
361370
end
@@ -384,7 +393,7 @@ class Metasploit3 < Msf::Auxiliary
384393
let(:backtrace) do
385394
[
386395
'Backtrace Line 1',
387-
'Backtrace Line 2'
396+
'Backtrace Line 2'
388397
]
389398
end
390399

@@ -424,9 +433,9 @@ class Metasploit3 < Msf::Auxiliary
424433
let(:version_compatibility_error) do
425434
Msf::Modules::VersionCompatibilityError.new(
426435
:module_path => module_path,
427-
:module_reference_name => module_reference_name,
428-
:minimum_api_version => infinity,
429-
:minimum_core_version => infinity
436+
:module_reference_name => module_reference_name,
437+
:minimum_api_version => infinity,
438+
:minimum_core_version => infinity
430439
)
431440
end
432441

@@ -509,7 +518,7 @@ class Metasploit3 < Msf::Auxiliary
509518
end
510519

511520
it 'should record the load error' do
512-
subject.should_receive(:load_error).with(module_path, version_compatibility_error)
521+
subject.should_receive(:load_error).with(module_path, version_compatibility_error)
513522
subject.load_module(parent_path, type, module_reference_name).should be_false
514523
end
515524

@@ -621,87 +630,6 @@ class Metasploit3 < Msf::Auxiliary
621630
subject.load_module(parent_path, type, module_reference_name).should be_true
622631
end
623632

624-
context 'with module_reference_name already in module_manager' do
625-
let(:framework) do
626-
framework = mock('Framework', :datastore => {})
627-
framework.stub_chain(:events, :on_module_load)
628-
629-
framework
630-
end
631-
632-
let(:metasploit_class) do
633-
@original_namespace_module::Metasploit3
634-
end
635-
636-
let(:module_manager) do
637-
Msf::ModuleManager.new(framework)
638-
end
639-
640-
before(:each) do
641-
# remove the stub from before(:each) in context 'with version compatibility'
642-
module_manager.unstub(:on_module_load)
643-
644-
# remove the stubs from before(:each) in context 'with file changed'
645-
module_manager.unstub(:delete)
646-
module_manager.unstub(:module_set)
647-
end
648-
649-
it 'should not cause an ambiguous module_reference_name in the module_manager' do
650-
module_manager[module_reference_name] = metasploit_class
651-
652-
subject.load_module(parent_path, type, module_reference_name).should be_true
653-
module_manager.send(:ambiguous_module_reference_name_set).should be_empty
654-
end
655-
656-
it 'should not cause an ambiguous module_reference_name in the type module_set' do
657-
module_set = module_manager.module_set(type)
658-
module_set[module_reference_name] = metasploit_class
659-
660-
subject.load_module(parent_path, type, module_reference_name).should be_true
661-
module_set.send(:ambiguous_module_reference_name_set).should be_empty
662-
end
663-
664-
context 'without file changed' do
665-
before(:each) do
666-
module_manager.stub(:file_changed => false)
667-
end
668-
669-
context 'with :force => true' do
670-
it 'should not cause an ambiguous module_reference_name in the module_manager' do
671-
module_manager[module_reference_name] = metasploit_class
672-
673-
subject.load_module(parent_path, type, module_reference_name, :force => true).should be_true
674-
module_manager.send(:ambiguous_module_reference_name_set).should be_empty
675-
end
676-
677-
it 'should not cause an ambiguous module_reference_name in the type module_set' do
678-
module_set = module_manager.module_set(type)
679-
module_set[module_reference_name] = metasploit_class
680-
681-
subject.load_module(parent_path, type, module_reference_name, :force => true).should be_true
682-
module_set.send(:ambiguous_module_reference_name_set).should be_empty
683-
end
684-
end
685-
686-
context 'with :reload => true' do
687-
it 'should not cause an ambiguous module_reference_name in the module_manager' do
688-
module_manager[module_reference_name] = metasploit_class
689-
690-
subject.load_module(parent_path, type, module_reference_name, :reload => true).should be_true
691-
module_manager.send(:ambiguous_module_reference_name_set).should be_empty
692-
end
693-
694-
it 'should not cause an ambiguous module_reference_name in the type module_set' do
695-
module_set = module_manager.module_set(type)
696-
module_set[module_reference_name] = metasploit_class
697-
698-
subject.load_module(parent_path, type, module_reference_name, :reload => true).should be_true
699-
module_set.send(:ambiguous_module_reference_name_set).should be_empty
700-
end
701-
end
702-
end
703-
end
704-
705633
it 'should call module_manager.on_module_load' do
706634
module_manager.should_receive(:on_module_load)
707635
subject.load_module(parent_path, type, module_reference_name).should be_true
@@ -728,9 +656,9 @@ class Metasploit3 < Msf::Auxiliary
728656
count_by_type.has_key?(type).should be_false
729657
subject.load_module(
730658
parent_path,
731-
type,
732-
module_reference_name,
733-
:count_by_type => count_by_type
659+
type,
660+
module_reference_name,
661+
:count_by_type => count_by_type
734662
).should be_true
735663
count_by_type[type].should == 1
736664
end
@@ -743,9 +671,9 @@ class Metasploit3 < Msf::Auxiliary
743671

744672
subject.load_module(
745673
parent_path,
746-
type,
747-
module_reference_name,
748-
:count_by_type => count_by_type
674+
type,
675+
module_reference_name,
676+
:count_by_type => count_by_type
749677
).should be_true
750678

751679
incremented_count = original_count + 1
@@ -763,8 +691,8 @@ class Metasploit3 < Msf::Auxiliary
763691
let(:namespace_module_names) do
764692
[
765693
'Msf',
766-
'Modules',
767-
relative_name
694+
'Modules',
695+
relative_name
768696
]
769697
end
770698

@@ -794,8 +722,8 @@ class Metasploit3 < Msf::Auxiliary
794722
"end\n" \
795723
"end\n" \
796724
"end",
797-
anything,
798-
anything
725+
anything,
726+
anything
799727
)
800728

801729
namespace_module = mock('Namespace Module')
@@ -810,8 +738,8 @@ class Metasploit3 < Msf::Auxiliary
810738
:module_eval
811739
).with(
812740
anything,
813-
described_class_pathname.to_s,
814-
anything
741+
described_class_pathname.to_s,
742+
anything
815743
)
816744

817745
namespace_module = mock('Namespace Module')
@@ -826,8 +754,8 @@ class Metasploit3 < Msf::Auxiliary
826754
:module_eval
827755
).with(
828756
anything,
829-
anything,
830-
described_class::NAMESPACE_MODULE_LINE - namespace_module_names.length
757+
anything,
758+
described_class::NAMESPACE_MODULE_LINE - namespace_module_names.length
831759
)
832760

833761
namespace_module = mock('Namespace Module')
@@ -861,7 +789,7 @@ class Metasploit3 < Msf::Auxiliary
861789
'Mod0'
862790
end
863791

864-
before(:each) do
792+
before(:each) do
865793
# copy to local variable so it is accessible in instance_eval
866794
relative_name = self.relative_name
867795

@@ -870,7 +798,7 @@ class Metasploit3 < Msf::Auxiliary
870798
remove_const relative_name
871799
end
872800
end
873-
end
801+
end
874802

875803
it 'should return nil if the module is not defined' do
876804
Msf::Modules.const_defined?(relative_name).should be_false
@@ -1019,7 +947,7 @@ class Metasploit3
1019947
end
1020948

1021949
it 'should remove the pre-existing namespace module' do
1022-
Msf::Modules.should_receive(:remove_const).with(relative_name)
950+
Msf::Modules.should_receive(:remove_const).with(relative_name)
1023951

1024952
subject.send(:namespace_module_transaction, module_full_name) do |namespace_module|
1025953
true
@@ -1129,13 +1057,13 @@ class Metasploit3
11291057
end
11301058

11311059
it 'should create a new namespace module' do
1132-
expect {
1133-
Msf::Modules.const_get(relative_name)
1134-
}.to raise_error(NameError)
1060+
expect {
1061+
Msf::Modules.const_get(relative_name)
1062+
}.to raise_error(NameError)
11351063

1136-
subject.send(:namespace_module_transaction, module_full_name) do |namespace_module|
1137-
Msf::Modules.const_get(relative_name).should == namespace_module
1138-
end
1064+
subject.send(:namespace_module_transaction, module_full_name) do |namespace_module|
1065+
Msf::Modules.const_get(relative_name).should == namespace_module
1066+
end
11391067
end
11401068

11411069
context 'with an Exception from the block' do
@@ -1200,7 +1128,7 @@ class Metasploit3
12001128
subject.send(:namespace_module_transaction, module_full_name) do |namespace_module|
12011129
Msf::Modules.const_defined?(relative_name).should be_true
12021130

1203-
created_namespace_module = namespace_module
1131+
created_namespace_module = namespace_module
12041132

12051133
true
12061134
end
@@ -1344,7 +1272,7 @@ class Metasploit2
13441272

13451273
subject.send(:restore_namespace_module, parent_module, relative_name, @original_namespace_module)
13461274

1347-
parent_module.const_defined?(relative_name).should be_true
1275+
parent_module.const_defined?(relative_name).should be_true
13481276
parent_module.const_get(relative_name).should == @original_namespace_module
13491277
end
13501278
end
@@ -1353,10 +1281,10 @@ class Metasploit2
13531281

13541282
context '#typed_path' do
13551283
it 'should delegate to the class method' do
1356-
type = Msf::MODULE_EXPLOIT
1284+
type = Msf::MODULE_EXPLOIT
13571285

1358-
described_class.should_receive(:typed_path).with(type, module_reference_name)
1359-
subject.send(:typed_path, type, module_reference_name)
1286+
described_class.should_receive(:typed_path).with(type, module_reference_name)
1287+
subject.send(:typed_path, type, module_reference_name)
13601288
end
13611289
end
13621290

@@ -1405,4 +1333,4 @@ class Metasploit2
14051333
end
14061334
end
14071335
end
1408-
end
1336+
end

0 commit comments

Comments
 (0)