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

125 lines
4.3 KiB
Ruby
Raw Normal View History

##
# This module requires Metasploit: http//metasploit.com/download
# 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-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"
}
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.
},
'License' => MSF_LICENSE,
'Author' => 'Borja Merino <bmerinofe[at]gmail.com>',
'Platform' => 'win',
'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']])
], self.class)
end
def run
driver = datastore['DRIVER_PATH']
start = datastore['START_TYPE']
error = datastore['ERROR_TYPE']
service = 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']
unless is_admin?
2014-01-03 19:39:14 +00:00
print_error("Administrator or better privileges needed. Try 'getsystem' first.")
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-30 00:49:13 +00:00
unless file_exist?(driver)
print_error("Driver #{driver} does not exist.")
return
end
2013-12-30 00:49:13 +00:00
inst = install_driver(driver: driver, start: start, name: name, error: error, service: service)
2013-12-17 12:10:27 +00:00
if inst
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
end
2013-12-30 00:49:13 +00:00
def install_driver(opts={})
service_all_access = 0xF01FF
2013-12-31 02:20:12 +00:00
service_type = SERVICE_TYPE[opts[:service]]
service_error_type = ERROR_TYPE[opts[:error]]
service_start_type = START_TYPE[opts[:start]]
advapi32 = client.railgun.advapi32
2013-12-30 00:49:13 +00:00
name = opts[:name]
2013-12-17 12:10:27 +00:00
# Default access: sc_manager_all_access (0xF003F)
ro = open_sc_manager()
2013-12-30 00:49:13 +00:00
rc = advapi32.CreateServiceA(ro, name, name, service_all_access, service_type, service_start_type, service_error_type, opts[:driver], nil, nil, nil, nil, nil)
2013-12-17 12:10:27 +00:00
close_sc_manager(ro)
2013-12-30 00:49:13 +00:00
if rc['GetLastError'] == Windows::Error::SUCCESS
print_status("Service object \"#{name}\" added to the Service Control Manager database.")
2013-12-17 12:10:27 +00:00
close_sc_manager(rc['return'])
return true
2013-12-30 00:49:13 +00:00
elsif rc['GetLastError'] == Windows::Error::SERVICE_EXISTS
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)
print_error("Path of driver file in \"#{name}\" service: #{service["Command"]}.")
else
print_error("There was an error opening the driver handler. GetLastError=#{rc['GetLastError']}.")
end
2013-12-17 12:10:27 +00:00
return false
end
end