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
|
#
# Copyright:: Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative "../resource"
class Chef
class Resource
class WindowsDefender < Chef::Resource
unified_mode true
provides :windows_defender
description "Use the **windows_defender** resource to enable or disable the Microsoft Windows Defender service."
introduced "17.3"
examples <<~DOC
**Configure Windows Defender AV settings**:
```ruby
windows_defender 'Configure Defender' do
realtime_protection true
intrusion_protection_system true
lock_ui true
scan_archives true
scan_scripts true
scan_email true
scan_removable_drives true
scan_network_files false
scan_mapped_drives false
action :enable
end
```
**Disable Windows Defender AV**:
```ruby
windows_defender 'Disable Defender' do
action :disable
end
```
DOC
# DisableIOAVProtection
property :realtime_protection, [true, false],
default: true,
description: "Enable realtime scanning of downloaded files and attachments."
# DisableIntrusionPreventionSystem
property :intrusion_protection_system, [true, false],
default: true,
description: "Enable network protection against exploitation of known vulnerabilities."
# UILockdown
property :lock_ui, [true, false],
description: "Lock the UI to prevent users from changing Windows Defender settings.",
default: false
# DisableArchiveScanning
property :scan_archives, [true, false],
default: true,
description: "Scan file archives such as .zip or .gz archives."
# DisableScriptScanning
property :scan_scripts, [true, false],
default: false,
description: "Scan scripts in malware scans."
# DisableEmailScanning
property :scan_email, [true, false],
default: false,
description: "Scan e-mails for malware."
# DisableRemovableDriveScanning
property :scan_removable_drives, [true, false],
default: false,
description: "Scan content of removable drives."
# DisableScanningNetworkFiles
property :scan_network_files, [true, false],
default: false,
description: "Scan files on a network."
# DisableScanningMappedNetworkDrivesForFullScan
property :scan_mapped_drives, [true, false],
default: true,
description: "Scan files on mapped network drives."
load_current_value do
values = powershell_exec!("Get-MPpreference").result
lock_ui values["UILockdown"]
realtime_protection !values["DisableIOAVProtection"]
intrusion_protection_system !values["DisableIntrusionPreventionSystem"]
scan_archives !values["DisableArchiveScanning"]
scan_scripts !values["DisableScriptScanning"]
scan_email !values["DisableEmailScanning"]
scan_removable_drives !values["DisableRemovableDriveScanning"]
scan_network_files !values["DisableScanningNetworkFiles"]
scan_mapped_drives !values["DisableScanningMappedNetworkDrivesForFullScan"]
end
action :enable, description: "Enable and configure Windows Defender." do
windows_service "Windows Defender" do
service_name "WinDefend"
action %i{start enable}
startup_type :automatic
end
converge_if_changed do
powershell_exec!(set_mppreference_cmd)
end
end
action :disable, description: "Disable Windows Defender." do
windows_service "Windows Defender" do
service_name "WinDefend"
action %i{disable stop}
end
end
action_class do
require "chef/mixin/powershell_type_coercions"
include Chef::Mixin::PowershellTypeCoercions
PROPERTY_TO_PS_MAP = {
realtime_protection: "DisableIOAVProtection",
intrusion_protection_system: "DisableIntrusionPreventionSystem",
scan_archives: "DisableArchiveScanning",
scan_scripts: "DisableScriptScanning",
scan_email: "DisableEmailScanning",
scan_removable_drives: "DisableRemovableDriveScanning",
scan_network_files: "DisableScanningNetworkFiles",
scan_mapped_drives: "DisableScanningMappedNetworkDrivesForFullScan",
}.freeze
def set_mppreference_cmd
cmd = "Set-MpPreference -Force"
cmd << " -UILockdown #{type_coercion(new_resource.lock_ui)}"
# the values are the opposite in Set-MpPreference and our properties so we have to iterate
# over the list and negate the provided values so it makes sense with the cmdlet flag's expected value
PROPERTY_TO_PS_MAP.each do |prop, flag|
next if new_resource.send(prop).nil? || current_resource.send(prop) == new_resource.send(prop)
cmd << " -#{flag} #{type_coercion(!new_resource.send(prop))}"
end
cmd
end
end
end
end
end
|