Microsoft Windows – Double Dereference in NtEnumerateKey Elevation of Privilege

  • 作者: Google Security Research
    日期: 2018-09-19
  • 类别:
    平台:
  • 来源:https://www.exploit-db.com/exploits/45436/
  • Windows: Double Dereference in NtEnumerateKey Elevation of Privilege
    Platform: Windows 10 1803 (not vulnerable in earlier versions)
    Class: Elevation of Privilege
    
    Summary: A number of registry system calls do not correctly handle pre-defined keys resulting in a double dereference which can lead to EoP.
    
    Description:
    
    The registry contains a couple of predefined keys, to generate performance information. These actually exist in the the machine hive under \Registry\Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib. When these keys are opened the kernel returns a status code of STATUS_PREDEFINED_KEY, but it also returns a handle to the key. 
    
    The kernel doesn’t allow these keys to be used for many operations such as enumeration of subkeys and values, so these system calls check before the key is used and returns STATUS_INVALID_HANDLE. The code for this in NtEnumerateKey looks like the following:
    
    status = ObReferenceObjectByHandle(Handle, KEY_ENUMERATE, CmKeyObjectType, &Object);
    if ( status >= 0 && Object->Type != 'ky02' ){
    status = STATUS_INVALID_HANDLE;
    ObfDereferenceObject(Object); <-- Dereference object,
     }
     if (status < 0) {
     goto EXIT_LABEL;
     }
    
    This code in itself is fine, but in 1803 at the exit label we find the following additional code:
    
    if (Object)
    ObfDereferenceObject(Object);
    
    This results in the object being dereferenced twice. Due the way the object reference counting works this will not be noticed until the key handle is closed, which results in a REFERENCE_BY_POINTER bugcheck being generated. This might only be a local DoS if the issue was caught earlier, but because the caller can do other things with the corrupted object we can potentially turn this into a UaF and from there elevate privileges. For example the provided PoC passes the handle to NtDuplicateObject which results in the kernel modifying a free pool allocation.
    
    I’d recommend ensuring you check all similar functions such as NtEnumerateValueKey as there seems to be a common issue, perhaps it’s a macro or template which is generating the bad code.
    
    The following is an example dump from a crash, at the end the !pool command is used on the object address to demonstrate the memory allocation was freed before being modified.
    
    Use !analyze -v to get detailed debugging information.
    
    BugCheck 18, {0, ffff8e0db3a0f7a0, 2, ffffffffffffffff}
    
    Probably caused by : ntkrnlmp.exe ( nt!ObfDereferenceObjectWithTag+155dd9 )
    
    Followup: MachineOwner
    ---------
    
    0: kd> !analyze -v
    *******************************************************************************
    * *
    *Bugcheck Analysis*
    * *
    *******************************************************************************
    
    REFERENCE_BY_POINTER (18)
    Arguments:
    Arg1: 0000000000000000, Object type of the object whose reference count is being lowered
    Arg2: ffff8e0db3a0f7a0, Object whose reference count is being lowered
    Arg3: 0000000000000002, Reserved
    Arg4: ffffffffffffffff, Reserved
    	The reference count of an object is illegal for the current state of the object.
    	Each time a driver uses a pointer to an object the driver calls a kernel routine
    	to increment the reference count of the object. When the driver is done with the
    	pointer the driver calls another kernel routine to decrement the reference count.
    	Drivers must match calls to the increment and decrement routines. This bugcheck
    	can occur because an object's reference count goes to zero while there are still
    	open handles to the object, in which case the fourth parameter indicates the number
    	of opened handles. It may also occur when the object’s reference count drops below zero
    	whether or not there are open handles to the object, and in that case the fourth parameter
    	contains the actual value of the pointer references count.
    
    Debugging Details:
    ------------------
    
    
    DUMP_CLASS: 1
    
    DUMP_QUALIFIER: 401
    
    BUILD_VERSION_STRING:17134.1.amd64fre.rs4_release.180410-1804
    
    SYSTEM_MANUFACTURER:Microsoft Corporation
    
    VIRTUAL_MACHINE:HyperV
    
    SYSTEM_PRODUCT_NAME:Virtual Machine
    
    SYSTEM_SKU:None
    
    SYSTEM_VERSION:Hyper-V UEFI Release v3.0
    
    BIOS_VENDOR:Microsoft Corporation
    
    BIOS_VERSION:Hyper-V UEFI Release v3.0
    
    BIOS_DATE:03/02/2018
    
    BASEBOARD_MANUFACTURER:Microsoft Corporation
    
    BASEBOARD_PRODUCT:Virtual Machine
    
    BASEBOARD_VERSION:Hyper-V UEFI Release v3.0
    
    DUMP_TYPE:1
    
    BUGCHECK_P1: 0
    
    BUGCHECK_P2: ffff8e0db3a0f7a0
    
    BUGCHECK_P3: 2
    
    BUGCHECK_P4: ffffffffffffffff
    
    CPU_COUNT: 2
    
    CPU_MHZ: a98
    
    CPU_VENDOR:GenuineIntel
    
    CPU_FAMILY: 6
    
    CPU_MODEL: 8e
    
    CPU_STEPPING: 9
    
    CPU_MICROCODE: 6,8e,9,0 (F,M,S,R)SIG: FFFFFFFF'00000000 (cache) FFFFFFFF'00000000 (init)
    
    DEFAULT_BUCKET_ID:WIN8_DRIVER_FAULT
    
    BUGCHECK_STR:0x18
    
    PROCESS_NAME:PoC_NtEnumerateKey_EoP.exe
    
    CURRENT_IRQL:0
    
    ANALYSIS_SESSION_HOST:DESKTOP-JA4I3EF
    
    ANALYSIS_SESSION_TIME:06-19-2018 13:36:38.0158
    
    ANALYSIS_VERSION: 10.0.15063.468 amd64fre
    
    LAST_CONTROL_TRANSFER:from fffff80357473ab9 to fffff8035742c330
    
    STACK_TEXT:
    ffffb78e`5a91f678 fffff803`57473ab9 : 00000000`00000018 00000000`00000000 ffff8e0d`b3a0f7a0 00000000`00000002 : nt!KeBugCheckEx
    ffffb78e`5a91f680 fffff803`57751b9b : 00000000`00000000 00000000`00000000 00020019`00000000 ffffb78e`5a91f7c0 : nt!ObfDereferenceObjectWithTag+0x155dd9
    ffffb78e`5a91f6c0 fffff803`5775157d : ffffe58b`763cf580 00000a50`00000040 ffffe58b`75c75f20 00000000`00000001 : nt!ObDuplicateObject+0x58b
    ffffb78e`5a91f980 fffff803`5743c943 : ffffe58b`763c4700 00000000`008fe098 ffffb78e`5a91fa28 00000000`00000000 : nt!NtDuplicateObject+0x12d
    ffffb78e`5a91fa10 00007ffa`f3cda634 : 00000000`00000000 00000000`00000000 00000000`00000000 00000000`00000000 : nt!KiSystemServiceCopyEnd+0x13
    00000000`008fe078 00000000`00000000 : 00000000`00000000 00000000`00000000 00000000`00000000 00000000`00000000 : 0x00007ffa`f3cda634
    
    
    STACK_COMMAND:kb
    
    THREAD_SHA1_HASH_MOD_FUNC:4fc60443ee144421725d502d6e3b53056b889c26
    
    THREAD_SHA1_HASH_MOD_FUNC_OFFSET:c219a3da6c3050112ed885b130b5ebbab9cdff96
    
    THREAD_SHA1_HASH_MOD:f08ac56120cad14894587db086f77ce277bfae84
    
    FOLLOWUP_IP: 
    nt!ObfDereferenceObjectWithTag+155dd9
    fffff803`57473ab9 ccint 3
    
    FAULT_INSTR_CODE:4e8d48cc
    
    SYMBOL_STACK_INDEX:1
    
    SYMBOL_NAME:nt!ObfDereferenceObjectWithTag+155dd9
    
    FOLLOWUP_NAME:MachineOwner
    
    MODULE_NAME: nt
    
    IMAGE_NAME:ntkrnlmp.exe
    
    DEBUG_FLR_IMAGE_TIMESTAMP:5b1a4590
    
    BUCKET_ID_FUNC_OFFSET:155dd9
    
    FAILURE_BUCKET_ID:0x18_OVER_DEREFERENCE_nt!ObfDereferenceObjectWithTag
    
    BUCKET_ID:0x18_OVER_DEREFERENCE_nt!ObfDereferenceObjectWithTag
    
    PRIMARY_PROBLEM_CLASS:0x18_OVER_DEREFERENCE_nt!ObfDereferenceObjectWithTag
    
    TARGET_TIME:2018-06-19T20:33:20.000Z
    
    OSBUILD:17134
    
    OSSERVICEPACK:0
    
    SERVICEPACK_NUMBER: 0
    
    OS_REVISION: 0
    
    SUITE_MASK:272
    
    PRODUCT_TYPE:1
    
    OSPLATFORM_TYPE:x64
    
    OSNAME:Windows 10
    
    OSEDITION:Windows 10 WinNt TerminalServer SingleUserTS
    
    OS_LOCALE:
    
    USER_LCID:0
    
    OSBUILD_TIMESTAMP:2018-06-08 02:00:00
    
    BUILDDATESTAMP_STR:180410-1804
    
    BUILDLAB_STR:rs4_release
    
    BUILDOSVER_STR:10.0.17134.1.amd64fre.rs4_release.180410-1804
    
    ANALYSIS_SESSION_ELAPSED_TIME:13a4
    
    ANALYSIS_SOURCE:KM
    
    FAILURE_ID_HASH_STRING:km:0x18_over_dereference_nt!obfdereferenceobjectwithtag
    
    FAILURE_ID_HASH:{4139309c-4e9f-52f0-ac5e-4041e7a86a20}
    
    Followup: MachineOwner
    ---------
    
    0: kd> !pool ffff8e0db3a0f7a0
    Pool page ffff8e0db3a0f7a0 region is Paged pool
     ffff8e0db3a0f000 size:150 previous size:0(Free )FMfn
     ffff8e0db3a0f150 size:130 previous size:150(Free) Free
     ffff8e0db3a0f280 size: 40 previous size:130(Allocated)MPan
     ffff8e0db3a0f2c0 size: 50 previous size: 40(Free )SeAt
     ffff8e0db3a0f310 size: c0 previous size: 50(Free )Se
     ffff8e0db3a0f3d0 size: 50 previous size: c0(Free) Free
     ffff8e0db3a0f420 size:220 previous size: 50(Allocated)FMfn
     ffff8e0db3a0f640 size: a0 previous size:220(Allocated)Sect
     ffff8e0db3a0f6e0 size: 50 previous size: a0(Free) Free
    *ffff8e0db3a0f730 size:100 previous size: 50(Free ) *Key 
    		Pooltag Key: Key objects
     ffff8e0db3a0f830 size: 10 previous size:100(Free) Free
     ffff8e0db3a0f840 size: e0 previous size: 10(Allocated)NtFs
     ffff8e0db3a0f920 size: c0 previous size: e0(Allocated)FIcs
     ffff8e0db3a0f9e0 size: c0 previous size: c0(Free )SeTd
     ffff8e0db3a0faa0 size:560 previous size: c0(Allocated)Ntff
    
    
    
    
    Proof of Concept:
    
    I’ve provided a PoC as a C# project. This only demonstrates the issue and proves that it would be possible to force this issue into a UaF even with the mitigations on reference counting.
    
    1) Compile the C# project. It will need to grab the NtApiDotNet from NuGet to work.
    2) Run the PoC on an machine with Windows 10 1803, I’ve only tested x64.
    3) The OS should crash, inspect it in a kernel debugger or from the crash dump.
    
    Expected Result:
    The OS ignores the pre-defined key as expected.
    
    Observed Result:
    The object’s reference count is corrupted.
    
    
    Proof of Concept:
    https://gitlab.com/exploit-database/exploitdb-bin-sploits/-/raw/main/bin-sploits/45436.zip