Exploit-DB #34131
Description: Microsoft Windows XP SP3 - 'BthPan.sys' Arbitrary Write Privilege Escalation
Type: local
Platform: windows
Date: 2014-07-21
Author: KoreLogic

Exploit:
"""
Title: Microsoft XP SP3 BthPan.sys Arbitrary Write Privilege Escalation
Advisory ID: KL-001-2014-002
Publication Date: 2014-07-18
Publication URL: https://www.korelogic.com/Resources/Advisories/KL-001-2014-002.txt


1. Vulnerability Details

     Affected Vendor: Microsoft
     Affected Product: Bluetooth Personal Area Networking
     Affected Versions: 5.1.2600.5512
     Platform: Microsoft Windows XP SP3
     CWE Classification: CWE-123: Write-what-where Condition
     Impact: Privilege Escalation
     Attack vector: IOCTL
     CVE ID: CVE-2014-4971

2. Vulnerability Description

     A vulnerability within the BthPan module allows an attacker to
     inject memory they control into an arbitrary location they
     define. This can be used by an attacker to overwrite
     HalDispatchTable+0x4 and execute arbitrary code by subsequently
     calling NtQueryIntervalProfile.

3. Technical Description

     A userland process can create a handle into the BthPan device
     and subsequently make DeviceIoControlFile() calls into that
     device. During the IRP handler routine for 0x0012b814 the user
     provided OutputBuffer address is not validated. This allows an
     attacker to specify an arbitrary address and write
     (or overwrite) the memory residing at the specified address.
     This is classicaly known as a write-what-where vulnerability and
     has well known exploitation methods associated with it.

     A stack trace from our fuzzing can be seen below. In our fuzzing
     testcase, the specified OutputBuffer in the DeviceIoControlFile()
     call is 0xffff0000.

STACK_TEXT:
b1e065b8 8051cc7f 00000050 ffff0[PII_REDACTED]1 nt!KeBugCheckEx+0x1b
b1e06618 805405d4 00000001 ffff0[PII_REDACTED]0 nt!MmAccessFault+0x8e7
b1e06618 804f3b76 00000001 ffff0[PII_REDACTED]0 nt!KiTrap0E+0xcc
b1e066e8 804fdaf1 8216cc80 b1e06734 b1e06728 nt!IopCompleteRequest+0x92
b1e06738 [PII_REDACTED] [PII_REDACTED] nt!KiDeliverApc+0xb3
b1e06758 804fb4a7 8055b1c0 81bdeda8 b1e0677c nt!KiUnlockDispatcherDatabase+0xa8
b1e06768 80534b09 8055b1c0 81f7a290 81f016b8 nt!KeInsertQueue+0x25
b1e0677c f83e26ec 81f7a[PII_REDACTED]0 b1e067a8 nt!ExQueueWorkItem+0x1b
b1e0678c b272b5a1 81f7a[PII_REDACTED]0 81e002d8 NDIS!NdisScheduleWorkItem+0x21
b1e067a8 b273a544 b1e067c8 b273a30e 8216cc40 bthpan!BthpanReqAdd+0x16b
b1e069e8 b273a62b 8216cc40 00000258 81e6f550 bthpan!IoctlDispatchDeviceControl+0x1a8
b1e06a00 f83e94bb 81e6f550 8216cc40 81d74d68 bthpan!IoctlDispatchMajor+0x93
b1e06a18 f83e9949 81e6f550 8216cc40 8217e6e8 NDIS!ndisDummyIrpHandler+0x48
b1e06ab4 804ee129 81e6f550 8216cc40 806d32d0 NDIS!ndisDeviceControlIrpHandler+0x5c
b1e06ac4 80574e56 8216ccb0 81d74d68 8216cc40 nt!IopfCallDriver+0x31
b1e06ad8 80575d11 81e6f550 8216cc40 81d74d68 nt!IopSynchronousServiceTail+0x70
b1e06b80 8056e57c 000006a8 [PII_REDACTED] nt!IopXxxControlFile+0x5e7
b1e06bb4 b1a2506f 000006a8 [PII_REDACTED] nt!NtDeviceIoControlFile+0x2a
WARNING: Stack unwind information not available. Following frames may be wrong.

     Reviewing the FOLLOWUP_IP value from the WinDBG '!analyze -v'
     command shows the fault originating in the bthpan driver.

FOLLOWUP_IP:
bthpan!BthpanReqAdd+16b
b272b5a1 ebc2            jmp     bthpan!BthpanReqAdd+0x12f (b272b565)

     Reviewing the TRAP_FRAME at the time of crash we can see
     IopCompleteRequest() copying data from InputBuffer into the
     OutputBuffer. InputBuffer is another parameter provided to the
     DeviceIoControlFile() function and is therefore controllable by
     the attacker. The edi register contains the invalid address
     provided during the fuzz testcase.

TRAP_FRAME:  b1e06630 -- (.trap 0xffffffffb1e06630)
ErrCode = 00000002
eax=0000006a ebx=8216cc40 ecx=0000001a edx=00000001 esi=81e002d8 edi=ffff0000
eip=804f3b76 esp=b1e066a4 ebp=b1e066e8 iopl=0         nv up ei pl nz na po cy
cs=0008  ss=0010  ds=0023  es=0023  fs=0030  gs=0000             efl=00010203
nt!IopCompleteRequest+0x92:
804f3b76 f3a5            rep movs dword ptr es:[edi],dword ptr [esi]

     A write-what-where vulnerability can be leveraged to obtain
     escalated privileges. To do so, an attacker will need to
     allocate memory in userland that is populated with shellcode
     designed to find the Token for PID 4 (System) and then overwrite
     the token for its own process. By leveraging the vulnerability
     in BthPan it is then possible to overwrite the pointer at
     HalDispatchTable+0x4 with a pointer to our shellcode. Calling
     NtQueryIntervalProfile() will subsequently call
     HalDispatchTable+0x4, execute our shellcode, and elevate the
     privilege of the exploit process.

4. Mitigation and Remediation Recommendation

     None. A patch is not likely to be forthcoming from the vendor.

5. Credit

     This vulnerability was discovered by Matt Bergin of KoreLogic
     Security, Inc.

6. Disclosure Timeline

   2014.04.28 - Initial contact; sent Microsoft report and PoC.
   2014.04.28 - Microsoft acknowledges receipt of vulnerability
                report; states XP is no longer supported and asks if
                the vulnerability affects other versions of Windows.
   2014.04.29 - KoreLogic asks Microsoft for clarification of their
                support policy for XP.
   2014.04.29 - Microsoft says XP-only vulnerabilities will not be
                addressed with patches.
   2014.04.29 - KoreLogic asks if Microsoft intends to address the
                vulnerability report.
   2014.04.29 - Microsoft opens case to investigate the impact of the
                vulnerability on non-XP systems.
   2014.05.06 - Microsoft asks again if this vulnerability affects
                non-XP systems.
   2014.05.14 - KoreLogic informs Microsoft that the vulnerability
                report is for XP and other Windows versions have not
                been examined.
   2014.06.11 - KoreLogic inf