From 23a4426899e6627449ac56ba973d77c1fe09cf44 Mon Sep 17 00:00:00 2001 From: 4ra1n <2023503307@qq.com> Date: Thu, 27 Jun 2024 01:46:32 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E7=B1=BB=E9=BB=91?= =?UTF-8?q?=E5=90=8D=E5=8D=95=E6=96=B0=E5=A2=9E=E6=AD=A3=E5=88=99=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=20`classBlackRegexList`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.MD | 2 ++ .../jar/obfuscator/config/BaseConfig.java | 19 ++++++++++++++----- .../jar/obfuscator/utils/PackageUtil.java | 10 ++++++++++ src/main/resources/config.yaml | 4 ++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 141f912..b2293fe 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -9,8 +9,10 @@ 更新日志: - [功能] 支持配置随机数来源为 `CPU` 的 `RDRAND` 指令 +- [功能] 类黑名单新增正则配置 `classBlackRegexList` - [功能] 对于错误和默认的 `AES KEY` 配置自动生成随机的 - [BUG] 不应该对 `JNI` 类进行混淆 +- [BUG] 处理错误配置文件可能导致的异常 感谢以下用户的贡献: diff --git a/src/main/java/me/n1ar4/jar/obfuscator/config/BaseConfig.java b/src/main/java/me/n1ar4/jar/obfuscator/config/BaseConfig.java index 5c7eef8..dba0900 100644 --- a/src/main/java/me/n1ar4/jar/obfuscator/config/BaseConfig.java +++ b/src/main/java/me/n1ar4/jar/obfuscator/config/BaseConfig.java @@ -35,6 +35,7 @@ public class BaseConfig { private String[] obfuscateChars; private String[] methodBlackList; private String[] classBlackList; + private String[] classBlackRegexList; private String[] rootPackages; private String mainClass; private boolean modifyManifest; @@ -45,6 +46,14 @@ public class BaseConfig { private boolean useCpuRDRAND; + public String[] getClassBlackRegexList() { + return classBlackRegexList != null ? classBlackRegexList : new String[0]; + } + + public void setClassBlackRegexList(String[] classBlackRegexList) { + this.classBlackRegexList = classBlackRegexList; + } + public boolean isUseCpuRDRAND() { return useCpuRDRAND; } @@ -94,7 +103,7 @@ public void setDecryptMethodName(String decryptMethodName) { } public String[] getRootPackages() { - return rootPackages; + return rootPackages!=null ? rootPackages : new String[0]; } public void setRootPackages(String[] rootPackages) { @@ -102,7 +111,7 @@ public void setRootPackages(String[] rootPackages) { } public String[] getClassBlackList() { - return classBlackList; + return classBlackList != null ? classBlackList : new String[0]; } public void setClassBlackList(String[] classBlackList) { @@ -126,7 +135,7 @@ public void setStringAesKey(String stringAesKey) { } public String[] getMethodBlackList() { - return methodBlackList; + return methodBlackList!=null ? methodBlackList : new String[0]; } public void setMethodBlackList(String[] methodBlackList) { @@ -288,7 +297,7 @@ public void setMaxJunkOneClass(int maxJunkOneClass) { } public String[] getObfuscatePackage() { - return obfuscatePackage; + return obfuscatePackage!=null ? obfuscatePackage : new String[0]; } public void setObfuscatePackage(String[] obfuscatePackage) { @@ -296,7 +305,7 @@ public void setObfuscatePackage(String[] obfuscatePackage) { } public String[] getObfuscateChars() { - return obfuscateChars; + return obfuscateChars!=null ? obfuscateChars : new String[0]; } public void setObfuscateChars(String[] obfuscateChars) { diff --git a/src/main/java/me/n1ar4/jar/obfuscator/utils/PackageUtil.java b/src/main/java/me/n1ar4/jar/obfuscator/utils/PackageUtil.java index a1524d2..6e6d36b 100644 --- a/src/main/java/me/n1ar4/jar/obfuscator/utils/PackageUtil.java +++ b/src/main/java/me/n1ar4/jar/obfuscator/utils/PackageUtil.java @@ -9,6 +9,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class PackageUtil { private static final List internalList = new ArrayList<>(); @@ -48,6 +50,14 @@ public static boolean inBlackClass(String className, BaseConfig config) { return true; } } + for (String s : config.getClassBlackRegexList()) { + className = className.replace(".", "/"); + Pattern pattern = Pattern.compile(s, Pattern.DOTALL); + Matcher matcher = pattern.matcher(className); + if (matcher.matches()) { + return true; + } + } for (String s : internalList) { s = s.replace(".", "/"); if (className.equals(s)) { diff --git a/src/main/resources/config.yaml b/src/main/resources/config.yaml index 69d7ecc..17f9b4f 100644 --- a/src/main/resources/config.yaml +++ b/src/main/resources/config.yaml @@ -31,6 +31,10 @@ rootPackages: [ me.n1ar4, org.n1ar4 ] # 不对某些类做混淆(不混淆其中的所有内容) # 例如反射调用/JAVAFX FXML绑定等情况 classBlackList: [ javafx.controller.DemoController ] +# 不对指定正则的类进行混淆 +# 注意这里的类名匹配是 java/lang/String 而不是 java.lang.String +# 该配置和 classBlackList 同时生效 +classBlackRegexList: [ java/.*, com/intellij/.* ] # 不对某些 method 名做混淆 正则 # visit.* 忽略 JAVA ASM 的 visitCode visitMethod 等方法 # start.* 忽略 JAVAFX 因为启动基于 start 方法