Skip to content

Instantly share code, notes, and snippets.

@Pai-Po
Last active December 17, 2019 02:57
Show Gist options
  • Save Pai-Po/a1f1c02d2438beb0c7b2e67a8e8d14d5 to your computer and use it in GitHub Desktop.
Save Pai-Po/a1f1c02d2438beb0c7b2e67a8e8d14d5 to your computer and use it in GitHub Desktop.
WdmlibIoCreateDeviceSecure(IoCreateDeviceSecure) on Nt 6.x, doesn't support specified SID. this is a solution
// Copyright (c) 2019 Po. All rights reserved.
// Created on 2019-12-16
// Author: Po
{
status = IoCreateDevice(
DrvObj,
0,
&ustrDevName,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
&pDevObj );
if ( NT_SUCCESS( status ) ) {
POBJECT_TYPE* pObjType;
HANDLE hFile;
UNICODE_STRING ustrName;
RtlInitUnicodeString( &ustrName, L"IoDeviceObjectType" );
pObjType = (POBJECT_TYPE*)MmGetSystemRoutineAddress( &ustrName );
status = ObOpenObjectByPointer(
pDevObj,
OBJ_KERNEL_HANDLE,
NULL,
0,
*pObjType,
KernelMode,
&hFile );
if ( NT_SUCCESS( status ) ) {
//S-1-5-21-2919905370-567116316-3914385833-1000
PSID pUserSID = NULL;
ULONG ulSubAuthCont = 5;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
ULONG ulSID = sizeof( SID ) + ( ulSubAuthCont - 1 ) * sizeof( ULONG );
pUserSID = ExAllocatePoolWithTag( NonPagedPool, ulSID, 'OPOP' );
if ( pUserSID ) {
RtlZeroMemory( pUserSID, ulSID );
RtlInitializeSid( pUserSID, &NtAuthority, (UCHAR)ulSubAuthCont );
*RtlSubAuthoritySid( pUserSID, 0 ) = 21;
*RtlSubAuthoritySid( pUserSID, 1 ) = 2919905370;
*RtlSubAuthoritySid( pUserSID, 2 ) = 567116316;
*RtlSubAuthoritySid( pUserSID, 3 ) = 3914385833;
*RtlSubAuthoritySid( pUserSID, 4 ) = 1000;
SetObjectSID( hFile, pUserSID );
ExFreePoolWithTag( pUserSID, 'OPOP');
ObCloseHandle( hFile, KernelMode );
}
}
}
}
/* currently support only one sid */
NTSTATUS SetObjectSID( HANDLE FileHandle, PSID Sid ) {
PVOID pDacl = NULL;
ULONG ulDaclLen = 0;
SECURITY_DESCRIPTOR sa;
NTSTATUS status = STATUS_UNSUCCESSFUL;
status = RtlCreateSecurityDescriptor(
&sa,
SECURITY_DESCRIPTOR_REVISION );
if ( !NT_SUCCESS( status ) )
goto _error;
ulDaclLen = sizeof( ACL ) + sizeof( ACCESS_ALLOWED_ACE ) + RtlLengthSid( Sid );
pDacl = ExAllocatePoolWithTag( PagedPool, ulDaclLen, 'lcaD' );
if ( pDacl == NULL ) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto _error;
}
status = RtlCreateAcl( pDacl, ulDaclLen, ACL_REVISION );
if ( !NT_SUCCESS( status ) )
goto _error;
status = RtlAddAccessAllowedAce( pDacl,
ACL_REVISION,
FILE_ALL_ACCESS,
Sid );
if ( !NT_SUCCESS( status ) )
goto _error;
status = RtlSetDaclSecurityDescriptor( &sa,
TRUE,
pDacl,
FALSE );
if ( !NT_SUCCESS( status ) )
goto _error;
sa.Control |= SE_DACL_PRESENT;
sa.Control |= SE_DACL_DEFAULTED;
sa.Control |= SE_DACL_PROTECTED;
//sa.Control |= SE_SELF_RELATIVE;
status = ZwSetSecurityObject(
FileHandle,
DACL_SECURITY_INFORMATION,
&sa );
if ( !NT_SUCCESS( status ) )
goto _error;
goto _exit;
_error:
_exit:
if ( pDacl ) {
ExFreePoolWithTag( pDacl, 'lcaD' );
pDacl = NULL;
}
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment