Book Image

PowerShell Automation and Scripting for Cybersecurity

By : Miriam C. Wiesner
5 (2)
Book Image

PowerShell Automation and Scripting for Cybersecurity

5 (2)
By: Miriam C. Wiesner

Overview of this book

Take your cybersecurity skills to the next level with this comprehensive guide to PowerShell security! Whether you’re a red or blue teamer, you’ll gain a deep understanding of PowerShell’s security capabilities and how to use them. After revisiting PowerShell basics and scripting fundamentals, you’ll dive into PowerShell Remoting and remote management technologies. You’ll learn how to configure and analyze Windows event logs and understand the most important event logs and IDs to monitor your environment. You’ll dig deeper into PowerShell’s capabilities to interact with the underlying system, Active Directory and Azure AD. Additionally, you’ll explore Windows internals including APIs and WMI, and how to run PowerShell without powershell.exe. You’ll uncover authentication protocols, enumeration, credential theft, and exploitation, to help mitigate risks in your environment, along with a red and blue team cookbook for day-to-day security tasks. Finally, you’ll delve into mitigations, including Just Enough Administration, AMSI, application control, and code signing, with a focus on configuration, risks, exploitation, bypasses, and best practices. By the end of this book, you’ll have a deep understanding of how to employ PowerShell from both a red and blue team perspective.
Table of Contents (19 chapters)
1
Part 1: PowerShell Fundamentals
6
Part 2: Digging Deeper – Identities, System Access, and Day-to-Day Security Tasks
12
Part 3: Securing PowerShell – Effective Mitigations In Detail

Enumerating GPOs

To enumerate which GPOs were linked in the current environment, you can use ADSI accelerators:

By using the [adsi] accelerator, you can provide a DistinguishedName path to show the gplink property, which will display the GPOs linked to that particular path. To query a GPO that was linked to the PSSecComputers OU (OU=PSSecComputers,DC=PSSec,DC=local), we could use the following code snippet to query it:

$DistinguishedName = "LDAP://OU=PSSecComputers,DC=PSSec,DC=local"
$obj = [adsi]$DistinguishedName
$obj.gplink

The following screenshot shows the result of this query:

Figure 6.2 – Querying GPOs using the ADSI accelerator

Figure 6.2 – Querying GPOs using the ADSI accelerator

You can also use [adsisearcher] to filter for GPOs linked to the environment, as shown in the following example:

$GpoFilter = "(objectCategory=groupPolicyContainer)"
$Searcher = [adsisearcher]$GpoFilter
$Searcher.SearchRoot = [adsi]"LDAP://DC=PSSec,DC=local"
...