Forzare un’applicazione ad essere eseguita senza richiedere privilegi amministrativi

Talvolta può capitare che alcune applicazioni richiedano di essere eseguite con privilegi amministrativi senza che ve ne sia la reale necessità in quanto si è provveduto a garantire gli opportuni privilegi per accedere a cartelle e/o chiavi di registro. A volete la necessità che l’applicazione sia eseguita con privilegi di amministrazione dipende dalla bad practice di mantenere i file di dati nello stesso percorso in cui è installata l’applicazione e questo può comportare l’impossibilità di modificare i dati senza privilegi amministrativi se il path d’installazione si trova in una sottocartella di %ProgramFiles%.

In tali scenari di solito legati ad applicazioni legacy on in attesa di aggiornamenti è possibile ovviare creando sul desktop un file Start-AsNonAdmin.cmd su cui trascinare gli eseguibili da avviare come utente normale anche se l’eseguibile richiederebbe privilegi amminstrativi.

File Start-AsNonAdmin.cmd:

cmd /min /C “set __COMPAT_LAYER=RUNASINVOKER && start “” %1″

 

In alternativa è anche possibile creare un file di avvio specifico per una applicaizione:

File StartMyApplication-AsNonAdmin.cmd:

Set ApplicationPath=”Path my application”
cmd /min /C “set __COMPAT_LAYER=RUNASINVOKER && start “” %ApplicationPath%”

La variabile d’ambiente __COMPAT_LAYER purtroppo non è molto documenta, ma se impostata a RUNASINVOKER consente di sopprimere le richieste della UAC e di avviare l’applicazione senza elevare i privilegi.

A riguardo si vedano i seguenti:

Dal seguente Is RunAsInvoker a secret, even higher UAC setting?:

“What RunAsInvoker does is to ignore any elevation request in the application’s manifest and treat the manifest as if it had said
<requestedExecutionLevel level=”asInvoker” uiAccess=”false” />
which is the default behavior. The program simply runs with the same privileges as the code that launched it. There is no attempt to elevate.”

“This means that if you run the program from an elevated command prompt, then the program stays elevated. If you run the program from a non-elevated command prompt, then the program stays non-elevated.”

“While it’s true that RunAsInvoker suppresses UAC prompts, that’s true because RunAsInvoker doesn’t perform any elevation. If you aren’t performing any elevation, then naturally you don’t need an elevation prompt. If the resulting process is elevated, then it means that the calling process was already elevated. You were already on the other side of the airtight hatchway.”

Dal seguente Run with elevated permissions:

“In some cases you may wish to run an application without elevation, this will restrict what the application can do.
The undocumented environment variable __COMPAT_LAYER can be used to lower the ExecutionLevel to RunAsInvoker”

Dal seguente Windows Environment Variables:

“%__COMPAT_LAYER% Set the ExecutionLevel to either RunAsInvoker (asInvoker), RunAsHighest(highestAvailable) or RunAsAdmin(requireAdministrator) for more see elevation and Q286705 / Application Compatibility Toolkit for other Compatibility Layers (colours,themes etc).”

Per una trattazione approfondita sull’elevazione dei privilegi, gli Integrity Levels e l’ UIAccess si veda il post di Tim Mangan (Microsoft MVP nella categoria Windows and Devices for IT) Today We Learn About Elevation, Integrity Levels, and UIAccess .