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 } | 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 // Currently we only support HKEY_LOCAL_MACHINE. Native implementation will 42 // require support for additinal HKEY if needed. 43 private static final int HKEY_LOCAL_MACHINE = 1; 44 45 static { 46 System.loadLibrary("jpackage"); 47 } 48 49 private WindowsRegistry() {} 50 51 /** 52 * Reads the registry value for DisableRealtimeMonitoring. 53 * @return true if DisableRealtimeMonitoring is set to 0x1, 54 * false otherwise. 55 */ 56 static final boolean readDisableRealtimeMonitoring() { 57 final String subKey = "Software\\Microsoft\\" 58 + "Windows Defender\\Real-Time Protection"; 59 final String value = "DisableRealtimeMonitoring"; 60 int result = readDwordValue(HKEY_LOCAL_MACHINE, subKey, value, 0); 61 return (result == 1); 62 } 63 64 static final List<String> readExclusionsPaths() { 65 List<String> result = new ArrayList<>(); 66 final String subKey = "Software\\Microsoft\\" 67 + "Windows Defender\\Exclusions\\Paths"; 68 long lKey = openRegistryKey(HKEY_LOCAL_MACHINE, subKey); 69 if (lKey == 0) { 70 return result; 71 } 72 73 String valueName; 74 int index = 0; 75 do { 76 valueName = enumRegistryValue(lKey, index); 77 if (valueName != null) { 78 result.add(valueName); 79 index++; 80 } 81 } while (valueName != null); 82 83 closeRegistryKey(lKey); 84 85 return result; 86 } 87 88 /** 89 * Reads DWORD registry value. 90 * 91 * @param key one of HKEY predefine value 92 * @param subKey registry sub key 93 * @param value value to read 94 * @param defaultValue default value in case if subKey or value not found 95 * or any other errors occurred 96 * @return value's data only if it was read successfully, otherwise 97 * defaultValue 98 */ 99 private static native int readDwordValue(int key, String subKey, 100 String value, int defaultValue); 101 102 /** 103 * Open registry key. 104 * 105 * @param key one of HKEY predefine value 106 * @param subKey registry sub key 107 * @return native handle to open key 108 */ 109 private static native long openRegistryKey(int key, String subKey); 110 111 /** 112 * Enumerates the values for registry key. 113 * 114 * @param lKey native handle to open key returned by openRegistryKey 115 * @param index index of value starting from 0. Increment until this 116 * function returns NULL which means no more values. 117 * @return returns value or NULL if error or no more data 118 */ 119 private static native String enumRegistryValue(long lKey, int index); 120 121 /** 122 * Close registry key. 123 * 124 * @param lKey native handle to open key returned by openRegistryKey 125 */ 126 private static native void closeRegistryKey(long lKey); 127 128 /** 129 * Compares two Windows paths regardless case and if paths are short or long. 130 * 131 * @param path1 path to compare 132 * @param path2 path to compare 133 * @return true if paths point to same location 134 */ 135 public static native boolean comparePaths(String path1, String path2); 136 } |