|
| 1 | +package uwu.narumi.deobfuscator.core.other.impl.sb27; |
| 2 | + |
| 3 | +import org.objectweb.asm.tree.AbstractInsnNode; |
| 4 | +import org.objectweb.asm.tree.LdcInsnNode; |
| 5 | +import org.objectweb.asm.tree.MethodInsnNode; |
| 6 | +import uwu.narumi.deobfuscator.api.asm.MethodContext; |
| 7 | +import uwu.narumi.deobfuscator.api.asm.MethodRef; |
| 8 | +import uwu.narumi.deobfuscator.api.asm.matcher.Match; |
| 9 | +import uwu.narumi.deobfuscator.api.asm.matcher.group.SequenceMatch; |
| 10 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.FieldMatch; |
| 11 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.FrameMatch; |
| 12 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.MethodMatch; |
| 13 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.NumberMatch; |
| 14 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.OpcodeMatch; |
| 15 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.StringMatch; |
| 16 | +import uwu.narumi.deobfuscator.api.transformer.Transformer; |
| 17 | + |
| 18 | +import javax.crypto.BadPaddingException; |
| 19 | +import javax.crypto.Cipher; |
| 20 | +import javax.crypto.IllegalBlockSizeException; |
| 21 | +import javax.crypto.NoSuchPaddingException; |
| 22 | +import javax.crypto.spec.SecretKeySpec; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.security.InvalidKeyException; |
| 25 | +import java.security.MessageDigest; |
| 26 | +import java.security.NoSuchAlgorithmException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Base64; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +/** |
| 35 | + * Transformer that decrypts strings obfuscated by Superblaubeere27. |
| 36 | + */ |
| 37 | +public class SuperblaubeereStringTransformer extends Transformer { |
| 38 | + /* |
| 39 | + invokevirtual java/lang/String.getBytes (Ljava/nio/charset/Charset;)[B |
| 40 | + invokevirtual java/security/MessageDigest.digest ([B)[B |
| 41 | + ldc "Blowfish" |
| 42 | + invokespecial javax/crypto/spec/SecretKeySpec.<init> ([BLjava/lang/String;)V |
| 43 | + astore v2 |
| 44 | + ldc "Blowfish" |
| 45 | + invokestatic javax/crypto/Cipher.getInstance (Ljava/lang/String;)Ljavax/crypto/Cipher; |
| 46 | + */ |
| 47 | + private static final Match STRING_DECRYPT_BLOWFISH_MATCH = SequenceMatch.of( |
| 48 | + MethodMatch.invokeVirtual().owner("java/lang/String").name("getBytes").desc("(Ljava/nio/charset/Charset;)[B"), |
| 49 | + MethodMatch.invokeVirtual().owner("java/security/MessageDigest").name("digest").desc("([B)[B"), |
| 50 | + StringMatch.of("Blowfish"), |
| 51 | + MethodMatch.invokeSpecial().owner("javax/crypto/spec/SecretKeySpec").name("<init>").desc("([BLjava/lang/String;)V") |
| 52 | + ); |
| 53 | + |
| 54 | + /* |
| 55 | + invokevirtual java/lang/String.getBytes (Ljava/nio/charset/Charset;)[B |
| 56 | + invokevirtual java/security/MessageDigest.digest ([B)[B |
| 57 | + bipush 8 |
| 58 | + invokestatic java/util/Arrays.copyOf ([BI)[B |
| 59 | + ldc "DES" |
| 60 | + invokespecial javax/crypto/spec/SecretKeySpec.<init> ([BLjava/lang/String;)V |
| 61 | + astore v2 |
| 62 | + ldc "DES" |
| 63 | + invokestatic javax/crypto/Cipher.getInstance (Ljava/lang/String;)Ljavax/crypto/Cipher; |
| 64 | + */ |
| 65 | + private static final Match STRING_DECRYPT_DES_MATCH = SequenceMatch.of( |
| 66 | + MethodMatch.invokeVirtual().owner("java/lang/String").name("getBytes").desc("(Ljava/nio/charset/Charset;)[B"), |
| 67 | + MethodMatch.invokeVirtual().owner("java/security/MessageDigest").name("digest").desc("([B)[B"), |
| 68 | + NumberMatch.of(8), |
| 69 | + MethodMatch.invokeStatic().owner("java/util/Arrays").name("copyOf").desc("([BI)[B"), |
| 70 | + StringMatch.of("DES"), |
| 71 | + MethodMatch.invokeSpecial().owner("javax/crypto/spec/SecretKeySpec").name("<init>").desc("([BLjava/lang/String;)V") |
| 72 | + ); |
| 73 | + |
| 74 | + /* |
| 75 | + new java/lang/String |
| 76 | + dup |
| 77 | + invokestatic java/util/Base64.getDecoder ()Ljava/util/Base64$Decoder; |
| 78 | + aload p0 |
| 79 | + getstatic java/nio/charset/StandardCharsets.UTF_8 Ljava/nio/charset/Charset; |
| 80 | + invokevirtual java/lang/String.getBytes (Ljava/nio/charset/Charset;)[B |
| 81 | + invokevirtual java/util/Base64$Decoder.decode ([B)[B |
| 82 | + getstatic java/nio/charset/StandardCharsets.UTF_8 Ljava/nio/charset/Charset; |
| 83 | + invokespecial java/lang/String.<init> ([BLjava/nio/charset/Charset;)V |
| 84 | + */ |
| 85 | + private static final Match STRING_DECRYPT_XOR_MATCH = SequenceMatch.of( |
| 86 | + OpcodeMatch.of(NEW), |
| 87 | + OpcodeMatch.of(DUP), |
| 88 | + MethodMatch.invokeStatic().owner("java/util/Base64").name("getDecoder").desc("()Ljava/util/Base64$Decoder;"), |
| 89 | + OpcodeMatch.of(ALOAD), |
| 90 | + FieldMatch.getStatic(), |
| 91 | + MethodMatch.invokeVirtual().owner("java/lang/String").name("getBytes").desc("(Ljava/nio/charset/Charset;)[B"), |
| 92 | + MethodMatch.invokeVirtual().owner("java/util/Base64$Decoder").name("decode").desc("([B)[B"), |
| 93 | + FieldMatch.getStatic(), |
| 94 | + MethodMatch.invokeSpecial().owner("java/lang/String").name("<init>").desc("([BLjava/nio/charset/Charset;)V") |
| 95 | + ); |
| 96 | + |
| 97 | + @Override |
| 98 | + protected void transform() throws Exception { |
| 99 | + scopedClasses().forEach(classWrapper -> { |
| 100 | + Map<MethodRef, Decryptor> decryptMethods = new HashMap<>(); |
| 101 | + |
| 102 | + // Find decrypt methods |
| 103 | + classWrapper.methods().forEach(methodNode -> { |
| 104 | + if (!methodNode.desc.equals("(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")) return; |
| 105 | + |
| 106 | + MethodContext methodContext = MethodContext.of(classWrapper, methodNode); |
| 107 | + if (STRING_DECRYPT_BLOWFISH_MATCH.findFirstMatch(methodContext) != null) { |
| 108 | + decryptMethods.put(MethodRef.of(classWrapper.classNode(), methodNode), SuperblaubeereStringTransformer::decryptStringBlowfish); |
| 109 | + } else if (STRING_DECRYPT_DES_MATCH.findFirstMatch(methodContext) != null) { |
| 110 | + decryptMethods.put(MethodRef.of(classWrapper.classNode(), methodNode), SuperblaubeereStringTransformer::decryptStringDES); |
| 111 | + } else if (STRING_DECRYPT_XOR_MATCH.findFirstMatch(methodContext) != null) { |
| 112 | + decryptMethods.put(MethodRef.of(classWrapper.classNode(), methodNode), SuperblaubeereStringTransformer::decryptXOR); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + if (decryptMethods.isEmpty()) { |
| 117 | + // No decrypt method found |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + Match STRING_DECRYPT_MATCH = MethodMatch.invokeStatic().and(Match.of(ctx -> decryptMethods.containsKey(MethodRef.of((MethodInsnNode)ctx.insn())))) |
| 122 | + .and(FrameMatch.stack(0, StringMatch.of().capture("key"))) |
| 123 | + .and(FrameMatch.stack(1, StringMatch.of().capture("encryptedString"))); |
| 124 | + |
| 125 | + // Decrypt all strings |
| 126 | + classWrapper.methods().forEach(methodNode -> STRING_DECRYPT_MATCH.findAllMatches(MethodContext.of(classWrapper, methodNode)).forEach(matchCtx -> { |
| 127 | + String encryptedString = matchCtx.captures().get("encryptedString").insn().asString(); |
| 128 | + String key = matchCtx.captures().get("key").insn().asString(); |
| 129 | + MethodRef decryptMethodRef = MethodRef.of((MethodInsnNode) matchCtx.insn()); |
| 130 | + |
| 131 | + //System.out.println("Found encrypted string: " + encryptedString); |
| 132 | + //System.out.println("Key: " + key); |
| 133 | + |
| 134 | + Decryptor decryptor = decryptMethods.get(decryptMethodRef); |
| 135 | + |
| 136 | + String decryptedString = decryptor.decrypt(encryptedString, key); |
| 137 | + //System.out.println(decryptedString); |
| 138 | + |
| 139 | + methodNode.instructions.set(matchCtx.insn(), new LdcInsnNode(decryptedString)); |
| 140 | + markChange(); |
| 141 | + |
| 142 | + // Cleanup |
| 143 | + Set<AbstractInsnNode> toRemove = new HashSet<>(matchCtx.collectedInsns()); |
| 144 | + toRemove.remove(matchCtx.insn()); // Remove self as it is already replaced |
| 145 | + toRemove.forEach(methodNode.instructions::remove); |
| 146 | + })); |
| 147 | + |
| 148 | + // Remove decrypt methods |
| 149 | + classWrapper.methods().removeIf(methodNode -> decryptMethods.containsKey(MethodRef.of(classWrapper.classNode(), methodNode))); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + private static String decryptStringBlowfish(String encryptedString, String key) { |
| 154 | + try { |
| 155 | + SecretKeySpec secretKeySpec = new SecretKeySpec(MessageDigest.getInstance("MD5").digest(key.getBytes(StandardCharsets.UTF_8)), "Blowfish"); |
| 156 | + Cipher cipher = Cipher.getInstance("Blowfish"); |
| 157 | + cipher.init(2, secretKeySpec); |
| 158 | + return new String(cipher.doFinal(Base64.getDecoder().decode(encryptedString.getBytes(StandardCharsets.UTF_8))), StandardCharsets.UTF_8); |
| 159 | + } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | |
| 160 | + InvalidKeyException e) { |
| 161 | + throw new RuntimeException(e); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static String decryptStringDES(String encryptedString, String key) { |
| 166 | + try { |
| 167 | + SecretKeySpec secretKeySpec = new SecretKeySpec(Arrays.copyOf(MessageDigest.getInstance("MD5").digest(key.getBytes(StandardCharsets.UTF_8)), 8), "DES"); |
| 168 | + Cipher cipher = Cipher.getInstance("DES"); |
| 169 | + cipher.init(2, secretKeySpec); |
| 170 | + return new String(cipher.doFinal(Base64.getDecoder().decode(encryptedString.getBytes(StandardCharsets.UTF_8))), StandardCharsets.UTF_8); |
| 171 | + } catch (Exception ex) { |
| 172 | + throw new RuntimeException(ex); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static String decryptXOR(String encryptedString, String key) { |
| 177 | + encryptedString = new String(Base64.getDecoder().decode(encryptedString.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); |
| 178 | + StringBuilder builder = new StringBuilder(); |
| 179 | + char[] keyChars = key.toCharArray(); |
| 180 | + int keyIndex = 0; |
| 181 | + |
| 182 | + for (char c : encryptedString.toCharArray()) { |
| 183 | + builder.append((char)(c ^ keyChars[keyIndex % keyChars.length])); |
| 184 | + keyIndex++; |
| 185 | + } |
| 186 | + |
| 187 | + return String.valueOf(builder); |
| 188 | + } |
| 189 | + |
| 190 | + @FunctionalInterface |
| 191 | + interface Decryptor { |
| 192 | + String decrypt(String encryptedString, String key); |
| 193 | + } |
| 194 | +} |
0 commit comments