NO

Author Topic: Check Application Privilages  (Read 3271 times)

Offline WiiLF23

  • Member
  • *
  • Posts: 89
Check Application Privilages
« 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.

Include header
Code: [Select]
#include <stdbool.h>

Check privilege
Code: [Select]
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
Code: [Select]
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;
}
« Last Edit: August 15, 2023, 04:15:28 AM by WiiLF23 »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Check Application Privilages
« Reply #1 on: August 15, 2023, 10:37:41 AM »
 :)
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: Check Application Privilages
« Reply #2 on: August 15, 2023, 11:36:44 AM »
Hello,

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

Code: [Select]
: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...

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Check Application Privilages
« Reply #3 on: August 15, 2023, 12:34:24 PM »
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