1 /*
   2  * Copyright (c) 2012, 2017, 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.packager.internal.windows;
  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.InputStream;
  33 import java.io.InputStreamReader;
  34 import java.io.PrintStream;
  35 import java.io.StringWriter;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 
  39 import static com.oracle.tools.packager.IOUtils.exec;
  40 
  41 public final class WindowsRegistry {
  42 
  43     private WindowsRegistry() {}
  44 
  45     /**
  46      * Reads the registry value for DisableRealtimeMonitoring.
  47      * @return true if DisableRealtimeMonitoring is set to 0x1, false otherwise.
  48      */
  49     public static final boolean readDisableRealtimeMonitoring() {
  50         boolean result = false;
  51         final String key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\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     public static final List<String> readExclusionsPaths() {
  79         List<String> result = new ArrayList();
  80         final String key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows Defender\\Exclusions\\Paths";
  81         String value = readRegistry(key, "");
  82 
  83         if (!value.isEmpty()) {
  84             final String reg = "REG_DWORD";
  85             final String hex = "0x0";
  86 
  87             int index = value.indexOf(key);
  88             if (index == 0) {
  89                 value = value.substring(index + key.length());
  90 
  91                 while (value.length() > 0) {
  92                     index = value.indexOf(reg);
  93                     String name = value.substring(0, index);
  94                     value = value.substring(index + reg.length());
  95                     index = value.indexOf(hex);
  96                     value = value.substring(index + hex.length());
  97 
  98                     if (index > 0) {
  99                         name = name.trim();
 100                         result.add(name);
 101                     }
 102                 }
 103             }
 104         }
 105 
 106         return result;
 107     }
 108     /**
 109      * @param key in the registry
 110      * @param subkey in the registry key
 111      * @return registry value or null if not found
 112      */
 113     public static final String readRegistry(String key, String subkey){
 114         String result = "";
 115 
 116         try {
 117             List<String> buildOptions = new ArrayList<>();
 118             buildOptions.add("reg");
 119             buildOptions.add("query");
 120             buildOptions.add("\"" + key + "\"");
 121 
 122             if (!subkey.isEmpty()) {
 123                 buildOptions.add("/v");
 124                 buildOptions.add(subkey);
 125             }
 126 
 127             try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos)) {
 128                 ProcessBuilder security = new ProcessBuilder(buildOptions);
 129                 exec(security, false, false, ps);
 130                 BufferedReader bfReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));
 131                 String line = null;
 132 
 133                 while((line = bfReader.readLine()) != null){
 134                     result += line;
 135                 }
 136             }
 137             catch (IOException e) {
 138             }
 139         }
 140         catch (Exception e) {
 141         }
 142 
 143         return result;
 144     }
 145 }