2005-04-11 03:37:02 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
require 'Rex/Post/File'
|
2005-04-11 04:56:24 +00:00
|
|
|
require 'Rex/Post/Meterpreter/Channel'
|
2005-04-12 05:37:11 +00:00
|
|
|
require 'Rex/Post/Meterpreter/Channels/Pools/File'
|
2005-04-11 03:37:02 +00:00
|
|
|
require 'Rex/Post/Meterpreter/Extensions/Stdapi/Stdapi'
|
2005-04-17 07:17:52 +00:00
|
|
|
require 'Rex/Post/Meterpreter/Extensions/Stdapi/Fs/IO'
|
|
|
|
require 'Rex/Post/Meterpreter/Extensions/Stdapi/Fs/FileStat'
|
2005-04-11 03:37:02 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Post
|
|
|
|
module Meterpreter
|
|
|
|
module Extensions
|
|
|
|
module Stdapi
|
2005-04-17 07:17:52 +00:00
|
|
|
module Fs
|
2005-04-11 03:37:02 +00:00
|
|
|
|
2005-04-17 07:17:52 +00:00
|
|
|
class File < Rex::Post::Meterpreter::Extensions::Stdapi::Fs::IO
|
2005-04-11 03:37:02 +00:00
|
|
|
|
2005-04-18 00:39:27 +00:00
|
|
|
SEPARATOR = "\\"
|
|
|
|
Separator = "\\"
|
|
|
|
|
2005-04-11 03:37:02 +00:00
|
|
|
include Rex::Post::File
|
|
|
|
|
|
|
|
class <<self
|
|
|
|
attr_accessor :client
|
|
|
|
end
|
|
|
|
|
2005-04-18 00:39:27 +00:00
|
|
|
def File.basename(*a)
|
|
|
|
path = a[0]
|
|
|
|
sep = "\\" + File::SEPARATOR
|
|
|
|
|
|
|
|
# I suck at regex.
|
|
|
|
path =~ /(.*)#{sep}(.*)$/
|
|
|
|
|
|
|
|
return $2
|
|
|
|
end
|
|
|
|
|
2005-04-18 02:55:01 +00:00
|
|
|
# Expands a file path
|
|
|
|
def File.expand_path(path)
|
|
|
|
request = Packet.create_request('stdapi_fs_file_expand_path')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_FILE_PATH, path)
|
|
|
|
|
|
|
|
response = client.send_request(request)
|
|
|
|
|
|
|
|
return response.get_tlv_value(TLV_TYPE_FILE_PATH)
|
|
|
|
end
|
|
|
|
|
2005-04-11 04:56:24 +00:00
|
|
|
def File.stat(name)
|
2005-04-17 07:17:52 +00:00
|
|
|
return client.fs.filestat.new(name)
|
2005-04-11 04:56:24 +00:00
|
|
|
end
|
|
|
|
|
2005-04-18 00:39:27 +00:00
|
|
|
# Upload one or more files to the remote computer the remote
|
|
|
|
# directory supplied in destination
|
|
|
|
def File.upload(destination, *src_files)
|
|
|
|
src_files.each { |src|
|
|
|
|
dest = destination
|
|
|
|
|
|
|
|
if (File.basename(destination) != ::File.basename(src))
|
|
|
|
dest += File::SEPARATOR + ::File.basename(src)
|
|
|
|
end
|
|
|
|
|
2005-04-18 01:52:21 +00:00
|
|
|
# Open the file on the remote side for writing and read
|
|
|
|
# all of the contents of the local file
|
2005-04-18 00:39:27 +00:00
|
|
|
dest_fd = client.fs.file.new(dest, "wb")
|
|
|
|
src_buf = ::IO.readlines(src).join
|
|
|
|
|
|
|
|
dest_fd.write(src_buf)
|
|
|
|
dest_fd.close
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Download one or more files from the remote computer to the local
|
|
|
|
# directory supplied in destination
|
|
|
|
def File.download(destination, *src_files)
|
|
|
|
src_files.each { |src|
|
|
|
|
dest = destination
|
|
|
|
|
|
|
|
if (::File.basename(destination) != File.basename(src))
|
|
|
|
dest += ::File::SEPARATOR + File.basename(src)
|
|
|
|
end
|
|
|
|
|
|
|
|
src_fd = client.fs.file.new(src, "rb")
|
|
|
|
dst_fd = ::File.new(dest, "wb")
|
|
|
|
|
2005-04-18 01:52:21 +00:00
|
|
|
# Keep transferring until EOF is reached...
|
2005-04-18 00:45:02 +00:00
|
|
|
begin
|
|
|
|
while ((data = src_fd.read) != nil)
|
|
|
|
dst_fd.write(data)
|
2005-04-18 00:39:27 +00:00
|
|
|
end
|
2005-04-18 00:45:02 +00:00
|
|
|
rescue EOFError
|
2005-04-18 00:39:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
src_fd.close
|
|
|
|
dst_fd.close
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Constructor
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
# Initializes and opens the specified file with the specified permissions
|
2005-04-11 03:37:02 +00:00
|
|
|
def initialize(name, mode = "r", perms = 0)
|
|
|
|
self.client = self.class.client
|
2005-04-11 04:56:24 +00:00
|
|
|
self.filed = _open(name, mode, perms)
|
2005-04-11 03:37:02 +00:00
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# IO implementators
|
|
|
|
#
|
|
|
|
##
|
2005-04-12 06:39:33 +00:00
|
|
|
|
|
|
|
# Returns whether or not the file has reach EOF
|
|
|
|
def eof
|
|
|
|
return self.filed.eof
|
|
|
|
end
|
2005-04-12 05:37:11 +00:00
|
|
|
|
|
|
|
# Returns the current position of the file pointer
|
|
|
|
def pos
|
|
|
|
return self.filed.tell
|
|
|
|
end
|
|
|
|
|
|
|
|
# Synonym for sysseek
|
|
|
|
def seek(offset, whence = SEEK_SET)
|
|
|
|
return self.sysseek(offset, whence)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Seeks to the supplied offset based on the supplied relativity
|
|
|
|
def sysseek(offset, whence = SEEK_SET)
|
|
|
|
return self.filed.seek(offset, whence)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Internal methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
# Creates a File channel using the supplied information
|
2005-04-11 04:56:24 +00:00
|
|
|
def _open(name, mode = "r", perms = 0)
|
2005-04-13 08:19:22 +00:00
|
|
|
return Rex::Post::Meterpreter::Channels::Pools::File.open(
|
|
|
|
self.client, name, mode, perms)
|
2005-04-11 03:37:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :client
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-04-17 07:17:52 +00:00
|
|
|
end; end; end; end; end; end
|