{"id":2921,"date":"2016-08-13T22:15:59","date_gmt":"2016-08-14T02:15:59","guid":{"rendered":"https:\/\/adsecurity.org\/?p=2921"},"modified":"2017-10-27T10:38:33","modified_gmt":"2017-10-27T14:38:33","slug":"powershell-security-bypasses-effective-mitigation","status":"publish","type":"post","link":"https:\/\/adsecurity.org\/?p=2921","title":{"rendered":"PowerShell Security: PowerShell Attack Tools, Mitigation, &#038; Detection"},"content":{"rendered":"<p>This post is a follow-up of sorts from my earlier posts on PowerShell, my <a href=\"https:\/\/adsecurity.org\/?page_id=1352\">PowerShell presentation at BSides Baltimore<\/a>, and my <a href=\"https:\/\/adsecurity.org\/?page_id=1352\">presentation at DEF CON 24<\/a>.<br \/>\nHopefully this post provides current information on PowerShell usage for both Blue and Red teams.<\/p>\n<p><strong>Related posts:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/adsecurity.org\/?p=2843\" target=\"_top\">BSides Charm Presentation Posted: PowerShell Security: Defending the Enterprise from the Latest Attack Platform <\/a><\/li>\n<li><a href=\"https:\/\/adsecurity.org\/?p=2668\" target=\"_top\">PowerShell Version 5 is Available for Download (again) <\/a><\/li>\n<li><a href=\"https:\/\/adsecurity.org\/?p=2604\" target=\"_top\">Detecting Offensive PowerShell Attack Tools <\/a><\/li>\n<li><a href=\"https:\/\/adsecurity.org\/?p=2277\" target=\"_top\">PowerShell Version 5 Security Enhancements <\/a><\/li>\n<\/ul>\n<h3><strong> The Evolution of PowerShell as an attack tool<\/strong><\/h3>\n<p>PowerShell is a built-in command shell available on every supported version of Microsoft Windows (Windows 7 \/ Windows 2008 R2 and newer) and provides incredible flexibility and functionality to manage Windows systems. This power makes PowerShell an enticing tool for attackers. Once an attacker can get code to run on a computer, they often invoke PowerShell code since it can be run in memory where antivirus can\u2019t see it. Attackers may also drop PowerShell script files (.ps1) to disk, but since PowerShell can download code from a website and run it in memory, that\u2019s often not necessary.<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/06\/PowerShellSecurity-PowerShell-I-Thought-This-Was-DOS.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2922\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/06\/PowerShellSecurity-PowerShell-I-Thought-This-Was-DOS.jpg\" alt=\"PowerShellSecurity-PowerShell-I-Thought-This-Was-DOS\" width=\"450\" height=\"268\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/06\/PowerShellSecurity-PowerShell-I-Thought-This-Was-DOS.jpg 450w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/06\/PowerShellSecurity-PowerShell-I-Thought-This-Was-DOS-300x179.jpg 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=q5pA49C7QJg\">Dave Kennedy &amp; Josh Kelley presented at DEF CON 18 (2010)<\/a> on how PowerShell could be leveraged by attackers. <a href=\"http:\/\/www.exploit-monday.com\/2012\/08\/Why-I-Choose-PowerShell.html\">Matt Graeber developed PowerSploit and blogged at Exploit-Monday.com<\/a> on why PowerShell is a great attack platform. Offensive PowerShell usage has been on the rise since the release of \u201c<a href=\"https:\/\/github.com\/PowerShellMafia\/PowerSploit\">PowerSploit<\/a>\u201d in 2012, though it wasn\u2019t until Mimikatz was PowerShell-enabled (aka <a href=\"https:\/\/raw.githubusercontent.com\/PowerShellEmpire\/Empire\/master\/data\/module_source\/credentials\/Invoke-Mimikatz.ps1\">Invoke-Mimikatz<\/a>) about a year later that PowerShell usage in attacks became more prevalent. PowerShell provides tremendous capability since it can run .Net code and execute dynamic code downloaded from another system (or the internet) and execute it in memory without ever touching disk. These features make PowerShell a preferred method for gaining and maintaining access to systems since they can move around using PowerShell without being seen. <a href=\"https:\/\/adsecurity.org\/?p=2277\">PowerShell Version 5<\/a> (v5) greatly improves the defensive posture of PowerShell and when run on a Windows 10 system, PowerShell attack capability is greatly reduced.<\/p>\n<p><!--more--><\/p>\n<h3><strong>Attackers have options<\/strong><\/h3>\n<p>This post obviously covers how attackers can subvert the latest security enhancements in PowerShell, including PowerShell v5.<br \/>\nKeep in mind that attackers have options. PowerShell is one option, but dropping a custom exe is another one.<br \/>\nOptions include:<\/p>\n<ul>\n<li>Custom executables (EXEs)<\/li>\n<li>Windows command tools<\/li>\n<li>Remote Desktop<\/li>\n<li>Sysinternal tools<\/li>\n<li>Windows Scripting Host<\/li>\n<\/ul>\n<ul>\n<li>VBScript<\/li>\n<li>CScript<\/li>\n<li>JavaScript<\/li>\n<li>Batch files<\/li>\n<li>PowerShell<\/li>\n<\/ul>\n<h3><strong>PowerShell attack capability<\/strong><\/h3>\n<p>There are a number of reasons why attackers love PowerShell:<\/p>\n<ul>\n<li>Run code in memory without touching disk.<\/li>\n<li>Download &amp; execute code from another system.<\/li>\n<li>Interface with .Net &amp; Windows APIs.<\/li>\n<li>Built-in remoting.<\/li>\n<li>CMD.exe is commonly blocked, though not PowerShell.<\/li>\n<li>Most organizations are not watching PowerShell activity.<\/li>\n<li>Many endpoint security products don\u2019t have visibility into PowerShell activity.<\/li>\n<\/ul>\n<p>PowerShell is often leveraged as part of client attack frequently invoked by one of the following (typically an Encoded Command (bypasses exec. policy).<\/p>\n<p><span style=\"text-decoration: underline;\">Typical PowerShell run options<\/span><\/p>\n<p style=\"padding-left: 30px;\">-WindowsStyle Hidden<br \/>\n-NoProfile<br \/>\n-ExecutionPolicy Bypass<br \/>\n-File &lt;FilePath&gt;<br \/>\n-Command &lt;Command&gt;<br \/>\n-EncodedCommand &lt;BASE64EncodedCommand&gt;<\/p>\n<h3><strong>Real World PowerShell Attack Tools<\/strong><\/h3>\n<h4><strong><a href=\"https:\/\/github.com\/PowerShellMafia\/PowerSploit\">PowerSploit<\/a><\/strong><\/h4>\n<p>Description: A PowerShell Post-Exploitation Framework used in many PowerShell attack tools.<br \/>\nUse: Recon, privilege escalation, credential theft, persistence.<br \/>\nAuthors: Matt Graeber (@Mattifestation) &amp; Chris Campbell (@obscuresec)<\/p>\n<p>Popular cmdlets:<\/p>\n<ul>\n<li>Invoke-DllInjection.ps1<\/li>\n<li>Invoke-Shellcode.ps1<\/li>\n<li>Invoke-WmiCommand.ps1<\/li>\n<li>Get-GPPPassword.ps1<\/li>\n<li>Get-Keystrokes.ps1<\/li>\n<li>Get-TimedScreenshot.ps1<\/li>\n<li>Get-VaultCredential.ps1<\/li>\n<\/ul>\n<ul>\n<li>Invoke-CredentialInjection.ps1<\/li>\n<li>Invoke-Mimikatz.ps1<\/li>\n<li>Invoke-NinjaCopy.ps1<\/li>\n<li>Invoke-TokenManipulation.ps1<\/li>\n<li>Out-Minidump.ps1<\/li>\n<li>VolumeShadowCopyTools.ps1<\/li>\n<li>Invoke-ReflectivePEInjection.ps1<\/li>\n<\/ul>\n<h4><strong><a href=\"https:\/\/github.com\/PowerShellMafia\/PowerSploit\/blob\/master\/Exfiltration\/Invoke-Mimikatz.ps1\">Invoke-Mimikatz<\/a><\/strong><\/h4>\n<p>Capabilities: <a href=\"https:\/\/adsecurity.org\/?p=2207\">Mimikatz <\/a>execution from PowerShell, Credential theft &amp; injection, Forged Kerberos ticket creation, Much more!<\/p>\n<p>Use: Credential theft &amp; reuse, Persistence<\/p>\n<p>Author: Joseph Bialek (@clymb3r)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Invoke-Mimikatz-DumpCreds-RunFromWeb.jpg\" width=\"651\" height=\"457\" \/><\/p>\n<h4><strong><a href=\"https:\/\/github.com\/PowerShellMafia\/PowerSploit\/blob\/master\/Recon\/PowerView.ps1\">PowerView<\/a><\/strong><\/h4>\n<p>Description: Pure PowerShell domain\/network situational awareness tool.<br \/>\nNow part of PowerSploit.<\/p>\n<p>Use: Recon<\/p>\n<p>Author: Will Harmjoy (@HarmJ0y)<\/p>\n<ul>\n<li>Get-NetUser<\/li>\n<li>Get-NetGroup<\/li>\n<li>Get-NetGroupMember<\/li>\n<li>Get-NetLocalGroup<\/li>\n<li>Get-NetSession<\/li>\n<li>Invoke-UserHunter<\/li>\n<li>Get-NetOU<\/li>\n<li>Find-GPOLocation<\/li>\n<li>Get-NetGPOGroup<\/li>\n<\/ul>\n<ul>\n<li>Get-ObjectACL<\/li>\n<li>Add-ObjectACL<\/li>\n<li>Invoke-ACLScanner<\/li>\n<li>Set-ADObject<\/li>\n<li>Invoke-DowngradeAccount<\/li>\n<li>Get-NetForest<\/li>\n<li>Get-NetForestTrust<\/li>\n<li>Get-NetForestDomain<\/li>\n<li>Get-NetDomainTrust<\/li>\n<li>Get-MapDomainTrust<\/li>\n<\/ul>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AD-Recon-PowerView-GetNetGPOGroup-01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3116\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AD-Recon-PowerView-GetNetGPOGroup-01.png\" alt=\"AD-Recon-PowerView-GetNetGPOGroup-01\" width=\"1852\" height=\"884\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AD-Recon-PowerView-GetNetGPOGroup-01.png 1852w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AD-Recon-PowerView-GetNetGPOGroup-01-300x143.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AD-Recon-PowerView-GetNetGPOGroup-01-768x367.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AD-Recon-PowerView-GetNetGPOGroup-01-1024x489.png 1024w\" sizes=\"auto, (max-width: 1852px) 100vw, 1852px\" \/><\/a><\/p>\n<h4><strong><a href=\"https:\/\/github.com\/PowerShellMafia\/PowerSploit\/blob\/master\/Privesc\/PowerUp.ps1\"> PowerUp<\/a><\/strong><\/h4>\n<p>Description: Identifies methods of local Privilege Escalation.<br \/>\nPart of PowerShell Empire.<\/p>\n<p>Use: Privilege Escalation<\/p>\n<p>Author: Will Harmjoy (@harmj0y)<\/p>\n<ul>\n<li>Get-ServiceUnquoted<\/li>\n<li>Get-ServiceFilePermission<\/li>\n<li>Get-ServicePermission<\/li>\n<li>Invoke-ServiceAbuse<\/li>\n<li>Install-ServiceBinary<\/li>\n<li>Get-RegAutoLogon<\/li>\n<\/ul>\n<ul>\n<li>Get-VulnAutoRun<\/li>\n<li>Get-VulnSchTask<\/li>\n<li>Get-UnattendedInstallFile<\/li>\n<li>Get-WebConfig<\/li>\n<li>Get-ApplicationHost<\/li>\n<li>Get-RegAlwaysInstallElevated<\/li>\n<\/ul>\n<h4><strong><a href=\"https:\/\/github.com\/samratashok\/nishang\">Nishang<\/a><\/strong><\/h4>\n<p>Description: PowerShell for penetration testing and offensive security.<br \/>\nUse: Recon, Credential Theft, Privilege Escalation, Persistence<br \/>\nAuthor: Nikhil Mitt (@nikhil_mitt)<\/p>\n<ul>\n<li>Get-Unconstrained<\/li>\n<li>Add-RegBackdoor<\/li>\n<li>Add-ScrnSaveBackdoor<\/li>\n<li>Gupt-Backdoor<\/li>\n<li>Invoke-ADSBackdoor<\/li>\n<li>Enabled-DuplicateToken<\/li>\n<li>Invoke-PsUaCme<\/li>\n<li>Remove-Update<\/li>\n<li>Check-VM<\/li>\n<li>Copy-VSS<\/li>\n<li>Get-Information<\/li>\n<li>Get-LSASecret<\/li>\n<\/ul>\n<ul>\n<li>Get-PassHashes<\/li>\n<li>Invoke-Mimikatz<\/li>\n<li>Show-TargetScreen<\/li>\n<li>Port-Scan<\/li>\n<li>Invoke-PoshRatHttp<\/li>\n<li>Invoke-PowerShellTCP<\/li>\n<li>Invoke-PowerShellWMI<\/li>\n<li>Add-Exfiltration<\/li>\n<li>Add-Persistence<\/li>\n<li>Do-Exfiltration<\/li>\n<li>Start-CaptureServer<\/li>\n<\/ul>\n<h4><strong><a href=\"https:\/\/github.com\/PowerShellEmpire\/Empire\">PowerShell Empire<\/a><\/strong><\/h4>\n<p>Current Version: 1.5 (3\/31\/2016)<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShellEmpire-Empire-Load-Screen-v1.5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3070\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShellEmpire-Empire-Load-Screen-v1.5.jpg\" alt=\"PowerShellEmpire-Empire-Load-Screen-v1.5\" width=\"472\" height=\"294\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShellEmpire-Empire-Load-Screen-v1.5.jpg 472w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShellEmpire-Empire-Load-Screen-v1.5-300x187.jpg 300w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><\/a><\/p>\n<p>Capabilities:<\/p>\n<ul>\n<li>PowerShell based Remote Access Trojan (RAT).<\/li>\n<li>Python server component (Kali Linux).<\/li>\n<li>AES Encrypted C2 channel.<\/li>\n<li>Dumps and tracks credentials in database.<\/li>\n<\/ul>\n<p>Use: Integrated modules providing Initial Exploitation, Recon, Credential Theft &amp; Reuse, as well as Persistence.<\/p>\n<p>Authors: Will Schroeder (@harmj0y) &amp; Justin Warner (@sixdub) &amp; Matt Nelson (@enigma0x3)<\/p>\n<p>Modules:<\/p>\n<ul>\n<li>Code Execution<\/li>\n<li>Collection<\/li>\n<li>Credentials<\/li>\n<li>Exfiltration<\/li>\n<li>Exploitation<\/li>\n<li>Lateral Movement<\/li>\n<li>Management<\/li>\n<li>Persistence<\/li>\n<li>Privilege Escalation<\/li>\n<li>Recon<\/li>\n<li>Situational Awareness<\/li>\n<li>Fun &amp; Trollsploit<\/li>\n<\/ul>\n<p>Cmdlets:<\/p>\n<ul>\n<li>Invoke-DllInjection<\/li>\n<li>Invoke-ReflectivePEInjection<\/li>\n<li>Invoke-ShellCode<\/li>\n<li>Get-ChromeDump<\/li>\n<li>Get-ClipboardContents<\/li>\n<li>Get-FoxDump<\/li>\n<li>Get-IndexedItem<\/li>\n<li>Get-Keystrokes<\/li>\n<li>Get-Screenshot<\/li>\n<li>Invoke-Inveigh<\/li>\n<li>Invoke-NetRipper<\/li>\n<li>Invoke-NinjaCopy<\/li>\n<li>Out-Minidump<\/li>\n<li>Invoke-EgressCheck<\/li>\n<li>Invoke-PostExfil<\/li>\n<li>Invoke-PSInject<\/li>\n<li>Invoke-RunAs<\/li>\n<li>MailRaider<\/li>\n<\/ul>\n<ul>\n<li>New-HoneyHash<\/li>\n<li>Set-MacAttribute<\/li>\n<li>Get-VaultCredential<\/li>\n<li>Invoke-DCSync<\/li>\n<li>Invoke-Mimikatz<\/li>\n<li>Invoke-PowerDump<\/li>\n<li>Invoke-TokenManipulation<\/li>\n<li>Exploit-Jboss<\/li>\n<li>Invoke-ThunderStruck<\/li>\n<li>Invoke-VoiceTroll<\/li>\n<li>Set-Wallpaper<\/li>\n<li>Invoke-InveighRelay<\/li>\n<li>Invoke-PsExec<\/li>\n<li>Invoke-SSHCommand<\/li>\n<\/ul>\n<ul>\n<li>Get-SecurityPackages<\/li>\n<li>Install-SSP<\/li>\n<li>Invoke-BackdoorLNK<\/li>\n<li>PowerBreach<\/li>\n<li>Get-GPPPassword<\/li>\n<li>Get-SiteListPassword<\/li>\n<li>Get-System<\/li>\n<li>Invoke-BypassUAC<\/li>\n<li>Invoke-Tater<\/li>\n<li>Invoke-WScriptBypassUAC<\/li>\n<li>PowerUp<\/li>\n<li>PowerView<\/li>\n<li>Get-RickAstley<\/li>\n<\/ul>\n<ul>\n<li>Find-Fruit<\/li>\n<li>HTTP-Login<\/li>\n<li>Find-TrustedDocuments<\/li>\n<li>Get-ComputerDetails<\/li>\n<li>Get-SystemDNSServer<\/li>\n<li>Invoke-Paranoia<\/li>\n<li>Invoke-WinEnum<\/li>\n<li>Get-SPN<\/li>\n<li>Invoke-ARPScan<\/li>\n<li>Invoke-PortScan<\/li>\n<li>Invoke-ReverseDNSLookup<\/li>\n<li>Invoke-SMBScanner<\/li>\n<\/ul>\n<h3><strong>Learning about Offensive PowerShell Tools<\/strong><\/h3>\n<p>Most of the best PS attack tools are in Empire, so download the <a href=\"https:\/\/github.com\/PowerShellEmpire\/Empire\/releases\">PowerShell Empire zip file<\/a> &amp; extract.<br \/>\nOnce extracted, review PS1 files in data\\module_source.<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3123\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source.png\" alt=\"PowerShell-Empire-ZipFile-Contents-Module_Source\" width=\"222\" height=\"347\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source.png 424w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-192x300.png 192w\" sizes=\"auto, (max-width: 222px) 100vw, 222px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-Credentials.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3122\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-Credentials.png\" alt=\"PowerShell-Empire-ZipFile-Contents-Module_Source-Credentials\" width=\"307\" height=\"178\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-Credentials.png 585w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-Credentials-300x174.png 300w\" sizes=\"auto, (max-width: 307px) 100vw, 307px\" \/><\/a>\u00a0 <a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-Persistence.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3124\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-Persistence.png\" alt=\"PowerShell-Empire-ZipFile-Contents-Module_Source-Persistence\" width=\"305\" height=\"180\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-Persistence.png 592w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Empire-ZipFile-Contents-Module_Source-Persistence-300x177.png 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/a><\/p>\n<h3><strong>PowerShell is more than PowerShell.exe<\/strong><\/h3>\n<p>Blocking access to PowerShell.exe is an \u201ceasy\u201d way to stop PowerShell capability, at least that\u2019s how it seems. The reality is that PowerShell is more than a single executable. PowerShell is a core component of Windows (not removable) exists in the System.Management.Automation.dll dynamic linked library file (DLL) and can host different runspaces which are effectively PowerShell instances (think PowerShell.exe &amp; PowerShell_ISE.exe). A custom PowerShell runspace can be instantiated via code, so PowerShell can be executed through a custom coded executable (such as MyPowershell.exe). In fact there are several current methods of running PowerShell code without Powershell.exe being executed. Justin Warner (<a href=\"https:\/\/twitter.com\/sixdub\">@SixDub<\/a>) blogged about <a href=\"http:\/\/www.sixdub.net\/?p=367\">bypassing PowerShell.exe on Red Team engagements in late 2014,\u00a0<\/a><a href=\"http:\/\/www.powershellempire.com\/?page_id=135\">aka PowerPick<\/a>). Since PowerShell code can be executed without running PowerShell.exe, blocking this executable is not an ideal solution to block attacks (and by &#8220;not an ideal solution&#8221; I mean this doesn&#8217;t stop PowerShell from being executed, so no it doesn&#8217;t solve the problem).<\/p>\n<p>There are two sides to every argument. On the &#8220;Block PowerShell&#8221; side, there is the positive result that initial attack code will not execute since PowerShell is not allowed to run, with potential issues later on due to Microsoft and\/or 3rd party requirements for PowerShell. Often an organization will &#8220;block&#8221; access to PowerShell.exe to stop the initial attack. There are side-effects to this, including potentially reduced management capability.<br \/>\nOn the &#8220;Don&#8217;t Block PowerShell&#8221; side, there are other ways to limit an attacker&#8217;s PowerShell capability without blocking PowerShell from running. Configuring PowerShell protection\/limitation via AppLocker is worth investigating (and testing) as well as setting Powershell to constrained language mode. For more on this, review the later section on &#8220;Limiting PowerShell Capability.&#8221;<\/p>\n<ul>\n<li>PowerShell = System.Management.Automation.dll<\/li>\n<li>Applications can run PowerShell code<\/li>\n<li>\u201cPowerShell ps = PowerShell.Create()\u201d<\/li>\n<li>Ben Ten\u2019s AwesomerShell<br \/>\n<a href=\"https:\/\/github.com\/Ben0xA\/AwesomerShell\">https:\/\/github.com\/Ben0xA\/AwesomerShell<\/a><\/li>\n<\/ul>\n<h3><strong>Executing PowerShell commands without PowerShell.exe<br \/>\n<\/strong><\/h3>\n<p><span style=\"text-decoration: underline;\">Starting with PowerShell v2:<\/span><\/p>\n<p><i><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.management.automation.powershell%28VS.85%29.aspx\">\u201cProvides methods that are used to create a pipeline of commands and invoke those commands either synchronously or asynchronously within a runspace. This class also provides access to the output streams that contain data that is generated when the commands are invoked. This class is primarily intended for host applications that programmatically use Windows PowerShell to perform tasks. This class is introduced in Windows PowerShell 2.0&#8243;.<\/a><\/i><\/p>\n<ul>\n<li>Create C# application that references Powershell System.Automation.dll assembly.<\/li>\n<li>Leverage Automation assembly\u2019s functions to execute PowerShell Code.<\/li>\n<li>Similar to how PowerShell.exe works.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/github.com\/leechristensen\/UnmanagedPowerShell\">Unmanaged PowerShell<\/a> by Lee Christensen (<a href=\"https:\/\/twitter.com\/tifkin_\">@tifkin_<\/a>) is the foundation for most PowerShell attack tools running outside of powershell.exe. It starts up .NET &amp; performs in-memory loading of a custom C# assembly that executes PowerShell from an unmanaged process.<br \/>\nThe Metasploit PowerShell module leverages unmanaged PowerShell since March 2016.<\/p>\n<p>Another PowerShell project that leverages unmanaged PowerShell is <strong>P0wnedShell<\/strong> a \u201cPowerShell Runspace Post Exploitation Toolkit\u201d. It runs PowerShell commands and functions within a powershell runspace environment (.NET) and includes many PowerShell attack tools, including those from PowerSploit, Nishang, PowerCat, Inveigh, etc all contained within a single executable.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/P0wnedShell-Calc.png\" width=\"1631\" height=\"870\" \/><br \/>\nThis project provides a simple \u2018Order by Number\u2019 for simple PowerShell attack tool execution.<br \/>\nI renamed it to &#8220;Calc.exe&#8221;, though I have never been able to get Calc to use more than a few MB of RAM. When I run Mimikatz through it, &#8220;Calc&#8221; uses &gt; 180MB. \ud83d\ude42<\/p>\n<h3><strong>PowerShell v5 Security Enhancements<br \/>\n<\/strong><\/h3>\n<p>I cover these in a <a href=\"https:\/\/adsecurity.org\/?p=2277\">previous post. <\/a>How about a quick refresher?<\/p>\n<ul>\n<li><strong>Script block logging<\/strong> &#8211; logs the PowerShell code actually executed by PowerShell. Without this enabled, obfuscated code is logged, making it far more difficult to create useful indicators.<br \/>\n<a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShellv5-Security-ScriptBlockLogging-InvokeMimikatz-PowerShellEvent-4104.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3109\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShellv5-Security-ScriptBlockLogging-InvokeMimikatz-PowerShellEvent-4104.png\" alt=\"PowerShellv5-Security-ScriptBlockLogging-InvokeMimikatz-PowerShellEvent-4104\" width=\"470\" height=\"380\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShellv5-Security-ScriptBlockLogging-InvokeMimikatz-PowerShellEvent-4104.png 538w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShellv5-Security-ScriptBlockLogging-InvokeMimikatz-PowerShellEvent-4104-300x243.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/a><\/li>\n<li><strong>System-wide transcripts<\/strong> &#8211; When enabled, a transcript file can be written to a write-only share for each PowerShell user per computer. If the share is offline, the computer will cache the file data until it&#8217;s back online.<br \/>\n<a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Transcription-InvokeMimikatz-cropped.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3004\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Transcription-InvokeMimikatz-cropped.png\" alt=\"PowerShell-v5-Transcription-InvokeMimikatz-cropped\" width=\"645\" height=\"720\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Transcription-InvokeMimikatz-cropped.png 1123w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Transcription-InvokeMimikatz-cropped-269x300.png 269w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Transcription-InvokeMimikatz-cropped-768x857.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Transcription-InvokeMimikatz-cropped-918x1024.png 918w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/a><\/li>\n<li><strong>Constrained PowerShell enforced with AppLocker<\/strong> &#8211; When PowerShell v5 installed and AppLocker in Allow mode, PowerShell operates in constrained language mode which is a limited language mode preventing and Windows API access. For more on this, keep reading. \ud83d\ude42<br \/>\n<a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3097\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools.png\" alt=\"PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools\" width=\"900\" height=\"262\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools.png 900w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools-300x87.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools-768x224.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><\/li>\n<li>The <strong>Anti-Malware Scan Interface (AMSI)<\/strong> in Windows 10 enables all script code to be scanned prior to execution by PowerShell and other Windows scripting engines. The Anti-Virus\/Anti-Malware solution on the system must support AMSI for it to scan the code. The great benefit is that all code delivered to the PowerShell engine is scanned, even code injected into memory that was downloaded from the internet. As of mid-2016, only Microsoft Defender and AVG support AMSI.<br \/>\n<a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Win10-AMSI-Graphic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3096\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Win10-AMSI-Graphic.jpg\" alt=\"PowerShell-v5-Win10-AMSI-Graphic\" width=\"728\" height=\"321\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Win10-AMSI-Graphic.jpg 728w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-Win10-AMSI-Graphic-300x132.jpg 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/a><\/li>\n<\/ul>\n<p>Unfortunately, most AntiVirus companies don&#8217;t see the benefit of AMSI<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3750\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSISupport.png\" alt=\"\" width=\"489\" height=\"309\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSISupport.png 1530w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSISupport-300x189.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSISupport-768x485.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSISupport-1024x647.png 1024w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>There are also PowerShell cmdlets to interact with Defender to get status on detected threats.<\/p>\n<p>The first detection shows a detected threat in a couple of different files on disk.<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3092\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report.jpg\" alt=\"AMSI-ThreatDetection-Report\" width=\"2655\" height=\"827\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report.jpg 2655w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-300x93.jpg 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-768x239.jpg 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-1024x319.jpg 1024w\" sizes=\"auto, (max-width: 2655px) 100vw, 2655px\" \/><\/a><\/p>\n<p>The second detection shows a detected threat in &#8220;PowerShell.exe_10.0.1058.0000000000010&#8221;. Hmmm, that&#8217;s odd.<br \/>\nIt detected a threat in memory that was downloaded from the internet and executed in memory. \ud83d\ude42<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-IEX-InvokeMimikatz-From-Web.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3090\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-IEX-InvokeMimikatz-From-Web.jpg\" alt=\"AMSI-ThreatDetection-Report-IEX-InvokeMimikatz-From-Web\" width=\"2613\" height=\"792\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-IEX-InvokeMimikatz-From-Web.jpg 2613w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-IEX-InvokeMimikatz-From-Web-300x91.jpg 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-IEX-InvokeMimikatz-From-Web-768x233.jpg 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/AMSI-ThreatDetection-Report-IEX-InvokeMimikatz-From-Web-1024x310.jpg 1024w\" sizes=\"auto, (max-width: 2613px) 100vw, 2613px\" \/><\/a><\/p>\n<p>There are issues with Windows 10&#8217;s AMSI, though Microsoft is making great strides in providing visibility in an area traditionally missed by Anti-Virus\/Anti-Malware.<\/p>\n<p>There are two primary methods of bypassing AMSI (at least for now):<\/p>\n<ul>\n<li><a href=\"https:\/\/cn33liz.blogspot.nl\/2016\/05\/bypassing-amsi-using-powershell-5-dll.html\">Provide &amp; use a custom amsi.dll and call that one from custom EXE.<\/a><\/li>\n<li>Matt Graeber described how to use reflection to bypass AMSI<br \/>\n<a href=\"https:\/\/twitter.com\/mattifestation\/status\/735261120487772160\">[Ref].Assembly.GetType(&#8216;System.Management .Automation.AmsiUtils&#8217;).GetField(&#8216;amsiInitFailed&#8217;,&#8217;NonPublic,Static&#8217;).SetValue($null,$true)<\/a><\/li>\n<\/ul>\n<p>Though with the appropriate rights, one can simply disable AntiMalware though there are logged events relating to this activity.<\/p>\n<p>Sometimes, &#8220;malicious&#8221; PowerShell code gets through.<\/p>\n<h3><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Windows10-AMSI-Defender-Fail-InvokeMimikatz.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3084\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Windows10-AMSI-Defender-Fail-InvokeMimikatz.jpg\" alt=\"Windows10-AMSI-Defender-Fail-InvokeMimikatz\" width=\"650\" height=\"440\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Windows10-AMSI-Defender-Fail-InvokeMimikatz.jpg 1754w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Windows10-AMSI-Defender-Fail-InvokeMimikatz-300x203.jpg 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Windows10-AMSI-Defender-Fail-InvokeMimikatz-768x520.jpg 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Windows10-AMSI-Defender-Fail-InvokeMimikatz-1024x694.jpg 1024w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/a><\/h3>\n<h3>Limiting PowerShell Capability<\/h3>\n<p>It&#8217;s not difficult to find a variety of recommendations regarding how to lock down PowerShell.<br \/>\nThese include:<\/p>\n<ol>\n<li>Remove PowerShell (not possible)<\/li>\n<li>Lock down PowerShell.exe (not 100% effective since PowerShell.exe is not Powershell)<\/li>\n<li>AppLocker control of PowerShell (can be effective if deployed properly)<\/li>\n<li>Constrained Language Mode<\/li>\n<\/ol>\n<p>Since PowerShell is used for system management and logon scripts (and more and more for application management as with Exchange and DSC), blocking PowerShell isn&#8217;t realistic (and again, not terribly effective).<\/p>\n<p>I prefer to configure PowerShell with Constrained language mode which locks down PowerShell to the core elements (no API or .NET access).<\/p>\n<h4><strong>Limiting PowerShell Attack Capability with Constrained Language Mode<br \/>\n<\/strong><\/h4>\n<p>Additionally, PowerShell supports various language modes that restrict what PowerShell can do. The PowerShell Constrained Language Mode was developed to support the Surface RT tablet device, though this mode is available in PowerShell in standard Windows as well. Constrained language mode limits the capability of PowerShell to base functionality removing advanced feature support such as .Net &amp; Windows API calls and COM access. The lack of this advanced functionality stops most PowerShell attack tools since they rely on these methods. The drawback to this approach is that in order to configured PowerShell to run in constrained mode, an environment variable must be set, either by running a command in PowerShell or via Group Policy.<\/p>\n<p>Constrained language mode is a useful interim PowerShell security measure and can mitigate many initial PowerShell attacks, though it is not a panacea. It should be considered minor mitigation method on roadmap to whitelisting. Keep in mind that bypassing Constrained PowerShell is possible and not all PowerShell \u201cattack scripts\u201d will be blocked \u2013 certainly the ones that use advanced functionality to reflectively load a DLL into memory like Invoke-Mimikatz will be blocked.<\/p>\n<p>Enable Constrained Language Mode:<br \/>\n<i> [Environment<\/i><i>]::<\/i><i>SetEnvironmentVariable<\/i><i>(\u2018__<\/i><i>PSLockdownPolicy<\/i><i>\u2018, \u20184\u2019, \u2018Machine<\/i><i>\u2018)<\/i><\/p>\n<p>Enable via Group Policy:<br \/>\n<em>Computer Configuration\\Preferences\\Windows Settings\\Environment<\/em><\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-GPO-EnvironmentalVariable.png\" rel=\"attachment wp-att-2624\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2624\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-GPO-EnvironmentalVariable.png\" sizes=\"auto, (max-width: 260px) 100vw, 260px\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-GPO-EnvironmentalVariable.png 425w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-GPO-EnvironmentalVariable-268x300.png 268w\" alt=\"PowerShell-Security-ConstrainedPowerShell-GPO-EnvironmentalVariable\" width=\"260\" height=\"291\" \/><\/a><\/p>\n<p>Once Constrained Language Mode is enabled, many PowerShell attack tools don\u2019t work since they rely on components blocked by constrained language.<br \/>\n<a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools.png\" rel=\"attachment wp-att-2625\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2625\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools.png\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools.png 900w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools-300x87.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/02\/PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools-768x224.png 768w\" alt=\"PowerShell-Security-ConstrainedPowerShell-Enabled-AttackTools\" width=\"900\" height=\"262\" \/><\/a><\/p>\n<p>This environment variable can be modified by an attacker once they have gained control of the system. Note that they would have to spawn a new PowerShell instance to run code in full language mode after changing the environment. These changes would be logged and could help the defender in identifying unusual activity on the system.<\/p>\n<p>Remove Constrained Language Mode:<br \/>\n<i>Remove-Item <\/i><i>Env<\/i><i>:\\__<\/i><i>PSLockdownPolicy<\/i><\/p>\n<p>Check Language Mode:<br \/>\n<i>$<\/i><i>ExecutionContext.SessionState.LanguageMode<\/i><\/p>\n<p>Enabling PowerShell Constrained Language mode is another method that can be used to mitigate PowerShell attacks.<\/p>\n<p><strong>Update:<\/strong><br \/>\nMatt Graeber continues to find ways around PowerShell barriers. Leverage AppLocker\/DeviceGuard for more effective controls with Constrained Language Mode. Microsoft continuously improves this position as well.<\/p>\n<p><a href=\"https:\/\/twitter.com\/mattifestation\/status\/921509830644269062\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3758\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Twitter-mattifestation-CLM.png\" alt=\"\" width=\"383\" height=\"377\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Twitter-mattifestation-CLM.png 1225w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Twitter-mattifestation-CLM-300x295.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Twitter-mattifestation-CLM-768x756.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Twitter-mattifestation-CLM-1024x1008.png 1024w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><em><strong>Pairing PowerShell v5 with AppLocker \u2013 Constrained Language Mode No Longer Easily Bypassed.<\/strong><\/em><\/p>\n<p>PowerShell v5 also supports automatic lock-down when AppLocker is deployed in \u201cAllow\u201d mode. Applocker Allow mode is true whitelisting and can prevent any unauthorized binary from being executed. PowerShell v5 detects when Applocker Allow mode is in effect and sets the PowerShell language to Constrained Mode, severely limiting the attack surface on the system. With Applocker in Allow mode and PowerShell running in Constrained Mode, it is not possible for an attacker to change the PowerShell language mode to full in order to run attack tools.When AppLocker is configured in \u201cAllow Mode\u201d, PowerShell reduces its functionality to \u201cConstrained Mode\u201d for interactive input and user-authored scripts. Constrained PowerShell only allows core PowerShell functionality and prevents execution of the extended language features often used by offensive PowerShell tools (direct .NET scripting, invocation of Win32 APIs via the Add-Type cmdlet, and interaction with COM objects).<\/p>\n<p>Note that scripts allowed by AppLocker policy such as enterprise signed code or in a trusted directory are executed in full PowerShell mode and not the Constrained PowerShell environment. This can\u2019t be easily bypassed by an attacker, even with admin rights.<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/01\/PowerShellv5-Security-ConstrainedPowerShell.png\" rel=\"attachment wp-att-2428\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2428\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/01\/PowerShellv5-Security-ConstrainedPowerShell.png\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/01\/PowerShellv5-Security-ConstrainedPowerShell-300x104.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/01\/PowerShellv5-Security-ConstrainedPowerShell.png 718w\" alt=\"PowerShellv5-Security-ConstrainedPowerShell\" width=\"718\" height=\"248\" \/><\/a><\/p>\n<p>If you&#8217;re really daring, lock down systems that should never use PowerShell to No Language Mods which means PowerShell is extremely limited.<\/p>\n<blockquote>\n<pre>NO LANGUAGE (NoLanguage)\r\n    In NoLanguage language mode, users may run commands, \r\n    but they cannot use any language elements.<\/pre>\n<\/blockquote>\n<p><a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/dn433292.aspx\">About PowerShell Language Modes<\/a><\/p>\n<p>&nbsp;<\/p>\n<h4><a href=\"https:\/\/github.com\/jaredhaight\/psattack\"><b>PS&gt;Attack<\/b><\/a><\/h4>\n<p>PS&gt;Attack is a self contained custom PowerShell console which includes many offensive PowerShell tools which calls PowerShell (System.Management.Automation.dll) through .Net. The PowerShell attack tools are encrypted (AV evasion) and decrypted to memory at run-time.<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Startup-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3067\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Startup-2.png\" alt=\"PSAttack-Startup-2\" width=\"583\" height=\"503\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Startup-2.png 1164w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Startup-2-300x259.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Startup-2-768x662.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Startup-2-1024x883.png 1024w\" sizes=\"auto, (max-width: 583px) 100vw, 583px\" \/><\/a><\/p>\n<p>There&#8217;s also a custom build tool for ensuring every built exe is different (AV bypass).<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-BuildTool.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3028\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-BuildTool.png\" alt=\"PSAttack-BuildTool\" width=\"630\" height=\"611\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-BuildTool.png 1801w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-BuildTool-300x291.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-BuildTool-768x745.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-BuildTool-1024x994.png 1024w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<p>PS&gt;Attack includes some of the most popular PowerShell attack tools:<\/p>\n<ul>\n<li>Powersploit\n<ul>\n<li>Invoke-Mimikatz<\/li>\n<li>Get-GPPPassword<\/li>\n<li>Invoke-NinjaCopy<\/li>\n<li>Invoke-Shellcode<\/li>\n<li>Invoke-WMICommand<\/li>\n<li>VolumeShadowCopyTools<\/li>\n<\/ul>\n<\/li>\n<li>PowerTools<\/li>\n<li>PowerUp<\/li>\n<li>PowerView<\/li>\n<li>Nishang<\/li>\n<li>Powercat<\/li>\n<li>Inveigh<\/li>\n<\/ul>\n<p>While PS&gt;Attack is simply one method that an attacker can leverage PowerShell offensive tools without running PowerShell.exe, it is extremely effective.<\/p>\n<p>Since PS&gt;Attack is calling PowerShell from an exe, the executed PowerShell code bypasses constrained language mode.<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellConstrainedLanguageMode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3031\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellConstrainedLanguageMode.png\" alt=\"PSAttack-PowerShellConstrainedLanguageMode\" width=\"1440\" height=\"1560\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellConstrainedLanguageMode.png 1440w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellConstrainedLanguageMode-277x300.png 277w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellConstrainedLanguageMode-768x832.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellConstrainedLanguageMode-945x1024.png 945w\" sizes=\"auto, (max-width: 1440px) 100vw, 1440px\" \/><\/a><\/p>\n<p>PS&gt;Attack PowerShell code runs in the earlier version of the PowerShell engine, if available.<br \/>\nThis means that if a system has PowerShell v2 (Windows 7 &amp; Windows Server 2008 R2), then any PowerShell code executed is not logged. Event if PowerShell v5 is installed with system-wide transcript or script block logging.<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellv5-Win7-NoLogging-PowerShellOperationalLog.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3009\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellv5-Win7-NoLogging-PowerShellOperationalLog.png\" alt=\"PSAttack-PowerShellv5-Win7-NoLogging-PowerShellOperationalLog\" width=\"702\" height=\"415\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellv5-Win7-NoLogging-PowerShellOperationalLog.png 1305w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellv5-Win7-NoLogging-PowerShellOperationalLog-300x177.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellv5-Win7-NoLogging-PowerShellOperationalLog-768x454.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PowerShellv5-Win7-NoLogging-PowerShellOperationalLog-1024x606.png 1024w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/><\/a><\/p>\n<p>Windows 10 provides the ability to remove PowerShell v2.0 (no, this doesn&#8217;t remove PowerShell).<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Windows10-PowerShellv2-Removed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3020\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Windows10-PowerShellv2-Removed.png\" alt=\"PSAttack-Windows10-PowerShellv2-Removed\" width=\"431\" height=\"378\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Windows10-PowerShellv2-Removed.png 899w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Windows10-PowerShellv2-Removed-300x263.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-Windows10-PowerShellv2-Removed-768x674.png 768w\" sizes=\"auto, (max-width: 431px) 100vw, 431px\" \/><\/a><\/p>\n<p>Once PowerShell v2 is removed from Windows 10, PS&gt;Attack usage is clearly logged.<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PSv5-EventID-4100-BypassExecutionPolicy-02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3024\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PSv5-EventID-4100-BypassExecutionPolicy-02.png\" alt=\"PSAttack-PSv5-EventID-4100-BypassExecutionPolicy-02\" width=\"570\" height=\"664\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PSv5-EventID-4100-BypassExecutionPolicy-02.png 1116w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PSv5-EventID-4100-BypassExecutionPolicy-02-258x300.png 258w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PSv5-EventID-4100-BypassExecutionPolicy-02-768x895.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PSAttack-PSv5-EventID-4100-BypassExecutionPolicy-02-879x1024.png 879w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"text-decoration: underline;\"><strong>Detecting custom EXEs calling PowerShell<\/strong><\/span><\/h3>\n<ul>\n<li>Event 800: HostApplication not standard Microsoft tool (PowerShell , PowerShell ISE, etc).<br \/>\n<a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-PowerShellLog-PSAttack-MinLog.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3008\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-PowerShellLog-PSAttack-MinLog.png\" alt=\"PowerShell-v5-PowerShellLog-PSAttack-MinLog\" width=\"514\" height=\"582\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-PowerShellLog-PSAttack-MinLog.png 702w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/PowerShell-v5-PowerShellLog-PSAttack-MinLog-265x300.png 265w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/a><\/li>\n<li>Event 800: Version mismatch between HostVersion &amp; EngineVersion (problematic).<\/li>\n<li>System.Management.Automation.dll hosted in non-standard processes.<br \/>\n<a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3119\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-01.png\" alt=\"Detect-PowerShellDLL-In-Process-01\" width=\"549\" height=\"97\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-01.png 1937w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-01-300x53.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-01-768x135.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-01-1024x180.png 1024w\" sizes=\"auto, (max-width: 549px) 100vw, 549px\" \/><\/a><br \/>\n<a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3120\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-02.png\" alt=\"Detect-PowerShellDLL-In-Process-02\" width=\"546\" height=\"141\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-02.png 1948w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-02-300x77.png 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-02-768x198.png 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2016\/08\/Detect-PowerShellDLL-In-Process-02-1024x264.png 1024w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/a><\/li>\n<li>Remember that custom EXEs can natively call .Net &amp; Windows APIs directly without PowerShell.<\/li>\n<\/ul>\n<h3><span style=\"text-decoration: underline;\"><strong>Detecting Offensive PowerShell Tools<\/strong><\/span><\/h3>\n<p>Step one is configuring PowerShell logging:<\/p>\n<ul>\n<li>Deploy PowerShell v5 (or newer) and enable module logging &amp; script block logging.<\/li>\n<li>Send the following PowerShell log event ids to the central logging solution: 400 &amp; 800<\/li>\n<li>Pull the following PowerShell Operational log event ids to the central logging solution: 4100, 4103, 4104<\/li>\n<li>Configuring system-wide transcription to send a log of all activity per user, per system to a write-only share, is incredibly valuable to catch suspicious\/malicious activity that can be missed or not logged to the event logs. Even better is ingesting these transcript text files into something like Splunk for further analysis.<\/li>\n<\/ul>\n<p>I have noted several required elements in most of the offensive PowerShell tools.<br \/>\nUsing the following indicators along with PowerShell module logging (preferably with script block logging), it&#8217;s possible to detect most PowerShell attack tools.<br \/>\nMake sure you properly tune these in your environment to weed out false positives.<\/p>\n<ul>\n<li>AdjustTokenPrivileges<\/li>\n<li>IMAGE_NT_OPTIONAL_HDR64_MAGIC<\/li>\n<li>Management.Automation.RuntimeException<\/li>\n<li>Microsoft.Win32.UnsafeNativeMethods<\/li>\n<li>ReadProcessMemory.Invoke<\/li>\n<li>Runtime.InteropServices<\/li>\n<li>SE_PRIVILEGE_ENABLED<\/li>\n<li>System.Security.Cryptography<\/li>\n<li>System.Reflection.AssemblyName<\/li>\n<li><i>System.Runtime.<\/i><i>InteropServices<\/i><\/li>\n<li>LSA_UNICODE_STRING<\/li>\n<li>MiniDumpWriteDump<\/li>\n<li>PAGE_EXECUTE_READ<\/li>\n<li>Net.Sockets.SocketFlags<\/li>\n<li>Reflection.Assembly<\/li>\n<li>SECURITY_DELEGATION<\/li>\n<li>TOKEN_ADJUST_PRIVILEGES<\/li>\n<li>TOKEN_ALL_ACCESS<\/li>\n<li>TOKEN_ASSIGN_PRIMARY<\/li>\n<li>TOKEN_DUPLICATE<\/li>\n<li>TOKEN_ELEVATION<\/li>\n<li>TOKEN_IMPERSONATE<\/li>\n<li>TOKEN_INFORMATION_CLASS<\/li>\n<li>TOKEN_PRIVILEGES<\/li>\n<li>TOKEN_QUERY<\/li>\n<li>Metasploit<\/li>\n<li>Advapi32.dll<\/li>\n<li>kernel32.dll<\/li>\n<li>msvcrt.dll<\/li>\n<li>ntdll.dll<\/li>\n<li>secur32.dll<\/li>\n<li>user32.dll<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Update: 2\/07\/2017:<\/strong><\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3544\" src=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2017\/02\/OffensivePowerShellIndicators-20170205.jpg\" alt=\"\" width=\"489\" height=\"285\" srcset=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2017\/02\/OffensivePowerShellIndicators-20170205.jpg 1815w, https:\/\/adsecurity.org\/wp-content\/uploads\/2017\/02\/OffensivePowerShellIndicators-20170205-300x175.jpg 300w, https:\/\/adsecurity.org\/wp-content\/uploads\/2017\/02\/OffensivePowerShellIndicators-20170205-768x448.jpg 768w, https:\/\/adsecurity.org\/wp-content\/uploads\/2017\/02\/OffensivePowerShellIndicators-20170205-1024x597.jpg 1024w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/p>\n<p>Note the addition of &#8220;KerberosRequestorSecurityToken&#8221; which is the PowerShell method to request Kerberos tickets (typically used for &#8220;<a href=\"https:\/\/adsecurity.org\/?s=kerberoasting\">Kerberoasting<\/a>&#8220;).<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is a follow-up of sorts from my earlier posts on PowerShell, my PowerShell presentation at BSides Baltimore, and my presentation at DEF CON 24. Hopefully this post provides current information on PowerShell usage for both Blue and Red teams. Related posts: BSides Charm Presentation Posted: PowerShell Security: Defending the Enterprise from the Latest &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/adsecurity.org\/?p=2921\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":3065,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,7,2],"tags":[873,977,998,880,881,882,996,874,878,336,868,879,866,997,994,871,995,865,999,875,872,877,876,869,864,143,863,867,788,69,232,965,696,862,858,870,494],"class_list":["post-2921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-security","category-powershell","category-technical-reference","tag-bypass-powershell-executionpolicy","tag-bypass-powershell-security","tag-constrained-language-mode","tag-detect-invoke-mimikatz","tag-detect-offensive-powershell","tag-detect-powershell-attack-tools","tag-detect-powershell-attacks","tag-executionpolicybypass","tag-invoke-expression","tag-invoke-mimikatz","tag-invokemimikatz","tag-new-object-net-webclient-downloadstring","tag-offensive-powershell","tag-offensive-powershell-indicators","tag-offensivepowershell","tag-powershell-attack-tool","tag-powershell-attack-tools","tag-powershell-attacks","tag-powershell-constrained-language","tag-powershell-constrained-language-mode","tag-powershell-execution-policy","tag-powershell-gpo","tag-powershell-logging-group-policy","tag-powershell-mimikatz","tag-powershell-exe","tag-powershellattack","tag-powershelllogging","tag-powershellmafia","tag-powershellsecurity","tag-powershellv5","tag-powersploit","tag-powerup","tag-powerview","tag-script-block-logging","tag-system-wide-transcript","tag-system-management-automation-dll","tag-windows10","item-wrap"],"_links":{"self":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/2921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2921"}],"version-history":[{"count":28,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/2921\/revisions"}],"predecessor-version":[{"id":3759,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/2921\/revisions\/3759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/media\/3065"}],"wp:attachment":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}