1 /* 2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jpackage.internal; 27 28 import java.io.BufferedReader; 29 import java.io.ByteArrayInputStream; 30 import java.io.ByteArrayOutputStream; 31 import java.io.IOException; 32 import java.io.InputStreamReader; 33 import java.io.PrintStream; 34 import java.util.ArrayList; 35 import java.util.List; 36 37 import static jdk.jpackage.internal.IOUtils.exec; 38 39 final class WindowsRegistry { 40 41 private WindowsRegistry() {} 42 43 /** 44 * Reads the registry value for DisableRealtimeMonitoring. 45 * @return true if DisableRealtimeMonitoring is set to 0x1, 46 * false otherwise. 47 */ 48 static final boolean readDisableRealtimeMonitoring() { 49 boolean result = false; 50 final String key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\" 51 + "Windows Defender\\Real-Time Protection"; 52 final String subkey = "DisableRealtimeMonitoring"; 53 String value = readRegistry(key, subkey); 54 55 if (!value.isEmpty()) { 56 // This code could be written better but this works. It validates 57 // that the result of readRegistry returned what we expect and then 58 // checks for a 0x0 or 0x1. 0x0 means real time monitoring is 59 // on, 0x1 means it is off. So this function returns true if 60 // real-time-monitoring is disabled. 61 int index = value.indexOf(subkey); 62 value = value.substring(index + subkey.length()); 63 String reg = "REG_DWORD"; 64 index = value.indexOf(reg); 65 value = value.substring(index + reg.length()); 66 String hex = "0x"; 67 index = value.indexOf(hex); 68 value = value.substring(index + hex.length()); 69 70 if (value.equals("1")) { 71 result = true; 72 } 73 } 74 75 return result; 76 } 77 78 static final List<String> readExclusionsPaths() { 79 List<String> result = new ArrayList<String>(); 80 final String key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\" 81 + "Windows Defender\\Exclusions\\Paths"; 82 String value = readRegistry(key, ""); 83 84 if (!value.isEmpty()) { 85 final String reg = "REG_DWORD"; 86 final String hex = "0x0"; 87 88 int index = value.indexOf(key); 89 if (index == 0) { 90 value = value.substring(index + key.length()); 91 92 while (value.length() > 0) { 93 index = value.indexOf(reg); 94 String name = value.substring(0, index); 95 value = value.substring(index + reg.length()); 96 index = value.indexOf(hex); 97 value = value.substring(index + hex.length()); 98 99 if (index > 0) { 100 name = name.trim(); 101 result.add(name); 102 } 103 } 104 } 105 } 106 107 return result; 108 } 109 110 /** 111 * @param key in the registry 112 * @param subkey in the registry key 113 * @return registry value or null if not found 114 */ 115 static final String readRegistry(String key, String subkey){ 116 String result = ""; 117 118 try { 119 List<String> buildOptions = new ArrayList<>(); 120 buildOptions.add("reg"); 121 buildOptions.add("query"); 122 buildOptions.add("\"" + key + "\""); 123 124 if (!subkey.isEmpty()) { 125 buildOptions.add("/v"); 126 buildOptions.add(subkey); 127 } 128 129 try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); 130 PrintStream ps = new PrintStream(baos)) { 131 ProcessBuilder security = new ProcessBuilder(buildOptions); 132 exec(security, false, false, ps); 133 BufferedReader bfReader = new BufferedReader( 134 new InputStreamReader( 135 new ByteArrayInputStream(baos.toByteArray()))); 136 String line = null; 137 138 while((line = bfReader.readLine()) != null){ 139 result += line; 140 } 141 } 142 catch (IOException e) { 143 } 144 } 145 catch (Exception e) { 146 } 147 148 return result; 149 } 150 }