2011-01-11 17:53:24 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
class Post
|
|
|
|
|
|
|
|
module File
|
|
|
|
|
|
|
|
#
|
|
|
|
# Writes a given string to a file specified
|
|
|
|
#
|
|
|
|
def file_local_write(file2wrt, data2wrt)
|
|
|
|
if not ::File.exists?(file2wrt)
|
|
|
|
::FileUtils.touch(file2wrt)
|
|
|
|
end
|
|
|
|
|
|
|
|
output = ::File.open(file2wrt, "a")
|
|
|
|
data2wrt.each_line do |d|
|
|
|
|
output.puts(d)
|
|
|
|
end
|
|
|
|
output.close
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns a MD5 checksum of a given local file
|
|
|
|
#
|
|
|
|
def file_local_digestmd5(file2md5)
|
|
|
|
if not ::File.exists?(file2md5)
|
|
|
|
raise "File #{file2md5} does not exists!"
|
|
|
|
else
|
|
|
|
require 'digest/md5'
|
|
|
|
chksum = nil
|
|
|
|
chksum = Digest::MD5.hexdigest(::File.open(file2md5, "rb") { |f| f.read})
|
|
|
|
return chksum
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns a SHA1 checksum of a given local file
|
|
|
|
#
|
|
|
|
def file_local_digestsha1(file2sha1)
|
|
|
|
if not ::File.exists?(file2sha1)
|
|
|
|
raise "File #{file2sha1} does not exists!"
|
|
|
|
else
|
|
|
|
require 'digest/sha1'
|
|
|
|
chksum = nil
|
|
|
|
chksum = Digest::SHA1.hexdigest(::File.open(file2sha1, "rb") { |f| f.read})
|
|
|
|
return chksum
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns a SHA256 checksum of a given local file
|
|
|
|
#
|
|
|
|
def file_local_digestsha2(file2sha2)
|
|
|
|
if not ::File.exists?(file2sha2)
|
|
|
|
raise "File #{file2sha2} does not exists!"
|
|
|
|
else
|
|
|
|
require 'digest/sha2'
|
|
|
|
chksum = nil
|
|
|
|
chksum = Digest::SHA256.hexdigest(::File.open(file2sha2, "rb") { |f| f.read})
|
|
|
|
return chksum
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Platform-agnostic file read. Returns contents of remote file +file_name+
|
|
|
|
# as a String.
|
|
|
|
#
|
|
|
|
def read_file(file_name)
|
|
|
|
data = nil
|
|
|
|
if session.type == "meterpreter"
|
|
|
|
data = read_file_meterpreter(file_name)
|
2011-05-30 22:14:11 +00:00
|
|
|
elsif session.type == "shell"
|
|
|
|
if session.platform == "windows"
|
|
|
|
data = session.shell_command_token("type \"#{file_name}\"")
|
|
|
|
else
|
2011-05-30 22:16:19 +00:00
|
|
|
data = session.shell_command_token("cat \'#{file_name}\'")
|
2011-05-30 22:14:11 +00:00
|
|
|
end
|
|
|
|
|
2011-01-11 17:53:24 +00:00
|
|
|
end
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2011-05-21 16:24:34 +00:00
|
|
|
#
|
2011-05-30 00:15:04 +00:00
|
|
|
# Platform-agnostic file write. Writes given object content to a remote file.
|
|
|
|
# Returns Boolean true if successful
|
|
|
|
#
|
|
|
|
def write_file(file_name, data)
|
|
|
|
if session.type == "meterpreter"
|
|
|
|
fd = session.fs.file.new(file_name, "wb")
|
|
|
|
fd.write(data)
|
|
|
|
fd.close
|
|
|
|
elsif session.respond_to? :shell_command_token
|
2011-05-30 22:14:11 +00:00
|
|
|
if session.platform == "windows"
|
|
|
|
session.shell_command_token("echo #{data} > \"#{file_name}\"")
|
|
|
|
else
|
|
|
|
session.shell_command_token("echo \'#{data}\' > \'#{file_name}\'")
|
|
|
|
end
|
2011-05-21 16:24:34 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
return true
|
2011-05-30 00:15:04 +00:00
|
|
|
end
|
2011-05-21 16:24:34 +00:00
|
|
|
|
2011-06-10 03:27:09 +00:00
|
|
|
#
|
2011-05-30 00:15:04 +00:00
|
|
|
# Platform-agnostic file append. Appends given object content to a remote file.
|
|
|
|
# Returns Boolean true if successful
|
|
|
|
#
|
|
|
|
def append_file(file_name, data)
|
|
|
|
if session.type == "meterpreter"
|
|
|
|
fd = session.fs.file.new(file_name, "wab")
|
|
|
|
fd.write(data)
|
|
|
|
fd.close
|
|
|
|
elsif session.respond_to? :shell_command_token
|
2011-05-30 22:14:11 +00:00
|
|
|
if session.platform == "windows"
|
|
|
|
session.shell_command_token("echo #{data} >> \"#{file_name}\"")
|
|
|
|
else
|
|
|
|
session.shell_command_token("echo \'#{data}\' >> \'#{file_name}\'")
|
|
|
|
end
|
2011-05-30 00:15:04 +00:00
|
|
|
end
|
2011-05-21 16:24:34 +00:00
|
|
|
return true
|
2011-05-30 00:15:04 +00:00
|
|
|
end
|
2011-05-21 16:24:34 +00:00
|
|
|
|
|
|
|
|
2011-01-11 17:53:24 +00:00
|
|
|
protected
|
|
|
|
#
|
|
|
|
# Meterpreter-specific file read. Returns contents of remote file
|
2011-07-14 21:59:35 +00:00
|
|
|
# +file_name+ as a String or nil if there was an error
|
2011-01-11 17:53:24 +00:00
|
|
|
#
|
|
|
|
def read_file_meterpreter(file_name)
|
2011-07-14 21:59:35 +00:00
|
|
|
begin
|
|
|
|
fd = session.fs.file.new(file_name, "rb")
|
|
|
|
rescue ::Rex::Post::Meterpreter::RequestError => e
|
|
|
|
print_error("Failed to open file: #{e.class} : #{e}")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2011-01-11 17:53:24 +00:00
|
|
|
data = ''
|
|
|
|
begin
|
|
|
|
until fd.eof?
|
|
|
|
data << fd.read
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
fd.close
|
|
|
|
end
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|