the_open_door
coffee
ESEN
← Back to index

Constrained Language Mode

ATT&CKT1059.001

Constrained Language Mode (CLM) is PowerShell's restricted mode: the console still opens and cmdlets still work, but everything that turns PowerShell into an execution platform is forbidden, namely instantiating arbitrary .NET types, calling the Windows API and using reflection. Hitting CLM is the sign that the host has real application control.

Mechanism. CLM does not turn itself on: AppLocker or WDAC impose it when they run in enforced mode, and it can also be pinned through an environment variable. Under it, a script using Add-Type, New-Object on disallowed types or [Ref].Assembly fails, which is exactly what most AMSI evasion and in-memory loading techniques need.

The first step is knowing which mode you are in, and why:


$ExecutionContext.SessionState.LanguageMode
Get-AppLockerPolicy -Effective -Xml        # who is enforcing it
ConstrainedLanguage

The known ways out say a lot about why the default configuration is not enough:

How to detect it. 4103 and 4104 keep logging under CLM, so an attempt to use a forbidden construct shows up with its error. Clear indicators are execution of powershell.exe -version 2 (and engine event 400 with EngineVersion=2.0), a child process hosting PowerShell without being the console, and AppLocker 8003/8004 right before an execution attempt from a user writable path.

Defence. Uninstall the PowerShell v2 feature, which removes the easiest way out. Move from AppLocker to WDAC, which applies the policy to the .NET engine itself and not only to the executable, and check that no rule allows a path the user can write to: a single rule like that makes the whole policy decorative.