Skip to content

Commit ebac853

Browse files
committed
Clean-up WindowsDefenderConfigurator after subsequent check refinements
Also rename 'Windows Defender' to 'Microsoft Defender' in texts and labels since it is now branded as such.
1 parent dd5068c commit ebac853

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WindowsDefenderConfigurator.java

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URISyntaxException;
2222
import java.nio.charset.StandardCharsets;
2323
import java.nio.file.Path;
24+
import java.util.Arrays;
2425
import java.util.Base64;
2526
import java.util.List;
2627
import java.util.Optional;
@@ -29,6 +30,7 @@
2930
import java.util.concurrent.Executors;
3031
import java.util.concurrent.Future;
3132
import java.util.stream.Collectors;
33+
import java.util.stream.Stream;
3234
import org.eclipse.core.runtime.CoreException;
3335
import org.eclipse.core.runtime.ILog;
3436
import org.eclipse.core.runtime.IProduct;
@@ -156,27 +158,23 @@ private static Boolean runExclusionCheck(IProgressMonitor m, Optional<Path> inst
156158
}
157159
Display display = Display.getDefault();
158160
HandlingOption decision = askForDefenderHandlingDecision(display);
159-
if (decision != null) {
160-
switch (decision) {
161-
case EXECUTE_EXCLUSION -> {
162-
if (isExclusionTamperProtectionEnabled(monitor.split(1))) {
163-
display.syncExec(() -> MessageDialog.openError(null, "Exclusion failed", //$NON-NLS-1$
164-
bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionFailed_Protected)));
165-
savePreference(ConfigurationScope.INSTANCE, PREFERENCE_STARTUP_CHECK_SKIP, "true"); //$NON-NLS-1$
166-
return null; // Consider selection as 'aborted' and don't show the dialog again on startup
167-
}
168-
try {
169-
WindowsDefenderConfigurator.excludeDirectoryFromScanning(monitor.split(2));
170-
savePreference(ConfigurationScope.INSTANCE, PREFERENCE_EXCLUDED_INSTALLATION_PATH,
171-
installLocation.map(Path::toString).orElse("")); //$NON-NLS-1$
172-
} catch (IOException e) {
173-
display.syncExec(() -> MessageDialog.openError(null, "Exclusion failed", //$NON-NLS-1$
174-
bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionFailed)));
175-
}
161+
if (decision == HandlingOption.EXECUTE_EXCLUSION) {
162+
if (isExclusionTamperProtectionEnabled(monitor.split(1))) {
163+
display.syncExec(() -> MessageDialog.openError(null, "Exclusion failed", //$NON-NLS-1$
164+
bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionFailed_Protected)));
165+
savePreference(ConfigurationScope.INSTANCE, PREFERENCE_STARTUP_CHECK_SKIP, "true"); //$NON-NLS-1$
166+
return null; // Consider selection as 'aborted' and don't show the dialog again on startup
176167
}
177-
case IGNORE_THIS_INSTALLATION -> savePreference(ConfigurationScope.INSTANCE, PREFERENCE_STARTUP_CHECK_SKIP,
178-
"true"); //$NON-NLS-1$
168+
try {
169+
WindowsDefenderConfigurator.excludeDirectoryFromScanning(monitor.split(2));
170+
savePreference(ConfigurationScope.INSTANCE, PREFERENCE_EXCLUDED_INSTALLATION_PATH,
171+
installLocation.map(Path::toString).orElse("")); //$NON-NLS-1$
172+
} catch (IOException e) {
173+
display.syncExec(() -> MessageDialog.openError(null, "Exclusion failed", //$NON-NLS-1$
174+
bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionFailed)));
179175
}
176+
} else if (decision == HandlingOption.IGNORE_THIS_INSTALLATION) {
177+
savePreference(ConfigurationScope.INSTANCE, PREFERENCE_STARTUP_CHECK_SKIP, "true"); //$NON-NLS-1$
180178
}
181179
return decision == HandlingOption.EXECUTE_EXCLUSION ? Boolean.TRUE : null;
182180
}
@@ -285,9 +283,8 @@ private static List<Path> getExecutablePath() {
285283
private static boolean isExclusionTamperProtectionEnabled(IProgressMonitor monitor) {
286284
// 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
287285
try { // Query the Windows Registry
288-
List<String> result = runProcess(List.of("powershell.exe", "-Command", //$NON-NLS-1$//$NON-NLS-2$
289-
"Get-ItemPropertyValue -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows Defender\\Features' -Name 'TPExclusions'"), //$NON-NLS-1$
290-
monitor);
286+
List<String> result = runPowershell(monitor, "-Command", //$NON-NLS-1$
287+
"Get-ItemPropertyValue -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows Defender\\Features' -Name 'TPExclusions'"); //$NON-NLS-1$
291288
return result.size() == 1 && "1".equals(result.get(0)); //$NON-NLS-1$
292289
} catch (IOException e) {
293290
return false;
@@ -298,8 +295,7 @@ private static boolean isWindowsDefenderServiceRunning(IProgressMonitor monitor)
298295
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-service?view=powershell-7.4
299296
// https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicecontrollerstatus?view=dotnet-plat-ext-8.0
300297
try {
301-
List<String> result = runProcess(List.of("powershell.exe", "-Command", "(Get-Service 'WinDefend').Status"), //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
302-
monitor);
298+
List<String> result = runPowershell(monitor, "-Command", "(Get-Service 'WinDefend').Status"); //$NON-NLS-1$ //$NON-NLS-2$
303299
return result.size() == 1 && "Running".equalsIgnoreCase(result.get(0)); //$NON-NLS-1$
304300
} catch (IOException e) {
305301
ILog.get().error("Failed to obtain 'WinDefend' service state", e); //$NON-NLS-1$
@@ -308,10 +304,9 @@ private static boolean isWindowsDefenderServiceRunning(IProgressMonitor monitor)
308304
}
309305

310306
private static boolean isWindowsDefenderActive(IProgressMonitor monitor) throws CoreException {
311-
// https://learn.microsoft.com/en-us/powershell/module/defender/get-mpcomputerstatus
312-
List<String> command = List.of("powershell.exe", "-Command", "(Get-MpComputerStatus).AMRunningMode"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
307+
// https://learn.microsoft.com/en-us/powershell/module/defender/get-mpcomputerstatus?view=windowsserver2019-ps
313308
try {
314-
List<String> lines = runProcess(command, monitor);
309+
List<String> lines = runPowershell(monitor, "-Command", "(Get-MpComputerStatus).AMRunningMode"); //$NON-NLS-1$ //$NON-NLS-2$
315310
String onlyLine = lines.size() == 1 ? lines.get(0) : ""; //$NON-NLS-1$
316311
return switch (onlyLine) {
317312
// Known values as listed in
@@ -329,8 +324,8 @@ public static String createAddExclusionsPowershellCommand(String extraSeparator)
329324
List<Path> paths = getExecutablePath();
330325
// For detailed explanations about how to read existing exclusions and how to
331326
// add new ones see:
332-
// https://learn.microsoft.com/en-us/powershell/module/defender/add-mppreference
333-
// https://learn.microsoft.com/en-us/powershell/module/defender/get-mppreference
327+
// https://learn.microsoft.com/en-us/powershell/module/defender/add-mppreference?view=windowsserver2019-ps
328+
// https://learn.microsoft.com/en-us/powershell/module/defender/get-mppreference?view=windowsserver2019-ps
334329
//
335330
// For .NET's stream API called LINQ see:
336331
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable
@@ -351,7 +346,7 @@ private static void excludeDirectoryFromScanning(IProgressMonitor monitor) throw
351346
// a second one with elevated rights is started and runs the
352347
// add-exclusions-command. For a detailed explanation of the Start-process
353348
// parameters see
354-
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process#parameters
349+
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.4#parameters
355350
//
356351
// In order to avoid quoting when passing a command through multiple
357352
// process-calls/command line processors, the command is passed as
@@ -360,11 +355,13 @@ private static void excludeDirectoryFromScanning(IProgressMonitor monitor) throw
360355
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe#-encodedcommand-base64encodedcommand
361356
String encodedCommand = Base64.getEncoder()
362357
.encodeToString(exclusionsCommand.getBytes(StandardCharsets.UTF_16LE)); // encoding as specified
363-
List<String> command = List.of("powershell.exe", //$NON-NLS-1$
364-
// Launch child powershell with administrator privileges
358+
runPowershell(monitor, // Launch child powershell with administrator privileges
365359
"Start-Process", "powershell", "-Verb", "RunAs", "-Wait", //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
366360
"-ArgumentList", "'-EncodedCommand " + encodedCommand + "'"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
367-
runProcess(command, monitor);
361+
}
362+
363+
private static List<String> runPowershell(IProgressMonitor monitor, String... arguments) throws IOException {
364+
return runProcess(Stream.concat(Stream.of("powershell.exe"), Arrays.stream(arguments)).toList(), monitor); //$NON-NLS-1$
368365
}
369366

370367
private static List<String> runProcess(List<String> command, IProgressMonitor monitor) throws IOException {

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -568,25 +568,25 @@ CheckedTreeSelectionDialog_nothing_available=No entries available.
568568
CheckedTreeSelectionDialog_select_all=Select &All
569569
CheckedTreeSelectionDialog_deselect_all=&Deselect All
570570

571-
WindowsDefenderConfigurator_statusCheck=Windows Defender Exclusion Check
572-
WindowsDefenderConfigurator_exclusionCheckMessage=Microsoft''s Windows Defender is active and could significantly decrease the startup and overall performance of {0}.\n\
573-
Select how this installation should be handled by Windows Defender:
574-
WindowsDefenderConfigurator_exclusionInformation=If Windows Defender is active and scans {0} it can significantly slow down its startup and overall performance.\n\
575-
To prevent a decrease in performance {0} can exclude itself and all files opened from real-time scanning by Windows Defender.\n\
571+
WindowsDefenderConfigurator_statusCheck=Microsoft Defender Exclusion Check
572+
WindowsDefenderConfigurator_exclusionCheckMessage=Microsoft Windows Defender is active and could significantly decrease the startup and overall performance of {0}.\n\
573+
Select how this installation should be handled by Microsoft Defender:
574+
WindowsDefenderConfigurator_exclusionInformation=If Microsoft Defender is active on Windows 10 or later and scans {0} it can significantly slow down its startup and overall performance.\n\
575+
To prevent a decrease in performance {0} can exclude its process and consequently all files touched by it from real-time scanning by the Defender.\n\
576576
Be aware that in general adding exclusions could affect this computer''s security.
577577
WindowsDefenderConfigurator_scriptShowLabel=Show Powershell script >>
578578
WindowsDefenderConfigurator_scriptHideLabel=<< Hide Powershell script
579579
WindowsDefenderConfigurator_scriptHint=Adding exclusions respectively running the Powershell script to do it requires administrator privileges.
580580
WindowsDefenderConfigurator_performExclusionChoice=Exclude {0} from being scanned to improve performance.\n\
581581
(In general adding exclusions may affect the security level of this computer)
582-
WindowsDefenderConfigurator_ignoreThisInstallationChoice=Keep {0} being scanned by Windows Defender.
582+
WindowsDefenderConfigurator_ignoreThisInstallationChoice=Keep {0} being scanned by Microsoft Defender.
583583
WindowsDefenderConfigurator_ignoreAllChoice=Skip exclusion check on startup for all new Eclipse-based installations
584584
WindowsDefenderConfigurator_detailsAndOptionsLinkText=See '<a>Startup and Shutdown</a>' preference for more details and configuration options.
585585
WindowsDefenderConfigurator_runExclusionFromPreferenceButtonLabel=Run exclusion check now
586-
WindowsDefenderConfigurator_statusInactive=Windows Defender is not active on this computer.
587-
WindowsDefenderConfigurator_statusCheckFailed=Failed to retrieve Windows Defender status.
588-
WindowsDefenderConfigurator_exclusionFailed=Failed to exclude {0} from being scanned by Windows Defender.
589-
WindowsDefenderConfigurator_exclusionFailed_Protected=Cannot exclude {0} from being scanned by Windows Defender.\nTamper protection for antivirus exclusions is enabled.
586+
WindowsDefenderConfigurator_statusInactive=Microsoft Defender is not active on this computer.
587+
WindowsDefenderConfigurator_statusCheckFailed=Failed to retrieve Microsoft Defender status.
588+
WindowsDefenderConfigurator_exclusionFailed=Failed to exclude {0} from being scanned by Microsoft Defender.
589+
WindowsDefenderConfigurator_exclusionFailed_Protected=Cannot exclude {0} from being scanned by Microsoft Defender.\nTamper protection for antivirus exclusions is enabled.
590590

591591
# ==============================================================================
592592
# Editor Framework

0 commit comments

Comments
 (0)