for this you can temporary impersonate with Linked token - so get own linked token, if it exist, set it to thread, call GetLogicalDrives()
and return to process token (Linked token have SECURITY_IMPERSONATION_LEVEL
== SecurityIdentification
as result it can be used very restrict )
#define BOOL_TO_ERR(b) ((b) ? NOERROR : GetLastError())
ULONG GetLogicalDrivesEx(PULONG pDrives)
{
HANDLE hToken;
ULONG err = BOOL_TO_ERR(OpenProcessToken(NtCurrentProcess(), TOKEN_QUERY, &hToken));
if (err != NOERROR)
{
return err;
}
union {
TOKEN_ELEVATION_TYPE tet;
TOKEN_LINKED_TOKEN tlt;
};
ULONG rcb;
err = BOOL_TO_ERR(GetTokenInformation(hToken, TokenElevationType, &tet, sizeof(tet), &rcb));
if (err == NOERROR)
{
if (tet == TokenElevationTypeFull)
{
err = BOOL_TO_ERR(GetTokenInformation(hToken, TokenLinkedToken, &tlt, sizeof(tlt), &rcb));
if (err == NOERROR)
{
if (NOERROR == (err = BOOL_TO_ERR(SetThreadToken(0, tlt.LinkedToken))))
{
err = (rcb = GetLogicalDrives()) ? NOERROR : GetLastError();
SetThreadToken(0, 0);
}
CloseHandle(tlt.LinkedToken);
}
}
else
{
err = (rcb = GetLogicalDrives()) ? NOERROR : GetLastError();
}
}
*pDrives = rcb;
return err;
}
void test()
{
ULONG Drives, Drives0 = GetLogicalDrives();
GetLogicalDrivesEx(&Drives);
WCHAR sz[32];
swprintf(sz, L"%08x %08x", Drives0, Drives);
MessageBoxW(0, sz, L"", MB_OK);
}
if no errors (GetLogicalDrivesEx
return NOERROR
) the Drives
is logical drives for not-elevated session, when Drives0 - for elevated (if of course you run as elevated)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…