Skip to content

Commit b6df023

Browse files
committed
Land rapid7#4989, @hmoore-r7's change to file: handling
Datastore options with file: are handled at set time
2 parents 76cdd28 + a07b68d commit b6df023

File tree

7 files changed

+96
-30
lines changed

7 files changed

+96
-30
lines changed

lib/msf/core/opt_address_range.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,18 @@ def type
1414

1515
def normalize(value)
1616
return nil unless value.kind_of?(String)
17-
if (value =~ /^file:(.*)/)
18-
path = $1
19-
return false if not File.exists?(path) or File.directory?(path)
20-
return File.readlines(path).map{ |s| s.strip}.join(" ")
21-
elsif (value =~ /^rand:(.*)/)
17+
if value =~ /^rand:(.*)/
2218
count = $1.to_i
2319
return false if count < 1
2420
ret = ''
25-
count.times {
26-
ret << " " if not ret.empty?
27-
ret << [ rand(0x100000000) ].pack("N").unpack("C*").map{|x| x.to_s }.join(".")
28-
}
21+
count.times do
22+
ret << ' ' if not ret.empty?
23+
ret << [ rand(0x100000000) ].pack('N').unpack('C*').map{|x| x.to_s }.join('.')
24+
end
2925
return ret
3026
end
31-
return value
27+
28+
value
3229
end
3330

3431
def valid?(value)

lib/msf/core/opt_raw.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ def type
1313
end
1414

1515
def normalize(value)
16-
if (value =~ /^file:(.*)/)
17-
path = $1
18-
begin
19-
value = File.read(path)
20-
rescue ::Errno::ENOENT, ::Errno::EISDIR
21-
value = nil
22-
end
23-
end
2416
value
2517
end
2618

lib/msf/core/opt_string.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ def type
1313
end
1414

1515
def normalize(value)
16-
if (value =~ /^file:(.*)/)
17-
path = $1
18-
begin
19-
value = File.read(path)
20-
rescue ::Errno::ENOENT, ::Errno::EISDIR
21-
value = nil
22-
end
23-
end
2416
value
2517
end
2618

lib/msf/ui/console/command_dispatcher/core.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,29 @@ def cmd_set(*args)
20702070
return true
20712071
end
20722072

2073+
# If the value starts with file: exists, and size isn't too big load the file as the value
2074+
# Otherwise keep the old value
2075+
if value =~ /^file:(.*)/
2076+
fname = $1
2077+
2078+
begin
2079+
fd = ::File.new(fname, 'rb')
2080+
rescue ::Errno::ENOENT
2081+
print_error('The file name specified does not exist')
2082+
value = datastore[name]
2083+
fd = nil
2084+
end
2085+
2086+
if fd && fd.stat.size > (1024 * 1024)
2087+
print_error('The file name specified is too big (over 1Mb)')
2088+
value = datastore[name]
2089+
fd.close
2090+
elsif fd
2091+
value = fd.read(fd.stat.size)
2092+
fd.close
2093+
end
2094+
end
2095+
20732096
if append
20742097
datastore[name] = datastore[name] + value
20752098
else

spec/lib/msf/core/opt_address_range_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
{ :value => "192.0.2.0-255", :normalized => "192.0.2.0-255" },
1111
{ :value => "192.0.2.0,1-255", :normalized => "192.0.2.0,1-255" },
1212
{ :value => "192.0.2.*", :normalized => "192.0.2.*" },
13-
{ :value => "192.0.2.0-192.0.2.255", :normalized => "192.0.2.0-192.0.2.255" },
14-
{ :value => "file:#{File.expand_path('short_address_list.txt',FILE_FIXTURES_PATH)}", :normalized => '192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5'},
13+
{ :value => "192.0.2.0-192.0.2.255", :normalized => "192.0.2.0-192.0.2.255" }
1514
]
1615
invalid_values = [
1716
# Too many dots

spec/lib/msf/core/opt_raw_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
describe Msf::OptRaw do
77

88
valid_values = [
9-
{ :value => 'foo', :normalized => 'foo' },
10-
{ :value => "file:#{File.expand_path('string_list.txt',FILE_FIXTURES_PATH)}",:normalized => "foo\nbar\nbaz" },
9+
{ :value => 'foo', :normalized => 'foo' }
1110
]
1211
invalid_values = []
1312

spec/lib/msf/ui/console/command_dispatcher/core_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,70 @@ def set_and_test_variable(name, framework_value, module_value, framework_re, mod
160160
it "should show the correct value when both the module and the framework have this variable" do
161161
set_and_test_variable(name, 'FRAMEWORK', 'MODULE', /^#{name} => FRAMEWORK$/, /^#{name} => MODULE$/)
162162
end
163+
164+
context "when using file: prefix in the value" do
165+
context "when the file exists" do
166+
167+
before(:each) do
168+
allow(::File).to receive(:new) do |filename, mode|
169+
fd = StringIO.new(file_contents, mode)
170+
fd
171+
end
172+
173+
allow_any_instance_of(::StringIO).to receive(:stat) do |io|
174+
file_contents
175+
end
176+
end
177+
178+
context "when the size is 1MB" do
179+
let(:file_name) do
180+
::Rex::Text.rand_text_alpha(10).upcase
181+
end
182+
183+
let(:file_contents) do
184+
::Rex::Text.rand_text_alpha(1024 * 1024).upcase
185+
end
186+
187+
it "should show the new value" do
188+
set_and_test_variable(name, nil, "file:/#{file_name}", nil, /^#{name} => #{file_contents}$/)
189+
end
190+
end
191+
192+
context "when the size is greater than 1MB" do
193+
let(:file_name) do
194+
::Rex::Text.rand_text_alpha(10).upcase
195+
end
196+
197+
let(:file_contents) do
198+
::Rex::Text.rand_text_alpha(1024 * 1025).upcase
199+
end
200+
201+
it "should show the old value" do
202+
set_and_test_variable(name, nil, "file:/#{file_name}", nil, /^#{name} => $/)
203+
end
204+
end
205+
206+
context "when the size is less than 1MB" do
207+
let(:file_name) do
208+
::Rex::Text.rand_text_alpha(10).upcase
209+
end
210+
211+
let(:file_contents) do
212+
::Rex::Text.rand_text_alpha(10).upcase
213+
end
214+
215+
it "should show the new value" do
216+
set_and_test_variable(name, nil, "file:/#{file_name}", nil, /^#{name} => #{file_contents}$/)
217+
end
218+
end
219+
end
220+
221+
context "when the file doesn't exist" do
222+
it "should show the old value" do
223+
set_and_test_variable(name, nil, "file:/#{::Rex::Text.rand_text_alpha(10).upcase}", nil, /^#{name} => $/)
224+
end
225+
end
226+
end
163227
end
164228
end
165229
end

0 commit comments

Comments
 (0)