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 PowerShel.
Genislab builds better products and faster go-to-market with Lean project man...
Practical PowerShell Programming for Professional People - DerbyCon 4
1. Practical PowerShell Programming
for
Professional People
Ben Ten
(@Ben0xA)
Slides: http://www.slideshare.net/BenTen0xA
DerbyCon 2014
2. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
About Me
Ben Ten (0xA)
@Ben0xA - twitter
Chicago - #burbsec
Vice President
Security Officer
Developer
PoshSec Framework Developer / Creator
Gamer
Geek
16. Practical Powe rShell Programming for Professional People
DerbyCon - 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!
17. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Overview
Feel free to interrupt and ask questions!
18. Languages and Development
Before we begin, a bit of a primer!
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
●Styles of Coding
●Syntax
●Getting Help
●Starting Out
19. Languages and Development
Styles of Coding/Scripting/Development
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
●Novice
●Avid Scripter
●Full Time Developer
●Code Monkey
20. Languages and Development
Styles of Coding/Scripting/Development
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
●Novice
●Avid Scripter
●Full Time Developer
●Code Monkey
21. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
22. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
23. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
24. Languages and Development
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Getting Help!
Interactive Help
● ?
●F1
●Intellisense (Ctrl+Space)
●Get-Help
25. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
26. Languages and Development
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
27. Languages and Development
@JimmyVo – DuckDuckVo!
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
28. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
29. PowerShell
Overview
PowerShell was designed by :
● Jeffrey Snover (@jsnover)
●Bruce Payette (@BrucePayette)
● James Truher
Initial release was November 14, 2006
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
30. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
31. PowerShell
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
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)
32. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
33. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
34. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
35. PowerShell
Cmdlet:
Written in a compiled .NET language.
Easier to deploy.
Help files are easier to write.
Has support for parameter validation.
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
36. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
37. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
38. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
40. PowerShell
HelloWorld.ps1
Enough of the primer! Let's get coding!
This is where you code along with me if
you can!
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
44. HelloWorld.ps1
Variable(s):
a symbolic name associated with a value
and whose associated value may be
changed.
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
45. HelloWorld.ps1
Hard-Coded:
Typing the value directly into your script.
Our “Hello World” text was hard-coded.
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
46. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
47. HelloWorld.ps1
PowerShell Variables:
Let's rewrite our HelloWorld.ps1 to use a
variable $message with our text “Hello
World”.
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
52. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
57. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
62. Getting Input
Write-Output is great. But how do you get
information from a user?
Read-Host
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
63. Getting Input
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
64. Getting Input
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
65. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
66. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
67. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
68. Conditional Logic
A Conditional Operator:
-and = both conditions must be true.
-or = only one of the conditions must be
true.
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
69. Conditional Logic
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
A Conditional Operator:
-eq = Equals
-lt = Less Than
-gt = Greater Than
-ne = Not Equal
-ge = Great Than or Equal
-le = Less Than or Equal
70. Conditional Logic
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
A Conditional Operator:
-Like
-NotLike
-Match
-NotMatch
-Contains
-NotContains
-In
-NotIn
-Replace
73. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
74. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
79. Parameters
A Parameter is:
A variable that allows you to pass an
object to a Cmdlet, Function, or Script.
Get-ChildItem
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
84. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
85. Objects vs Text
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
86. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Piping
Piping is:
a way of moving something, unchanged,
from one place to another.
87. Practical Powe rShell Programming for Professional People
DerbyCon - 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.
88. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Piping
89. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Piping
90. Practical Powe rShell Programming for Professional People
DerbyCon - 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.
91. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Loops
Loops:
●ForEach
●ForEach-Object
●For
●While
●Do While
●Do Until
92. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Loops
93. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Loops
94. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Loops
95. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Comments
Comments are defined by the # symbol.
Block comments are enclosed with <# and
#>.
.SYNOPSIS
.DESCRIPTION
.PARAMETER
.EXAMPLE
96. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Comments
97. 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 Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
98. Practical Powe rShell Programming for Professional People
DerbyCon - 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.
99. Practical Powe rShell Programming for Professional People
DerbyCon - 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!
100. Practical Powe rShell Programming for Professional People
DerbyCon - 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.
101. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Resources
PoshSec – https://github.com/PoshSec
PoshSec Framework
PowerSploit –
https://github.com/mattifestation/
Posh-SecMod –
https://github.com/DarkOperator/
102. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Resources
http://www.slideshare.net/BenTen0xA/
practical-powershell-programming-for-professional-
people
103. Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
Thanks!
@securitymoey
@jaysonstreet
@DerbyCon
@tonikjdk
@darkoperator
@mattifestation
@obscuresec
@harmj0y
104. Contact - Q&A
Ben Ten (0xA)
@Ben0xA - twitter
http://ben0xa.com
https://poshsec.org
web@ben0xa.com
Ben0xA – LinkedIn, Github, keybase, etc.
irc.freenode.net
#burbsec, #poshsec, #pssec
http://www.slideshare.net/BenTen0xA
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
QUESTIONS?!
105. Thank You!
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)
106. Thank You!
Practical Powe rShell Programming for Professional People
DerbyCon - Ben Ten (@Ben0xA)