Skip to content

Commit 9d92b38

Browse files
committed
Land rapid7#2121 - add specs for module search filter
2 parents a2ea5dd + 67d8c11 commit 9d92b38

File tree

2 files changed

+176
-9
lines changed

2 files changed

+176
-9
lines changed

lib/msf/core/module.rb

Lines changed: 2 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

@@ -680,9 +678,6 @@ def search_filter(search_string)
680678
k = res
681679

682680
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")
686681
is_server = (self.respond_to?(:stance) and self.stance == "aggressive")
687682
is_client = (self.respond_to?(:stance) and self.stance == "passive")
688683

@@ -719,9 +714,7 @@ def search_filter(search_string)
719714
when 'port'
720715
match = [t,w] if self.datastore['RPORT'].to_s =~ r
721716
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)
717+
match = [t,w] if Msf::MODULE_TYPES.any? { |modt| w == modt and self.type == modt }
725718
when 'app'
726719
match = [t,w] if (w == "server" and is_server)
727720
match = [t,w] if (w == "client" and is_client)
@@ -741,7 +734,7 @@ def search_filter(search_string)
741734
return true
742735
end
743736
end
744-
# Filter this module if we matched an exlusion keyword (-value)
737+
# Filter this module if we matched an exclusion keyword (-value)
745738
if mode == 1 and match
746739
return true
747740
end

spec/lib/msf/core/module_spec.rb

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
REF_TYPES = %w(CVE BID OSVDB EDB)
39+
40+
describe Msf::Module do
41+
describe '#search_filter' do
42+
let(:opts) { Hash.new }
43+
before { subject.stub(:fullname => '/module') }
44+
subject { Msf::Module.new(opts) }
45+
accept = []
46+
reject = []
47+
48+
context 'on a blank query' do
49+
it_should_behave_like 'search_filter', :accept => [''], :test_inverse => false
50+
end
51+
52+
context 'on a client module' do
53+
before { subject.stub(:stance => 'passive') }
54+
accept = %w(app:client)
55+
reject = %w(app:server)
56+
57+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
58+
end
59+
60+
context 'on a server module' do
61+
before { subject.stub(:stance => 'aggressive') }
62+
accept = %w(app:server)
63+
reject = %w(app:client)
64+
65+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
66+
end
67+
68+
context 'on a module with the author "joev"' do
69+
let(:opts) { ({ 'Author' => ['joev'] }) }
70+
accept = %w(author:joev author:joe)
71+
reject = %w(author:unrelated)
72+
73+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
74+
end
75+
76+
context 'on a module with the authors "joev" and "blarg"' do
77+
let(:opts) { ({ 'Author' => ['joev', 'blarg'] }) }
78+
accept = %w(author:joev author:joe)
79+
reject = %w(author:sinn3r)
80+
81+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
82+
end
83+
84+
context 'on a module that supports the osx platform' do
85+
let(:opts) { ({ 'Platform' => %w(osx) }) }
86+
accept = %w(platform:osx os:osx)
87+
reject = %w(platform:bsd platform:windows platform:unix os:bsd os:windows os:unix)
88+
89+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
90+
end
91+
92+
context 'on a module that supports the linux platform' do
93+
let(:opts) { ({ 'Platform' => %w(linux) }) }
94+
accept = %w(platform:linux os:linux)
95+
reject = %w(platform:bsd platform:windows platform:unix os:bsd os:windows os:unix)
96+
97+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
98+
end
99+
100+
context 'on a module that supports the windows platform' do
101+
let(:opts) { ({ 'Platform' => %w(windows) }) }
102+
accept = %w(platform:windows os:windows)
103+
reject = %w(platform:bsd platform:osx platform:unix os:bsd os:osx os:unix)
104+
105+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
106+
end
107+
108+
context 'on a module that supports the osx and linux platforms' do
109+
let(:opts) { ({ 'Platform' => %w(osx linux) }) }
110+
accept = %w(platform:osx platform:linux os:osx os:linux)
111+
reject = %w(platform:bsd platform:windows platform:unix os:bsd os:windows os:unix)
112+
113+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
114+
end
115+
116+
context 'on a module that supports the windows and irix platforms' do
117+
let(:opts) { ({ 'Platform' => %w(windows irix) }) }
118+
accept = %w(platform:windows platform:irix os:windows os:irix)
119+
reject = %w(platform:bsd platform:osx platform:linux os:bsd os:osx os:linux)
120+
121+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
122+
end
123+
124+
context 'on a module with a default RPORT of 5555' do
125+
before { subject.stub(:datastore => { 'RPORT' => 5555 }) }
126+
accept = %w(port:5555)
127+
reject = %w(port:5556)
128+
129+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
130+
end
131+
132+
context 'on a module with a #name of "blah"' do
133+
let(:opts) { ({ 'Name' => 'blah' }) }
134+
it_should_behave_like 'search_filter', :accept => %w(text:blah), :reject => %w(text:foo)
135+
it_should_behave_like 'search_filter', :accept => %w(name:blah), :reject => %w(name:foo)
136+
end
137+
138+
context 'on a module with a #fullname of "blah"' do
139+
before { subject.stub(:fullname => '/c/d/e/blah') }
140+
it_should_behave_like 'search_filter', :accept => %w(text:blah), :reject => %w(text:foo)
141+
it_should_behave_like 'search_filter', :accept => %w(path:blah), :reject => %w(path:foo)
142+
end
143+
144+
context 'on a module with a #description of "blah"' do
145+
let(:opts) { ({ 'Description' => 'blah' }) }
146+
it_should_behave_like 'search_filter', :accept => %w(text:blah), :reject => %w(text:foo)
147+
end
148+
149+
context 'when filtering by module #type' do
150+
all_module_types = Msf::MODULE_TYPES
151+
all_module_types.each do |mtype|
152+
context "on a #{mtype} module" do
153+
before(:each) { subject.stub(:type => mtype) }
154+
155+
accept = ["type:#{mtype}"]
156+
reject = all_module_types.reject { |t| t == mtype }.map { |t| "type:#{t}" }
157+
158+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
159+
end
160+
end
161+
end
162+
163+
REF_TYPES.each do |ref_type|
164+
ref_num = '1234-1111'
165+
context 'on a module with reference #{ref_type}-#{ref_num}' do
166+
let(:opts) { ({ 'References' => [[ref_type, ref_num]] }) }
167+
accept = ["#{ref_type.downcase}:#{ref_num}"]
168+
reject = %w(1235-1111 1234-1112 bad).map { |n| "#{ref_type.downcase}:#{n}" }
169+
170+
it_should_behave_like 'search_filter', :accept => accept, :reject => reject
171+
end
172+
end
173+
end
174+
end

0 commit comments

Comments
 (0)