Skip to content

Commit

Permalink
[优化] 优化 AES KEY 提到全局变量并支持配置
Browse files Browse the repository at this point in the history
  • Loading branch information
4ra1n committed Dec 17, 2024
1 parent b69cc3c commit bfc756a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
更新日志:

- [BUG] 修复之前版本参数混淆功能实际没有生效
- [优化] 优化 `AES KEY` 提到全局变量并支持配置
- [优化] 配置文件支持关闭某些 `ASM` 选项提高兼容性

感谢以下用户的贡献:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ enableAES: true
aesKey: OBF_DEFAULT_KEYS
# AES 解密方法名
aesDecName: iiLLiLi
# AES KEY 字段名
aesKeyField: iiiLLLi1i

# 是否启用全局字符串提取混淆
enableAdvanceString: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.n1ar4.clazz.obfuscator.asm;

import me.n1ar4.clazz.obfuscator.Const;
import me.n1ar4.clazz.obfuscator.core.ObfEnv;
import me.n1ar4.clazz.obfuscator.utils.AESUtil;
import me.n1ar4.templates.AESTemplates;
import org.objectweb.asm.*;
Expand All @@ -20,6 +21,14 @@ public StringEncryptChanger(ClassVisitor classVisitor, String aesKey, String aes
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
this.className = name;
super.visit(version, access, name, signature, superName, interfaces);
FieldVisitor fieldVisitor = this.cv.visitField(
// private static final String [AES KEY] = "AES KEY";
Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL,
ObfEnv.config.getAesKeyField(),
"Ljava/lang/String;",
null,
ObfEnv.config.getAesKey());
fieldVisitor.visitEnd();
}

@Override
Expand Down Expand Up @@ -161,7 +170,8 @@ public void visitLdcInsn(Object value) {
if (value instanceof String) {
try {
mv.visitLdcInsn(AESTemplates.encrypt((String) value, this.aesKey));
mv.visitLdcInsn(this.aesKey);
mv.visitFieldInsn(Opcodes.GETSTATIC, className,
ObfEnv.config.getAesKeyField(), "Ljava/lang/String;");
mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
className,
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/me/n1ar4/clazz/obfuscator/config/BaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class BaseConfig {

private String aesKey;
private String aesDecName;
private String aesKeyField;
private String[] obfuscateChars;
private String[] methodBlackList;

Expand All @@ -44,6 +45,7 @@ public static BaseConfig Default() {
config.setEnableXOR(true);
config.setEnableAES(true);
config.setAesKey("OBF_DEFAULT_KEYS");
config.setAesKeyField("iiiLLLi1i");
config.setAesDecName("iiLLiLi");
config.setEnableDeleteCompileInfo(true);
config.setEnableParamName(true);
Expand Down Expand Up @@ -86,6 +88,8 @@ public void show() {
ColorUtil.green(String.valueOf(enableAES)));
System.out.println(ColorUtil.yellow("AES Decrypt KEY-> ") +
ColorUtil.green(String.valueOf(aesKey)));
System.out.println(ColorUtil.yellow("AES KEY Field-> ") +
ColorUtil.green(String.valueOf(aesKeyField)));
System.out.println(ColorUtil.yellow("AES Decrypt Method-> ") +
ColorUtil.green(String.valueOf(aesDecName)));
System.out.println(ColorUtil.yellow("Enable Advance String Obfuscate -> ") +
Expand All @@ -100,6 +104,14 @@ public void show() {
ColorUtil.green(String.valueOf(maxJunkOneClass)));
}

public String getAesKeyField() {
return aesKeyField;
}

public void setAesKeyField(String aesKeyField) {
this.aesKeyField = aesKeyField;
}

public boolean isAsmAutoCompute() {
return asmAutoCompute;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static boolean initConfig(BaseConfig config) {
// field / method 都不应该包含这个字符串
NameUtil.exclude(config.getAdvanceStringName());
NameUtil.exclude(config.getAesDecName());
NameUtil.exclude(config.getAesKeyField());
ObfEnv.ADVANCE_STRING_NAME = config.getAdvanceStringName();

return true;
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ enableAES: true
aesKey: OBF_DEFAULT_KEYS
# AES 解密方法名
aesDecName: iiLLiLi
# AES KEY 字段名
aesKeyField: iiiLLLi1i

# 是否启用全局字符串提取混淆
enableAdvanceString: true
Expand Down

0 comments on commit bfc756a

Please sign in to comment.