-
Notifications
You must be signed in to change notification settings - Fork 9
/
HashGenerator.java
284 lines (258 loc) · 12.2 KB
/
HashGenerator.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.newrelic.agent.security.instrumentator.utils;
import com.newrelic.agent.security.intcodeagent.filelogging.FileLoggerThreadPool;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import com.newrelic.agent.security.intcodeagent.logging.DeployedApplication;
import net.openhft.hashing.LongHashFunction;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
public class HashGenerator {
private static final String TMP_DIR = "/tmp/";
private static final String ERROR2 = "Error : ";
private static final String TAR_SIZE = "tar size : ";
private static final String TAR_GZ = "tar.gz";
private static final String K2_TEMP_DIR = "NR-CSEC-";
private static final Set<String> JAVA_APPLICATION_ALLOWED_FILE_EXT = new HashSet<>(
Arrays.asList(new String[]{"java", "jsp", "class", "jar", "war", "ear"}));
private static final Set<String> OTHER_CRITICAL_FILE_EXT = new HashSet<>(
Arrays.asList(new String[]{"htm", "html", "js"}));
private static final String TWO_PIPES = "||";
private static final FileLoggerThreadPool logger = FileLoggerThreadPool.getInstance();
public static final String SHA_256 = "SHA-256";
public static final String FILE_FOR_SHA_CALC = "File for SHA calc : ";
public static final String UNSORTED_SHA_LIST = "Unsorted SHA list : ";
public static final String SORTED_SHA_LIST = "Sorted SHA list : ";
public static final String ERROR = "Error :";
public static final String WEBAPP_DETECTION_SHA_INFO_S = "Webapp detection SHA info : %s";
public static final String STRING_SEP = "-";
private static final LongHashFunction xxHashFunction = LongHashFunction.xx3(3214658854114272368L);
/**
* generates hash of a file content according to the algorithm provided.
*
* @return It returns the hash in string format
*/
private static String getChecksum(String data) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(SHA_256);
digest.update(data.getBytes());
byte[] hashedBytes = digest.digest();
return convertByteArrayToHexString(hashedBytes);
} catch (NoSuchAlgorithmException e) {
logger.log(LogLevel.SEVERE, ERROR, e, HashGenerator.class.getName());
}
return null;
}
/**
* generates hash of a file content according to the algorithm provided.
*
* @param file file object whose hash is to be calculated
* @return It returns the hash in string format
*/
public static String getChecksum(File file) {
try (FileInputStream inputStream = new FileInputStream(file)) {
MessageDigest digest = MessageDigest.getInstance(SHA_256);
byte[] bytesBuffer = new byte[102400];
int bytesRead = -1;
while ((bytesRead = inputStream.read(bytesBuffer)) != -1) {
digest.update(bytesBuffer, 0, bytesRead);
}
byte[] hashedBytes = digest.digest();
return convertByteArrayToHexString(hashedBytes);
} catch (FileNotFoundException e) {
} catch (NoSuchAlgorithmException | IOException e) {
}
return null;
}
/**
* convertByteArrayToHexString converts byte array to hex string.
*
* @param arrayBytes byte array of hash digest.
* @return returns the string format of digest byte array
*/
private static String convertByteArrayToHexString(byte[] arrayBytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < arrayBytes.length; i++) {
String hex = Integer.toHexString(0xFF & arrayBytes[i]);
if (hex.length() == 1) {
stringBuffer.append('0');
}
stringBuffer.append(hex);
}
return stringBuffer.toString();
}
public static void updateShaAndSize(DeployedApplication deployedApplication) {
File deplyementDirFile = new File(deployedApplication.getDeployedPath());
if (StringUtils.isBlank(deployedApplication.getDeployedPath())) {
logger.log(LogLevel.WARNING, "Empty deployed path detected. Not calculating SHA256 & size.", HashGenerator.class.getName());
return;
}
if (deplyementDirFile.isFile()) {
deployedApplication.setSha256(getChecksum(deplyementDirFile));
deployedApplication.setSize(FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(deplyementDirFile)));
} else {
deployedApplication.setSha256(getSHA256ForDirectory(deplyementDirFile.getAbsolutePath()));
deployedApplication.setSize(FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(deplyementDirFile)));
}
}
public static String getSHA256ForDirectory(String file) {
try {
File dir = new File(file);
if (dir.isDirectory()) {
List<String> sha256s = new ArrayList<>();
Collection<File> allFiles = FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
List<File> sortedFiles = new ArrayList<>(allFiles);
Collections.sort(sortedFiles);
for (File tempFile : sortedFiles) {
String extension = FilenameUtils.getExtension(tempFile.getName());
if (OTHER_CRITICAL_FILE_EXT.contains(extension)
|| JAVA_APPLICATION_ALLOWED_FILE_EXT.contains(extension)) {
sha256s.add(getChecksum(tempFile));
}
}
return getSHA256HexDigest(sha256s);
}
} catch (Exception e) {
logger.log(LogLevel.SEVERE, ERROR, e, HashGenerator.class.getName());
}
return null;
}
public static String getSHA256HexDigest(List<String> data) {
data.removeAll(Collections.singletonList(null));
String input = StringUtils.join(data);
return getChecksum(input);
}
public static String getSHA256HexDigest(String data) {
String input = StringUtils.join(data);
return getChecksum(input);
}
/**
* Gets the xxHash64 hex digest.
*
* @param data list of strings whose hash is to be generated
* @return the digest as a hex string
*/
public static String getXxHash64Digest(List<?> data) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream);
outputStream.writeObject(data);
return String.valueOf(xxHashFunction.hashBytes(byteArrayOutputStream.toByteArray()));
}
/**
* Gets the xxHash64 hex digest.
*
* @param data array of Integers whose hash is to be generated
* @return the digest as a hex string
*/
public static String getXxHash64Digest(int[] data) throws IOException {
return String.valueOf(xxHashFunction.hashInts(data));
}
public static void createTarGz(File tmpAppDir, File tmpTarFile) throws IOException {
BufferedOutputStream bOutputStream = null;
TarArchiveOutputStream tarArchiveOutputStream = null;
try {
bOutputStream = new BufferedOutputStream(new FileOutputStream(tmpTarFile));
tarArchiveOutputStream = new TarArchiveOutputStream(bOutputStream, StandardCharsets.UTF_8.displayName());
tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
addFilesToTarGZ(tmpAppDir.toString(), StringUtils.EMPTY, tarArchiveOutputStream);
} finally {
tarArchiveOutputStream.close();
bOutputStream.close();
}
}
@Deprecated
public static void calculateDirShaAndSize(DeployedApplication deployedApplication) {
File tmpAppDir = null;
File tmpTarFile = null;
try {
tmpAppDir = createTmpDirWithResource(deployedApplication.getDeployedPath());
tmpTarFile = Files.createTempFile(K2_TEMP_DIR, TAR_GZ).toFile();
createTarGz(tmpAppDir, tmpTarFile);
logger.log(LogLevel.FINER, TAR_SIZE + FileUtils.sizeOf(tmpTarFile), HashGenerator.class.getName());
deployedApplication.setSize(FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(tmpTarFile)));
deployedApplication.setSha256(getChecksum(tmpTarFile));
} catch (Throwable e) {
logger.log(LogLevel.SEVERE, ERROR2, e, HashGenerator.class.getName());
} finally {
try {
FileUtils.forceDelete(tmpTarFile);
FileUtils.forceDelete(tmpAppDir);
} catch (IOException e) {
}
}
}
private static File createTmpDirWithResource(String deployedPath) throws IOException {
File tmpShaDir;
tmpShaDir = Files.createTempDirectory(Paths.get(TMP_DIR), K2_TEMP_DIR).toFile();
FileUtils.copyDirectory(new File(deployedPath), tmpShaDir);
System.out.println("Directory is copied.");
removeNonResource(tmpShaDir);
System.out.println("Removed unwanted files!");
return tmpShaDir;
}
private static void removeNonResource(File dir) throws IOException {
if (dir.isDirectory()) {
Iterator<File> fileIterator = FileUtils.iterateFilesAndDirs(dir, TrueFileFilter.INSTANCE,
TrueFileFilter.INSTANCE);
while (fileIterator.hasNext()) {
File tempFile = fileIterator.next();
if (tempFile.isFile()) {
String extension = FilenameUtils.getExtension(tempFile.getName());
if (!(OTHER_CRITICAL_FILE_EXT.contains(extension)
|| JAVA_APPLICATION_ALLOWED_FILE_EXT.contains(extension))) {
logger.log(LogLevel.FINER, FILE_FOR_SHA_CALC + tempFile.getAbsolutePath(),
HashGenerator.class.getName());
FileUtils.forceDeleteOnExit(tempFile);
}
}
}
} else if (dir.isFile()) {
String extension = FilenameUtils.getExtension(dir.getName());
if (!OTHER_CRITICAL_FILE_EXT.contains(extension)) {
FileUtils.forceDeleteOnExit(dir);
} else if (!JAVA_APPLICATION_ALLOWED_FILE_EXT.contains(extension)) {
FileUtils.forceDeleteOnExit(dir);
}
}
}
private static void addFilesToTarGZ(String filePath, String parent, TarArchiveOutputStream tarArchive)
throws IOException {
File file = new File(filePath);
// Create entry name relative to parent file path
String entryName = parent + file.getName();
// add tar ArchiveEntry
tarArchive.putArchiveEntry(new TarArchiveEntry(file, entryName));
if (file.isFile()) {
System.out.println("File to add: " + file);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
// Write file content to archive
IOUtils.copy(bis, tarArchive);
tarArchive.closeArchiveEntry();
bis.close();
} else if (file.isDirectory()) {
// no need to copy any content since it is
// a directory, just close the outputstream
tarArchive.closeArchiveEntry();
// for files in the directories
List<File> files = Arrays.asList(file.listFiles());
System.out.println("All files : " + files);
Collections.sort(files);
for (File f : files) {
// recursively call the method for all the subdirectories
addFilesToTarGZ(f.getAbsolutePath(), entryName + File.separator, tarArchive);
}
}
}
}