{"id":1121,"date":"2015-06-09T17:16:44","date_gmt":"2015-06-09T17:16:44","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2015\/06\/09\/powershell-the-blue-team\/"},"modified":"2019-02-28T15:40:09","modified_gmt":"2019-02-28T23:40:09","slug":"powershell-the-blue-team","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/powershell-the-blue-team\/","title":{"rendered":"PowerShell \u2665 the Blue Team"},"content":{"rendered":"<p class=\"MsoNormal\"><i>(Warning: Long blog post ahead! If you\u2019d like to read (or share) this as a whitepaper, you can download it here: \u201c<a href=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2019\/02\/Scripting-Security-and-Protection-Advances-in-Windows-10.docx\">Scripting Security and Protection Advances in Windows 10<\/a>\u201d). <\/i><\/p>\n<p>At Microsoft, we invest an enormous amount of time and energy managing world-class cloud services and incredibly large enterprise networks. Security is critical for all of these \u2013 so what might surprise you is that we also invest enormous amounts of time and energy trying to break into those services. This is called <a href=\"http:\/\/azure.microsoft.com\/blog\/2014\/11\/11\/red-teaming-using-cutting-edge-threat-simulation-to-harden-the-microsoft-enterprise-cloud\/\">Red Teaming<\/a> \u2013 taking highly specialized groups of security experts and having them adopt the role of sophisticated adversaries.\nIn the last several releases of Windows, we\u2019ve been working hard to make the platform much more powerful for administrators, developers, and power users alike. The only problem is \u2013 the red teams are catching on.<\/p>\n<div style=\"border-top: #5b9bd5 1pt solid;border-right: medium none;border-bottom: #5b9bd5 1pt solid;margin-left: 0.6in;border-left: medium none;margin-right: 0.6in;padding: 10pt 0in 10pt 0in\">\n<p class=\"MsoIntenseQuote\" style=\"margin: 0.25in 0in\">A Note about Assume Breach\n<span style=\"color: black;font-style: normal\">\nIn this post, the assumption is that an attacker has already compromised (breached) a system through a malicious phishing email, security flaw in a custom website implementation, or similar attack.<\/span><\/p>\n<p><span style=\"color: black;font-style: normal\">When these security flaws are in software, they are found and patched. But we always assume the attackers will find some way to get in \u2013 even if only through a user being tricked into installing a malicious application on their computer.\\<\/span><\/p>\n<\/div>\n<p class=\"MsoNormal\">As with any occupation, job satisfaction for attackers (either funded by the company under attack or otherwise) plays an important role in influencing attacker behaviour. After all, who wants to extend their compromise of a system using error prone and hard-to-write C++ programs, when you can accomplish the same thing with an elegant and powerful scripting language like PowerShell?<\/p>\n<p>In this post, we\u2019ll discuss some important advances we\u2019ve made in scripting security and protection in the preview versions of PowerShell version 5, and Windows 10.<\/p>\n<h2>PowerShell <span style=\"font-family: 'Segoe UI Symbol',sans-serif\">\u2665<\/span> the Blue Team<\/h2>\n<p>When you take an assume-breach mindset, you have to assume that an attacker is already on your system. But then you\u2019re left with questions: What did they do? What systems did they connect to? Was any dynamic code invoked, and what was it?\nPowerShell version 5 (included in Windows 10, and also available for earlier operating systems through the Windows Management Framework) has made significant strides in making sure that the Blue Team has the information it needs to answer these questions.<\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.5in\"><i>KB 3000850 for PowerShell v4 on Windows 8.1 also includes many of these features, as called out below. <\/i><\/p>\n<p>PowerShell version 5 builds on the already strong infrastructure that PowerShell version 4 (and below) offers: transcription, module logging, and more. For more information about PowerShell Security Best Practices in these environments, see <a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2013\/12\/16\/powershell-security-best-practices.aspx\">http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2013\/12\/16\/powershell-security-best-practices.aspx<\/a>. In addition, FireEye has published an excellent document on Investigating PowerShell Attacks: <a href=\"http:\/\/www.fireeye.com\/resources\/pdfs\/fireeye-lazanciyan-investigating-powershell-attacks.pdf\">http:\/\/www.fireeye.com\/resources\/pdfs\/fireeye-lazanciyan-investigating-powershell-attacks.pdf<\/a>. This document goes into great detail for both proactive and reactive techniques.<\/p>\n<h2>Over-the-shoulder transcription<\/h2>\n<p>One of the quickest ways to get a summary of what\u2019s happening in a PowerShell session is to look over the shoulder of the person typing. You see their commands, the output of those commands, and all is well. Or it\u2019s not, but at least you\u2019ll know.\nPowerShell versions 4 and prior include support for over-the-shoulder transcription through the <span style=\"font-size: 9pt;font-family: 'Lucida Console';color: blue;line-height: 107%\">Start-Transcript<\/span> command. However, setting up ubiquitous transcription of PowerShell sessions is complex and error-prone. You need to include the command in the system startup profile of every system, and also need to add significant amounts of auditing to flag attackers that attempt to disable transcription.\nA secondary issue is that transcription was only supported in the interactive PowerShell console. Transcription of remoting sessions were not supported, nor was transcription in non-console hosts such as the PowerShell ISE.\nIn PowerShell version 5 and <i>KB 3000850<\/i>, <span style=\"font-size: 9pt;font-family: 'Lucida Console';color: blue;line-height: 107%\">Start-Transcript<\/span> now emits structured objects when you start a transcript (the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Path<\/span> property is useful), and has added much more useful information to its header:\n<a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2015\/06\/transcript_header.png\"><img decoding=\"async\" class=\"aligncenter wp-image-13395 size-full\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2015\/06\/transcript_header.png\" alt=\"\" width=\"950\" height=\"422\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/transcript_header.png 950w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/transcript_header-300x133.png 300w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/transcript_header-768x341.png 768w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/a><\/p>\n<p class=\"MsoNormal\">The filename now includes the computer that generated the transcript, a \u2018hash breaker\u2019 to prevent transcript collisions, and increased granularity in the transcript start time. While PowerShell v4 and below let you control the output path, you were then forced to properly randomize the transcript filename yourself. To improve this situation, we\u2019ve added the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">\u2013OutputDirectory<\/span> parameter to Start-Transcript.<\/p>\n<p class=\"MsoNormal\">In the header content, the \u201cUsername\u201d and \u201cRunAs User\u201d will normally be the same. If you\u2019ve enabled impersonation on a constrained PowerShell remoting endpoint (i.e.: PowerShell Just Enough Administration), the \u201cUsername\u201d field represents the connected user while the \u201cRunAs User\u201d represents the account being impersonated.<\/p>\n<p>When it comes to transcript content, PowerShell now transcribes (what it can) of console commands that manipulate the console buffer directly, and can now be enabled in hosts such as the PowerShell ISE.\nIf you want to more directly associate commands with their output for potential later analysis, use the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">\u2013IncludeInvocationHeader<\/span> parameter. This adds an additional header for each command that is invoked:\n<a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2015\/06\/invocation_header.png\"><img decoding=\"async\" class=\"aligncenter wp-image-13405 size-full\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2015\/06\/invocation_header.png\" alt=\"\" width=\"922\" height=\"365\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/invocation_header.png 922w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/invocation_header-300x119.png 300w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/invocation_header-768x304.png 768w\" sizes=\"(max-width: 922px) 100vw, 922px\" \/><\/a><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;background: white;line-height: normal\">To enable automatic transcription, enable the \u2018Turn on PowerShell Transcription\u2019 feature in Group Policy through <span style=\"font-size: 9pt;font-family: 'Courier New'\">Windows Components -&gt; Administrative Templates -&gt; Windows PowerShell<\/span>. For automation, the configuration settings are stored under <span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkred\">HKLM:\\Software\\Policies\\Microsoft\\Windows\\PowerShell\\Transcription<\/span>. The following PowerShell functions let you enable and disable the system-wide transcription policies.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #00008b\">function<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">Enable-PSTranscription<\/span>\n<span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #a9a9a9\">[<\/span><span style=\"color: #00bfff\">CmdletBinding<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #a9a9a9\">]<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #00008b\">param<\/span><span style=\"color: #000000\">(<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$OutputDirectory<\/span><span style=\"color: #a9a9a9\">,<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #008080\">[Switch]<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$IncludeInvocationHeader<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #006400\">## Ensure the base path exists<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;HKLM:\\Software\\Policies\\Microsoft\\Windows\\PowerShell\\Transcription&#8221;<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #00008b\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #a9a9a9\">-not<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">Test-Path<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$null<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">New-Item<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Force<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #006400\">## Enable transcription<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000ff\">Set-ItemProperty<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Name<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">EnableTranscripting<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Value<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #800080\">1<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #006400\">## Set the output directory<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #00008b\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$PSCmdlet<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">MyInvocation<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">BoundParameters<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ContainsKey<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">&#8220;OutputDirectory&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000ff\">Set-ItemProperty<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Name<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">OutputDirectory<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Value<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$OutputDirectory<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #006400\">## Set the invocation header<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #00008b\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$IncludeInvocationHeader<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000ff\">Set-ItemProperty<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Name<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">IncludeInvocationHeader<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Value<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #800080\">1<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">}<\/span>\n<span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #00008b\">function<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">Disable-PSTranscription<\/span>\n<span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000ff\">Remove-Item<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">HKLM:\\Software\\Policies\\Microsoft\\Windows\\PowerShell\\Transcription<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Force<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Recurse<\/span>\n<span style=\"color: #000000\">}<\/span><\/p>\n<p class=\"MsoNormal\">When enabled system-wide, PowerShell transcription even includes emulated transcription for hosts that don\u2019t even have an interface \u2013 such as this example C# program:<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: blue\">using<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\"> System;\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: blue\">using<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\"> System.Management.Automation;\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: blue\">namespace<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\"> IgnorantTranscriber\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">{\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0 <\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: blue\">class<\/span> <span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: #2b91af\">Program<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0 {\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: blue\">static<\/span> <span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: blue\">void<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\"> Main(<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: blue\">string<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">[] args)\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 { <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: blue\">var<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\"> processes = <\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: #2b91af\">PowerShell<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">.Create().AddCommand(<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: #a31515\">&#8220;Get-Process&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">).\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 AddParameter(<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: #a31515\">&#8220;Name&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">, <\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: #a31515\">&#8220;*e*&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">).Invoke();\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: #2b91af\">Console<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">.WriteLine(<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: #a31515\">&#8220;You have &#8220;<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\"> + processes.Count +\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: #a31515\">&#8221; processes with &#8216;e&#8217; in the name!&#8221;<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">);\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black\">\u00a0\u00a0\u00a0 }\n<\/span><span style=\"font-size: 9.5pt;font-family: consolas;background: white;color: black;line-height: 107%\">}<\/span><\/p>\n<p>When you run it, the logged content emulates what you might have seen:<\/p>\n<p class=\"MsoNoSpacing\" style=\"margin-left: 0.5in\"><span style=\"font-size: 9pt;font-family: 'Courier New'\">PS&gt;CommandInvocation(Get-Process): &#8220;Get-Process&#8221;\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">&gt;&gt; ParameterBinding(Get-Process): name=&#8221;Name&#8221;; value=&#8221;*e*&#8221;\n<\/span><\/p>\n<p class=\"MsoNoSpacing\" style=\"margin-left: 0.5in\"><span style=\"font-size: 9pt;font-family: 'Courier New'\">Handles\u00a0 NPM(K)\u00a0\u00a0\u00a0 PM(K)\u00a0\u00a0\u00a0\u00a0\u00a0 WS(K) VM(M)\u00a0\u00a0 CPU(s)\u00a0\u00a0\u00a0\u00a0 Id ProcessName\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">&#8212;&#8212;-\u00a0 &#8212;&#8212;\u00a0\u00a0\u00a0 &#8212;&#8211;\u00a0\u00a0\u00a0 \u00a0 &#8212;&#8211; &#8212;&#8211;\u00a0\u00a0 &#8212;&#8212;\u00a0\u00a0\u00a0\u00a0 &#8212; &#8212;&#8212;&#8212;&#8211;\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 135\u00a0\u00a0\u00a0\u00a0\u00a0 11\u00a0\u00a0\u00a0\u00a0 2496\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7716\u00a0 4096\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2548 Acmengine\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0 2451\u00a0\u00a0\u00a0\u00a0 121\u00a0\u00a0\u00a0 63952\u00a0\u00a0\u00a0\u00a0 188004\u00a0 4096\u00a0\u00a0\u00a0 45.80\u00a0\u00a0 1516 explorer\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 Idle\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 254\u00a0\u00a0\u00a0\u00a0\u00a0 22 \u00a0\u00a0 38132\u00a0\u00a0\u00a0\u00a0\u00a0 36248\u00a0\u00a0 229\u00a0\u00a0\u00a0\u00a0 0.64\u00a0\u00a0 2556 IgnorantTranscriber\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 452\u00a0\u00a0\u00a0\u00a0\u00a0 53\u00a0\u00a0\u00a0 93164\u00a0\u00a0\u00a0\u00a0\u00a0 64664\u00a0 4096\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1756 MsMpEng\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 147\u00a0\u00a0\u00a0\u00a0\u00a0 10\u00a0\u00a0\u00a0\u00a0 1872\u00a0\u00a0\u00a0\u00a0\u00a0 12524\u00a0 4096\u00a0\u00a0\u00a0\u00a0 0.08\u00a0\u00a0 3784 OpenWith\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 658\u00a0\u00a0\u00a0\u00a0\u00a0 33\u00a0\u00a0\u00a0 80680\u00a0\u00a0\u00a0\u00a0\u00a0 97852\u00a0 4096\u00a0\u00a0\u00a0\u00a0 3.61\u00a0\u00a0 1120 powershell\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 486\u00a0\u00a0\u00a0\u00a0\u00a0 30\u00a0\u00a0\u00a0 74876\u00a0\u00a0\u00a0\u00a0\u00a0 89780\u00a0 4096\u00a0\u00a0\u00a0\u00a0 2.64\u00a0\u00a0 2060 powershell\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 277\u00a0\u00a0\u00a0\u00a0\u00a0 10\u00a0\u00a0\u00a0\u00a0 3452\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8696\u00a0 4096\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 536 services\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 148\u00a0\u00a0\u00a0\u00a0\u00a0 12\u00a0\u00a0\u00a0\u00a0 3256\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 9840\u00a0 4096\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2608 sysparse\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 885\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0 120\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 136\u00a0\u00a0\u00a0\u00a0 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4 System\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 239\u00a0\u00a0\u00a0\u00a0\u00a0 18\u00a0\u00a0\u00a0\u00a0 3268\u00a0\u00a0\u00a0\u00a0\u00a0 12060\u00a0 4096\u00a0\u00a0\u00a0\u00a0 0.33\u00a0\u00a0 2896 taskhostex<\/span><\/p>\n<p>The <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">OutputDirectory<\/span> setting lets you collect transcripts to a central location (UNC path) for later review. If you implement this policy, ensure that access to the central share is limited to prevent users from reading previously-written transcripts. The following PowerShell script creates a \u201cTranscripts\u201d SMB share on a server that follows this best practice.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">md<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">c:\\Transcripts<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Kill all inherited permissions<\/span>\n<span style=\"color: #ff4500\">$acl<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Get-Acl<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">c:\\Transcripts<\/span>\n<span style=\"color: #ff4500\">$acl<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">SetAccessRuleProtection<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$true<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$false<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Grant Administrators full control<\/span>\n<span style=\"color: #ff4500\">$administrators<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #008080\">[System.Security.Principal.NTAccount]<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;Administrators&#8221;<\/span>\n<span style=\"color: #ff4500\">$permission<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$administrators<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;FullControl&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;ObjectInherit,ContainerInherit&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;None&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;Allow&#8221;<\/span>\n<span style=\"color: #ff4500\">$accessRule<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">System.Security.AccessControl.FileSystemAccessRule<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$permission<\/span>\n<span style=\"color: #ff4500\">$acl<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">AddAccessRule<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$accessRule<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Grant everyone else Write and ReadAttributes. This prevents users from listing<\/span>\n<span style=\"color: #006400\">## transcripts from other machines on the domain.<\/span>\n<span style=\"color: #ff4500\">$everyone<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #008080\">[System.Security.Principal.NTAccount]<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;Everyone&#8221;<\/span>\n<span style=\"color: #ff4500\">$permission<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$everyone<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;Write,ReadAttributes&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;ObjectInherit,ContainerInherit&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;None&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;Allow&#8221;<\/span>\n<span style=\"color: #ff4500\">$accessRule<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">System.Security.AccessControl.FileSystemAccessRule<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$permission<\/span>\n<span style=\"color: #ff4500\">$acl<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">AddAccessRule<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$accessRule<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Deny &#8220;Creator Owner&#8221; everything. This prevents users from<\/span>\n<span style=\"color: #006400\">## viewing the content of previously written files.<\/span>\n<span style=\"color: #ff4500\">$creatorOwner<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #008080\">[System.Security.Principal.NTAccount]<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;Creator Owner&#8221;<\/span>\n<span style=\"color: #ff4500\">$permission<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$creatorOwner<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;FullControl&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;ObjectInherit,ContainerInherit&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;InheritOnly&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;Deny&#8221;<\/span>\n<span style=\"color: #ff4500\">$accessRule<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">System.Security.AccessControl.FileSystemAccessRule<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$permission<\/span>\n<span style=\"color: #ff4500\">$acl<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">AddAccessRule<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$accessRule<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Set the ACL<\/span>\n<span style=\"color: #ff4500\">$acl<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Set-Acl<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">c:\\Transcripts\\<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Create the SMB Share, granting Everyone the right to read and write files. Specific<\/span>\n<span style=\"color: #006400\">## actions will actually be enforced by the ACL on the file folder.<\/span>\n<span style=\"color: #0000ff\">New-SmbShare<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Name<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">Transcripts<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Path<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">c:\\Transcripts<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-ChangeAccess<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">Everyone<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2>Deep script block logging<\/h2>\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\">A PowerShell \u201cscript block\u201d is the base level of executable code in PowerShell. It might represent a command typed interactively in the PowerShell console, supplied through the command line (\u201c<span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">PowerShell \u2013Command &lt;\u2026&gt;<\/span>\u201d), or wrapped in a function, script, workflow, or the like.<\/p>\n<p class=\"MsoNormal\">In addition to over-the-shoulder style transcription, PowerShell v5 and <i>KB 3000850<\/i> introduces deep script block logging. When you enable script block logging, PowerShell records the content of all script blocks that it processes. If a script block uses dynamic code generation (i.e.: <span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered;line-height: 107%\">$command<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console';line-height: 107%\"> <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;&#8216;Hello World'&#8221;<\/span>; <span style=\"color: blue\">Invoke-Expression<\/span> <span style=\"color: orangered\">$command<\/span><\/span>), PowerShell will log the invocation of this generated script block as well. This provides complete insight into the script-based activity on a system \u2013 including scripts or applications that leverage dynamic code generation in an attempt to evade detection.<\/p>\n<p class=\"MsoNormal\">As with transcription support, this deep script block logging applies to any application that hosts the PowerShell engine \u2013 the command line shell, ISE, or custom host.<\/p>\n<p class=\"MsoNormal\">To enable automatic transcription, enable the \u2018Turn on PowerShell Script Block Logging\u2019 feature in Group Policy through <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Windows Components -&gt; Administrative Templates -&gt; Windows PowerShell<\/span>. For automation, the configuration settings are stored under <span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkred;line-height: 107%\">HKLM:\\Software\\Policies\\Microsoft\\Windows\\PowerShell\\ScriptBlockLogging<\/span>. By default, PowerShell only logs scripts blocks the first time they are used. If you select \u2018<span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Log script block invocation start \/ stop events<\/span>\u2019, PowerShell also logs start and stop events for every time a script block is invoked. This latter setting can generate an extremely high volume of events, so should be enabled with caution.<\/p>\n<p class=\"MsoNormal\">The following PowerShell functions let you enable and disable the system-wide script block logging policies.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #00008b\">function<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">Enable-PSScriptBlockLogging<\/span>\n<span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;HKLM:\\Software\\Policies\\Microsoft\\Windows\\PowerShell\\ScriptBlockLogging&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #00008b\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #a9a9a9\">-not<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">Test-Path<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$null<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">New-Item<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Force<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">}<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000ff\">Set-ItemProperty<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Name<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">EnableScriptBlockLogging<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Value<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;1&#8221;<\/span>\n<span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #00008b\">function<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">Disable-PSScriptBlockLogging<\/span>\n<span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000ff\">Remove-Item<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">HKLM:\\Software\\Policies\\Microsoft\\Windows\\PowerShell\\ScriptBlockLogging<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Force<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Recurse<\/span>\n<span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #00008b\">function<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">Enable-PSScriptBlockInvocationLogging<\/span>\n<span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;HKLM:\\Software\\Policies\\Microsoft\\Windows\\PowerShell\\ScriptBlockLogging&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #00008b\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #a9a9a9\">-not<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">Test-Path<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$null<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">New-Item<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Force<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000ff\">Set-ItemProperty<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$basePath<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Name<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">EnableScriptBlockInvocationLogging<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Value<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;1&#8221;<\/span>\n<span style=\"color: #000000\">}<\/span><\/p>\n<p class=\"MsoNormal\">Most companies only realize the need to enable script block logging after it is too late. To provide some recourse in this situation, PowerShell automatically logs script blocks when they have content often used by malicious scripts. This automatic script block logging is not intended to replace antivirus or full script block logging \u2013 it only serves as a record of last resort.<\/p>\n<p>To disable automatic script block logging, set the \u201c<span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Turn on Script Block Logging<\/span>\u201d feature to \u201c<span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Disabled<\/span>\u201d. Alternatively, specify \u201c<span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">0<\/span>\u201d for the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">EnableScriptBlockLogging<\/span> registry key.\nWhen script block logging is enabled, PowerShell will log the following events to the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Microsoft-Windows-PowerShell\/Operational<\/span> log:<\/p>\n<table class=\"MsoTableGrid\" style=\"width: 329.25pt;border-collapse: collapse;margin-left: 30px\" border=\"1\" width=\"439\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr style=\"height: 12.85pt\">\n<td style=\"height: 12.85pt;border: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">EventId<\/p>\n<\/td>\n<td style=\"border-top: black 1pt solid;height: 12.85pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">4104 \/ 0x1008<\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 12.85pt\">\n<td style=\"border-top: medium none;height: 12.85pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Channel<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 12.85pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Operational<\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 13.6pt\">\n<td style=\"border-top: medium none;height: 13.6pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Level<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 13.6pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Verbose<\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 12.85pt\">\n<td style=\"border-top: medium none;height: 12.85pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Opcode<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 12.85pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Create<\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 13.6pt\">\n<td style=\"border-top: medium none;height: 13.6pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Task<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 13.6pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0CommandStart<\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 12.85pt\">\n<td style=\"border-top: medium none;height: 12.85pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Keyword<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 12.85pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Runspace<\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 53.8pt\">\n<td style=\"border-top: medium none;height: 53.8pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Message<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 53.8pt;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Creating Scriptblock text (%1 of %2):<\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0%3<\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0ScriptBlock ID: %4<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The text embedded in the message is the text of the script block compiled. The ScriptBlock ID is a GUID retained for the life of the script block.<\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.5in\"><b>Note<\/b>: Some script block texts (i.e.: <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Get-ChildItem<\/span>) might not <i>truly<\/i> be representative of its underlying functionality if that command was generated through PowerShell\u2019s dynamic keyword mechanism or an overridden function. For both of these situations, the original dynamic keyword definition (or malicious function definition) will be logged.<\/p>\n<p>When script block invocation logging is enabled, PowerShell also writes begin and end event markers:<\/p>\n<table class=\"MsoTableGrid\" style=\"border-collapse: collapse;margin-left: 30px\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td style=\"border: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">EventId<\/p>\n<\/td>\n<td style=\"border-top: black 1pt solid;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\"><span style=\"font-size: 9.5pt;font-family: consolas;color: black\">Start: 4105 \/ 0x1009\n(Complete: 4106 \/ 0x100A)<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Channel<\/p>\n<\/td>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Operational<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Level<\/p>\n<\/td>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Verbose<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Opcode<\/p>\n<\/td>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Open (\/ Close)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Task<\/p>\n<\/td>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0CommandStart (\/ CommandStop)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Keyword<\/p>\n<\/td>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Runspace<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: black 1pt solid;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">Message<\/p>\n<\/td>\n<td style=\"border-top: medium none;border-right: black 1pt solid;border-bottom: black 1pt solid;border-left: medium none;padding: 0in 5.4pt 0in 5.4pt\" valign=\"top\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\u00a0Started (\/ Completed) invocation of ScriptBlock ID: %1<\/p>\n<p>Runspace ID: %2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\">The ID is the GUID representing the script block (that can be correlated with event ID 4104), and the Runspace ID represents the runspace this script block was run in.<\/p>\n<p>Given that it represents the content of all PowerShell script invoked on a system, these events may contain sensitive data. To limit the information disclosure risk when script block logging is enabled, see <span class=\"MsoSubtleEmphasis\">Protected Event Logging<\/span>.\nPercent signs in the invocation message represent structured ETW properties. While they are replaced with the actual values in the message text, a more robust way to access them is to retrieve the message with the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Get-WinEvent<\/span> cmdlet, and then use the <b>Properties<\/b> array of the message.\nHere&#8217;s an example of how this functionality can help unwrap a malicious attempt to encrypt and obfuscate a script:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Malware<\/span>\n<span style=\"color: #00008b\">function<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">SuperDecrypt<\/span>\n<span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #00008b\">param<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$script<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$bytes<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #008080\">[Convert]<\/span><span style=\"color: #a9a9a9\">::<\/span><span style=\"color: #000000\">FromBase64String<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$script<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #006400\">## XOR \u201cencryption\u201d<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$xorKey<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #800080\">0x42<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #00008b\">for<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$counter<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #800080\">0<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$counter<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">-lt<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$bytes<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Length<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$counter<\/span><span style=\"color: #a9a9a9\">++<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #ff4500\">$bytes<\/span><span style=\"color: #a9a9a9\">[<\/span><span style=\"color: #ff4500\">$counter<\/span><span style=\"color: #a9a9a9\">]<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$bytes<\/span><span style=\"color: #a9a9a9\">[<\/span><span style=\"color: #ff4500\">$counter<\/span><span style=\"color: #a9a9a9\">]<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">-bxor<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$xorKey<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #008080\">[System.Text.Encoding]<\/span><span style=\"color: #a9a9a9\">::<\/span><span style=\"color: #000000\">Unicode<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">GetString<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$bytes<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #ff4500\">$decrypted<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">SuperDecrypt<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;FUIwQitCNkInQm9CCkItQjFCNkJiQmVCEkI1QixCJkJlQg==&#8221;<\/span>\n<span style=\"color: #0000ff\">Invoke-Expression<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$decrypted<\/span><\/p>\n<p>Running this generates the following log entries:<\/p>\n<div style=\"border-top: medium none;border-right: medium none;border-bottom: windowtext 1pt solid;border-left: medium none;padding: 0in 0in 1pt 0in\">\n<p class=\"MsoNormal\" style=\"padding: 0in\">\n<\/div>\n<p class=\"MsoNoSpacing\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Compiling Scriptblock text (1 of 1):<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 function SuperDecrypt\n{\nparam($script)<\/p>\n<p class=\"MsoNoSpacing\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $bytes = [Convert]::FromBase64String($script)\n<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0## XOR &#8220;encryption&#8221;\n$xorKey = 0x42\nfor($counter = 0; $counter -lt $bytes.Length; $counter++)\n{\n$bytes[$counter] = $bytes[$counter] -bxor $xorKey\n}<\/p>\n<p>[System.Text.Encoding]::Unicode.GetString($bytes)\n}<\/p>\n<div style=\"border-top: medium none;border-right: medium none;border-bottom: windowtext 1pt solid;border-left: medium none;padding: 0in 0in 1pt 0in\">\n<p class=\"MsoNoSpacing\" style=\"padding: 0in\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ScriptBlock ID: ad8ae740-1f33-42aa-8dfc-1314411877e3<\/p>\n<\/div>\n<p class=\"MsoNoSpacing\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Compiling Scriptblock text (1 of 1):<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $decrypted = SuperDecrypt &#8220;FUIwQitCNkInQm9CCkItQjFCNkJiQmVCEkI1QixCJkJlQg==&#8221;<\/p>\n<p>ScriptBlock ID: ba11c155-d34c-4004-88e3-6502ecb50f52<\/p>\n<div style=\"border-top: medium none;border-right: medium none;border-bottom: windowtext 1pt solid;border-left: medium none;padding: 0in 0in 1pt 0in\"><\/div>\n<p class=\"MsoNoSpacing\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Compiling Scriptblock text (1 of 1):<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Invoke-Expression $decrypted<\/p>\n<div style=\"border-top: medium none;border-right: medium none;border-bottom: windowtext 1pt solid;border-left: medium none;padding: 0in 0in 1pt 0in\">\n<p class=\"MsoNoSpacing\" style=\"padding: 0in\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ScriptBlock ID: 856c01ca-85d7-4989-b47f-e6a09ee4eeb3<\/p>\n<\/div>\n<p class=\"MsoNoSpacing\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Compiling Scriptblock text (1 of 1):<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Write-Host &#8216;Pwnd&#8217;<\/p>\n<div style=\"border-top: medium none;border-right: medium none;border-bottom: windowtext 1pt solid;border-left: medium none;padding: 0in 0in 1pt 0in\">\n<p class=\"MsoNoSpacing\" style=\"padding: 0in\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span lang=\"ES-US\">ScriptBlock ID: 5e618414-4e77-48e3-8f65-9a863f54b4c8 <\/span><\/p>\n<\/div>\n<p class=\"MsoNormal\">\n<p>If the script block length exceeds what ETW is capable of holding in a single event, Windows PowerShell breaks the script into multiple parts. Here is sample code to recombine a script from its log messages:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #ff4500\">$created<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Get-WinEvent<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-FilterHashtable<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">@{<\/span>\n<span style=\"color: #000000\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000\">ProviderName<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #8b0000\">&#8220;Microsoft-Windows-PowerShell&#8221;<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">Id<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #800080\">4104<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Where-Object<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">&lt;<\/span><span style=\"color: #8a2be2\">Criteria&gt;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #ff4500\">$sortedScripts<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$created<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">sort<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$_<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Properties<\/span><span style=\"color: #a9a9a9\">[<\/span><span style=\"color: #800080\">0<\/span><span style=\"color: #a9a9a9\">]<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Value<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">}<\/span>\n<span style=\"color: #ff4500\">$mergedScript<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">-join<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$sortedScripts<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">%<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #ff4500\">$_<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Properties<\/span><span style=\"color: #a9a9a9\">[<\/span><span style=\"color: #800080\">2<\/span><span style=\"color: #a9a9a9\">]<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Value<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p class=\"MsoNormal\">As with all logging systems that have a limited retention buffer (i.e.: ETW logs), one attack against this infrastructure is to flood the log with spurious events to hide earlier evidence. To protect yourself from this attack, ensure that you have some form of event log collection set up (i.e.: Windows Event Forwarding, <a href=\"http:\/\/www.nsa.gov\/ia\/_files\/app\/Spotting_the_Adversary_with_Windows_Event_Log_Monitoring.pdf\">http:\/\/www.nsa.gov\/ia\/_files\/app\/Spotting_the_Adversary_with_Windows_Event_Log_Monitoring.pdf<\/a>) to move event logs off of the computer as soon as possible.<\/p>\n<p>&nbsp;<\/p>\n<h2>Antimalware Scan Interface Integration<\/h2>\n<p><a><span style=\"color: #000000\">In Windows 10, the Antimalware, Security and Identity, PowerShell, VBScript, and JScript teams have collaborated to allow applications to become active participants in malware defense. To do this, we&#8217;re introducing a brand new way to help protect customers from dynamic script-based malware and non-traditional avenues of attack. <\/span><\/a>\nWe&#8217;re calling this AMSI &#8212; the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/dn889587(v=vs.85).aspx\">Antimalware Scan Interface<\/a>. PowerShell now submits all script content (interactive and otherwise) to the registered antimalware engine \u2013 including additional calls for scripts the employ obfuscation or layer dynamic code evaluation. For more information about how PowerShell interacts with the Antimalware Scan Interface, see: <a href=\"http:\/\/blogs.technet.com\/b\/mmpc\/archive\/2015\/06\/09\/windows-10-to-offer-application-developers-new-malware-defenses.aspx\">http:\/\/blogs.technet.com\/b\/mmpc\/archive\/2015\/06\/09\/windows-10-to-offer-application-developers-new-malware-defenses.aspx<\/a>.<\/p>\n<h2>Cryptographic Message Syntax (CMS) encryption and decryption cmdlets<\/h2>\n<p>PowerShell version 5 and <i>KB 3000850<\/i> introduces support for protection of content using the <i>Cryptographic Message Syntax<\/i> (CMS) format. These cmdlets support encryption and decryption of content using the IETF standard format for cryptographically protecting messages as documented by <a href=\"http:\/\/tools.ietf.org\/html\/rfc5652\">RFC5652<\/a>.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Get-CmsMessage [-Content] &lt;string&gt; <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Get-CmsMessage [-Path] &lt;string&gt; <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Get-CmsMessage [-LiteralPath] &lt;string&gt; <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Protect-CmsMessage [-To] &lt;CmsMessageRecipient[]&gt; [-Content] &lt;string&gt; [[-OutFile] &lt;string&gt;] <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Protect-CmsMessage [-To] &lt;CmsMessageRecipient[]&gt; [-Path] &lt;string&gt; [[-OutFile] &lt;string&gt;] <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Protect-CmsMessage [-To] &lt;CmsMessageRecipient[]&gt; [-LiteralPath] &lt;string&gt; [[-OutFile] &lt;string&gt;] <\/span><\/p>\n<p class=\"CodeCxSpMiddle\" style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Unprotect-CmsMessage [-EventLogRecord] &lt;EventLogRecord&gt; [[-To] &lt;CmsMessageRecipient[]&gt;] [-IncludeContext] <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Unprotect-CmsMessage [-Content] &lt;string&gt; [[-To] &lt;CmsMessageRecipient[]&gt;] [-IncludeContext] <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Unprotect-CmsMessage [-Path] &lt;string&gt; [[-To] &lt;CmsMessageRecipient[]&gt;] [-IncludeContext] <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 8pt;line-height: 115%\">Unprotect-CmsMessage [-LiteralPath] &lt;string&gt; [[-To] &lt;CmsMessageRecipient[]&gt;] [-IncludeContext] <\/span><\/p>\n<p>The CMS encryption standard implements public key cryptography, where the keys used to encrypt content (the <i>public key<\/i>) and the keys used to decrypt content (the <i>private key<\/i>) are different.\nYour public key can be shared widely, and is not sensitive data. If any content is encrypted with this public key, only your private key can decrypt it. For more information about Public Key Cryptography, see: <a href=\"http:\/\/en.wikipedia.org\/wiki\/Public-key_cryptography\"><span style=\"text-decoration: none;color: windowtext\">http:\/\/en.wikipedia.org\/wiki\/Public-key_cryptography<\/span><\/a>.\nTo be recognized in Windows PowerShell, encryption certificates require a unique key usage identifier (EKU) to identify them as data encryption certificates (like the identifiers for &#8216;Code Signing&#8217;, &#8216;Encrypted Mail&#8217;).\nHere is an example of creating a certificate that is good for Document Encryption:\n(Change the text in <b>Subject<\/b> to your name, email, or other identifier), and put in a file (i.e.: DocumentEncryption.inf):<\/p>\n<pre class=\"CodeCxSpFirst\" style=\"padding-left: 30px\">[Version]\r\nSignature = \"$Windows NT$\"\r\n[Strings]\r\nszOID_ENHANCED_KEY_USAGE = \"2.5.29.37\"\r\nszOID_DOCUMENT_ENCRYPTION = \"1.3.6.1.4.1.311.80.1\"\r\n[NewRequest]\r\nSubject = \"<a href=\"mailto:cn=me@somewhere.com\"><span style=\"font-family: 'Arial',sans-serif\">cn=me@somewhere.com<\/span><\/a>\"\r\nMachineKeySet = false\r\nKeyLength = 2048\r\nKeySpec = AT_KEYEXCHANGE\r\nHashAlgorithm = Sha1\r\nExportable = true\r\nRequestType = Cert\r\nKeyUsage = \"CERT_KEY_ENCIPHERMENT_KEY_USAGE | CERT_DATA_ENCIPHERMENT_KEY_USAGE\"\r\nValidityPeriod = \"Years\"\r\nValidityPeriodUnits = \"1000\"\r\n\u00a0\r\n[Extensions]\r\n%szOID_ENHANCED_KEY_USAGE% = \"{text}%szOID_DOCUMENT_ENCRYPTION%\"<\/pre>\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\">Then run:<\/p>\n<pre style=\"padding-left: 30px\">certreq -new DocumentEncryption.inf DocumentEncryption.cer<\/pre>\n<p>And you can now encrypt and decrypt content:<\/p>\n<p class=\"MsoNormal\" style=\"background: #012456;margin-left: 0.5in\"><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">106 [C:\\temp]\n&gt;&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime;line-height: 107%\">$protected<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray;line-height: 107%\">=<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: teal;line-height: 107%\">&#8220;Hello World&#8221;<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\"> | <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow;line-height: 107%\">Protect-CmsMessage<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray;line-height: 107%\">-To<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: teal;line-height: 107%\"><a href=\"mailto:*me@somewhere.com*\">*me@somewhere.com*<\/a>\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">\n107 [C:\\temp]\n&gt;&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime;line-height: 107%\">$protected<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">\n&#8212;&#8211;BEGIN CMS&#8212;&#8211;\nMIIBqAYJKoZIhvcNAQcDoIIBmTCCAZUCAQAxggFQMIIBTAIBADA0MCAxHjAcBgNVBAMMFWxlZWhv\nbG1AbWljcm9zb2Z0LmNvbQIQQYHsbcXnjIJCtH+OhGmc1DANBgkqhkiG9w0BAQcwAASCAQAnkFHM\nproJnFy4geFGfyNmxH3yeoPvwEYzdnsoVqqDPAd8D3wao77z7OhJEXwz9GeFLnxD6djKV\/tF4PxR\nE27aduKSLbnxfpf\/sepZ4fUkuGibnwWFrxGE3B1G26MCenHWjYQiqv+Nq32Gc97qEAERrhLv6S4R\nG+2dJEnesW8A+z9QPo+DwYU5FzD0Td0ExrkswVckpLNR6j17Yaags3ltNVmbdEXekhi6Psf2MLMP\nTSO79lv2L0KeXFGuPOrdzPAwCkV0vNEqTEBeDnZGrjv\/5766bM3GW34FXApod9u+VSFpBnqVOCBA\nDVDraA6k+xwBt66cV84OHLkh0kT02SIHMDwGCSqGSIb3DQEHATAdBglghkgBZQMEASoEEJbJaiRl\nKMnBoD1dkb\/FzSWAEBaL8xkFwCu0e1ZtDj7nSJc=\n&#8212;&#8211;END CMS&#8212;&#8211;\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">108 [C:\\temp]\n&gt;&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime;line-height: 107%\">$protected<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\"> | <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow;line-height: 107%\">Unprotect-CmsMessage<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: teal;line-height: 107%\">\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">Hello World <\/span><\/p>\n<p>Any parameter of type CMSMessageRecipient supports identifiers in the following formats:<\/p>\n<p class=\"MsoListParagraphCxSpFirst\" style=\"margin: 10pt 0in 10pt 0.75in;line-height: 115%;text-indent: -0.25in\"><span style=\"font-family: symbol\">\u00b7<span style=\"font: 7pt 'Times New Roman'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>An actual certificate (as retrieved from the certificate provider)<\/p>\n<p class=\"MsoListParagraphCxSpMiddle\" style=\"margin: 10pt 0in 10pt 0.75in;line-height: 115%;text-indent: -0.25in\"><span style=\"font-family: symbol\">\u00b7<span style=\"font: 7pt 'Times New Roman'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>Path to the a file containing the certificate<\/p>\n<p class=\"MsoListParagraphCxSpMiddle\" style=\"margin: 10pt 0in 10pt 0.75in;line-height: 115%;text-indent: -0.25in\"><span style=\"font-family: symbol\">\u00b7<span style=\"font: 7pt 'Times New Roman'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>Path to a directory containing the certificate<\/p>\n<p class=\"MsoListParagraphCxSpMiddle\" style=\"margin: 10pt 0in 10pt 0.75in;line-height: 115%;text-indent: -0.25in\"><span style=\"font-family: symbol\">\u00b7<span style=\"font: 7pt 'Times New Roman'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>Thumbprint of the certificate (used to look in the certificate store)<\/p>\n<p class=\"MsoListParagraphCxSpLast\" style=\"margin: 10pt 0in 10pt 0.75in;line-height: 115%;text-indent: -0.25in\"><span style=\"font-family: symbol\">\u00b7<span style=\"font: 7pt 'Times New Roman'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>Subject name of the certificate (used to look in the certificate store)<\/p>\n<p class=\"MsoNormal\">To view document encryption certificates in the certificate provider, you can use the &#8211;<b>DocumentEncryptionCert<\/b> dynamic parameter for <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Get-ChildItem<\/span> (<span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">dir<\/span>):<\/p>\n<pre style=\"padding-left: 30px\">58 [Cert:\\currentuser\\my]\r\n &gt;&gt; dir -DocumentEncryptionCert<\/pre>\n<h3>Interoperability of CMS Content<\/h3>\n<p>Because the CMS format is an IETF standard, PowerShell supports the decryption of content generated by other conforming tools, and the content it generates can be decrypted by other conforming tools.\nOne of the more popular implementations to support the CMS message format is the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Public-key_cryptography\">OpenSSL<\/a> library and command-line toolchain. The primary challenge when exchanging data with the OpenSSL library comes from the OpenSSL assumption that the content is contained within an email message body in the P7M format. Fortunately, these text-based headers are relatively easy to add and remove.\nThe following PowerShell commands demonstrate using OpenSSL and PowerShell to encrypt and decrypt content generated by the other application.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Install the OpenSSL package<\/span>\n<span style=\"color: #0000ff\">Install-Package<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">OpenSSL.Light<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## OpenSSL requires certificates in the PEM format. To create this,<\/span>\n<span style=\"color: #006400\">## export the Windows certificate in PFX format, and ensure that<\/span>\n<span style=\"color: #006400\">## the PFX is protected by a password (rather than account) as<\/span>\n<span style=\"color: #006400\">## OpenSSL doesn&#8217;t support group-protected PFX files<\/span>\n<span style=\"color: #a9a9a9\">&amp;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;C:\\Program Files\\OpenSSL\\bin\\openssl.exe&#8221;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">pkcs12<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-in<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">C:\\temp\\cert.pfx<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-out<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">c:\\temp\\cert.pem<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-nodes<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## 1) Encrypt with PowerShell, decrypt with OpenSSL.<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## First, protect some content in PowerShell.<\/span>\n<span style=\"color: #0000ff\">Get-Process<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Protect-CmsMessage<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-To<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;*myRecipient*&#8221;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Set-Content<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">encrypted.txt<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">&lt;#\nPowerShell uses BEGIN CMS \/ END CMS sigils to signify encrypted content.\nOpenSSL requires an email-header:<\/span><\/p>\n<p>MIME-Version: 1.0\nContent-Disposition: attachment; filename=&#8221;smime.p7m&#8221;\nContent-Type: application\/pkcs7-mime; smime-type=enveloped-data; name=&#8221;smime.p7m&#8221;\nContent-Transfer-Encoding: base64\nSo, tweak the data.\n#&gt;<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #ff4500\">$p7mHeader<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">@&#8217;\nMIME-Version: 1.0\nContent-Disposition: attachment; filename=&#8221;smime.p7m&#8221;\nContent-Type: application\/pkcs7-mime; smime-type=enveloped-data; name=&#8221;smime.p7m&#8221;\nContent-Transfer-Encoding: base64\n&#8216;@<\/span>\n<span style=\"color: #ff4500\">$unixContent<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Get-Content<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">encrypted.txt<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Select-String<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-notmatch<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;&#8212;-&#8220;<\/span>\n<span style=\"color: #ff4500\">$p7mHeader<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;`r`n&#8221;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #ff4500\">$unixContent<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Set-Content<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">encrypted_unix.txt<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Encoding<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">ASCII<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Finally, decrypt with OpenSSL. Ensure that the content is encoded as ASCII.<\/span>\n<span style=\"color: #a9a9a9\">&amp;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;C:\\Program Files\\OpenSSL\\bin\\openssl.exe&#8221;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">cms<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-decrypt<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-in<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">encrypted_unix.txt<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-recip<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">.\\cert.pem<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## 2) Encrypt with OpenSSL, decrypt with PowerShell<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## First, protect some content with OpenSSL<\/span>\n<span style=\"color: #ff4500\">$encrypted<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #0000ff\">Get-Process<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">|<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">&amp;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;C:\\Program Files\\OpenSSL\\bin\\openssl.exe&#8221;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">cms<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-encrypt<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-recip<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">.\\cert.pem<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Change the OpenSSL mail header to the standard CMS header<\/span>\n<span style=\"color: #8b0000\">&#8220;&#8212;&#8211;BEGIN CMS&#8212;&#8211;&#8220;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">$(<\/span><span style=\"color: #ff4500\">$encrypted<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">-notmatch<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8b0000\">&#8220;:&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #8b0000\">&#8220;&#8212;&#8211;END CMS&#8212;&#8211;&#8220;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #a9a9a9\">&gt;<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">encrypted.cms<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #006400\">## Finally, decrypt with PowerShell<\/span>\n<span style=\"color: #0000ff\">Unprotect-CmsMessage<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #000080\">-Path<\/span><span style=\"color: #000000\">\u00a0<\/span><span style=\"color: #8a2be2\">.\\encrypted.cms<\/span><\/p>\n<h2>Secure code generation APIs<\/h2>\n<p>Whenever you write code that may be subjected to attacker-controlled input, code injection vulnerabilities are among the most dangerous type of bug. A good example of code that may be subjected to attacker-controlled input are functions that you expose in a constrained PowerShell runspace. If an attacker can exploit a code injection vulnerability in one of those functions, they can execute code as though it were part of the function itself. That code would not be subject to the restrictions that you\u2019ve applied to the constrained runspace.\nAlmost every language can be subject to code injection vulnerabilities if used incorrectly. In SQL, this is called \u201cSQL Injection\u201d. In web sites, this is called \u201cCross site scripting\u201d. In CGI applications, shell scripts, or tools that invoke system commands &#8211; this is called \u201cCommand injection\u201d.\nIn PowerShell, the most common source of code injection vulnerabilities comes from including attacker-controlled input in a string that you submit to the <span style=\"font-size: 9pt;font-family: 'Lucida Console';color: blue;line-height: 107%\">Invoke-Expression<\/span> command. For example:<\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkblue\">function<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: blueviolet\">Get-MyAcl<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">{ <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0 \u00a0\u00a0<span style=\"color: darkblue\">param<\/span>(<span style=\"color: orangered\">$Path<\/span>)\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0 \u00a0\u00a0<span style=\"color: blue\">Invoke-Expression<\/span> <span style=\"color: darkred\">&#8220;Get-Acl <\/span><span style=\"color: orangered\">$Path<\/span><span style=\"color: darkred\">&#8220;<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">} <\/span><\/p>\n<p>If <span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered;line-height: 107%\">$Path<\/span> contains input such as \u201c<span class=\"CodeChar\"><span style=\"font-size: 10pt;color: red;line-height: 107%\">;<\/span><\/span><span class=\"CodeChar\"><span style=\"font-size: 10pt;line-height: 107%\"> Write-Host Pwnd<\/span><\/span>\u201d, the attacker can now execute the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Write-Host<\/span> cmdlet (or much worse!) as well.\nThe Invoke-Expression cmdlet should almost always be avoided, as PowerShell (like other languages) has many features that take its place more securely.<\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgreen\">## Invoke a static command<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: blue\">Get-Acl<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: navy\">-Path<\/span> <span style=\"color: blueviolet\">c:\\temp\\file.txt\n<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgreen\">## Supply a dynamic parameter value<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgreen\">## with a variable reference<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered\">$paramValue<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;c:\\temp\\file.txt&#8221;<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: blue\">Get-Acl<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: navy\">-Path<\/span> <span style=\"color: orangered\">$paramValue<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\">\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgreen\">## Supply both a dynamic parameter name and<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgreen\">## value through &#8216;splatting&#8217;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered\">$parameters<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: darkgray\">=<\/span> @{ Path <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;c:\\temp\\file.txt&#8221;<\/span> } <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: blue\">Get-Acl<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: orangered\">@parameters<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\">\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgreen\">## Supply a dynamic command name, parameter name,<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgreen\">## and parameter value through the invocation<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgreen\">## operator and splatting<\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered\">$commandName<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;Get-Acl&#8221;<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered\">$parameters<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: darkgray\">=<\/span> @{ Path <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;c:\\temp\\file.txt&#8221;<\/span> } <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkgray\">&amp;<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: orangered\">$commandName<\/span> <span style=\"color: orangered\">@parameters <\/span><\/span><\/p>\n<p>If you are ever truly required to generate PowerShell scripts after making all attempts to avoid it, PowerShell version 5 and <i>KB 3000850<\/i> introduces APIs to support secure generation of scripts that may contain attacker input.<\/p>\n<p class=\"MsoNormal\" style=\"background: #012456\"><span style=\"font-size: 8pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">13 [C:\\temp]\n&gt;&gt; [<\/span><span style=\"font-size: 8pt;font-family: consolas;background: #012456;color: silver;line-height: 107%\">System.Management.Automation.Language.CodeGeneration<\/span><span style=\"font-size: 8pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">] | <\/span><span style=\"font-size: 8pt;font-family: consolas;background: #012456;color: yellow;line-height: 107%\">gm<\/span> <span style=\"font-size: 8pt;font-family: consolas;background: #012456;color: gray;line-height: 107%\">\u2013static\n<\/span><span style=\"font-size: 8pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">\u00a0\u00a0 TypeName: System.Management.Automation.Language.CodeGeneration\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0MemberType Definition\n&#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-\nEquals\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Method\u00a0\u00a0\u00a0\u00a0\u00a0static bool Equals(System.Object objA, System.Object objB)\nEscapeBlockCommentContent\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Method\u00a0\u00a0\u00a0\u00a0\u00a0static string EscapeBlockCommentContent(string value)\nEscapeFormatStringContent\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Method\u00a0\u00a0\u00a0\u00a0\u00a0static string EscapeFormatStringContent(string value)\nEscapeSingleQuotedStringContent Method\u00a0\u00a0\u00a0\u00a0\u00a0static string EscapeSingleQuotedStringContent(string value)\nEscapeVariableName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Method\u00a0\u00a0\u00a0\u00a0\u00a0static string EscapeVariableName(string value)<\/span><\/p>\n<p>If you are placing attacker-controlled input within a string (i.e.: for a command argument), ensure that you place it within a single-quoted string. Then, use <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">EscapeSingleQuotedStringContent<\/span> on the content itself. This ensures that single quotes (or their equivalents \u2013 for there are several) in the attacker input are escaped properly.\nFor example:<\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered\">$attackerInput<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;Hello&#8217;World&#8221;<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered\">$escapedAttackerInput<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;&#8216;&#8221;<\/span> <span style=\"color: darkgray\">+<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 <span style=\"color: darkgray\">[<\/span><span style=\"color: teal\">Management.Automation.Language.CodeGeneration<\/span><span style=\"color: darkgray\">]::<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 EscapeSingleQuotedStringContent(<span style=\"color: orangered\">$attackerInput<\/span>) <span style=\"color: darkgray\">+<\/span> <span style=\"color: darkred\">&#8220;&#8216;&#8221;<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: orangered\">$newScript<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;Write-Host <\/span><span style=\"color: orangered\">$escapedAttackerInput<\/span><span style=\"color: darkred\">&#8220;<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"background: white;margin: 0in 0in 0pt 0.5in;line-height: normal\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: blue\">Invoke-Expression<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: orangered\">$newScript<\/span><\/span><\/p>\n<p>Safe escaping of content to be included within block comments, format strings, or variable names is also supported.<\/p>\n<h2>Constrained PowerShell<\/h2>\n<p>When a system is sensitive, one of the most powerful ways to limit the damage an attack can have is to reduce the capabilities of that attack. Windows\u2019 security controls come in many forms \u2013 creating a hierarchy of protections that incrementally add value.<\/p>\n<table class=\"MsoNormalTable\" style=\"width: 100%;border-collapse: collapse\" border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr style=\"height: 24.75pt\">\n<td style=\"border-top: white 1pt solid;height: 24.75pt;border-right: white 1pt solid;width: 17.24%;background: #5b9bd5;border-bottom: white 3pt solid;border-left: white 1pt solid;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"17%\">\n<p class=\"MsoNoSpacing\">Control<\/p>\n<\/td>\n<td style=\"border-top: white 1pt solid;height: 24.75pt;border-right: white 1pt solid;width: 27%;background: #5b9bd5;border-bottom: white 3pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"27%\">\n<p class=\"MsoNoSpacing\">Benefit<\/p>\n<\/td>\n<td style=\"border-top: white 1pt solid;height: 24.75pt;border-right: white 1pt solid;width: 25%;background: #5b9bd5;border-bottom: white 3pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"25%\">\n<p class=\"MsoNoSpacing\">Impact Without Control<\/p>\n<\/td>\n<td style=\"border-top: white 1pt solid;height: 24.75pt;border-right: white 1pt solid;width: 30.74%;background: #5b9bd5;border-bottom: white 3pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"30%\">\n<p class=\"MsoNoSpacing\">Limitations<\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 101.25pt\">\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 17.24%;background: #5b9bd5;border-bottom: white 1pt solid;border-left: white 1pt solid;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"17%\">\n<p class=\"MsoNoSpacing\">Antivirus \/ Antimalware<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 27%;background: #d2deef;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"27%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Can limit the execution of malware known to the AV industry.<\/span><\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 25%;background: #d2deef;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"25%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Attacker can write and run any code, custom C++ applications, internet tools, etc.<\/span><\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 30.74%;background: #d2deef;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"30%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Can be disabled by administrators. AV signatures can be evaded if the attacker is capable of recompiling or modifying an application.<\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 101.25pt\">\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 17.24%;background: #5b9bd5;border-bottom: white 1pt solid;border-left: white 1pt solid;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"17%\">\n<p class=\"MsoNoSpacing\">Applocker in Deny Mode<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 27%;background: #eaeff7;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"27%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Can limit the execution of malware known to your organization.<\/span><\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 25%;background: #eaeff7;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"25%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Attacker can write and run any code, custom C++ applications, etc., as long as they aren\u2019t well known attack tools or exploits.<\/span><\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 30.74%;background: #eaeff7;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"30%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Can be disabled by administrators. Only blocks known evil \/ undesirable malware, can be bypassed with only minor application changes.<\/span><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 101.25pt\">\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 17.24%;background: #5b9bd5;border-bottom: white 1pt solid;border-left: white 1pt solid;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"17%\">\n<p class=\"MsoNoSpacing\">Applocker in Allow Mode<\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 27%;background: #d2deef;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"27%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Can prevent the execution of unknown \/ unapproved applications.<\/span><\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 25%;background: #d2deef;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"25%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Attacker can write arbitrary custom applicatons, as long as they are not detected by AV or Applocker Deny rules.<\/span><\/p>\n<\/td>\n<td style=\"border-top: medium none;height: 101.25pt;border-right: white 1pt solid;width: 30.74%;background: #d2deef;border-bottom: white 1pt solid;border-left: medium none;padding: 0.75pt 5.4pt 0in 5.4pt\" valign=\"top\" width=\"30%\">\n<p class=\"MsoNoSpacing\"><span style=\"color: black\">Can be disabled by administrators. Attacker can still leverage in-box tools like VBScript, Office macros, HTA applications, local web pages, PowerShell, etc.<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;line-height: normal\">\n<p>These protections are, of course, in addition to the regular Windows user permissions model. Applications don\u2019t need to prevent users from modifying system-wide registry keys because Windows itself enforces those protections.\nThe strongest form of protection is when a system employs AppLocker in \u2018Allow Mode\u2019, where only specific known applications are allowed to run.\nPrior to PowerShell version 5, a limitation of AppLocker\u2019s \u2018Allow Mode\u2019 was that interactive PowerShell input was not subject to this policy. While Allow Mode might prevent unknown PowerShell scripts from running, it would not prevent the equivalent commands entered at an interactive prompt.\nIn version 5, PowerShell now reduces its functionality to \u201cConstrained Mode\u201d for both interactive input and user-authored scripts when it detects that PowerShell scripts have an \u2018Allow Mode\u2019 policy applied to them. Constrained PowerShell limits the language mode to Constrained Language (as described in <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/dn433292.aspx\">about_Language_Modes<\/a>), a mode first introduced for Windows RT.\nConstrained Language doesn\u2019t limit the capability of the core PowerShell language \u2013 familiar techniques such as variables, loops, and functions are all supported. It does, however, limit the extended language features that can lead to unverifiable code execution such as direct .NET scripting, invocation of Win32 APIs via the Add-Type cmdlet, and interaction with COM objects.\nScripts that are allowed by the AppLocker policy (for example: signed by the enterprise\u2019s trusted code signing certificate, or in a trusted directory) are not subject to Constrained Language. They have access to the extended capabilities of the PowerShell language disallowed by Constrained Language. This includes unverifiable extensions such as .NET scripting, and invocation of Win32 APIs.\nFor more information on configuring AppLocker, see <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/dd723678(v=ws.10).aspx\">https:\/\/technet.microsoft.com\/en-us\/library\/dd723678(v=ws.10).aspx<\/a>.\nHere\u2019s an example PowerShell command that lets you experiment with AppLocker in \u2018Allow Mode\u2019 for all scripts (i.e.: blocking all VBScripts, batch files, and PowerShell scripts by default), and then allows only PowerShell scripts from <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">c:\\trusted<\/span> to run.<\/p>\n<p class=\"MsoNormal\" style=\"background: #012456;margin-left: 0.5in\"><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">PS C:\\&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime;line-height: 107%\">$whitelistApplockerPolicy<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray;line-height: 107%\">=<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow;line-height: 107%\">New-AppLockerPolicy<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray;line-height: 107%\">-RuleType<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\"> Path <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray;line-height: 107%\">-FileInformation<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: teal;line-height: 107%\">&#8220;c:\\trusted\\*.ps1&#8221;\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">PS C:\\&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime;line-height: 107%\">$existingApplockerPolicy<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray;line-height: 107%\">=<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow;line-height: 107%\">Get-AppLockerPolicy<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray;line-height: 107%\">\u2013Local\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">PS C:\\&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow;line-height: 107%\">Set-AppLockerPolicy<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime;line-height: 107%\">$whitelistApplockerPolicy\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">PS C:\\&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow;line-height: 107%\">powershell\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">Windows PowerShell\nCopyright (C) 2015 Microsoft Corporation. All rights reserved.\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">\nPS C:\\&gt; $executionContext.SessionState.LanguageMode\nConstrainedLanguage<\/span><\/p>\n<p><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">PS C:\\&gt; [Math]::Sqrt([Math]::Pi)\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: red;line-height: 107%\">Cannot invoke method. Method invocation is supported only on core types in this language mode.\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: red;line-height: 107%\">At line:1 char:1\n+ [Math]::Sqrt([Math]::Pi)\n+ ~~~~~~~~~~~~~~~~~~~~~~~~\n+ CategoryInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : InvalidOperation: (:) [], RuntimeException\n+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage<\/span>\n<span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">PS C:\\&gt; &#8216;[Math]::Sqrt([Math]::Pi)&#8217; &gt; c:\\trusted\\trusted.ps1\nPS C:\\&gt; c:\\trusted\\trusted.ps1\n1.77245385090552<\/span>\nPS C:\\&gt; exit\nPS C:\\&gt;\u00a0<span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow;line-height: 107%\">Set-AppLockerPolicy<\/span>\u00a0<span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime;line-height: 107%\">$existingApplockerPolicy<\/span>\nBeware \u2013 if users can add or edit files in <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">c:\\trusted<\/span>, then this policy offers no protection. Users in that situation can simply put scripts in that directory to bypass the policy. Also, if your AppLocker policy doesn\u2019t similarly limit executables, then this policy offers no protection. Users in that situation can simply run an executable to bypass the policy.<\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.5in\"><i>In order to enforce its policies, AppLocker requires the <\/i><i><span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">AppIDSvc<\/span> service to be running. When enabling a policy, be sure to set the service to Auto Start. <\/i><\/p>\n<p>As mentioned previously, Constrained PowerShell layers on top of the Windows permissions model. Because of that, it (like AppLocker) should be applied to regular user accounts and not system administrators. Administrator accounts can bypass the policy by simply changing or disabling it.<\/p>\n<h2><a>Protected Event Logging<\/a><\/h2>\n<p>One concern when increasing the amount of logging on a system is the danger that logged content may contain sensitive data. For example, if you log the content of every PowerShell script that was run, there is the possibility that a script may contain credentials or other sensitive data.\nIf an attacker later compromises a machine that has logged this data, it may provide them with additional information with which to extend their reach.\nTo prevent this dilemma, Windows 10 introduces Protected Event Logging. Protected Event Logging lets participating applications encrypt sensitive data as they write it to the event log. You can then decrypt and process these logs once you\u2019ve moved them to a more secure and centralized log collector.\nOne common technique to move event logs to a more secure and centralized log collector is built in to Windows: Windows Event Forwarding. A great document on setting up Windows Event Forwarding is available from the NSA: \u201c<a href=\"http:\/\/www.nsa.gov\/ia\/_files\/app\/Spotting_the_Adversary_with_Windows_Event_Log_Monitoring.pdf\">Spotting the Adversary with Windows Event Log Monitoring<\/a>\u201d. Other options are System Center Operations Manager, or commercially available Security Information and Event Management (SIEM) systems.<\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.5in\"><i>In Windows 10, PowerShell is the only application that participates in Protected Event Logging. <\/i><\/p>\n<p>Protected Event Logging protects event log content through the IETF Cryptographic Message Syntax (CMS) standard. The CMS encryption standard implements public key cryptography, where the keys used to encrypt content (the <i>public key<\/i>) and the keys used to decrypt content (the <i>private key<\/i>) are separate.\nYour public key can be shared widely, and is not sensitive data. If any content is encrypted with this public key, only your private key can decrypt it. For more information about Public Key Cryptography, see: <a href=\"http:\/\/en.wikipedia.org\/wiki\/Public-key_cryptography\"><span style=\"text-decoration: none;color: windowtext\">http:\/\/en.wikipedia.org\/wiki\/Public-key_cryptography<\/span><\/a>.\nWhen you implement a protected event logging policy, you deploy a public key to all machines that have event log data you want to protect. You retain the corresponding private key to post-process the event logs at a more secure location such as a central event log collector, or SIEM aggregator.\nTo enable Protected Event Logging, enable the \u2018Enable Protected Event Logging\u2019 feature in Group Policy through <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Windows Components -&gt; Administrative Templates -&gt; Event Logging<\/span>. This setting requires an encryption certificate, which you can provide in one of several forms:<\/p>\n<ul>\n<li class=\"MsoListParagraphCxSpFirst\" style=\"text-indent: -0.25in\"><span style=\"font: 7pt 'Times New Roman'\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span>The content of a base-64 encoded X.509 certificate (for example, as offered by the \u2018Export\u2019 option in Certificate Manager)<\/li>\n<li class=\"MsoListParagraphCxSpMiddle\" style=\"text-indent: -0.25in\"><span style=\"font: 7pt 'Times New Roman'\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span>The thumbprint of a certificate that can be found in the Local Machine certificate store (usually deployed by PKI infrastructure)<\/li>\n<li class=\"MsoListParagraphCxSpMiddle\" style=\"text-indent: -0.25in\"><span style=\"font: 7pt 'Times New Roman'\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span>The full path to a certificate (can be local, or a remote share)<\/li>\n<li class=\"MsoListParagraphCxSpMiddle\" style=\"text-indent: -0.25in\"><span style=\"font: 7pt 'Times New Roman'\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span>The path to a directory containing a certificate or certificates (can be local, or a remote share)<\/li>\n<li class=\"MsoListParagraphCxSpLast\" style=\"text-indent: -0.25in\"><span style=\"font: 7pt 'Times New Roman'\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/span>The subject name of a certificate that can be found in the Local Machine certificate store (usually deployed by PKI infrastructure)<\/li>\n<\/ul>\n<p>The resulting certificate must have &#8216;Document Encryption&#8217; as an enhanced key usage (1.3.6.1.4.1.311.80.1), as well as either Data Encipherment or Key Encipherment key usages enabled.\nYou can also use the following PowerShell function to enable protected event logging:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkblue\">function<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: blueviolet\">Enable-ProtectedEventLogging<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">{\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 <span style=\"color: darkblue\">param<\/span>(\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: darkgray\">[<\/span><span style=\"color: deepskyblue\">Parameter<\/span>(Mandatory)<span style=\"color: darkgray\">]<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: orangered\">$Certificate<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 ) <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;background: white;line-height: normal;padding-left: 30px\"><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 <span style=\"color: orangered\">$basePath<\/span> <span style=\"color: darkgray\">=<\/span> <span style=\"color: darkred\">&#8220;HKLM:\\Software\\Policies\\Microsoft\\Windows\\EventLog\\ProtectedEventLogging&#8221;<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 <span style=\"color: darkblue\">if<\/span>(<span style=\"color: darkgray\">-not<\/span> (<span style=\"color: blue\">Test-Path<\/span> <span style=\"color: orangered\">$basePath<\/span>))\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 {\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: orangered\">$null<\/span> <span style=\"color: darkgray\">=<\/span> <span style=\"color: blue\">New-Item<\/span> <span style=\"color: orangered\">$basePath<\/span> \u2013<span style=\"color: navy\">Force<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 } <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;background: white;line-height: normal;padding-left: 30px\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;background: white;line-height: normal;padding-left: 30px\"><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 <span style=\"color: blue\">Set-ItemProperty<\/span> <span style=\"color: orangered\">$basePath<\/span> <span style=\"color: navy\">-Name<\/span> <span style=\"color: blueviolet\">EnableProtectedEventLogging<\/span> <span style=\"color: navy\">-Value<\/span> <span style=\"color: darkred\">&#8220;1&#8221;<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 <span style=\"color: blue\">Set-ItemProperty<\/span> <span style=\"color: orangered\">$basePath<\/span> <span style=\"color: navy\">-Name<\/span> <span style=\"color: blueviolet\">EncryptionCertificate<\/span> <span style=\"color: navy\">-Value<\/span> <span style=\"color: orangered\">$Certificate<\/span> <\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;background: white;line-height: normal;padding-left: 30px\"><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">} <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 9pt;font-family: 'Lucida Console';color: darkblue\">function<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\"> <span style=\"color: blueviolet\">Disable-ProtectedEventLogging<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">{\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">\u00a0\u00a0\u00a0 <span style=\"color: blue\">Remove-Item<\/span> <span style=\"color: blueviolet\">HKLM:\\Software\\Policies\\Microsoft\\Windows\\EventLog\\ProtectedEventLogging<\/span> <span style=\"color: navy\">-Force<\/span> \u2013<span style=\"color: navy\">Recurse<\/span>\n<\/span><span style=\"font-size: 9pt;font-family: 'Lucida Console'\">} <\/span><\/p>\n<p>While the Group Policy template for Protected Event Logging only exists in Windows 10, PowerShell version 5 and PowerShell in KB3000850 supports protected event logging if the settings are configured manually.<\/p>\n<p class=\"MsoNormal\">For example:<\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;background: #012456;line-height: normal\"><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\">PS C:\\temp&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime\">$cert<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray\">=<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow\">Get-Content<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\"> C:\\temp\\ProtectedEventLogging.cer <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray\">\u2013Raw\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\">PS C:\\temp&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow\">Enable-ProtectedEventLogging <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray\">\u2013Certificate <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: lime\">$cert<\/span><\/p>\n<p>Protected event logging must be configured <i>in addition<\/i> to enabling any application-specific logging. Enabling protected event logging doesn\u2019t automatically enable event sources such as PowerShell script block logging.\nIf an application cannot properly resolve the encryption certificate during logging, it will log a warning message into its event log channel, and then continue to log the data without event log protection.\nWhen configuring the encryption certificate for deployment, ensure that it doesn\u2019t include the private key. If the certificate includes the private key, then it can also be used to decrypt the protected event log content.\nThe following commands show how to determine if a Document Encryption certificate on a node has been deployed with a private key:<\/p>\n<p class=\"MsoNoSpacing\" style=\"margin-left: 0.5in\"><span style=\"font-size: 9pt;font-family: 'Courier New'\">PS Cert:\\CurrentUser\\My&gt; dir \u2013DocumentEncryptionCert<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\n<\/span><\/p>\n<p class=\"MsoNoSpacing\" style=\"margin-left: 0.5in\"><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0\u00a0\u00a0 Directory: Microsoft.PowerShell.Security\\Certificate::CurrentUser\\My<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0 <\/span><\/p>\n<p class=\"MsoNoSpacing\" style=\"margin-left: 0.5in\"><span style=\"font-size: 9pt;font-family: 'Courier New'\">Thumbprint\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Subject\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">&#8212;&#8212;&#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212;&#8212;-\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">5EE994BD4C0B79ADFAA7890D7D3FBE820CF03282\u00a0 CN=ProtectedEventLogging\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">\u00a0<\/span><\/p>\n<p class=\"MsoNoSpacing\" style=\"margin-left: 0.5in\"><span style=\"font-size: 9pt;font-family: 'Courier New'\">PS Cert:\\CurrentUser\\My&gt; (dir -DocumentEncryptionCert).HasPrivateKey\n<\/span><span style=\"font-size: 9pt;font-family: 'Courier New'\">True<\/span><\/p>\n<p>To post-process the content of protected event log messages, use the PowerShell <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Unprotect-CmsMessage<\/span> cmdlet and <em><span style=\"font-family: 'Calibri',sans-serif\">Cryptographic Message Syntax (CMS) encryption and decryption cmdlets<\/span><\/em>.\nFor example, the following PowerShell commands automatically decrypt encrypted event log messages, provided that an appropriate decryption certificate (i.e.: one that has the private key) is installed on the machine:\n<a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2015\/06\/post_process_1.png\"><img decoding=\"async\" class=\"size-full wp-image-13406 aligncenter\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2015\/06\/post_process_1.png\" alt=\"\" width=\"805\" height=\"748\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/post_process_1.png 805w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/post_process_1-300x279.png 300w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2015\/06\/post_process_1-768x714.png 768w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/a><\/p>\n<p class=\"MsoNormal\">To retain the structure of the actual event log entry (while just decrypting the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">Message<\/span> field), use the <span style=\"font-size: 9pt;font-family: 'Courier New';line-height: 107%\">\u2013IncludeContext<\/span> parameter:<\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0pt;background: #012456;line-height: normal\"><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\">PS C:\\temp&gt; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow\">Get-WinEvent<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\"> Microsoft-Windows-PowerShell\/Operational | <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow\">?<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\"> Id <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray\">-eq<\/span> <span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: white\">4104<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\"> |\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\">&gt;&gt;&gt;\u00a0\u00a0\u00a0\u00a0 <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: yellow\">Unprotect-CmsMessage<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\"> \u2013<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: gray\">IncludeContext\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\">\u00a0\u00a0 ProviderName: Microsoft-Windows-PowerShell\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\">TimeCreated\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Id LevelDisplayName Message\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\">&#8212;&#8212;&#8212;&#8211;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; &#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;-\n<\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0\">4\/3\/2015 11:47:13 AM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4104 Verbose\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Creating Scriptblock text (1 of 1):&#8230; <\/span><span style=\"font-size: 9pt;font-family: consolas;background: #012456;color: #eeedf0;line-height: 107%\">\u00a0<\/span><\/p>\n<p>As you can tell, we\u2019ve put a lot of effort into making PowerShell an extremely transparent platform for the Blue Team in the context of an Assume Breach mindset. So go out and start hunting!\nLee Holmes [MSFT]\nWindows PowerShell Engineering<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Warning: Long blog post ahead! If you\u2019d like to read (or share) this as a whitepaper, you can download it here: \u201cScripting Security and Protection Advances in Windows 10\u201d). At Microsoft, we invest an enormous amount of time and energy managing world-class cloud services and incredibly large enterprise networks. Security is critical for all of [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>(Warning: Long blog post ahead! If you\u2019d like to read (or share) this as a whitepaper, you can download it here: \u201cScripting Security and Protection Advances in Windows 10\u201d). At Microsoft, we invest an enormous amount of time and energy managing world-class cloud services and incredibly large enterprise networks. Security is critical for all of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/1121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}