forked from eclipse-platform/eclipse.platform.ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowsDefenderConfigurator.java
407 lines (376 loc) · 18.5 KB
/
WindowsDefenderConfigurator.java
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
399
400
401
402
403
404
405
406
407
/*******************************************************************************
* Copyright (c) 2023, 2024 Hannes Wellmann and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Hannes Wellmann - initial API and implementation
*******************************************************************************/
package org.eclipse.ui.internal;
import static org.eclipse.ui.internal.WorkbenchPlugin.PI_WORKBENCH;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.widgets.ButtonFactory;
import org.eclipse.jface.widgets.LinkFactory;
import org.eclipse.jface.widgets.WidgetFactory;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.osgi.service.event.propertytypes.EventTopics;
import org.osgi.service.prefs.BackingStoreException;
/**
* A start-up event handler that, if running on Windows, suggests the user to
* exclude the current installation directory from being scanned by the Windows
* Defender, if it is active on the computer.
*/
@Component(service = EventHandler.class)
@EventTopics(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE)
public class WindowsDefenderConfigurator implements EventHandler {
private static final String PREFERENCE_EXCLUDED_INSTALLATION_PATH = "windows.defender.excluded.path"; //$NON-NLS-1$
private static final String PREFERENCE_STARTUP_CHECK_APP = "windows.defender.startup.check.app"; //$NON-NLS-1$
public static final String PREFERENCE_STARTUP_CHECK_SKIP = "windows.defender.startup.check.skip"; //$NON-NLS-1$
public static final boolean PREFERENCE_STARTUP_CHECK_SKIP_DEFAULT = false;
@Reference
protected IPreferencesService preferences;
@Override
public void handleEvent(Event event) {
if (runStartupCheck()) {
Job job = Job.create(WorkbenchMessages.WindowsDefenderConfigurator_statusCheck, m -> {
SubMonitor monitor = SubMonitor.convert(m, 10);
Optional<Path> installLocation = getInstallationLocation();
if (installLocation.isPresent()) {
String checkedPath = getPreference(ConfigurationScope.INSTANCE)
.get(PREFERENCE_EXCLUDED_INSTALLATION_PATH, ""); //$NON-NLS-1$
if (!checkedPath.isBlank() && installLocation.get().equals(Path.of(checkedPath))) {
return; // This installation has already been checked at the current location
}
}
monitor.worked(1);
runExclusionCheck(monitor.split(9), installLocation);
});
job.setSystem(true);
job.schedule();
}
}
protected boolean runStartupCheck() {
if (Platform.OS.isWindows() && !Platform.inDevelopmentMode()) {
if (preferences.getBoolean(PI_WORKBENCH, PREFERENCE_STARTUP_CHECK_SKIP,
PREFERENCE_STARTUP_CHECK_SKIP_DEFAULT, null)) {
return false;
}
String permittedApp = preferences.getString(PI_WORKBENCH, PREFERENCE_STARTUP_CHECK_APP,
"org.eclipse.ui.ide.workbench", null); //$NON-NLS-1$
return permittedApp.equals(getRunningApplicationId());
}
return false;
}
private static String getRunningApplicationId() {
@SuppressWarnings("restriction")
String appId = System.getProperty(org.eclipse.core.internal.runtime.InternalPlatform.PROP_APPLICATION);
if (appId != null) {
return appId;
}
IProduct product = Platform.getProduct();
return product != null ? product.getApplication() : null;
}
/**
* Opens the dialog to run the exclusion, regardless of any preference.
*
* @return {@code true} if this installation is now excluded, {@code false} if
* Windows Defender is inactive and null if the process was aborted.
*/
public static Boolean runCheckEnforced(IProgressMonitor m) throws CoreException {
Optional<Path> installLocation = getInstallationLocation();
return runExclusionCheck(m, installLocation);
}
private enum HandlingOption {
EXECUTE_EXCLUSION, IGNORE_THIS_INSTALLATION;
}
/**
* Performs the exclusion of this installation from Windows defender.
*
* @return {@code true} if this installation is now excluded, {@code false} if
* Windows Defender is inactive and null if the process was aborted.
*/
private static Boolean runExclusionCheck(IProgressMonitor m, Optional<Path> installLocation) throws CoreException {
SubMonitor monitor = SubMonitor.convert(m, 5);
if (!isWindowsDefenderServiceRunning(monitor.split(1)) || !isWindowsDefenderActive(monitor.split(1))) {
return Boolean.FALSE;
}
Display display = Display.getDefault();
HandlingOption decision = askForDefenderHandlingDecision(display);
if (decision != null) {
switch (decision) {
case EXECUTE_EXCLUSION -> {
if (isExclusionTamperProtectionEnabled(monitor.split(1))) {
display.syncExec(() -> MessageDialog.openError(null, "Exclusion failed", //$NON-NLS-1$
bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionFailed_Protected)));
savePreference(ConfigurationScope.INSTANCE, PREFERENCE_STARTUP_CHECK_SKIP, "true"); //$NON-NLS-1$
return null; // Consider selection as 'aborted' and don't show the dialog again on startup
}
try {
WindowsDefenderConfigurator.excludeDirectoryFromScanning(monitor.split(2));
savePreference(ConfigurationScope.INSTANCE, PREFERENCE_EXCLUDED_INSTALLATION_PATH,
installLocation.map(Path::toString).orElse("")); //$NON-NLS-1$
} catch (IOException e) {
display.syncExec(() -> MessageDialog.openError(null, "Exclusion failed", //$NON-NLS-1$
bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionFailed)));
}
}
case IGNORE_THIS_INSTALLATION -> savePreference(ConfigurationScope.INSTANCE, PREFERENCE_STARTUP_CHECK_SKIP,
"true"); //$NON-NLS-1$
}
}
return decision == HandlingOption.EXECUTE_EXCLUSION ? Boolean.TRUE : null;
}
private static HandlingOption askForDefenderHandlingDecision(Display display) {
String message = bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionCheckMessage);
return display.syncCall(() -> {
HandlingOption[] choice = new HandlingOption[] { null };
MessageDialog dialog = new MessageDialog(display.getActiveShell(),
WorkbenchMessages.WindowsDefenderConfigurator_statusCheck, null, message, MessageDialog.INFORMATION,
0, IDialogConstants.PROCEED_LABEL, IDialogConstants.CANCEL_LABEL) {
@Override
protected Control createCustomArea(Composite parent) {
ButtonFactory radioButtonFactory = WidgetFactory.button(SWT.RADIO | SWT.WRAP);
Button performExclusion = radioButtonFactory
.text(bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_performExclusionChoice))
.onSelect(e -> {
choice[0] = HandlingOption.EXECUTE_EXCLUSION;
getButton(0).setEnabled(true);
}).create(parent);
Point size = performExclusion.computeSize(SWT.DEFAULT, SWT.DEFAULT);
GridDataFactory.swtDefaults().hint((int) (size.x * 0.6), (int) (size.y * 2.3)).indent(0, 5)
.applyTo(performExclusion);
Button keepScanning = radioButtonFactory
.text(bindProductName(
WorkbenchMessages.WindowsDefenderConfigurator_ignoreThisInstallationChoice))
.onSelect(e -> {
choice[0] = HandlingOption.IGNORE_THIS_INSTALLATION;
getButton(0).setEnabled(true);
}).create(parent);
GridDataFactory.swtDefaults().indent(0, 5).applyTo(keepScanning);
if (!PlatformUI.isWorkbenchRunning()) {
return parent; // Only show the link to the preferences if the workbench is available
}
LinkFactory.newLink(SWT.WRAP)
.text(WorkbenchMessages.WindowsDefenderConfigurator_detailsAndOptionsLinkText)
.onSelect((e -> {
PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(null,
"org.eclipse.ui.preferencePages.Startup", null, null); //$NON-NLS-1$
dialog.setBlockOnOpen(false);
dialog.open();
this.setReturnCode(Window.CANCEL);
this.close();
})).create(parent);
return parent;
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
getButton(0).setEnabled(false);
getButton(1).forceFocus(); // prevents auto-focusing one of the radio-buttons which selects them
}
};
int open = dialog.open();
return open == Window.OK ? choice[0] : null;
});
}
public static String bindProductName(String message) {
String name = Optional.ofNullable(Platform.getProduct()).map(IProduct::getName).orElse("this application"); //$NON-NLS-1$
return NLS.bind(message, name);
}
public static IEclipsePreferences getPreference(IScopeContext instance) {
return instance.getNode(PI_WORKBENCH);
}
public static void savePreference(IScopeContext scope, String key, String value) throws CoreException {
IEclipsePreferences preferences = getPreference(scope);
if (value != null) {
preferences.put(key, value);
} else {
preferences.remove(key);
}
try {
preferences.flush();
} catch (BackingStoreException e) {
throw new CoreException(Status.error("Failed to safe preference " + preferences, e)); //$NON-NLS-1$
}
}
private static Optional<Path> getInstallationLocation() {
// For install location see:
// https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fruntime-options.html&anchor=locations
try {
Location installLocation = Platform.getConfigurationLocation();
return Optional.of(Path.of(installLocation.getURL().toURI())); // assume location has a file-URL
} catch (URISyntaxException e) { // ignore
}
return Optional.empty();
}
private static List<Path> getExecutablePath() {
@SuppressWarnings("restriction")
String eclipseLauncher = System.getProperty(org.eclipse.osgi.internal.location.EquinoxLocations.PROP_LAUNCHER);
return List.of(Path.of(eclipseLauncher));
}
private static boolean isExclusionTamperProtectionEnabled(IProgressMonitor monitor) {
// https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/manage-tamper-protection-intune?view=o365-worldwide#how-to-determine-whether-antivirus-exclusions-are-tamper-protected-on-a-windows-device
try { // Query the Windows Registry
List<String> result = runProcess(List.of("powershell.exe", "-Command", //$NON-NLS-1$//$NON-NLS-2$
"Get-ItemPropertyValue -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows Defender\\Features' -Name 'TPExclusions'"), //$NON-NLS-1$
monitor);
return result.size() == 1 && "1".equals(result.get(0)); //$NON-NLS-1$
} catch (IOException e) {
return false;
}
}
private static boolean isWindowsDefenderServiceRunning(IProgressMonitor monitor) {
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-service?view=powershell-7.4
// https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicecontrollerstatus?view=dotnet-plat-ext-8.0
try {
List<String> result = runProcess(List.of("powershell.exe", "-Command", "(Get-Service 'WinDefend').Status"), //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
monitor);
return result.size() == 1 && "Running".equalsIgnoreCase(result.get(0)); //$NON-NLS-1$
} catch (IOException e) {
ILog.get().error("Failed to obtain 'WinDefend' service state", e); //$NON-NLS-1$
return false;
}
}
private static boolean isWindowsDefenderActive(IProgressMonitor monitor) throws CoreException {
// https://learn.microsoft.com/en-us/powershell/module/defender/get-mpcomputerstatus
List<String> command = List.of("powershell.exe", "-Command", "(Get-MpComputerStatus).AMRunningMode"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
try {
List<String> lines = runProcess(command, monitor);
String onlyLine = lines.size() == 1 ? lines.get(0) : ""; //$NON-NLS-1$
return switch (onlyLine.toLowerCase(Locale.ENGLISH)) {
// Known values as listed in
// https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/microsoft-defender-antivirus-windows#use-powershell-to-check-the-status-of-microsoft-defender-antivirus
case "sxs passive mode", "passive mode" -> false; //$NON-NLS-1$ //$NON-NLS-2$
case "normal", "edr block mode" -> true; //$NON-NLS-1$//$NON-NLS-2$
default -> throw new IOException("Process terminated with unexpected result:\n" + String.join("\n", lines)); //$NON-NLS-1$//$NON-NLS-2$
};
} catch (IOException e) {
throw new CoreException(Status.error(WorkbenchMessages.WindowsDefenderConfigurator_statusCheckFailed, e));
}
}
public static String createAddExclusionsPowershellCommand(String extraSeparator) {
List<Path> paths = getExecutablePath();
// For detailed explanations about how to read existing exclusions and how to
// add new ones see:
// https://learn.microsoft.com/en-us/powershell/module/defender/add-mppreference
// https://learn.microsoft.com/en-us/powershell/module/defender/get-mppreference
//
// For .NET's stream API called LINQ see:
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable
String excludedPaths = paths.stream().map(Path::toString).map(p -> '"' + p + '"')
.collect(Collectors.joining(',' + extraSeparator));
final String exclusionType = "ExclusionProcess"; //$NON-NLS-1$
return String.join(';' + extraSeparator, "$exclusions=@(" + extraSeparator + excludedPaths + ')', //$NON-NLS-1$
"$existingExclusions=[Collections.Generic.HashSet[String]](Get-MpPreference)." + exclusionType, //$NON-NLS-1$
"if($existingExclusions -eq $null) { $existingExclusions = New-Object Collections.Generic.HashSet[String] }", //$NON-NLS-1$
"$exclusionsToAdd=[Linq.Enumerable]::ToArray([Linq.Enumerable]::Where($exclusions,[Func[object,bool]]{param($ex)!$existingExclusions.Contains($ex)}))", //$NON-NLS-1$
"if($exclusionsToAdd.Length -gt 0){ Add-MpPreference -" + exclusionType + " $exclusionsToAdd }"); //$NON-NLS-1$ //$NON-NLS-2$
}
private static void excludeDirectoryFromScanning(IProgressMonitor monitor) throws IOException {
String exclusionsCommand = createAddExclusionsPowershellCommand(""); //$NON-NLS-1$
// In order to change the Windows Defender configuration a powershell with
// Administrator privileges is needed and therefore from a basic powershell
// a second one with elevated rights is started and runs the
// add-exclusions-command. For a detailed explanation of the Start-process
// parameters see
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process#parameters
//
// In order to avoid quoting when passing a command through multiple
// process-calls/command line processors, the command is passed as
// base64-encoded string to the elevated second powershell.
// For details about the -EncodedCommand argument see (and the EXAMPLES section)
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe#-encodedcommand-base64encodedcommand
String encodedCommand = Base64.getEncoder()
.encodeToString(exclusionsCommand.getBytes(StandardCharsets.UTF_16LE)); // encoding as specified
List<String> command = List.of("powershell.exe", //$NON-NLS-1$
// Launch child powershell with administrator privileges
"Start-Process", "powershell", "-Verb", "RunAs", "-Wait", //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
"-ArgumentList", "'-EncodedCommand " + encodedCommand + "'"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
runProcess(command, monitor);
}
private static List<String> runProcess(List<String> command, IProgressMonitor monitor) throws IOException {
ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
Process process = new ProcessBuilder(command).start();
Future<List<String>> processLines = newSingleThreadExecutor.submit(() -> {
try (BufferedReader inputReader = process.inputReader();) {
return inputReader.lines().filter(l -> !l.isBlank()).map(String::strip).toList();
}
});
newSingleThreadExecutor.shutdown();
try {
while (!processLines.isDone()) {
if (monitor.isCanceled()) {
process.destroy();
process.descendants().forEach(ProcessHandle::destroy);
processLines.cancel(true);
throw new OperationCanceledException();
}
Thread.onSpinWait();
Thread.sleep(5);
}
if (process.isAlive()) {
process.destroyForcibly();
process.descendants().forEach(ProcessHandle::destroyForcibly);
throw new IOException("Process timed-out and it was attempted to forcefully termiante it"); //$NON-NLS-1$
} else if (process.exitValue() != 0) {
throw new IOException("Process failed with exit-code " + process.exitValue()); //$NON-NLS-1$
}
return processLines.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new OperationCanceledException();
} catch (ExecutionException e) {
throw new IOException(e.getCause());
}
}
}