Add jar validate feature

This commit is contained in:
Ghost_chu committed 2021-02-25 17:46:44 +08:00
1 parent dcbb93e62f
commit 8d846ee98d
6 files changed
+144 -18

No files matched your search

@@ -678,13 +678,13 @@ public class QuickShop extends JavaPlugin {
environmentChecker = new org.maxgamer.quickshop.util.envcheck.EnvironmentChecker(this);
ResultReport resultReport = environmentChecker.run();
if (resultReport.getFinalResult().ordinal() > CheckResult.WARNING.ordinal()) {
StringBuilder builder = new StringBuilder();
StringJoiner joiner = new StringJoiner("\n", "", "");
for (Entry<EnvCheckEntry, ResultContainer> result : resultReport.getResults().entrySet()) {
if (result.getValue().getResult().ordinal() > CheckResult.WARNING.ordinal()) {
builder.append(String.format("- [%s/%s] %s", result.getKey().name(), result.getValue().getResult().getDisplay(), result.getValue().getResultMessage())).append("\n");
joiner.add(String.format("- [%s/%s] %s", result.getValue().getResult().getDisplay(), result.getKey().name(), result.getValue().getResultMessage()));
}
}
bootError = new BootError(this.getLogger(), builder.toString());
bootError = new BootError(this.getLogger(), joiner.toString());
//noinspection ConstantConditions
getCommand("qs").setTabCompleter(this); //Disable tab completer
return;
@@ -650,9 +650,10 @@ public class ContainerShop implements Shop {
@Override
public void setSignText(@NotNull String[] lines) {
Util.ensureThread(false);
for (Sign sign : this.getSigns()) {
List<Sign> signs = this.getSigns();
for (Sign sign : signs) {
if (Arrays.equals(sign.getLines(), lines)) {
//Util.debugLog("Skipped new sign text setup: Same content");
Util.debugLog("Skipped new sign text setup: Same content");
continue;
}
for (int i = 0; i < lines.length; i++) {
@@ -661,6 +662,11 @@ public class ContainerShop implements Shop {
sign.update(true);
Bukkit.getPluginManager().callEvent(new ShopSignUpdateEvent(this, sign));
}
Util.debugLog("Updated " + signs.size() + " signs. ");
Util.debugLog("Content " + Util.array2String(lines) + ". ");
//Update the recognize method after converted
if (getShopVersion() == 0) {
setShopVersion(1);
@@ -20,7 +20,7 @@
package org.maxgamer.quickshop.util.envcheck;
public enum CheckResult {
PASSED("Pass"), WARNING("Warning"), STOP_WORKING("Error"), DISABLE_PLUGIN("Fatal");
PASSED("Pass"), WARNING("Warning"), STOP_WORKING("Error"), DISABLE_PLUGIN("Fatal"), KILL_SERVER("Kill");
private final String display;
CheckResult(String display) {
@@ -29,14 +29,19 @@ import org.bukkit.plugin.RegisteredListener;
import org.jetbrains.annotations.NotNull;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.util.GameVersion;
import org.maxgamer.quickshop.util.JsonUtil;
import org.maxgamer.quickshop.util.ReflectFactory;
import org.maxgamer.quickshop.util.Util;
import org.maxgamer.quickshop.util.security.JarVerifyTool;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
public class EnvironmentChecker {
@@ -151,11 +156,51 @@ public class EnvironmentChecker {
}
}
@EnvCheckEntry(name = "Signature Verify", priority = 0)
public ResultContainer securityVerify() {
JarVerifyTool tool = new JarVerifyTool();
try {
ClassLoader loader = this.getClass().getClassLoader();
if (loader.getResourceAsStream("META-INF/MANIFEST.MF") == null
|| loader.getResourceAsStream("META-INF/SELFSIGN.DSA") == null
|| loader.getResourceAsStream("META-INF/SELFSIGN.SF") == null) {
plugin.getLogger().warning("The signature not exists in QuickShop jar. The jar has been modified or you're running custom build.");
return new ResultContainer(CheckResult.STOP_WORKING, "Security risk detected, QuickShop jar has been modified.");
}
String jarPath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
Util.debugLog("JarPath selected: " + jarPath);
JarFile jarFile = new JarFile(jarPath);
List<JarEntry> modifiedEntry = tool.verify(jarFile);
if (modifiedEntry.isEmpty()) {
return new ResultContainer(CheckResult.PASSED, "The jar is valid. No issues detected");
} else {
modifiedEntry.forEach(jarEntry -> {
plugin.getLogger().warning(">> Modified Class Detected <<");
plugin.getLogger().warning("Name: " + jarEntry.getName());
plugin.getLogger().warning("CRC: " + jarEntry.getCrc());
plugin.getLogger().warning(JsonUtil.getGson().toJson(jarEntry));
});
plugin.getLogger().severe("QuickShop detected the jar has been modified. This usually caused by the file downloaded damaged or virus infected. You should run virus scanning to prevent virus infected.");
plugin.getLogger().severe("To prevent serve server fail, QuickShop has been disabled.");
return new ResultContainer(CheckResult.STOP_WORKING, "Security risk detected, QuickShop jar has been modified.");
}
} catch (IOException ioException) {
plugin.getLogger().log(Level.WARNING, "ALERT: QuickShop cannot validate itself. This may caused by your have deleted QuickShop's jar while server running.", ioException);
return new ResultContainer(CheckResult.WARNING, "Failed to validate file digital signature, Security ");
}
//tool.verify()
}
@EnvCheckEntry(name = "EnvChecker SelfTest", priority = 1)
public ResultContainer selfTest() {
return new ResultContainer(CheckResult.PASSED, "I'm fine :)");
}
@SneakyThrows
@EnvCheckEntry(name = "Java Runtime Environment Version Test", priority = 1)
public ResultContainer jrevTest() {
@@ -0,0 +1,53 @@
/*
* This file is a part of project QuickShop, the name is JarVerifyTool.java
* Copyright (C) PotatoCraft Studio and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.maxgamer.quickshop.util.security;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarVerifyTool {
@NotNull
public List<JarEntry> verify(@NotNull JarFile jar) throws IOException {
List<JarEntry> modified = new ArrayList<>();
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
try {
byte[] buffer = new byte[8192];
InputStream is = jar.getInputStream(entry);
//noinspection StatementWithEmptyBody
while ((is.read(buffer, 0, buffer.length)) != -1) {
// We just read. This will throw a SecurityException
// if a signature/digest check fails.
}
} catch (SecurityException se) {
modified.add(entry);
}
}
return modified;
}
}