From 8d846ee98d317094fab38f411c30c863ff03440c Mon Sep 17 00:00:00 2001
From: Ghost_chu <2908803755@qq.com>
Date: Thu, 25 Feb 2021 17:46:44 +0800
Subject: [PATCH] Add jar validate feature
---
pom.xml | 46 +++++++++++-----
.../org/maxgamer/quickshop/QuickShop.java | 6 +--
.../quickshop/shop/ContainerShop.java | 10 +++-
.../quickshop/util/envcheck/CheckResult.java | 2 +-
.../util/envcheck/EnvironmentChecker.java | 45 ++++++++++++++++
.../util/security/JarVerifyTool.java | 53 +++++++++++++++++++
6 files changed, 144 insertions(+), 18 deletions(-)
create mode 100644 src/main/java/org/maxgamer/quickshop/util/security/JarVerifyTool.java
diff --git a/pom.xml b/pom.xml
index 231e1ce13..f47a9793e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,17 +88,17 @@
false
false
false
-
-
- *:*
-
- META-INF/*.SF
- META-INF/*.DSA
- META-INF/*.RSA
- META-INF/MANIFEST.MF
-
-
-
+
+
+
+
+
+
+
+
+
+
+
org.slf4j.
@@ -178,7 +178,6 @@
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
org.maxgamer.quickshop.javaw.bootstrap.Bootstrap
-
@@ -214,6 +213,29 @@
+
+
+ org.apache.maven.plugins
+ maven-jarsigner-plugin
+ 3.0.0
+
+
+ install
+ sign
+
+ sign
+
+
+
+
+ ${project.basedir}/target/${artifactId}.jar
+ ${project.basedir}/keystore.jks
+ selfsigned
+ ${env.quickshop-signer-pwd}
+ ${env.quickshop-signer-pwd}
+
+
+
diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java
index b2d5179f3..4fe0fe0f0 100644
--- a/src/main/java/org/maxgamer/quickshop/QuickShop.java
+++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java
@@ -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 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;
diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java
index 2ffc342c6..bd506dfe5 100644
--- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java
+++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java
@@ -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 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);
diff --git a/src/main/java/org/maxgamer/quickshop/util/envcheck/CheckResult.java b/src/main/java/org/maxgamer/quickshop/util/envcheck/CheckResult.java
index 2d735d8ad..480aa7823 100644
--- a/src/main/java/org/maxgamer/quickshop/util/envcheck/CheckResult.java
+++ b/src/main/java/org/maxgamer/quickshop/util/envcheck/CheckResult.java
@@ -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) {
diff --git a/src/main/java/org/maxgamer/quickshop/util/envcheck/EnvironmentChecker.java b/src/main/java/org/maxgamer/quickshop/util/envcheck/EnvironmentChecker.java
index 161889d1e..c667825a4 100644
--- a/src/main/java/org/maxgamer/quickshop/util/envcheck/EnvironmentChecker.java
+++ b/src/main/java/org/maxgamer/quickshop/util/envcheck/EnvironmentChecker.java
@@ -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 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() {
diff --git a/src/main/java/org/maxgamer/quickshop/util/security/JarVerifyTool.java b/src/main/java/org/maxgamer/quickshop/util/security/JarVerifyTool.java
new file mode 100644
index 000000000..fc5dfbb17
--- /dev/null
+++ b/src/main/java/org/maxgamer/quickshop/util/security/JarVerifyTool.java
@@ -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 .
+ *
+ */
+
+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 verify(@NotNull JarFile jar) throws IOException {
+ List modified = new ArrayList<>();
+ Enumeration 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;
+ }
+}