> For the complete documentation index, see [llms.txt](https://docs.iredteam.cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.iredteam.cn/offensive-security/code-execution/powershell-constrained-language-mode-bypass.md).

# Powershell Constrained Language Mode ByPass

Understanding ConstrainedLanguageMode

Constrained Language Mode in short locks down the nice features of Powershell usually required for complex attacks to be carried out.

## Powershell Inside Powershell

For fun - creating another powershell instance inside powershell without actually spawning a new `powershell.exe` process:

![](https://2061087890-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTyPaV5dZf-eW1HRanG%2Fsync%2F66c400f0a8ff9c914c93b266ed8d33e84ceceb26.gif?generation=1613807655789224\&alt=media)

## Constrained Language Mode

Enabling constrained language mode, that does not allow powershell execute complex attacks (i.e. mimikatz):

```csharp
```

Checking constrained language mode is enabled:

```csharp
PS C:\Users\mantvydas> $ExecutionContext.SessionState.LanguageMode
ConstrainedLanguage
```

![](https://2061087890-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTyPaV5dZf-eW1HRanG%2Fsync%2Fd6c52330183e9a7e046069ee6ba5b29482eb628a.png?generation=1613807655413527\&alt=media)

With `ConstrainedLanguage`, trying to download a file from remote machine, we get `Access Denied`:

![](https://2061087890-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTyPaV5dZf-eW1HRanG%2Fsync%2F70007fced2fc89b1f0676ad55f25440cdb17b369.png?generation=1613807655678227\&alt=media)

However, if you have access to the system and enough privileges to change environment variables, the lock can be lifted by removing the variable `__PSLockdownPolicy` and re-spawning another powershell instance.

### Powershell Downgrade

If you have the ability to downgrade to Powershell 2.0, this can allow you to bypass the `ConstrainedLanguage`mode. Note how `$ExecutionContext.SessionState.LanguageMode` keeps returning `ConstrainedLangue` in powershell instances that were not launched with `-version Powershell 2` until it does not:

![](https://2061087890-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MTyPaV5dZf-eW1HRanG%2Fsync%2Ff4cf16df5e17ff59a3d06cccd12d6cf8c131a353.png?generation=1613807655682913\&alt=media)

## References

{% embed url="<https://blogs.msdn.microsoft.com/powershell/2017/11/02/powershell-constrained-language-mode/>" %}

{% embed url="<https://www.blackhillsinfosec.com/powershell-without-powershell-how-to-bypass-application-whitelisting-environment-restrictions-av/>" %}

{% embed url="<https://adsecurity.org/?p=2604>" %}

{% embed url="<https://pentestn00b.wordpress.com/2017/03/20/simple-bypass-for-powershell-constrained-language-mode/>" %}
