Constrained Language Mode
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 itConstrainedLanguage
The known ways out say a lot about why the default configuration is not enough:
- PowerShell v2, which predates CLM and ignores it entirely (
powershell.exe -version 2), if the feature is still installed. - A different interpreter than
powershell.exe: a custom runspace inside a C# binary compiled on the host, which does not inherit the console process policy. - Badly placed path rules: if the policy allows execution from a directory the user can write to, CLM stops applying to whatever runs from there. That is the same flaw an AppLocker bypass exploits.
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.