Skip to content

Commit

Permalink
Version 15.7
Browse files Browse the repository at this point in the history
Temporarily disabled remote database while it gets looked into
Cleaned up code
Added missing @author javadocs
Added some missing try-with-resources
Updated dependencies
Updated local database
Removed ReflectionUtils.java
Removed Service.java
Updated comment in StringUtils to reflect current Java version
  • Loading branch information
OpticFusion1 committed Feb 11, 2023
1 parent dfc394c commit 3d0ac11
Show file tree
Hide file tree
Showing 34 changed files with 179 additions and 483 deletions.
2 changes: 1 addition & 1 deletion MCAntiMalware-Core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>MCAntiMalware</artifactId>
<version>15.6</version>
<version>15.7</version>
<packaging>jar</packaging>

<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,18 @@ public void run() {

private void init() {
LOGGER.info(I18n.tl("initializing_start") + I18n.tl("github_issues"));

cache = new CacheContainer(DATABASE);
notifHandler = new NotificationHandler(parser.getNotificationType());

new CheckRegistery(this).registerChecks();

scanner = new RealTimeScanner(this, parser.getScanDirectory());

if (parser.singleScan()) {
scanner.scanFiles();
return;
}

if (parser.shouldScanSingleFile()) {
scanner.addFileToQueue(parser.getScanFile().toPath());
return;
}

if (!parser.shouldDisableAutoUpdate()) {
LOGGER.info(I18n.tl("setup_auto_update"));
new Updater(this).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public abstract class BaseCheck {
private String sourceFilePath = "";
private int line = -1;

public abstract List<CheckResult> process(ClassNode classNode, Path rootFolder, Path zipFile,
CacheContainer cache);
public abstract List<CheckResult> process(ClassNode classNode, Path rootFolder, Path zipFile, CacheContainer cache);

public boolean isPlugin(Path rootDir) {
return Files.exists(rootDir.resolve("plugin.yml")) || Files.exists(rootDir.resolve("bungee.yml"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public List<CheckResult> process(ClassNode classNode, Path rootFolder, Path zipF
}
if (found) {
List<CheckResult> result = new ArrayList<>();
result.add(new CheckResult("Spigot", "MALWARE", "SystemAccess", variant[0], getSourceFilePath(),
getClassNodePath(), getLine()));
result.add(new CheckResult("Spigot", "MALWARE", "SystemAccess", variant[0], getSourceFilePath(), getClassNodePath(), getLine()));
return result;
}
return new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class Database {

private static final String DATABASE_URL = "https://raw.githubusercontent.com/OpticFusion1/MCAntiMalwareDatabase/master/database.db";
private static final String DATABASE_URL = "https://github.com/OpticFusion1/MCAntiMalwareDatabase/blob/master/database.db?raw=true";
private Connection connection;

public Database() {
Expand All @@ -45,11 +45,13 @@ public Database() {
public void downloadAndConnect() {
try {
File tempFile = File.createTempFile("antiMalware" + UUID.randomUUID().toString(), ".db");
try {
Files.copy(new URL(DATABASE_URL).openStream(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// try {
// Files.copy(new URL(DATABASE_URL).openStream(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// System.out.println("COPYING FROM DATABASE_URL");
// } catch (IOException e) {
// LOGGER.exception(e);
Files.copy(Database.class.getResource("/database.db").openStream(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
// }
connection = DriverManager.getConnection("jdbc:sqlite:" + tempFile.toURI());
} catch (SQLException | IOException e) {
LOGGER.info("Unable to load Database");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,19 @@ public NotificationHandler(String notificationType) {
break;
case "popup":
switch (System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH)) {
case "mac":
case "darwin":
case "linux":
case "mac", "darwin", "linux" ->
sender = new LinuxNotifSender();
break;
case "win":
case "win" -> {
if (!SystemTray.isSupported()) {
LOGGER.alert(I18n.tl("notification_os_not_supported", System.getProperty("os.name")));
break;
}
sender = new WindowsNotifSender();
break;
default:
}
default ->
LOGGER.alert(I18n.tl("notification_os_not_found"));
break;
}

default:
sender = new ConsoleNotifSender();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
import optic_fusion1.antimalware.check.CheckResult;
import optic_fusion1.antimalware.database.Database;
import optic_fusion1.antimalware.notifications.NotificationHandler;
import optic_fusion1.antimalware.utils.I18n;
import optic_fusion1.antimalware.utils.Utils;
import static optic_fusion1.antimalware.utils.I18n.tl;
import static optic_fusion1.antimalware.utils.Utils.fileSystemForZip;
import static optic_fusion1.antimalware.utils.Utils.unzip;
import static optic_fusion1.antimalware.utils.Utils.validClassPath;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.objectweb.asm.tree.ClassNode;
Expand Down Expand Up @@ -103,12 +105,12 @@ public void run() {
LOGGER.exception(e);
}
if (SCANNABLE_FILES.size() % 100 == 0) {
System.out.println(I18n.tl("scanner_remaining_files", SCANNABLE_FILES.size()));
System.out.println(tl("scanner_remaining_files", SCANNABLE_FILES.size()));
}
}

public void scanFiles() {
LOGGER.info(I18n.tl("scanner_scan_start"));
LOGGER.info(tl("scanner_scan_start"));
try {
Files.walk(scanDirectory.toPath()).filter(Files::isRegularFile)
.forEachOrdered(this::addFileToQueue);
Expand All @@ -125,7 +127,7 @@ private void scanZip(Path zippedFile) {
destination.mkdirs();
}
try {
Utils.unzip(new ZipFile(zippedFile.toFile()), destination);
unzip(new ZipFile(zippedFile.toFile()), destination);
File file = new File(destination, ".info");
file.createNewFile();
try (PrintWriter pw = new PrintWriter(file)) {
Expand Down Expand Up @@ -195,8 +197,7 @@ private void scanFile(Path file) {
LOGGER.exception(ex);
}
}
if (!fileName.endsWith(".jar") && !fileName.endsWith(".zip") && !fileName.endsWith(".rar")
&& !isPlugin(file)) {
if (!fileName.endsWith(".jar") && !fileName.endsWith(".zip") && !fileName.endsWith(".rar") && !isPlugin(file)) {
return;
}
try {
Expand All @@ -222,13 +223,13 @@ private void scanFile(Path file) {
sendNotification(file, checkResult);
// return;
}
System.out.println(I18n.tl("scanner_blacklisted_not_in_database", file));
System.out.println(tl("scanner_blacklisted_not_in_database", file));
} catch (SQLException ex) {
LOGGER.exception(ex);
}
}
}
try (FileSystem fs = Utils.fileSystemForZip(file)) {
try (FileSystem fs = fileSystemForZip(file)) {
if (fs == null) {
return;
}
Expand Down Expand Up @@ -354,7 +355,7 @@ private void scanFile(Path file) {
}
cache.clearCache(file); // Attempt at fixing memory issues
if (commandLineParser.shouldPrintNotInfectedMessages() && !possiblyMalicious) {
LOGGER.info(I18n.tl("scanner_probably_safe", file));
LOGGER.info(tl("scanner_probably_safe", file));
}
if (zippedFilePath != null) {
zippedFilePath = null;
Expand Down Expand Up @@ -384,7 +385,7 @@ private WhitelistResult isFileWhitelisted(Path file) {
String fileChecksum = DigestUtils.sha1Hex(Files.newInputStream(file));
WhitelistResult result = isChecksumWhitelisted(fileChecksum);
if (result == WhitelistResult.WHITELISTED && commandLineParser.shouldPrintNotInfectedMessages()) {
LOGGER.info(I18n.tl("scanner_probably_safe_whitelisted", file.getFileName().toString()));
LOGGER.info(tl("scanner_probably_safe_whitelisted", file.getFileName().toString()));
}
return result;
} catch (IOException e) {
Expand Down Expand Up @@ -422,8 +423,7 @@ public boolean awaitTermination(int time, TimeUnit unit) {
}

private WhitelistResult isChecksumWhitelisted(String checksum) {
return antiMalware.getCache().containsWhitelistedChecksum(checksum) ? WhitelistResult.WHITELISTED
: WhitelistResult.NOT_WHITELISTED;
return antiMalware.getCache().containsWhitelistedChecksum(checksum) ? WhitelistResult.WHITELISTED : WhitelistResult.NOT_WHITELISTED;
}

public enum Status {
Expand All @@ -433,11 +433,8 @@ public enum Status {
public Status getStatus() {
if (executorService == null) {
return Status.NOT_RUNNING;
} else if (executorService.getTaskCount() == 0) {
return Status.WAITING;
} else {
return Status.SCANNING;
}
return executorService.getTaskCount() == 0 ? Status.WAITING : Status.SCANNING;
}

public File getScanDirectory() {
Expand Down Expand Up @@ -473,14 +470,12 @@ protected Stream<Path> walkThroughFiles(Path dir) {
}
}

public boolean validClassPath(Path classPath) {
return classPath.toString().endsWith(".class") && !classPath.toString().contains("__MACOSX");
}

private boolean isPlugin(Path file) {
try {
ZipFile zipFile = new ZipFile(file.toFile());
ZipEntry zipEntry = zipFile.getEntry("plugin.yml");
ZipEntry zipEntry;
try (ZipFile zipFile = new ZipFile(file.toFile())) {
zipEntry = zipFile.getEntry("plugin.yml");
}
return zipEntry != null;
} catch (IOException ex) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public USBDriveScanner(AntiMalware antiMalware) {

@Override
public void usbDriveEvent(USBStorageEvent event) {
if (event.getEventType() != DeviceEventType.CONNECTED) {
if (event.eventType() != DeviceEventType.CONNECTED) {
return;
}
for (File file : event.getStorageDevice().getRootDirectory().listFiles()) {
if (file.isDirectory()) {
scanner.addDirectoryToQueue(file.toPath());
} else {
for (File file : event.storageDevice().getRootDirectory().listFiles()) {
if (!file.isDirectory()) {
scanner.addFileToQueue(file.toPath());
continue;
}
scanner.addDirectoryToQueue(file.toPath());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
import optic_fusion1.antimalware.AntiMalware;
import static optic_fusion1.antimalware.AntiMalware.LOGGER;
import optic_fusion1.antimalware.utils.I18n;
import optic_fusion1.antimalware.utils.Service;

//TODO: Correct class messages
public class DirectoryWatcher implements Runnable, Service {
public class DirectoryWatcher implements Runnable {

public enum Event {
ENTRY_CREATE,
Expand Down Expand Up @@ -78,20 +77,11 @@ private static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>) event;
}

@Override
public void startService() throws Exception {
mExecutor = Executors.newSingleThreadExecutor();
mWatcherTask = mExecutor.submit(this);
}

@Override
public void stopService() {
mWatcherTask.cancel(true);
mWatcherTask = null;
mExecutor.shutdown();
mExecutor = null;
}

@Override
public void run() {
WatchService watchService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected Object handleGetObject(String key) {
};
private final transient Locale defaultLocale = Locale.getDefault();
private transient Locale currentLocale = defaultLocale;
private transient Map<String, MessageFormat> messageFormatCache = new HashMap<String, MessageFormat>();
private transient Map<String, MessageFormat> messageFormatCache = new HashMap<>();
private transient ResourceBundle customBundle;
private transient ResourceBundle localeBundle;
private final transient ResourceBundle defaultBundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ public static double square(double num) {
}

public static int toInt(@Nullable Object object) {
if (object instanceof Number) {
return ((Number) object).intValue();
if (object instanceof Number number) {
return number.intValue();
}

try {
return Integer.parseInt(object.toString());
} catch (NumberFormatException | NullPointerException ex) {
Expand All @@ -57,10 +56,9 @@ public static int toInt(@Nullable Object object) {
}

public static float toFloat(@Nullable Object object) {
if (object instanceof Number) {
return ((Number) object).floatValue();
if (object instanceof Number number) {
return number.floatValue();
}

try {
return Float.parseFloat(object.toString());
} catch (NumberFormatException | NullPointerException ex) {
Expand All @@ -70,10 +68,9 @@ public static float toFloat(@Nullable Object object) {
}

public static double toDouble(@Nullable Object object) {
if (object instanceof Number) {
return ((Number) object).doubleValue();
if (object instanceof Number number) {
return number.doubleValue();
}

try {
return Double.parseDouble(object.toString());
} catch (NumberFormatException | NullPointerException ex) {
Expand All @@ -83,10 +80,9 @@ public static double toDouble(@Nullable Object object) {
}

public static long toLong(@Nullable Object object) {
if (object instanceof Number) {
return ((Number) object).longValue();
if (object instanceof Number number) {
return number.longValue();
}

try {
return Long.parseLong(object.toString());
} catch (NumberFormatException | NullPointerException ex) {
Expand All @@ -96,10 +92,9 @@ public static long toLong(@Nullable Object object) {
}

public static short toShort(@Nullable Object object) {
if (object instanceof Number) {
return ((Number) object).shortValue();
if (object instanceof Number number) {
return number.shortValue();
}

try {
return Short.parseShort(object.toString());
} catch (NumberFormatException | NullPointerException ex) {
Expand All @@ -109,10 +104,9 @@ public static short toShort(@Nullable Object object) {
}

public static byte toByte(@Nullable Object object) {
if (object instanceof Number) {
return ((Number) object).byteValue();
if (object instanceof Number number) {
return number.byteValue();
}

try {
return Byte.parseByte(object.toString());
} catch (NumberFormatException | NullPointerException ex) {
Expand Down
Loading

0 comments on commit 3d0ac11

Please sign in to comment.