2013-12-13 23:07:04 +00:00
|
|
|
##
|
2014-10-17 16:47:33 +00:00
|
|
|
# This module requires Metasploit: http://metasploit.com/download
|
2013-12-13 23:07:04 +00:00
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
|
|
|
##
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Post
|
|
|
|
|
|
|
|
include Msf::Post::File
|
|
|
|
include Msf::Post::Windows::Priv
|
2013-12-17 12:10:27 +00:00
|
|
|
include Msf::Post::Windows::Services
|
2014-01-02 19:41:37 +00:00
|
|
|
include Msf::Post::Windows::Error
|
2013-12-13 23:07:04 +00:00
|
|
|
|
2013-12-31 02:20:12 +00:00
|
|
|
START_TYPE = {
|
|
|
|
"demand" => "SERVICE_DEMAND_START",
|
|
|
|
"boot" => "SERVICE_BOOT_START",
|
|
|
|
"auto" => "SERVICE_AUTO_START",
|
|
|
|
"disabled" => "SERVICE_DISABLED",
|
|
|
|
"system" => "SERVICE_SYSTEM_START"
|
|
|
|
}
|
|
|
|
|
|
|
|
ERROR_TYPE = {
|
|
|
|
"critical" => "SERVICE_ERROR_CRITICAL",
|
|
|
|
"normal" => "SERVICE_ERROR_NORMAL",
|
|
|
|
"severe" => "SERVICE_ERROR_SEVERE",
|
|
|
|
"ignore" => "SERVICE_ERROR_IGNORE"
|
|
|
|
}
|
|
|
|
|
|
|
|
SERVICE_TYPE = {
|
|
|
|
"kernel" => "SERVICE_KERNEL_DRIVER",
|
|
|
|
"file_system" => "SERVICE_FILE_SYSTEM_DRIVER",
|
|
|
|
"adapter" => "SERVICE_ADAPTER",
|
|
|
|
"recognizer" => "SERVICE_RECOGNIZER_DRIVER"
|
|
|
|
}
|
|
|
|
|
2013-12-13 23:07:04 +00:00
|
|
|
def initialize(info={})
|
|
|
|
super( update_info( info,
|
|
|
|
'Name' => 'Windows Manage Driver Loader',
|
|
|
|
'Description' => %q{
|
2014-01-02 21:37:58 +00:00
|
|
|
This module loads a KMD (Kernel Mode Driver) using the Windows Service API.
|
2013-12-13 23:07:04 +00:00
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Author' => 'Borja Merino <bmerinofe[at]gmail.com>',
|
2014-06-11 20:10:33 +00:00
|
|
|
'Platform' => 'win',
|
2013-12-13 23:07:04 +00:00
|
|
|
'SessionTypes' => [ 'meterpreter' ]
|
|
|
|
))
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
2013-12-30 00:49:13 +00:00
|
|
|
OptString.new('DRIVER_PATH', [true, 'Driver path in %SYSTEMROOT%. Example: c:\\windows\\system32\\msf.sys']),
|
|
|
|
OptString.new('DRIVER_NAME', [false, 'Driver Name.']),
|
|
|
|
OptEnum.new('START_TYPE', [true, 'Start type.', 'auto', [ 'boot', 'system', 'auto', 'demand','disabled']]),
|
|
|
|
OptEnum.new('SERVICE_TYPE', [true, 'Service type.', 'kernel', [ 'kernel', 'file_system', 'adapter', 'recognizer']]),
|
|
|
|
OptEnum.new('ERROR_TYPE', [true, 'Error type.', 'ignore', [ 'ignore', 'normal', 'severe', 'critical']])
|
2013-12-13 23:07:04 +00:00
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
driver = datastore['DRIVER_PATH']
|
2014-01-03 15:55:03 +00:00
|
|
|
start = START_TYPE[datastore['START_TYPE']]
|
|
|
|
error = ERROR_TYPE[datastore['ERROR_TYPE']]
|
|
|
|
service = SERVICE_TYPE[datastore['SERVICE_TYPE']]
|
2013-12-30 00:49:13 +00:00
|
|
|
|
|
|
|
name = datastore['DRIVER_NAME'].blank? ? Rex::Text.rand_text_alpha((rand(8)+6)) : datastore['DRIVER_NAME']
|
2013-12-13 23:07:04 +00:00
|
|
|
|
|
|
|
unless is_admin?
|
2014-01-03 19:39:14 +00:00
|
|
|
print_error("Administrator or better privileges needed. Try 'getsystem' first.")
|
2013-12-13 23:07:04 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-01-02 18:54:54 +00:00
|
|
|
unless driver =~ Regexp.new(Regexp.escape(expand_path("%SYSTEMROOT%")), Regexp::IGNORECASE)
|
2013-12-30 00:49:13 +00:00
|
|
|
print_error("The driver must be inside %SYSTEMROOT%.")
|
|
|
|
return
|
|
|
|
end
|
2013-12-13 23:07:04 +00:00
|
|
|
|
2013-12-30 00:49:13 +00:00
|
|
|
unless file_exist?(driver)
|
|
|
|
print_error("Driver #{driver} does not exist.")
|
2013-12-13 23:07:04 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-01-03 15:55:03 +00:00
|
|
|
inst = install_driver(name, path: driver, starttype: start, error_control: error, service_type: service)
|
2013-12-30 00:49:13 +00:00
|
|
|
|
2014-01-03 15:55:03 +00:00
|
|
|
if inst == Windows::Error::SUCCESS
|
2013-12-17 12:10:27 +00:00
|
|
|
ss = service_start(name)
|
|
|
|
case ss
|
2014-01-02 19:41:37 +00:00
|
|
|
when Windows::Error::SUCCESS
|
2013-12-17 12:10:27 +00:00
|
|
|
print_good("Driver loaded successfully.")
|
2014-01-02 19:41:37 +00:00
|
|
|
when Windows::Error::SERVICE_ALREADY_RUNNING
|
2013-12-30 00:49:13 +00:00
|
|
|
print_error("Service already started.")
|
2014-01-02 19:41:37 +00:00
|
|
|
when Windows::Error::SERVICE_DISABLED
|
2013-12-17 12:10:27 +00:00
|
|
|
print_error("Service disabled.")
|
|
|
|
else
|
|
|
|
print_error("There was an error starting the service.")
|
|
|
|
end
|
|
|
|
end
|
2013-12-13 23:07:04 +00:00
|
|
|
end
|
|
|
|
|
2014-01-03 15:55:03 +00:00
|
|
|
def install_driver(name, opts={})
|
|
|
|
rc = service_create(name, opts)
|
2013-12-30 00:49:13 +00:00
|
|
|
|
2014-01-03 15:55:03 +00:00
|
|
|
if rc == Windows::Error::SUCCESS
|
2013-12-30 00:49:13 +00:00
|
|
|
print_status("Service object \"#{name}\" added to the Service Control Manager database.")
|
2013-12-17 12:10:27 +00:00
|
|
|
return true
|
2014-01-03 15:55:03 +00:00
|
|
|
elsif rc == Windows::Error::SERVICE_EXISTS
|
2013-12-13 23:07:04 +00:00
|
|
|
print_error("The specified service already exists.")
|
2013-12-17 12:10:27 +00:00
|
|
|
# Show ImagePath just to know if the service corresponds to the desired driver.
|
|
|
|
service = service_info(name)
|
2014-01-03 15:55:03 +00:00
|
|
|
print_error("Path of driver file in \"#{name}\" service: #{service[:path]}.")
|
2013-12-13 23:07:04 +00:00
|
|
|
else
|
2014-01-03 15:55:03 +00:00
|
|
|
print_error("There was an error opening the driver handler. GetLastError=#{rc}.")
|
2013-12-13 23:07:04 +00:00
|
|
|
end
|
2013-12-17 12:10:27 +00:00
|
|
|
return false
|
2013-12-13 23:07:04 +00:00
|
|
|
end
|
|
|
|
end
|