Skip to content

Commit ea5673e

Browse files
author
HD Moore
committed
Merge pull request #12 from jvazquez-r7/review_4989
Keep old values when bad file plus specs
2 parents c7da9d6 + 831e652 commit ea5673e

File tree

3 files changed

+90
-12
lines changed

3 files changed

+90
-12
lines changed

lib/msf/core/option_container.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,17 +378,18 @@ def type
378378

379379
def normalize(value)
380380
return nil unless value.kind_of?(String)
381-
if (value =~ /^rand:(.*)/)
381+
if value =~ /^rand:(.*)/
382382
count = $1.to_i
383383
return false if count < 1
384384
ret = ''
385-
count.times {
386-
ret << " " if not ret.empty?
387-
ret << [ rand(0x100000000) ].pack("N").unpack("C*").map{|x| x.to_s }.join(".")
388-
}
385+
count.times do
386+
ret << ' ' unless ret.empty?
387+
ret << [ rand(0x100000000) ].pack('N').unpack('C*').map{|x| x.to_s }.join('.')
388+
end
389389
return ret
390390
end
391-
return value
391+
392+
value
392393
end
393394

394395
def valid?(value)

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,13 +2068,26 @@ def cmd_set(*args)
20682068
return true
20692069
end
20702070

2071-
# If the value starts with file: and exists, load the file as the value
2072-
if value =~ /^file:(.*)/ && ::File.file?($1)
2071+
# If the value starts with file: exists, and size isn't too big load the file as the value
2072+
# Otherwise keep the old value
2073+
if value =~ /^file:(.*)/
20732074
fname = $1
2074-
if ::File.size(fname) > (1024*1024)
2075-
print_error("The file name specified is too big (over 1Mb)")
2076-
else
2077-
::File.open(fname, "rb") {|fd| value = fd.read(fd.stat.size) }
2075+
2076+
begin
2077+
fd = ::File.new(fname, 'rb')
2078+
rescue ::Errno::ENOENT
2079+
print_error('The file name specified does not exist')
2080+
value = datastore[name]
2081+
fd = nil
2082+
end
2083+
2084+
if fd && fd.stat.size > (1024 * 1024)
2085+
print_error('The file name specified is too big (over 1Mb)')
2086+
value = datastore[name]
2087+
fd.close
2088+
elsif fd
2089+
value = fd.read(fd.stat.size)
2090+
fd.close
20782091
end
20792092
end
20802093

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)