Détection du droit admin du process en cours.

jlbrd Messages postés 13 Date d'inscription mercredi 29 octobre 2003 Statut Membre Dernière intervention 19 avril 2005 - 19 sept. 2004 à 21:11
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 19 sept. 2004 à 21:54
Bonjour, dans mon prog certains traitements nécessitent les droits admin. Comme tester que le programme possède ces droits ou pas ?

2 réponses

cs_Nebula Messages postés 787 Date d'inscription samedi 8 juin 2002 Statut Membre Dernière intervention 7 juin 2007 2
19 sept. 2004 à 21:27
Essaie d'ouvrir une clé dans HKEY_LOCAL_MACHINE en écriture, si l'utilisateur n'est pas admin çà devrait échouer.
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
19 sept. 2004 à 21:54
Ici je charge en dynamique les fonctions pour que puisse tourner sur les winbebes. Tu adaptes en + simple si tu vises seulement les noyaux NT.

typedef BOOL (__stdcall *pOPENPROCESSTOKEN) (HANDLE,DWORD,PHANDLE);
typedef BOOL (__stdcall *pGETTOKENINFORMATION) (HANDLE,TOKEN_INFORMATION_CLASS,LPVOID,DWORD,PDWORD);
typedef BOOL (__stdcall *pALLOCINITSID) (PSID_IDENTIFIER_AUTHORITY,BYTE,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,PSID*);
typedef BOOL (__stdcall *pEQUALSID) (PSID,PSID);
typedef PVOID (__stdcall *pFREESID) (PSID);

DWORD IsUserAdmin() // RETOURNE != 0 SI APPELANT EST ADMIN
{
HANDLE htoken;
BYTE infos[1024];
PTOKEN_GROUPS pgroups = (PTOKEN_GROUPS)infos;
DWORD dwlen;
PSID psidAdmins;
SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
BOOL b = 0;
pOPENPROCESSTOKEN pOPTK;
pGETTOKENINFORMATION pGTINF;
pALLOCINITSID pALLIS;
pEQUALSID pEQSID;
pFREESID pFRSD;
HINSTANCE hdll = LoadLibrary("Advapi32.dll");
if(!hdll) return 0;
pOPTK = (pOPENPROCESSTOKEN) GetProcAddress(hdll, "OpenProcessToken");
if(!pOPTK) goto relDll;
pGTINF = (pGETTOKENINFORMATION) GetProcAddress(hdll, "GetTokenInformation");
if(!pGTINF) goto relDll;
if(!pOPTK(GetCurrentProcess(),TOKEN_READ,&htoken)) goto relDll;
b = pGTINF(htoken,TokenGroups,infos, 1024, &dwlen);
CloseHandle(htoken);
if(b) goto relDll;
pALLIS = (pALLOCINITSID) GetProcAddress(hdll, "AllocateAndInitializeSid");
if(!pALLIS) goto relDll;
pEQSID = (pEQUALSID) GetProcAddress(hdll, "EqualSid");
if(!pEQSID) goto relDll;
pFRSD = (pFREESID) GetProcAddress(hdll, "FreeSid");
if(!pFRSD) goto relDll;
if(!pALLIS(&siaNtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmins)) goto relDll;
for(UINT x=0; x < pgroups->GroupCount; x++) {
if(pEQSID(psidAdmins, pgroups->Groups[x].Sid)) {b = 1; break;}
}
pFRSD(&psidAdmins);
relDll: FreeLibrary(hdll);
return b;
}

ciao...
BruNews, MVP VC++
0
Rejoignez-nous