RegistryQuery.java
4.86 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.pdfg.common;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RegistryQuery {
public static final String APPDATA = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\AppData";
private static Logger logger = LoggerFactory.getLogger(RegistryQuery.class);
private static final String SZ = "REG_SZ";
private static final String XSZ = "REG_EXPAND_SZ";
private static final String MSZ = "REG_MULTI_SZ";
private static final String BINARY = "REG_BINARY";
private static final String DWORD = "REG_DWORD";
private static final String QWORD = "REG_QWORD";
private static void SeparateArguments(String key, StringBuilder FolderPath, StringBuilder ArgumentValue) {
int divider = key.lastIndexOf("\\");
FolderPath.append("\"");
FolderPath.append(key.substring(0, divider));
FolderPath.append("\"");
ArgumentValue.append(key.substring(divider + 1));
}
public static String getRegistryValue(String key) {
String value = "";
try {
StringBuilder FolderPath = new StringBuilder();
StringBuilder ArgumentValue = new StringBuilder();
RegistryQuery.SeparateArguments(key, FolderPath, ArgumentValue);
String fp = FolderPath.toString();
String keyVal = ArgumentValue.toString();
if (fp == null || "".equals(fp) || keyVal == null || "".equals(keyVal)) {
logger.warn("The Folder path or the Argument value is null, please check the the key passed \n");
return value;
}
String[] find_system_value_command = new String[]{"REG", "QUERY", fp, "/v", keyVal};
Process prc = Runtime.getRuntime().exec(find_system_value_command);
InputStream is = prc.getInputStream();
BufferedReader bfr = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = "";
while ((line = bfr.readLine()) != null && !line.contains(keyVal)) {
}
if (line == null) {
logger.warn("There is no value in registry which satisfies the key value passed \n");
return value;
}
int indexOfValue = (line = line.trim()).indexOf("REG_SZ");
if (indexOfValue != -1) {
indexOfValue += "REG_SZ".length();
} else {
indexOfValue = line.indexOf("REG_EXPAND_SZ");
if (indexOfValue != -1) {
indexOfValue += "REG_EXPAND_SZ".length();
} else {
indexOfValue = line.indexOf("REG_BINARY");
if (indexOfValue != -1) {
indexOfValue += "REG_BINARY".length();
} else {
indexOfValue = line.indexOf("REG_DWORD");
if (indexOfValue != -1) {
indexOfValue += "REG_DWORD".length();
} else {
indexOfValue = line.indexOf("REG_QWORD");
if (indexOfValue != -1) {
indexOfValue += "REG_QWORD".length();
} else {
indexOfValue = line.indexOf("REG_MULTI_SZ");
if (indexOfValue != -1) {
indexOfValue += "REG_MULTI_SZ".length();
} else {
logger.warn("Invalid Registry Entry: Please check the registry value you are looking for \n");
return value;
}
}
}
}
}
}
if (indexOfValue >= 0) {
if ((line = line.substring(indexOfValue).trim()).startsWith("%")) {
int second_percentage_sign = line.indexOf(37, 1);
if (second_percentage_sign != -1) {
String env = line.substring(1, second_percentage_sign);
value = System.getenv(env) + line.substring(second_percentage_sign + 1);
}
} else {
value = line;
}
} else {
logger.warn("Error while retrieving system environment variable: " + key + " NOT FOUND...............");
}
prc.waitFor();
}
catch (Exception e) {
logger.warn("Error while retrieving system environment variable: " + key, (Throwable)e);
}
return value;
}
}