Skip to content

Commit 86d6bce

Browse files
committed
[FixRM rapid7#8312] - Fix file handle leaks
Fix file handle leaks for [SeeRM rapid7#8312]
1 parent 6191023 commit 86d6bce

File tree

6 files changed

+102
-51
lines changed

6 files changed

+102
-51
lines changed

modules/auxiliary/analyze/jtr_aix.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,24 @@ def initialize
3131
end
3232

3333
def run
34-
wordlist = Rex::Quickfile.new("jtrtmp")
34+
begin
35+
wordlist = Rex::Quickfile.new("jtrtmp")
3536

36-
wordlist.write( build_seed().join("\n") + "\n" )
37-
wordlist.close
37+
wordlist.write( build_seed().join("\n") + "\n" )
38+
ensure
39+
wordlist.close
40+
end
3841

3942
hashlist = Rex::Quickfile.new("jtrtmp")
4043

4144
myloots = myworkspace.loots.find(:all, :conditions => ['ltype=?', 'aix.hashes'])
4245
unless myloots.nil? or myloots.empty?
4346
myloots.each do |myloot|
47+
usf = ''
4448
begin
45-
usf = File.open(myloot.path, "rb")
49+
File.open(myloot.path, "rb") do |f|
50+
usf = f.read
51+
end
4652
rescue Exception => e
4753
print_error("Unable to read #{myloot.path} \n #{e}")
4854
next

modules/auxiliary/analyze/jtr_linux.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ def run
4949
myloots = myworkspace.loots.where('ltype=?', 'linux.hashes')
5050
unless myloots.nil? or myloots.empty?
5151
myloots.each do |myloot|
52+
usf = ''
5253
begin
53-
usf = File.open(myloot.path, "rb")
54+
File.open(myloot.path, "rb") do |f|
55+
usf = f.read
56+
end
5457
rescue Exception => e
5558
print_error("Unable to read #{myloot.path} \n #{e}")
5659
end

modules/auxiliary/client/smtp/emailer.rb

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,82 @@ def initialize(info = {})
4646
deregister_options('SUBJECT')
4747
end
4848

49+
def load_yaml_conf
50+
opts = {}
51+
52+
File.open(datastore['YAML_CONFIG'], "rb") do |f|
53+
yamlconf = YAML::load(fileconf)
54+
55+
opts['to'] = yamlconf['to']
56+
opts['from'] = yamlconf['from']
57+
opts['subject'] = yamlconf['subject']
58+
opts['type'] = yamlconf['type']
59+
opts['msg_file'] = yamlconf['msg_file']
60+
opts['wait'] = yamlconf['wait']
61+
opts['add_name'] = yamlconf['add_name']
62+
opts['sig'] = yamlconf['sig']
63+
opts['sig_file'] = yamlconf['sig_file']
64+
opts['attachment'] = yamlconf['attachment']
65+
opts['attachment_file'] = yamlconf['attachment_file']
66+
opts['attachment_file_type'] = yamlconf['attachment_file_type']
67+
opts['attachment_file_name'] = yamlconf['attachment_file_name']
68+
69+
### payload options ###
70+
opts['make_payload'] = yamlconf['make_payload']
71+
opts['zip_payload'] = yamlconf['zip_payload']
72+
opts['msf_port'] = yamlconf['msf_port']
73+
opts['msf_ip'] = yamlconf['msf_ip']
74+
opts['msf_payload'] = yamlconf['msf_payload']
75+
opts['msf_filename'] = yamlconf['msf_filename']
76+
opts['msf_change_ext'] = yamlconf['msf_change_ext']
77+
opts['msf_payload_ext'] = yamlconf['msf_payload_ext']
78+
end
79+
80+
opts
81+
end
82+
83+
def load_file(fname)
84+
buf = ''
85+
File.open(fname, 'rb') do |f|
86+
buf = f.read
87+
end
88+
89+
buf
90+
end
91+
4992
def run
5093

51-
fileconf = File.open(datastore['YAML_CONFIG'], "rb")
52-
yamlconf = YAML::load(fileconf)
53-
54-
fileto = yamlconf['to']
55-
from = yamlconf['from']
56-
subject = yamlconf['subject']
57-
type = yamlconf['type']
58-
msg_file = yamlconf['msg_file']
59-
wait = yamlconf['wait']
60-
add_name = yamlconf['add_name']
61-
sig = yamlconf['sig']
62-
sig_file = yamlconf['sig_file']
63-
attachment = yamlconf['attachment']
64-
attachment_file = yamlconf['attachment_file']
94+
yamlconf = load_yaml_conf
95+
96+
fileto = yamlconf['to']
97+
from = yamlconf['from']
98+
subject = yamlconf['subject']
99+
type = yamlconf['type']
100+
msg_file = yamlconf['msg_file']
101+
wait = yamlconf['wait']
102+
add_name = yamlconf['add_name']
103+
sig = yamlconf['sig']
104+
sig_file = yamlconf['sig_file']
105+
attachment = yamlconf['attachment']
106+
attachment_file = yamlconf['attachment_file']
65107
attachment_file_type = yamlconf['attachment_file_type']
66108
attachment_file_name = yamlconf['attachment_file_name']
67109

68-
### payload options ###
69-
make_payload = yamlconf['make_payload']
70-
zip_payload = yamlconf['zip_payload']
71-
msf_port = yamlconf['msf_port']
72-
msf_ip = yamlconf['msf_ip']
73-
msf_payload = yamlconf['msf_payload']
74-
msf_filename = yamlconf['msf_filename']
75-
msf_change_ext = yamlconf['msf_change_ext']
76-
msf_payload_ext = yamlconf['msf_payload_ext']
77-
110+
make_payload = yamlconf['make_payload']
111+
zip_payload = yamlconf['zip_payload']
112+
msf_port = yamlconf['msf_port']
113+
msf_ip = yamlconf['msf_ip']
114+
msf_payload = yamlconf['msf_payload']
115+
msf_filename = yamlconf['msf_filename']
116+
msf_change_ext = yamlconf['msf_change_ext']
117+
msf_payload_ext = yamlconf['msf_payload_ext']
78118

79119
tmp = Dir.tmpdir
80120

81121
datastore['MAILFROM'] = from
82122

83-
msg = File.open(msg_file, 'rb').read
84-
email_sig = File.open(sig_file, 'rb').read
123+
msg = load_file(msg_file)
124+
email_sig = load_file(sig_file)
85125

86126
if (type !~ /text/i and type !~ /text\/html/i)
87127
print_error("YAML config: #{type}")
@@ -154,7 +194,7 @@ def run
154194
end
155195

156196
if sig
157-
data_sig = File.open(sig_file, 'rb').read
197+
data_sig = load_file(sig_file)
158198
email_msg_body = "#{email_msg_body}\n#{data_sig}"
159199
end
160200

@@ -172,7 +212,7 @@ def run
172212

173213
if attachment
174214
if attachment_file_name
175-
data_attachment = File.open(attachment_file, 'rb').read
215+
data_attachment = load_file(attachment_file)
176216
mime_msg.add_part(Rex::Text.encode_base64(data_attachment, "\r\n"), attachment_file_type, "base64", "attachment; filename=\"#{attachment_file_name}\"")
177217
end
178218
end

modules/auxiliary/gather/d20pass.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,20 @@ def parseusers(f, userentryptr)
240240

241241
def parse(fh)
242242
print_status("Parsing file")
243-
f = File.open(fh.path, 'rb')
244-
used = f.read(4)
245-
if used != "USED"
246-
print_error "Invalid Configuration File!"
247-
return
248-
end
249-
f.seek(0x38)
250-
start = makefptr(f.read(4))
251-
userptr = findentry(f, "B014USER", start)
252-
if userptr != nil
253-
parseusers(f, userptr)
254-
else
255-
print_error "Error finding the user table in the configuration."
243+
File.open(fh.path, 'rb') do |f|
244+
used = f.read(4)
245+
if used != "USED"
246+
print_error "Invalid Configuration File!"
247+
return
248+
end
249+
f.seek(0x38)
250+
start = makefptr(f.read(4))
251+
userptr = findentry(f, "B014USER", start)
252+
if userptr != nil
253+
parseusers(f, userptr)
254+
else
255+
print_error "Error finding the user table in the configuration."
256+
end
256257
end
257258
end
258259

modules/auxiliary/scanner/sap/sap_icm_urlscan.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ def run_host(ip)
8181

8282
# Load URLs
8383
urls_to_check = []
84-
f = File.open(url_file)
85-
f.each_line do |line|
86-
urls_to_check.push line
84+
File.open(url_file) do |f|
85+
f.each_line do |line|
86+
urls_to_check.push line
87+
end
8788
end
8889

8990
print_status("#{rhost}:#{rport} Beginning URL check")

modules/post/windows/gather/enum_chrome.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def extension_mailvelope(username, extname)
8484

8585

8686
def parse_prefs(username, filepath)
87-
f = File.open(filepath, 'rb')
88-
until f.eof
87+
prefs = ''
88+
File.open(filepath, 'rb') do |f|
8989
prefs = f.read
9090
end
9191
results = ActiveSupport::JSON.decode(prefs)

0 commit comments

Comments
 (0)