PowerShell Overview
PowerShell is the Windows scripting language and shell environment, built on the .NET framework.
That lets PowerShell 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, making PowerShell somewhat object oriented. Running a cmdlet lets you act on the output object, which makes it convenient to pass output from one cmdlet to another. The normal format of a cmdlet is Verb-Noun; for example, the cmdlet to list commands is Get-Command.
Common verbs include:
- Get
- Start
- Stop
- Read
- Write
- New
- Out
Introduction to PowerShell Basics
Now that cmdlets make sense, here is how to use them. The main thing to remember is that 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, run:
Get-Help Command-Name
Passing the -examples flag shows how to use the command, returning output like the following:
Get-Help Get-Command Examples
Using Get-Command
Get-Command gets all the cmdlets installed on the current device. The great thing about this cmdlet is that it allows pattern matching, like Get-Command Verb-* or Get-Command *-Noun.
Running Get-Command New-* to view all the cmdlets for the verb new displays the following:
Get-Command New-*
Object Manipulation
As noted above, the output of every cmdlet is an object. To actually manipulate that output, a few things need to be worked out:
- Passing output to other cmdlets
- Using specific object cmdlets to extract information
The Pipeline (|) passes output from one cmdlet to another. A major difference from other shells is that instead of passing text or a string to the command after the pipe, PowerShell passes an object to the next cmdlet. Like every object in an object-oriented framework, it contains methods and properties. Methods are functions that can be applied to a cmdlet's output; properties are like variables in that output. To view these details, pass a cmdlet's output to the Get-Member cmdlet:
Verb-Noun | Get-Member
An example of viewing the members for Get-Command:
Get-Command | Get-Member -MemberType Method
Get-Command | Get-Member -MemberType Method
The flag above shows you can also select between methods and properties.
Creating Objects From Previous cmdlets
One way to manipulate objects is pulling the properties out of a cmdlet's output and creating a new object. This is done with the Select-Object cmdlet.
Here is an example of listing the directories and selecting just the mode and the name:
Get-ChildItem | Select-Object -Property Mode, Name
You can also use these flags to select particular information:
- first - gets the first x object
- last - gets the last x object
- unique - shows the unique objects
- skip - skips x objects
Filtering Objects
When retrieving output objects, you may want only those that match a specific value. Use Where-Object to filter on the value of properties. The general format is:
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 the Where-Object cmdlet.
Where -operator is one of the following:
- Contains - If any item in the property value is an exact match for the specified value/
- EQ - If the property value is the same as the specified value.
- GT - If the property value is greater than the specified value
For a full list of operators, use this link.
Here's an Example of Checking the Stopped Processes
Get-Service | Where-Object -Property Status -eq Stopped
Sort Object
When a cmdlet outputs a lot of information, you may need to sort it to read it more efficiently. Pipe the output of a cmdlet to Sort-Object. The format is:
Verb-Noun | Sort-Object
Here is an example of sorting the list of directories:
Get-ChildItem | Sort-Object
With the basics covered, the rest is using PowerShell offensively to enumerate and exploit.
Introduction to Offensive PowerShell
With all this background, how does it apply to attacking a Windows network? Offensive PowerShell can enumerate and attack Windows and Windows Active Directory.
Basic Offensive PowerShell
Most offensive PowerShell comes from modules like ActiveDirectory and PowerView used to enumerate and exploit, though PowerShell also has a few built-in cmdlets useful on the offensive.
Using Modules in PowerShell
PowerShell can import modules such as ActiveDirectory and PowerView to expand the available cmdlets. To import a module, either use Import-Module <Module> or use dot space dot backslash <Module> (. .\Module).
Examples of importing modules:
Import-Module Module
. .\Module.ps1
Note: . .\ will only work with PowerShell script files. All other modules need Import-Module; for example, ActiveDirectory can only be imported with Import-Module.
Get-ADDomain
Get-ADDomain pulls a large majority of the information about the domain you're attacking. It lists all the Domain Controllers for an environment, the NetBIOS domain name, the FQDN (Fully Qualified Domain Name), and much more. With Select-Object, you can filter out some of the unnecessary objects it would otherwise display (like Containers, Group Policy Objects, and so on):
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 when a bidirectional trust is set up, as it may let you gain a foothold in another domain on the LAN. Like Get-ADDomain, the output is large, 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 to PowerView. It provides a ton of information about the trusts within the AD domain: whether it is a one-way or bidirectional trust, who the source is, who the target is, and much more. One required field is -Filter; it is required when you want to filter on a specific domain/trust, and if you do not (as in most cases), simply 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 enumeration and exploitation of an AD domain. The focus here is only on PowerView's ability to enumerate information about the domain and its associated trusts. You can get the .ps1 here.
Get-NetDomain
Get-NetDomain is similar to the ActiveDirectory module's Get-ADDomain but contains a lot less information, which can be better. Basic info such as the forest, Domain Controllers, and domain name are enumerated.
Get-NetDomain
Get-NetDomain
Get-NetDomainController
Get-NetDomainController lists all the Domain Controllers within the network. This is useful for initial reconnaissance, especially if you do not have a Windows device joined to the domain.
Get-NetDomainController
Get-NetDomainControllers
Get-NetForest
Get-NetForest is similar to Get-ADForest and provides 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 the Select-Object filter applied. It's short, sweet and to the point.
Get-NetDomainTrust
Get-NetDomainTrust