-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryScanner.java
More file actions
75 lines (63 loc) · 2.64 KB
/
DirectoryScanner.java
File metadata and controls
75 lines (63 loc) · 2.64 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
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DirectoryScanner {
// Basic filter for system files/folders to ignore
private static final Set<String> IGNORED_NAMES = new HashSet<>(Arrays.asList(
"Windows", "Program Files", "Program Files (x86)", ".git", ".idea", "node_modules", "$Recycle.Bin",
"System Volume Information"));
private PatternMatcher patternMatcher;
private InfectionReport report;
public DirectoryScanner(PatternMatcher patternMatcher, InfectionReport report) {
this.patternMatcher = patternMatcher;
this.report = report;
}
public void scanDirectory(File directory) {
if (directory == null || !directory.exists()) {
System.out.println("[-] Invalid directory.");
return;
}
if (IGNORED_NAMES.contains(directory.getName())) {
// System.out.println("[*] Skipping system/ignored directory: " +
// directory.getName());
return;
}
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
scanDirectory(file); // Recursive call
} else {
scanFile(file);
}
}
}
}
private void scanFile(File file) {
// Skip binary files or massive files if needed (simplified check)
if (file.length() > 10 * 1024 * 1024) { // Skip > 10MB
return;
}
// Basic check to try and only scan text-based files
// (A real tool would check magic numbers, but this is simple)
if (!isLikelyTextFile(file)) {
// System.out.println("Skipping non-text file: " + file.getName());
return;
}
System.out.print("."); // Progress indicator
List<String> threats = patternMatcher.scanFile(file);
if (!threats.isEmpty()) {
report.addInfection(file, threats);
}
}
private boolean isLikelyTextFile(File file) {
String name = file.getName().toLowerCase();
return name.endsWith(".txt") || name.endsWith(".java") || name.endsWith(".py") ||
name.endsWith(".js") || name.endsWith(".html") || name.endsWith(".css") ||
name.endsWith(".json") || name.endsWith(".xml") || name.endsWith(".md") ||
name.endsWith(".log") || name.endsWith(".csv") || name.endsWith(".bat") ||
name.endsWith(".sh");
}
}