Check Application Privilages

Started by WiiLF23, August 15, 2023, 04:13:23 AM

Previous topic - Next topic

WiiLF23

This will check the application if it is executed with administrative privileges. Perhaps this can be added as a parameter in the manifest, but this is useful nonetheless.

Include header

#include <stdbool.h>


Check privilege

bool IsAdmin(void) {
    BOOL isAdmin;
    SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
    PSID adminGroup;
   
    if (!AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
        DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminGroup)) {
        return false;
    }
   
    if (!CheckTokenMembership(NULL, adminGroup, &isAdmin)) {
        FreeSid(adminGroup);
        return false;
    }
   
    FreeSid(adminGroup);
    return isAdmin != 0;
}


Usage

static INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_INITDIALOG:
               if (!IsAdmin()) {
                     MessageBox(hwndDlg, L"Must be in Administrator mode.", L"MyApp", MB_OK | MB_ICONWARNING);
     EndDialog(hwndDlg, 0);
     DestroyWindow(hwndDlg);
             return TRUE;
}

          // other cases
    }

    return FALSE;
}

frankie

"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Vortex

Hello,

Nice work. Another method presented by the tool Reg Converter is to use the batch file portion below :

:IsAdmin
Reg.exe query "HKU\S-1-5-19\Environment"
If Not %ERRORLEVEL% EQU 0 (
Cls & Echo You must have administrator rights to continue ...
Pause & Exit
)
Cls
goto:eof


https://www.sordum.org/8478/reg-converter-v1-2/
Code it... That's all...

John Z

Quote from: WiiLF23 on August 15, 2023, 04:13:23 AM
This will check the application if it is executed with administrative privileges. Perhaps this can be added as a parameter in the manifest, but this is useful nonetheless.

Very nice!

John Z