Powershell Security Limit Language Features Secure Communication Track Abuse
Total Page:16
File Type:pdf, Size:1020Kb
PowerShell Security Limit language features Secure communication Track abuse Michael Pietroforte Wolfgang Sommergut Cover Designer: Claudia Wolff 1. Edition 2020 ISBN: 9781672847827 © 2020 WindowsPro / Wolfgang Sommergut All rights reserved. No portion of this book may be reproduced in any form without permission from the publisher, except as permitted by U.S. copy- right law. Every effort has been made to ensure that the content provided in this book is accurate and helpful for our readers at publishing time. However, this is not an exhaustive treatment of the subjects. No liability is assumed for losses or damages due to the information provided. You are responsi- ble for your own choices, actions, and results. Michael Pietroforte Wolfgang Sommergut PowerShell Security Limit language features, secure communications, track abuse Control execution of scripts using execution policy, code signing and constrained language mode Secure PowerShell remoting with SSH und TLS Delegate administrative tasks with JEA Audit and analyze PowerShell activities, encrypt logs Improve code quality following best practices About the authors Michael Pietroforte is the founder and editor in chief of 4sysops. He has more than 35 years of experience in IT management and system administration. Wolfgang Sommergut has over 20 years of expe- rience in IT journalism. He has also worked as a system administrator and as a tech consultant. Today he runs the German publication WindowsPro.de. Table of contents 1 PowerShell as a hacking tool: Prevent abuse of scripts .................... 8 1.1 Lax default configuration of PowerShell ................................... 9 1.2 Hacking tools for PowerShell .................................................. 10 1.3 General blocking of PowerShell .............................................. 12 1.4 Circumvention through alternative shells ............................... 14 1.5 Secure PowerShell with integrated mechanisms .................... 15 2 Restrict execution of scripts ............................................................ 20 2.1 Setting an execution policy ..................................................... 20 2.2 Signing PowerShell scripts ....................................................... 25 2.3 Reduce PowerShell risks with Constrained Language Mode ... 36 3 Secure communication ................................................................... 48 3.1 Installing OpenSSH on Windows 10 and Server 2019 ............. 48 3.2 PowerShell remoting with SSH public key authentication ...... 57 3.3 Creating a self-signed certificate ............................................. 64 3.4 Remoting over HTTPS with a self-signed certificate ................ 71 4 Just Enough Administration ............................................................ 81 4.1 JEA Session Configuration ....................................................... 81 4.2 Defining and assigning role functions ..................................... 92 5 Audit PowerShell activities .............................................................. 98 5.1 Log commands in a transcription file ...................................... 98 5.2 Scriptblock logging: Record commands in the event log ...... 106 5.3 Issuing certificates for document encryption ....................... 112 5.4 Encrypt event logs and files with PowerShell and GPO ......... 119 5.5 Audit PowerShell keys in the registry .................................... 127 6 Improve PowerShell code ............................................................. 134 6.1 Avoiding errors using strict mode ......................................... 134 6.2 Checking code with ScriptAnalyzer ....................................... 140 7 More security with ScriptRunner .................................................. 145 7.1 PowerShell management solution ........................................ 145 7.2 Five steps to safe automation and delegation ...................... 146 7.3 Additional information .......................................................... 151 Lax default configuration of PowerShell 1 PowerShell as a hacking tool: Prevent abuse of scripts PowerShell is a powerful tool for system administration and as such also a perfect means for hackers. Due to the tight integration into the system, attempts to simply block PowerShell provide a false impression of security. The best protection is provided by PowerShell's own mechanisms. PowerShell offers almost unlimited access to the resources of a Windows computer and also can automate numerous applications such as Ex- change. Users aren't limited to the many modules and cmdlets, but can also integrate .NET classes, Windows APIs, and COM objects. These capa- bilities are particularly dangerous in the hands of attackers. Since many versions of With Windows Server, Microsoft avoids to activate any roles and features on a freshly installed machine in order to minimize the attack surface. On such a locked down system users must explicitly add all required services. 8 Lax default configuration of PowerShell 1.1 Lax default configuration of PowerShell However with PowerShell, the full range of functions is available from the start on every Windows PC, if you put aside the "protection" by a restric- tive execution policy. However, it is not recommended to leave this state as it is. You don't only have to fear malicious PowerShell experts who can exploit all potentials of a script. In fact, even basic knowledge is sufficient to pen- etrate systems with the help of various hacking tools. 9 Hacking tools for PowerShell 1.2 Hacking tools for PowerShell Quite a number of them can be easily obtained as open source via Github. These include the extensive script and module collections PowerSploit, PowerShell Empire, Nishang or PowerUp. You might assume that your computers are well protected by virus scan- ners which detect and block these hacking tools. In fact, Windows De- fender, for example, intervenes after the download and quarantines the scripts. Windows Defender prevents the download of PowerSploit However, in contrast to binary files, scripts can be changed quite easily to fool a signature based recognition. For example, you can copy Invoke- Mimikatz from the browser window and paste it into an editor like Pow- erShell_ISE to experiment with the code. 10 Hacking tools for PowerShell This blog post by Carrie Roberts demonstrates how to outwit most virus scanners by searching and replacing a few significant code snippets. At this point, the technique discussed there may not be up to date any more, but a bit of experimenting will probably reveal how virus scanners detect this script. Otherwise, various AMSI-Bypasses can help you to overwhelm Win- dows Defender. 11 General blocking of PowerShell 1.3 General blocking of PowerShell To prevent such threats, many companies will take a radical measure and disable PowerShell altogether. In centrally managed environments, black- listing with AppLocker or the Software Restriction Policies is an effective solution. If you decide to use the software restriction, you create two new hash rules and connect them to powershell.exe and powershell_ise.exe. For the security level choose Not allowed. If you block the programs at the user level, admins can be excluded. Blocking powershell.exe with software restriction policies This approach has two disadvantages. Firstly, it can be an obstacle to sys- tem administration, because PowerShell has become an indispensable 12 General blocking of PowerShell tool for most admins. For example, PowerShell logon scripts that are exe- cuted in the security context of a user will no longer work. 13 Circumvention through alternative shells 1.4 Circumvention through alternative shells More serious, however, is that PowerShell comprises more than just pow- ershell.exe or power-shell_ise.exe and therefore cannot be permanently blocked by denying access to these two files. Rather, it is a system compo- nent (System.Management.Automation) that cannot be removed and can be used by various runspaces. Attackers could thus access PowerShell from any of their own programs. It is therefore no surprise that already shells exist that can be integrated into your own code or that can be executed directly. Among them are p0wnedShell or PowerOPS. In addition, numerous versions of PowerShell 6 and 7 are available for download in ZIP format, which can be easily unpacked into a directory and executed. Frequent previews of PowerShell 7 would keep admins busy, because they always have to create new rules to cover all these versions. And last but not least, another workaround is to compile PowerShell- Scripts into executable files. They are also not dependent on pow- ershell.exe. 14 Secure PowerShell with integrated mechanisms 1.5 Secure PowerShell with integrated mechanisms Instead of completely banishing PowerShell without achieving real secu- rity, it makes more sense to use its security features. These were further improved with version 5, so that you should update PCs to the latest ver- sion of PowerShell. It is also highly recommended to remove PowerShell 2.0, which is still pre- installed as an optional feature and can be uninstalled in Windows 8.1 and Server 2012 or higher. With this old version, all major restrictions for Pow- erShell can be circumvented. PowerShell 2.0 is an optional feature starting with Windows 8 and Server 2012 and is ena- bled by default. 15 Secure PowerShell with integrated mechanisms One of the key security mechanisms of Windows PowerShell is the Con- strained Language