-
Notifications
You must be signed in to change notification settings - Fork 5
/
enable_logs.ps1
398 lines (334 loc) · 17.2 KB
/
enable_logs.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<#PSScriptInfo
.VERSION
1.2
.AUTHOR
bobby-tablez (Tim Peck)
.GUID
a5d40ad0-297b-4269-80f9-934f6341367c
.SYNOPSIS
Enables detailed logging telemetry for a host.
.DESCRIPTION
This module provides a large amount of logging telemetry. This includes Sysinternals Sysmon, PowerShell module and scriptblock logging, and audit policies for key event IDs. This script can be modified to suit organizational needs, however it should be tested first as it can generate a huge amount of log data depending on the host.
.NOTES
Use at your own risk.
.LINK
https://raw.githubusercontent.com/bobby-tablez/Invoke-XORfuscation/main/Invoke-XORfuscation.ps1
https://www.securonix.com/blog/improving-blue-team-threat-detection-with-enhanced-siem-telemetry/
.PARAMETER -sysmononly
This will ONLY download and install sysmon. If sysmon is already present, it will perform a reinstall using the provided XML import config file.
.EXAMPLE
enable_logs.ps1 -sysmononly (-so)
.PARAMETER -y
This will skip the "are you sure?" prompt upon initial execution.
.EXAMPLE
enable_logs.ps1 -y (-yes)
enable_logs.ps1 -y -sysmononly
.PARAMETER -config
Bring your own XML config file. When the -config argument is passed, supply a direct URL to a Sysmon config import file. When no argument is supplied it will download: "https://raw.githubusercontent.com/bobby-tablez/FT-Sysmon-Config/master/ft-sysmonconfig-export.xml"
.EXAMPLE
enable_logs.ps1 -y
enable_logs.ps1 -y -sysmononly
enable_logs.ps1 -yes -sysmononly -config https://raw.githubusercontent.com/SwiftOnSecurity/sysmon-config/master/sysmonconfig-export.xml
.PARAMETER -driver (-d)
Modify the Sysmon driver name. Limited to 8 characters.
.EXAMPLE
enable_logs.ps1 -y -driver "sccm"
.PARAMETER -name (-n)
Modify the Sysmon binary file's name. The binary name will effectively become the service name. https://www.darkoperator.com/blog/2018/10/5/operating-offensively-against-sysmon
.EXAMPLE
enable_logs.ps1 -y -d "sccm" -name "sccm_service.exe"
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>
Param(
[Alias("so")]
[switch]$sysmononly,
[Alias("y")]
[switch]$yes,
[Alias("c")]
[string]$config,
[Alias("d")]
[string]$driver,
[Alias("n")]
[string]$name
)
# Check for administrator privs
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host -ForegroundColor Red "Enable All The Logs! requires Administrator privileges. Please rerun in an admin PowerShell console."
exit
}
# Bypass the warning prompt when -y arguement is supplied
If (-Not $yes){
If ($sysmononly){
$confirmation = $(Write-Host -f Yellow -NoNewLine "CAUTION: This script will download and install Sysmon. Do you want to continue? (y/n): "; Read-Host)
}Else {
$confirmation = $(Write-Host -f Yellow -NoNewLine "CAUTION: This script will download and install Sysmon and make GPO and registry changes that will increase log volume. Continue? (y/n): "; Read-Host)
}
If (-not($confirmation -eq 'y')) {
Write-Host "`nBye!"
Exit
}
}
# checkmark characters green/red
$cm = [char]0x2713
$ex = [char]0x274C
$sysmonURL = "https://download.sysinternals.com/files/Sysmon.zip"
$sysmonOut = "$env:temp\Sysmon.zip"
$sysmonConfOut = "$env:temp\sysmon-config.xml"
# Use user-supplied config import file if -config arguement is supplied
If ($config) {
$sysmonConf = $config
} Else {
$sysmonConf = "https://raw.githubusercontent.com/bobby-tablez/FT-Sysmon-Config/master/ft-sysmonconfig-export.xml"
}
# Grab the new driver name, check that the length does not exceed 8 characters
Try {
if ($driver) {
if ($driver.Length -le 8) {
$driver = "-d $driver"
} else {
Write-Host "[ " -nonewline; Write-Host $ex -f red -nonewline; Write-Host "Error: Driver string must be 8 characters or less."
exit 1
}
}
} Catch {
Write-Host "An unexpected error occurred: $_"
Exit 1
}
# Begin actions
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Downloading Sysmon"
Try {
Invoke-WebRequest -URI $sysmonURL -OutFile $sysmonOut
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Sysmon downloaded"
} Catch {
$errorSysmon = $_.Exception.Message
Write-Host "[ " -nonewline; Write-Host $ex -f red -nonewline; Write-Host " ] Error occurred while downloading Sysmon: $errorSysmon"
Exit 1
}
# Sysmon XML file
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Downloading Sysmon config import file"
Try {
Invoke-WebRequest -URI $sysmonConf -OutFile $sysmonConfOut
# Attempt to load the file as an XML to validate its content
[xml]$xmlContent = Get-Content -Path $sysmonConfOut
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Import config file downloaded and validated as XML"
} Catch {
$errorXML = $_.Exception.Message
If ($_ -is [System.Xml.XmlException]) {
Write-Host "[ " -nonewline; Write-Host $ex -f red -nonewline; Write-Host " ] The downloaded config import file is not valid XML: $errorXML"
exit 1
} Else {
Write-Host "[ " -nonewline; Write-Host $ex -f red -nonewline; Write-Host " ] Error occurred while downloading the config import file: $errorXML"
Exit 1
}
}
# Extract Sysmon archive contents into %TEMP%
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Extracting Sysmon archive"
Try {
Expand-Archive $sysmonOut -Destination $env:temp -ErrorAction Stop -Force
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Sysmon archive extracted"
} Catch {
$errorZIP = $_.Exception.Message
Write-Host "[ " -nonewline; Write-Host $ex -f red -nonewline; Write-Host " ] Error occurred while extracting the Sysmon archive: $errorZIP"
Exit 1
}
Function Rename-Sysmon {
Param (
[string]$sourcePath,
[string]$destinationPath
)
If (Test-Path $sourcePath) {
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Renaming binary to $destinationPath"
Rename-Item -Path $sourcePath -NewName $destinationPath
} Else {
Write-Host "[ " -nonewline; Write-Host "ERROR" -f red -nonewline; Write-Host " ] Source binary not found: $sourcePath"
Exit 1
}
}
# Used to uninstall sysmon prior to (re)installation
Function Uninstall-Sysmon {
Param (
[string]$serviceName,
[string]$exePath
)
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($service.Length -gt 0) {
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Uninstalling Existing version of $serviceName"
Start-Process -FilePath $exePath -ArgumentList "-u force" -Wait
Sleep 3
}
}
# Get CPU arch
Function b64{
$b64test = [Environment]::Is64BitOperatingSystem #Used to install x86/x64 Sysmon per OS architecture
return $b64test
}
# Set veriables of service and file names based on cpu arch
If (b64) {
$originalExePath = "$env:temp\Sysmon64.exe"
$ServiceName = "Sysmon64"
} Else {
$originalExePath = "$env:temp\Sysmon.exe"
$ServiceName = "Sysmon"
}
If ($name) {
$SysmonExt = "$name.exe"
$newExePath = "$env:temp\$SysmonExt"
Rename-Sysmon -sourcePath $originalExePath -destinationPath $newExePath
$sysmonExe = $newExePath
} Else {
$sysmonExe = $originalExePath
}
# Uninstall Sysmon if present (consideration for original and/or renamed binaries)
Uninstall-Sysmon -serviceName $ServiceName -exePath $originalExePath
If ($name) {
Uninstall-Sysmon -serviceName $ServiceName -exePath $sysmonExe
}
# Install Sysmon
If ($sysmononly) {
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Installing Sysmon64"
} Else {
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Installing Sysmon"
}
Start-Process -FilePath $sysmonExe -ArgumentList "-accepteula -i $sysmonConfOut $driver" -Wait
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Sysmon successfully installed"
Write-Host ""
# Begin non-Sysmon related tasks (unless -sysmononly is supplied)
If (-Not $sysmononly){
# PowerShell logging registry changes
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Enabling PowerShell scriptblock logging"
$PSregPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging"
$PSregValName = "EnableScriptBlockLogging"
$PSregValDat = 1
If (-not (Test-Path $PSregPath)) {
New-Item -Path $PSregPath -ItemType Directory -Force | Out-Null
}
Set-ItemProperty -Path $PSregPath -Name $PSregValName -Value $PSregValDat -Type DWord | Out-Null
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] PowerShell Script Block Logging enabled"
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Enabling PowerShell module logging"
$PSMregPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging"
$PSMregValName = "EnableModuleLogging"
$PSMregValDat = 1
If (-not (Test-Path $PSMregPath)) {
New-Item -Path $PSMregPath -ItemType Directory -Force | Out-Null
}
Set-ItemProperty -Path $PSMregPath -Name $PSMregValName -Value $PSMregValDat -Type DWord | Out-Null
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] PowerShell Module Logging enabled"
# Applies defined policies in $auditSettings
Function ApplyPolicy {
Param (
[Parameter(Mandatory)]
[string]$subcategory,
[Parameter(Mandatory)]
[string]$success,
[Parameter(Mandatory)]
[string]$failure
)
$invokeAudit = "auditpol /set /subcategory:`"$subcategory`" /success:$success /failure:$failure"
Invoke-Expression -Command $invokeAudit | Out-Null
}
# Used to enable CommandLine w/4688
Function ProcStartCMD {
$cmdRegPath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System\Audit"
$cmdRegValue = "ProcessCreationIncludeCmdLine_Enabled"
$cmdRegData = 1
If (-not (Test-Path $cmdRegPath)) {
New-Item -Path $cmdRegPath -ItemType Directory -Force
}
Set-ItemProperty -Path $cmdRegPath -Name $cmdRegValue -Value $cmdRegData -Type DWord
}
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Configuring Audit Policies"
# Policy settings
$auditSettings = @{
# Account Logon
"Credential Validation" = @{ Success="enable"; Failure="enable" }
"Kerberos Authentication Service" = @{ Success="enable"; Failure="enable" }
"Kerberos Service Ticket Operations" = @{ Success="enable"; Failure="enable" }
"Other Account Logon Events" = @{ Success="enable"; Failure="enable" }
# Account Management
"Application Group Management" = @{ Success="enable"; Failure="enable" }
"Computer Account Management" = @{ Success="enable"; Failure="enable" }
"Distribution Group Management" = @{ Success="enable"; Failure="enable" }
"Other Account Management Events" = @{ Success="enable"; Failure="enable" }
"Security Group Management" = @{ Success="enable"; Failure="enable" }
"User Account Management" = @{ Success="enable"; Failure="enable" }
# Detailed Tracking
"DPAPI Activity" = @{ Success="enable"; Failure="enable" }
"Plug and Play Events" = @{ Success="enable"; Failure="disable"}
"Process Creation" = @{ Success="enable"; Failure="enable" }
"Process Termination" = @{ Success="enable"; Failure="enable" }
"RPC Events" = @{ Success="enable"; Failure="enable" }
# DS Access
"Detailed Directory Service Replication" = @{ Success="disable"; Failure="disable"}
"Directory Service Access" = @{ Success="enable"; Failure="enable" }
"Directory Service Changes" = @{ Success="enable"; Failure="enable" }
"Directory Service Replication" = @{ Success="enable"; Failure="enable" }
# Logon/Logoff
"Account Lockout" = @{ Success="enable"; Failure="enable" }
"User / Device Claims" = @{ Success="enable"; Failure="enable" }
"Group Membership" = @{ Success="enable"; Failure="enable" }
"IPsec Extended Mode" = @{ Success="disable"; Failure="disable"}
"IPsec Main Mode" = @{ Success="disable"; Failure="disable"}
"IPsec Quick Mode" = @{ Success="disable"; Failure="disable"}
"Logoff" = @{ Success="enable"; Failure="enable" }
"Logon" = @{ Success="enable"; Failure="enable" }
"Network Policy Server" = @{ Success="disable"; Failure="disable"}
"Other Logon/Logoff Events" = @{ Success="enable"; Failure="enable" }
"Special Logon" = @{ Success="disable"; Failure="disable"}
# Object Access
"Application Generated" = @{ Success="enable"; Failure="enable" }
"Certification Services" = @{ Success="enable"; Failure="enable" }
"Detailed File Share" = @{ Success="enable"; Failure="enable" }
"File Share" = @{ Success="enable"; Failure="enable" }
"File System" = @{ Success="enable"; Failure="enable" }
"Filtering Platform Connection" = @{ Success="enable"; Failure="enable" }
"Filtering Platform Packet Drop" = @{ Success="enable"; Failure="enable" }
"Handle Manipulation" = @{ Success="enable"; Failure="enable" }
"Kernel Object" = @{ Success="enable"; Failure="enable" }
"Other Object Access Events" = @{ Success="enable"; Failure="enable"}
"Registry" = @{ Success="enable"; Failure="enable" }
"SAM" = @{ Success="disable"; Failure="disable"}
# Policy Change
"Audit Policy Change" = @{ Success="enable"; Failure="enable" }
"Authentication Policy Change" = @{ Success="enable"; Failure="enable" }
"Authorization Policy Change" = @{ Success="enable"; Failure="enable" }
"Filtering Platform Policy Change" = @{ Success="enable"; Failure="enable" }
"MPSSVC Rule-Level Policy Change" = @{ Success="disable"; Failure="disable"}
"Other Policy Change Events" = @{ Success="disable"; Failure="disable"}
# Privilege Use
"Non Sensitive Privilege Use" = @{ Success="disable"; Failure="disable"}
"Other Privilege Use Events" = @{ Success="disable"; Failure="disable"}
"Sensitive Privilege Use" = @{ Success="disable"; Failure="disable"}
# System
"IPsec Driver" = @{ Success="disable"; Failure="disable"}
"Other System Events" = @{ Success="disable"; Failure="disable"}
"Security State Change" = @{ Success="disable"; Failure="disable"}
"Security System Extension" = @{ Success="enable"; Failure="enable" }
"System Integrity" = @{ Success="enable"; Failure="enable" }
}
# Apply each policy
Foreach ($policy in $auditSettings.Keys) {
$settings = $auditSettings[$policy]
ApplyPolicy -subcategory $policy -success $settings.Success -failure $settings.Failure
}
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Audit Policies Configured"
# Enable command line w/ Process Creation
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Enabling command line logging w/ EVID: 4688"
ProcStartCMD
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Event ID 4688 enabled with commandline"
# Upate GPOs using gpupdate
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Updating GPOs using gpupdate"
Invoke-Expression -Command "gpupdate /force" | Out-Null
}
# Clean Up
Write-Host "[ " -nonewline; Write-Host $cm -f green -nonewline; Write-Host " ] Cleaning up"
Remove-Item $sysmonOut,$sysmonConfOut,$sysmonExe,$env:temp\sysmon64.exe,$env:temp\sysmon64a.exe -ErrorAction SilentlyContinue | Out-Null
Write-Host -f green "`nDone!"