Skip to content

Commit f4b0ab8

Browse files
committed
Adds 141 passing specs to Msf::Module#search_filter.
* tests exclusion functionality, type: matching, port: matching, app: matching, platform: matching, author: matching, text: matching, name: matching, and path: matching. [RM rapid7#4790]
1 parent 54af292 commit f4b0ab8

File tree

2 files changed

+169
-9
lines changed

2 files changed

+169
-9
lines changed

lib/msf/core/module.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,8 @@ def self.is_usable
110110
# hash.
111111
#
112112
def initialize(info = {})
113-
114113
@module_info_copy = info.dup
115114

116-
117115
self.module_info = info
118116
generate_uuid
119117

@@ -133,6 +131,7 @@ def initialize(info = {})
133131

134132
# Create and initialize the option container for this module
135133
self.options = OptionContainer.new
134+
136135
self.options.add_options(info['Options'], self.class)
137136
self.options.add_advanced_options(info['AdvancedOptions'], self.class)
138137
self.options.add_evasion_options(info['EvasionOptions'], self.class)
@@ -680,9 +679,6 @@ def search_filter(search_string)
680679
k = res
681680

682681
refs = self.references.map{|x| [x.ctx_id, x.ctx_val].join("-") }
683-
is_exploit = (self.type == "exploit")
684-
is_auxiliary = (self.type == "auxiliary")
685-
is_post = (self.type == "post")
686682
is_server = (self.respond_to?(:stance) and self.stance == "aggressive")
687683
is_client = (self.respond_to?(:stance) and self.stance == "passive")
688684

@@ -719,9 +715,7 @@ def search_filter(search_string)
719715
when 'port'
720716
match = [t,w] if self.datastore['RPORT'].to_s =~ r
721717
when 'type'
722-
match = [t,w] if (w == "exploit" and is_exploit)
723-
match = [t,w] if (w == "auxiliary" and is_auxiliary)
724-
match = [t,w] if (w == "post" and is_post)
718+
match = [t,w] if Msf::MODULE_TYPES.any? { |modt| w == modt and self.type == modt }
725719
when 'app'
726720
match = [t,w] if (w == "server" and is_server)
727721
match = [t,w] if (w == "client" and is_client)
@@ -741,7 +735,7 @@ def search_filter(search_string)
741735
return true
742736
end
743737
end
744-
# Filter this module if we matched an exlusion keyword (-value)
738+
# Filter this module if we matched an exclusion keyword (-value)
745739
if mode == 1 and match
746740
return true
747741
end

spec/lib/msf/core/module_spec.rb

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# -*- coding:binary -*-
2+
require 'spec_helper'
3+
require 'msf/core/module'
4+
require 'msf/core/module/platform_list'
5+
6+
shared_examples "search_filter" do |opts|
7+
accept = opts[:accept] || []
8+
reject = opts[:reject] || []
9+
10+
accept.each do |query|
11+
it "should accept a query containing '#{query}'" do
12+
# if the subject matches, search_filter returns false ("don't filter me out!")
13+
subject.search_filter(query).should be_false
14+
end
15+
16+
unless opts.has_key?(:test_inverse) and not opts[:test_inverse]
17+
it "should reject a query containing '-#{query}'" do
18+
subject.search_filter("-#{query}").should be_true
19+
end
20+
end
21+
end
22+
23+
reject.each do |query|
24+
it "should reject a query containing '#{query}'" do
25+
# if the subject doesn't matches, search_filter returns true ("filter me out!")
26+
subject.search_filter(query).should be_true
27+
end
28+
29+
unless opts.has_key?(:test_inverse) and not opts[:test_inverse]
30+
it "should accept a query containing '-#{query}'" do
31+
subject.search_filter("-#{query}").should be_true # what? why?
32+
end
33+
end
34+
end
35+
end
36+
37+
38+
describe Msf::Module do
39+
describe '#search_filter' do
40+
before { subject.stub(:type => 'server') }
41+
let(:opts) { Hash.new }
42+
subject { Msf::Module.new(opts) }
43+
44+
accept = []
45+
reject = []
46+
47+
context 'on a blank query' do
48+
it_should_behave_like 'search_filter', :accept => [''], :test_inverse => false
49+
end
50+
51+
context 'on a client module' do
52+
before { subject.stub(:stance => 'passive') }
53+
accept = %w(app:client)
54+
reject = %w(app:server)
55+
56+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
57+
end
58+
59+
context 'on a server module' do
60+
before { subject.stub(:stance => 'aggressive') }
61+
accept = %w(app:server)
62+
reject = %w(app:client)
63+
64+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
65+
end
66+
67+
context 'on a module with the author "joev"' do
68+
let(:opts) { ({ 'Author' => ['joev'] }) }
69+
accept = %w(author:joev author:joe)
70+
reject = %w(author:unrelated)
71+
72+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
73+
end
74+
75+
context 'on a module with the authors "joev" and "blarg"' do
76+
let(:opts) { ({ 'Author' => ['joev', 'blarg'] }) }
77+
accept = %w(author:joev author:joe)
78+
reject = %w(author:sinn3r)
79+
80+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
81+
end
82+
83+
context 'on a module that supports the osx platform' do
84+
let(:opts) { ({ 'Platform' => %w(osx) }) }
85+
accept = %w(platform:osx)
86+
reject = %w(platform:bsd platform:windows platform:unix)
87+
88+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
89+
end
90+
91+
context 'on a module that supports the linux platform' do
92+
let(:opts) { ({ 'Platform' => %w(linux) }) }
93+
accept = %w(platform:linux)
94+
reject = %w(platform:bsd platform:windows platform:unix)
95+
96+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
97+
end
98+
99+
context 'on a module that supports the windows platform' do
100+
let(:opts) { ({ 'Platform' => %w(windows) }) }
101+
accept = %w(platform:windows)
102+
reject = %w(platform:bsd platform:osx platform:unix)
103+
104+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
105+
end
106+
107+
context 'on a module that supports the osx and linux platforms' do
108+
let(:opts) { ({ 'Platform' => %w(osx linux) }) }
109+
accept = %w(platform:osx platform:linux)
110+
reject = %w(platform:bsd platform:windows platform:unix)
111+
112+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
113+
end
114+
115+
context 'on a module that supports the windows and irix platforms' do
116+
let(:opts) { ({ 'Platform' => %w(windows irix) }) }
117+
accept = %w(platform:windows platform:irix)
118+
reject = %w(platform:bsd platform:osx platform:linux)
119+
120+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
121+
end
122+
123+
context 'on a module with a default RPORT of 5555' do
124+
before { subject.stub(:datastore => { 'RPORT' => 5555 }) }
125+
accept = %w(port:5555)
126+
reject = %w(port:5556)
127+
128+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
129+
end
130+
131+
context 'on a module with a #name of "blah"' do
132+
before { subject.stub(:name => 'blah') }
133+
it_should_behave_like 'search_filter', :accept => %w(text:blah), :reject => %w(text:foo)
134+
it_should_behave_like 'search_filter', :accept => %w(name:blah), :reject => %w(name:foo)
135+
end
136+
137+
context 'on a module with a #fullname of "blah"' do
138+
before { subject.stub(:fullname => 'blah/blah') }
139+
it_should_behave_like 'search_filter', :accept => %w(text:blah), :reject => %w(text:foo)
140+
it_should_behave_like 'search_filter', :accept => %w(path:blah), :reject => %w(path:foo)
141+
end
142+
143+
context 'on a module with a #description of "blah"' do
144+
before { subject.stub(:description => 'blah') }
145+
it_should_behave_like 'search_filter', :accept => %w(text:blah), :reject => %w(text:foo)
146+
end
147+
148+
context 'when filtering by module #type' do
149+
all_module_types = Msf::MODULE_TYPES
150+
all_module_types.each do |mtype|
151+
context "on a #{mtype} module" do
152+
before(:each) { subject.stub(:type => mtype) }
153+
154+
accept = ["type:#{mtype}"]
155+
reject = all_module_types.reject { |t| t == mtype }.map { |t| "type:#{t}" }
156+
157+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
158+
end
159+
end
160+
end
161+
162+
#
163+
# Still missing 'cve:', 'bid:', 'osvdb:', and 'edb:' test cases...
164+
#
165+
end
166+
end

0 commit comments

Comments
 (0)