Skip to content

Commit a0c473f

Browse files
committed
Upload memory usage optimization
Optimize xor_bytes memory usage, use small buffer for upload, add verbosity
1 parent d5ab7b1 commit a0c473f

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,23 @@ def File.upload_file(dest_file, src_file, &stat)
275275
# Open the file on the remote side for writing and read
276276
# all of the contents of the local file
277277
stat.call('uploading', src_file, dest_file) if (stat)
278-
dest_fd = client.fs.file.new(dest_file, "wb")
279-
src_buf = ''
280-
281-
::File.open(src_file, 'rb') { |f|
282-
src_buf = f.read(f.stat.size)
283-
}
278+
dest_fd = nil
279+
src_fd = nil
280+
buf_size = 8 * 1024 * 1024
284281

285282
begin
286-
dest_fd.write(src_buf)
283+
dest_fd = client.fs.file.new(dest_file, "wb")
284+
src_fd = ::File.open(src_file, "rb")
285+
src_size = src_fd.stat.size
286+
while (buf = src_fd.read(buf_size))
287+
dest_fd.write(buf)
288+
percent = dest_fd.pos.to_f / src_size.to_f * 100.0
289+
msg = "Uploaded #{Filesize.new(dest_fd.pos).pretty} of #{src_size} (#{percent.round(2)}%)"
290+
stat.call(msg, src_file, dest_file)
291+
end
287292
ensure
288-
dest_fd.close
293+
src_fd.close unless src_fd.nil?
294+
dest_fd.close unless dest_fd.nil?
289295
end
290296
stat.call('uploaded', src_file, dest_file) if (stat)
291297
end

lib/rex/post/meterpreter/packet.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,12 @@ def from_r(key=nil)
874874
# Xor a set of bytes with a given XOR key.
875875
#
876876
def xor_bytes(xor_key, bytes)
877+
xor_key = xor_key.bytes
877878
result = ''
878-
bytes.bytes.zip(xor_key.bytes.cycle).each do |b|
879-
result << (b[0].ord ^ b[1].ord).chr
879+
i = 0
880+
bytes.each_byte do |b|
881+
result << (b ^ xor_key[i % xor_key.length]).chr
882+
i += 1
880883
end
881884
result
882885
end

0 commit comments

Comments
 (0)