Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting elevated privileges on Windows Server 2008 or higher

I have an C#, .Net 4.6.1 Windows Forms Application running on Windows Server Platforms (2008 or higher) which requires to be "Run as Administrator". Elevated privileges are required because the application changes User Access Rights on various folders (underneath the IIS Default Web Site Root if that matters).

I have no luck in detecting if the application has been "Run as Administrator". If I start the application normally (that is not as Administrator) the following code

var isAdmin = WindowsIdentity.GetCurrent().Owner.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid);

returns true but the code which changes some User Access Rights on a Directory fails with a Insufficient Privileges Error.

If I run the application as administrator the above check also returns true, but the changing of User Access rights works just fine.

Other attempts I have made without success:

  • Using the GetTokenInformation method inside the advapi32.dll as suggested here
  • Adding a manifest file to the application where I set the requestedExecutionLevel to requireAdministrator

Thanks in advance for any help.

like image 404
Mats Avatar asked Jan 26 '16 11:01

Mats


People also ask

Which account has the highest level of privilege on a Windows system?

In Windows systems, the Administrator account holds superuser privileges. Each Windows computer has at least one administrator account. The Administrator account allows the user to install software, and change local configurations and settings, and more.

What is the elevated privileges window?

What are Elevated Privileges? Elevated privileges is when a user is granted the ability to do more than a standard user. A standard user is someone that has “zero administrative” privileges in any capacity.

What is elevated privilege?

What are elevated privileges? Elevated privileges are defined as roles or permissions that if misused or compromised could allow a person to exploit the university systems for his or her own gain or illicit purpose.


2 Answers

The following must work (I hope so; I have a Windows client and it's working with me).

var Identity = WindowsIdentity.GetCurrent();
var Principal = new WindowsPrincipal(Identity);
bool IsAdmin = Principal.IsInRole(WindowsBuiltInRole.Administrator);
like image 79
Ahmad Alloush Avatar answered Oct 06 '22 06:10

Ahmad Alloush


Try to change the permissions of a known folder and if there is an exception then you know the program has not been run as administrator.

like image 22
Dave3of5 Avatar answered Oct 06 '22 06:10

Dave3of5