practical powershell programming for professional people
DESCRIPTION
The best hackers are those that can write their own tools or modify existing ones. Regardless of whether you are blue team, red team, purple team, white hat, gray hat, or black hat, PowerShell should be in your repository of tools. While I encourage people to learn other languages as well, PowerShell is a dynamic tool and should not be overlooked. This talk is meant to be an introductory (101) session for PowerShell. I will be giving you a crash course in PowerShell scripting that will equip you to create practical PowerShell scripts for defense, offense, and even some fun things to mess around with people. This talk is designed for anyone who has never done any PowerShell or just starting to learn. Bring your laptop with PowerShell 3.0 or later, and your favorite text editor (like SublimeText) installed so you can script along with me. You will be able to write a functioning PowerShell script by the end of this talk! Come see the potential power you can unlock by learning PowerShell; and to see how often I can abuse the letter P!TRANSCRIPT
Practical PowerShell Programmingfor
Professional People
Ben Ten(@Ben0xA)
Converge Detroit 2014
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
About Me
Ben Ten (0xA)@Ben0xA - twitter
Chicago - #burbsecVice PresidentSecurity OfficerDeveloperPoshSec Framework Developer / CreatorGamerGeek
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Overview
● Languages and Development● PowerShell Scripting ● Resources● Q&A
● PSA: This is mostly live code scripting in PowerShell. Please code along with me!
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Overview
Feel free to interrupt and ask questions!
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Before we begin, a bit of a primer!
● Styles of Coding● Syntax● Getting Help● Starting Out
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Styles of Coding/Scripting/Development
● Novice● Avid Scripter● Full Time Developer● Code Monkey
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Styles of Coding/Scripting/Development
● Novice● Avid Scripter● Full Time Developer● Code Monkey
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Syntax
syn•tax (sĭn tăks ) – the rules that govern ˈ ˌhow a script, or program, is developed in a given language.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Syntax
White Space, parens (), commas, periods, quotes (“ vs '), tabs, braces [], curly brackets {}, colons :, semi-colons ;, all play an integral part in the syntax of a language!
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Getting Help!
RTF Manual/Docs/Reference
Often times, the documentation will have an answer for what you are trying to accomplish. *NOT ALWAYS THOUGH*
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Getting Help!
Interactive Help
● ?● F1● Intellisense (Ctrl+Space)● Get-Help
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Getting Help!
Search Engines FTW!
Google is not the end all in searches. For Development I prefer DuckDuckGo!
https://duckduckgo.com
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Languages and Development
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Overview
PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Overview
PowerShell was designed by :● Jeffrey Snover (@jsnover)● Bruce Payette (@BrucePayette)● James Truher
Initial release was November 14, 2006
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Overview
PowerShell is a part of the Windows Management Framework. WMF 5.0 was released on April 3, 2014.
For today's scripting we will be using WMF 3.0.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
You will need:
● Windows Management Framework 3.0● Microsoft .NET Framework 4.5● Text Editor (your choice)
● Sublime Text http://www.sublimetext.com/
● Komodo Edit http://komodoide.com/komodo-edit/
● PowerShell ISE (comes with WMF)
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
File Name Extensions
.ps1 – Script Files
.psm1 – Script Module Files
.psd1 – Script Manifest Files
.ps1xml – Formatting and Type Files
.dll - Cmdlet and Provider Assemblies
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
File Name Extensions
.ps1 – Script Files
.psm1 – Script Module Files
.psd1 – Script Manifest Files
.ps1xml – Formatting and Type Files
.dll - Cmdlet and Provider Assemblies
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Cmdlets, Functions, and Scripts Oh My!
From a functional standpoint, cmdlets, functions, and scripts are practically the same.
They are a way to call a specific block of code.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Cmdlet:
Written in a compiled .NET language.Easier to deploy.Help files are easier to write.Has support for parameter validation.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Function:
Written in a PowerShell language.Has to be deployed with a library.Help is written inside the function.Parameter validation has to be done in the function itself.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Script:
Written in a PowerShell language.Is invoked by calling the .ps1 file.Deployed by itself or in a manifest file.Can contain functions.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Set-ExecutionPolicy
Before you can run your custom scripts you have to set the ExecutionPolicy to RemoteSigned.
In PowerShell type:Set-ExecutionPolicy RemoteSigned
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
PowerShell
HelloWorld.ps1
Enough of the primer! Let's get coding!
This is where you code along with me if you can!
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Variable(s):
a symbolic name associated with a value and whose associated value may be changed.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Hard-Coded:
Typing the value directly into your script. Our “Hello World” text was hard-coded.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
PowerShell Variables:
A PowerShell variable is defined with the dollar sign $ followed by the name of the variable.
For example: $message is a variable.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
PowerShell Variables:
Let's rewrite our HelloWorld.ps1 to use a variable $message with our text “Hello World”.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Quotes! Single vs Double
Double Quotes (“) will attempt to resolve any variables before anything is printed to the screen.
Single Quotes (') will print exactly what is typed between the quotes.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Backtick `
The backtick, or grave accent, is a special escape character. This means that you want the next character to be printed and not interpreted in anyway.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
HelloWorld.ps1
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Getting Input
Write-Output is great. But how do you get information from a user?
Read-Host
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Getting Input
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Getting Input
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Condition is:
a feature of a programming language which perform a different set of computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Condition is:
Is the stop light is green? Keep going.Is the stop light is red? Stop.Is the stop light is yellow? Floor it!!!!
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Condition expressed:● If - Beginning of the condition.● Else - Evaluates only if preceding condition(s)
is(are) false.● ElseIf – Evaluates if preceding condition(s)
is(are) false with a new condition.
● Switch – Multiple conditions for a single variable or object.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Conditional Operator:
-and = both conditions must be true.
-or = only one of the conditions must be true.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Conditional Operator:
-eq = Equals-lt = Less Than-gt = Greater Than-ne = Not Equal-ge = Great Than or Equal-le = Less Than or Equal
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
A Conditional Operator:
-Like-NotLike-Match-NotMatch-Contains-NotContains
-In-NotIn-Replace
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Operator Precedence:
When operators have equal precedence, Windows PowerShell evaluates them from left to right. The exceptions are the assignment operators, the cast operators, and the negation operators (!, -not, -bnot), which are evaluated from right to left.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Operator Precedence:
You can use enclosures, such as parentheses, to override the standard precedence order and force Windows PowerShell to evaluate the enclosed part of an expression before an unenclosed part.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Conditional Logic
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Parameters
A Parameter is:
A variable that allows you to pass an object to a Cmdlet, Function, or Script.
Get-ChildItem
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Parameters
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Parameters
Get-Help Get-ChildItem
Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Exclude <String[]>[-Name] [-Recurse] [-UseTransaction [<SwitchParameter>]] [<CommonParameters>
Get-ChildItem [[-Filter] <String>] [-Exclude <String[]>] [-Force] [-Include-LiteralPath <String[]> [-UseTransaction [<SwitchParameter>]] [<CommonParame
Get-ChildItem [-Attributes <FileAttributes]>] [-Directory] [-File] [-Force][-UseTransaction] [<CommonParameters>]
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Parameters
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Parameters
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Objects vs Text
PowerShell is Object Based.
Even if you see text on the screen, that text is actually a “String” object.
You can access the members of the object using the . operator after the variable name.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Objects vs Text
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Piping
Piping is:
a way of moving something, unchanged, from one place to another.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Piping
Piping is represented by the | (pipe) character.
A pipe takes the object from the left side and passes it to the right side.
Note: When passing to another cmdlet, $_ is used to reference the passed object.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Piping
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Piping
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Loops
Loops:
A way to perform the same block of code for a specific number of times, until a specific condition is met, or while a specific condition exists.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Loops
Loops:
● ForEach● ForEach-Object● For● While● Do While● Do Until
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Loops
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Loops
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Loops
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Comments
Comments are defined by the # symbol.Block comments are enclosed with <# and #>.
.SYNOPSIS
.DESCRIPTION
.PARAMETER
.EXAMPLE
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Comments
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Putting it all Together
The final program!
Requirements:● Search all files.● Find the ones that were modified in a specific date range.
● Create a list of those files and display them.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Pitfalls
Don't overuse the Pipe! Not everything has to be done in a single line.
It's more important that you understand the code before you try to condense it to a single line.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Pitfalls
With Loops, start small then open the valve all the way!
You can get more than you wanted, or get stuck in an endless loop.
Especially true when doing File operations!
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Resources
Freenode (irc.freenode.net)#PowerShell, #pssec, #poshsec channels.
Learn Windows PowerShell in a Month of Lunches ~ Don Jones
Carlos Perez – PowerShell Workshop at DerbyCon.
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Contact - Q&A
Ben Ten (0xA)@Ben0xA - twitterhttp://ben0xa.comhttps://[email protected] – LinkedIn, Github, keybase, etc.
irc.freenode.net#burbsec, #poshsec, #pssec
QUESTIONS?!
Practical PowerShell Programming for Professional PeopleConverge Detroit - Ben Ten (@Ben0xA)
Thank You!