PowerShell Overview
PowerShell basics first (cmdlets, objects, the pipeline, filtering and sorting), then how to use PowerShell offensively: the built-in ActiveDirectory cmdlets and PowerView for enumerating a domain and its trusts.
What PowerShell Is
PowerShell is the Windows scripting language and shell environment, built on the .NET framework.
Because of that, PowerShell can execute .NET functions directly from its shell. Most PowerShell commands, called cmdlets, are written in .NET. Unlike other scripting languages and shell environments, the output of these cmdlets is objects, which makes PowerShell somewhat object oriented. It also means you can perform actions on the output object, which makes it convenient to pass output from one cmdlet to another.
Cmdlets follow a Verb-Noun format. For example, the cmdlet to list commands is Get-Command.
Common verbs:
- Get
- Start
- Stop
- Read
- Write
- New
- Out
Introduction to PowerShell Basics
Now that we understand how cmdlets work, let's look at how to use them. The main thing to remember: Get-Command and Get-Help are your best friends.
Using Get-Help
Get-Help displays information about a cmdlet. To get help about a particular command:
Get-Help Command-Name
Passing the -examples flag shows exactly how to use the command. The output looks like this:
Get-Help Get-Command Examples
Using Get-Command
Get-Command gets all the cmdlets installed on the current device. The great thing about it is that it supports pattern matching:
Get-Command Verb-* or Get-Command *-Noun
Running Get-Command New-* lists all the cmdlets for the verb New:
Get-Command New-*
Object Manipulation
The output of every cmdlet is an object. To actually manipulate that output, we need to know two things:
- how to pass output to other cmdlets
- how to use specific object cmdlets to extract information
The Pipeline and Get-Member
The pipeline (|) passes output from one cmdlet to another. The big difference from other shells is that PowerShell passes an object to the next cmdlet instead of text or a string. Like every object in object-oriented frameworks, it contains methods and properties. Think of methods as functions that can be applied to the cmdlet's output, and properties as variables in that output.
To view these details, pass the output of a cmdlet to the Get-Member cmdlet:
Verb-Noun | Get-Member
An example that views the members of Get-Command:
Get-Command | Get-Member -MemberType Method
Get-Command | Get-Member -MemberType Method
The -MemberType flag in that command shows that you can choose between methods and properties.
Creating Objects From Previous Cmdlets
One way of manipulating objects is to pull properties out of a cmdlet's output and create a new object from them. This is done with the Select-Object cmdlet.
Here's an example that lists the directories and selects only the mode and the name:
Get-ChildItem | Select-Object -Property Mode, Name
These flags select particular information:
first: gets the first x objectslast: gets the last x objectsunique: shows the unique objectsskip: skips x objects
Filtering Objects
When retrieving output objects, you may want only the ones that match a very specific value. Where-Object filters based on the value of properties.
The general format:
Verb-Noun | Where-Object -Property PropertyName -operator Value
Verb-Noun | Where-Object {$_.PropertyName -operator Value}
The second version uses the $_ operator to iterate through every object passed to Where-Object.
-operator is one of the following:
Contains: if any item in the property value is an exact match for the specified valueEQ: if the property value is the same as the specified valueGT: if the property value is greater than the specified value
For a full list of operators, use this link.
Here's an example that checks for stopped services:
Get-Service | Where-Object -Property Status -eq Stopped
Sort Object
When a cmdlet outputs a lot of information, sorting it makes the information easier to extract. Pipe the output of the cmdlet to Sort-Object:
Verb-Noun | Sort-Object
Here's an example that sorts the list of directories:
Get-ChildItem | Sort-Object
With the basics covered, we can move on to using PowerShell offensively to enumerate and exploit.
Introduction to Offensive PowerShell
How do we apply all of this to attacking a Windows network? Offensive PowerShell can be used to enumerate and attack Windows and Windows Active Directory.
Basic Offensive PowerShell
Most offensive PowerShell comes from modules like ActiveDirectory and PowerView, but PowerShell also has a few built-in cmdlets that can be used offensively.
Using Modules in PowerShell
PowerShell can import modules such as ActiveDirectory and PowerView to expand the list of available cmdlets. To import a module, use either Import-Module <Module> or dot space dot backslash <Module> (. .\Module).
Examples of importing modules:
Import-Module Module
. .\Module.ps1
Note: . .\ only works with PowerShell script files. All other modules have to be imported with Import-Module. For example, ActiveDirectory can only be imported with Import-Module.
Get-ADDomain
Get-ADDomain pulls most of the information about the domain you're attacking. It can list all of the Domain Controllers for an environment, the NetBIOS domain name, the FQDN (Fully Qualified Domain Name) and much more. Select-Object filters out unnecessary objects that would otherwise be displayed (like Containers, Group Policy Objects, and much more).
Get-ADDomain | Select-Object NetBIOSName, DNSRoot, InfrastructureMaster
Get-ADDomain | Select-Object NetBIOSName, DNSRoot, InfrastrucuteMaster
Get-ADForest
Get-ADForest pulls all the domains within a forest and lists them. This is useful if a bidirectional trust is set up, since it may allow a foothold in another domain on the LAN. Like Get-ADDomain, it produces a lot of output, so Select-Object trims it down.
Get-ADForest | Select-Object Domains
Get-ADForest | Select-Object Domains
Get-ADTrust
Get-ADTrust is the last built-in PowerShell cmdlet covered here before moving over to PowerView. It gives a lot of information about the trusts within the AD domain: whether a trust is one-way or bidirectional, who the source is, who the target is, and much more.
-Filter is a required field. It's there in case you want to filter on a specific domain/trust. If you don't (as in most circumstances), provide a * to wildcard the results.
Get-ADTrust -Filter * | Select-Object Direction,Source,Target
Get-ADTrust -Filter * | Select-Object Direction,Source,Target
Introduction to PowerView
PowerView (part of PowerSploit by PowerShellMafia) is an excellent suite of tools for enumerating and exploiting an AD domain. This section only covers PowerView's ability to enumerate information about the domain and its trusts. The .ps1 is available here.
Get-NetDomain
Get-NetDomain is similar to the ActiveDirectory module's Get-ADDomain but returns a lot less information, which can be better. It enumerates basic info such as the forest, Domain Controllers, and domain name.
Get-NetDomain
Get-NetDomain
Get-NetDomainController
Get-NetDomainController lists all of the Domain Controllers within the network. This is incredibly useful for initial reconnaissance, especially without a Windows device that's joined to the domain.
Get-NetDomainController
Get-NetDomainControllers
Get-NetForest
Get-NetForest is similar to Get-ADForest and gives similar output: all the associated domains, the root domain, and the Domain Controllers for the root domain.
Get-NetForest
Get-NetForest
Get-NetDomainTrust
Get-NetDomainTrust is similar to Get-ADTrust with our Select-Object filter applied to it. It's short, sweet and to the point.
Get-NetDomainTrust
Get-NetDomainTrust