forked from hardentools/hardentools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows_asr.go
296 lines (258 loc) · 10.5 KB
/
windows_asr.go
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
// Hardentools
// Copyright (C) 2017-2021 Security Without Borders
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
// Windows Defender Attack Surface Reduction (ASR) rules.
// Need the following prerequisites to work:
// - Windows 10 >= 1709
// - Endpoints are using Windows Defender Antivirus as the sole antivirus protection app.
// - Using any other antivirus app will cause Windows Defender AV to disable itself.
// - Real-time protection is enabled.
// - Cloud protection is enabled (needed for some of the ASR rules only)
// More details here:
// - https://docs.microsoft.com/en-us/windows/threat-protection/windows-defender-exploit-guard/attack-surface-reduction-exploit-guard
// - https://docs.microsoft.com/en-us/windows/threat-protection/windows-defender-exploit-guard/enable-attack-surface-reduction
// - https://docs.microsoft.com/en-us/windows/threat-protection/windows-defender-exploit-guard/evaluate-attack-surface-reduction
import (
"errors"
"fmt"
"strings"
"golang.org/x/sys/windows/registry"
)
var ruleIDArray = []string{
"BE9BA2D9-53EA-4CDC-84E5-9B1EEEE46550", // Block executable content from email client and webmail.
"D4F940AB-401B-4EFC-AADC-AD5F3C50688A", // Block Office applications from creating child processes.
"3B576869-A4EC-4529-8536-B80A7769E899", // Block Office applications from creating executable content.
"75668C1F-73B5-4CF0-BB93-3ECF5CB7CC84", // Block Office applications from injecting code into other processes.
"D3E037E1-3EB8-44C8-A917-57927947596D", // Block JavaScript or VBScript from launching downloaded executable content.
"5BEB7EFE-FD9A-4556-801D-275E5FFC04CC", // Block execution of potentially obfuscated scripts.
"92E97FA1-2EDF-4476-BDD6-9DD0B4DDDC7B", // Block Win32 API calls from Office macro.
// Leaving the following out for now, since it also blocks hardentools
// and perhaps also other "not well known to Microsoft" tools.
// "01443614-CD74-433A-B99E-2ECDC07BFC25", // Block executable files from running unless they meet a prevalence, age, or trusted list criterion.
"B2B3F03D-6A65-4F7B-A9C7-1C7EF74A9BA4", // Block untrusted and unsigned processes that run from USB.
"C1DB55AB-C21A-4637-BB3F-A12568109D35", // Use advanced protection against ransomware.
"D1E49AAC-8F56-4280-B9BA-993A6D77406C", // Block process creations originating from PSExec and WMI commands.
"26190899-1602-49e8-8b27-eb1d0a1ce869", // Block Office communication application from creating child processes.
"7674ba52-37eb-4a4f-a9a1-f0f9a1619a2c", // Block Adobe Reader from creating child processes.
"e6db77e5-3df2-4cf1-b95a-636979351e5b", // Block persistence through WMI event subscription.
"9e6c4e1f-7d60-472f-ba1a-a39ef669e4b2"} // Block credential stealing from the Windows local security authority subsystem.
var actionsArrayHardended = []bool{true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true}
var actionsArrayNotHardended = []bool{false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false}
// WindowsASRStruct ist the struct for HardenInterface implementation.
type WindowsASRStruct struct {
shortName string
longName string
description string
hardenByDefault bool
}
// WindowsASR contains Names for Windows ASR implementation of hardenInterface.
var WindowsASR = &WindowsASRStruct{
shortName: "Windows ASR rules",
longName: "Windows ASR rules",
description: "Windows Attack Surface Reduction (ASR) rules",
hardenByDefault: true,
}
// Harden method.
func (asr WindowsASRStruct) Harden(harden bool) error {
if harden {
// Harden only if we have at least Windows 10 - 1709.
if checkWindowsVersion() {
// TODO: Save original state and restore on restore.
// Show notification if Windows Defender ist not activated and
// Cloud Protection is not enabled?
warnIfWindowsDefenderNotActive()
// Set the settings for AttackSurfaceReduction using Add-MpPreference.
for i, ruleID := range ruleIDArray {
err := AddMPPreference(ruleID, actionsArrayHardended[i])
if err != nil {
return err
}
}
} else {
Info.Println("Windows ASR not activated, since it needs at least Windows 10 - 1709")
}
} else {
// Restore (but only if we have at least Windows 10 - 1709).
if checkWindowsVersion() {
// Set the settings for AttackSurfaceReduction using Add-MpPreference.
for i, ruleID := range ruleIDArray {
err := AddMPPreference(ruleID, actionsArrayNotHardended[i])
if err != nil {
return err
}
}
}
}
return nil
}
// IsHardened checks if ASR is already hardened.
func (asr WindowsASRStruct) IsHardened() bool {
if !checkWindowsVersion() {
return false
}
psString := fmt.Sprintf("$prefs = Get-MpPreference; $prefs.AttackSurfaceReductionRules_Ids")
ruleIDsOut, err := executeCommand("PowerShell.exe", "-Command", psString)
if err != nil {
Info.Printf("ERROR: WindowsASR: Verify if Windows Defender is running. Executing Powershell.exe with command \"%s\" failed.", psString)
Info.Printf("ERROR: WindowsASR: Powershell Output was: %s", ruleIDsOut)
// In case command does not work we assume we are not hardened.
return false
}
psString = fmt.Sprintf("$prefs = Get-MpPreference; $prefs.AttackSurfaceReductionRules_Actions")
ruleActionsOut, err := executeCommand("PowerShell.exe", "-Command", psString)
if err != nil {
Info.Printf("ERROR: WindowsASR: Verify if Windows Defender is running. Executing Powershell.exe with command \"%s\" failed.", psString)
Info.Printf("ERROR: WindowsASR: Powershell Output was: %s", ruleActionsOut)
// In case command does not work we assume we are not hardened.
return false
}
// Verify if all relevant ruleIDs are there.
// Split/remove line feeds and carriage return.
currentRuleIDs := strings.Split(ruleIDsOut, "\r\n")
currentRuleActions := strings.Split(ruleActionsOut, "\r\n")
for i, ruleIDdebug := range currentRuleIDs {
if len(ruleIDdebug) > 0 {
Trace.Printf("ruleID %d = %s with action = %s\n", i, ruleIDdebug, currentRuleActions[i])
}
}
// Compare to hardened state.
for i, ruleIDHardened := range ruleIDArray {
// Check if rule exists by iterating over all ruleIDs.
var existsAndEqual = false
for j, currentRuleID := range currentRuleIDs {
if strings.ToLower(ruleIDHardened) == strings.ToLower(currentRuleID) {
// Verify if setting is enabled.
if currentRuleActions[j] == "1" && actionsArrayHardended[i] == true {
// Everything is fine.
existsAndEqual = true
} else {
return false
}
}
}
if existsAndEqual == false {
return false
}
}
// It seems all relevant hardening is in place.
return true
}
// AddMPPreference sets a ASR rule using Add-MpPreference.
func AddMPPreference(ruleID string, enabled bool) error {
// Example: Add-MpPreference -AttackSurfaceReductionRules_Ids
// 75668C1F-73B5-4CF0-BB93-3ECF5CB7CC84
// -AttackSurfaceReductionRules_Actions Enabled
var action string
if enabled {
action = "Enabled"
} else {
action = "Disabled"
}
psString := fmt.Sprintf("Add-MpPreference -AttackSurfaceReductionRules_Ids %s -AttackSurfaceReductionRules_Actions %s", ruleID, action)
Trace.Printf("WindowsASR: Executing Powershell.exe with command \"%s\"", psString)
out, err := executeCommand("PowerShell.exe", "-Command", psString)
if err != nil {
Info.Printf("ERROR: WindowsASR: Verify if Windows Defender is running. Executing Powershell.exe with command \"%s\" failed. ", psString)
Info.Printf("ERROR: WindowsASR: Powershell Output was: %s", out)
return errors.New("Executing powershell cmdlet Add-MpPreference failed (" + ruleID + " = " + action + ")")
}
Trace.Printf("WindowsASR: Powershell output was:\n%s", out)
return nil
}
// Name returns Name.
func (asr WindowsASRStruct) Name() string {
return asr.shortName
}
// LongName returns Long Name.
func (asr WindowsASRStruct) LongName() string {
return asr.longName
}
// Description returns description.
func (asr WindowsASRStruct) Description() string {
return asr.description
}
// HardenByDefault returns if subject should be hardened by default.
func (asr WindowsASRStruct) HardenByDefault() bool {
return asr.hardenByDefault
}
// checkWindowsVersion checks if hardentools is running on Windows 10 with
// Patch Level >= 1709.
func checkWindowsVersion() bool {
k, err := registry.OpenKey(registry.LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
registry.QUERY_VALUE)
if err != nil {
return false
}
defer k.Close()
maj, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
if err != nil {
return false
}
if maj < 10 {
return false
}
min, _, err := k.GetIntegerValue("CurrentMinorVersionNumber")
if err != nil {
return false
}
if min < 0 {
return false
}
cb, _, err := k.GetStringValue("CurrentBuild")
if err != nil {
return false
}
if strings.Compare(cb, "15254") < 0 {
return false
}
return true
}
// warnIfWindowsDefenderNotActive shows a notification if Windows Defender
// settings might prevent ASR rules from working.
func warnIfWindowsDefenderNotActive() {
// Cloud Protection.
{
command := "(Get-MpPreference).MAPSReporting"
expectedValue := "2"
out, err := executeCommand("PowerShell.exe", "-Command", command)
if err != nil {
Info.Printf("Could not verify if Windows Defender Cloud Protection is enabled due to error accessing registry")
return
}
out = strings.ReplaceAll(out, "\r\n", "")
if out != expectedValue {
// show notification
showInfoDialog("Windows Defender Cloud Protection is not enabled.\nSome ASR rules won't work.")
}
}
// Real-time protection.
{
command := "(Get-MpPreference).DisableRealtimeMonitoring"
expectedValue := "False"
out, err := executeCommand("PowerShell.exe", "-Command", command)
if err != nil {
Info.Printf("Could not verify if Windows Defender Cloud Protection is enabled due to error accessing registry")
return
}
out = strings.ReplaceAll(out, "\r\n", "")
if out != expectedValue {
showInfoDialog("Windows Defender Realtime Protection is not enabled.\nASR rules won't work.")
}
}
}