|
| 1 | +package uwu.narumi.deobfuscator.core.other.impl.qprotect; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.Nullable; |
| 4 | +import org.objectweb.asm.tree.AbstractInsnNode; |
| 5 | +import org.objectweb.asm.tree.FieldInsnNode; |
| 6 | +import org.objectweb.asm.tree.IntInsnNode; |
| 7 | +import org.objectweb.asm.tree.LdcInsnNode; |
| 8 | +import org.objectweb.asm.tree.MethodInsnNode; |
| 9 | +import org.objectweb.asm.tree.MethodNode; |
| 10 | +import uwu.narumi.deobfuscator.api.asm.ClassWrapper; |
| 11 | +import uwu.narumi.deobfuscator.api.asm.FieldRef; |
| 12 | +import uwu.narumi.deobfuscator.api.asm.MethodContext; |
| 13 | +import uwu.narumi.deobfuscator.api.asm.MethodRef; |
| 14 | +import uwu.narumi.deobfuscator.api.asm.matcher.Match; |
| 15 | +import uwu.narumi.deobfuscator.api.asm.matcher.MatchContext; |
| 16 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.FieldMatch; |
| 17 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.FrameMatch; |
| 18 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.MethodMatch; |
| 19 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.NumberMatch; |
| 20 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.OpcodeMatch; |
| 21 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.StringMatch; |
| 22 | +import uwu.narumi.deobfuscator.api.transformer.Transformer; |
| 23 | +import uwu.narumi.deobfuscator.core.other.impl.universal.pool.UniversalNumberPoolTransformer; |
| 24 | + |
| 25 | +import javax.crypto.Cipher; |
| 26 | +import javax.crypto.SecretKeyFactory; |
| 27 | +import javax.crypto.spec.IvParameterSpec; |
| 28 | +import javax.crypto.spec.PBEKeySpec; |
| 29 | +import javax.crypto.spec.SecretKeySpec; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.Base64; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.HashSet; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +/** |
| 38 | + * Transforms AES encrypted strings in qProtect obfuscated code. Example here: {@link qprotect.AESStringEncryption} |
| 39 | + */ |
| 40 | +public class qProtectAESStringEncryptionTransformer extends Transformer { |
| 41 | + private static final Match DECRYPT_STRING_MATCH = MethodMatch.invokeStatic().desc("(Ljava/lang/String;Ljava/lang/String;[B)Ljava/lang/String;").capture("decrypt-method") |
| 42 | + .and(FrameMatch.stack(0, FieldMatch.getStatic().capture("iv-array"))) |
| 43 | + .and(FrameMatch.stack(1, StringMatch.of().capture("password"))) |
| 44 | + .and(FrameMatch.stack(2, StringMatch.of().capture("encrypted-data"))); |
| 45 | + |
| 46 | + private final Set<MethodRef> initIVArrayMethods = new HashSet<>(); |
| 47 | + |
| 48 | + @Override |
| 49 | + protected void transform() throws Exception { |
| 50 | + Map<FieldRef, byte[]> ivArrays = new HashMap<>(); |
| 51 | + Map<MethodRef, Integer> iterationCounts = new HashMap<>(); |
| 52 | + |
| 53 | + scopedClasses().forEach(classWrapper -> classWrapper.methods().forEach(methodNode -> { |
| 54 | + DECRYPT_STRING_MATCH.findAllMatches(MethodContext.of(classWrapper, methodNode)).forEach(matchCtx -> { |
| 55 | + // Decrypt method |
| 56 | + MethodInsnNode decryptMethodInsn = (MethodInsnNode) matchCtx.captures().get("decrypt-method").insn(); |
| 57 | + MethodRef decryptMethodRef = MethodRef.of(decryptMethodInsn); |
| 58 | + // IV array field |
| 59 | + FieldInsnNode ivArrayFieldInsn = (FieldInsnNode) matchCtx.captures().get("iv-array").insn(); |
| 60 | + FieldRef ivArrayFieldRef = FieldRef.of(ivArrayFieldInsn); |
| 61 | + String password = matchCtx.captures().get("password").insn().asString(); |
| 62 | + String encryptedData = matchCtx.captures().get("encrypted-data").insn().asString(); |
| 63 | + |
| 64 | + // Get the IV array from the field |
| 65 | + byte[] ivArray = ivArrays.computeIfAbsent(ivArrayFieldRef, (k) -> { |
| 66 | + return extractIvArray(classWrapper, ivArrayFieldRef); |
| 67 | + }); |
| 68 | + |
| 69 | + // Get iteration count |
| 70 | + int iterationCount = iterationCounts.computeIfAbsent(MethodRef.of(decryptMethodInsn), (k) -> { |
| 71 | + // Find the decrypt method |
| 72 | + MethodNode decryptMethod = classWrapper.findMethod(decryptMethodRef).orElseThrow(); |
| 73 | + return extractIterationCount(MethodContext.of(classWrapper, decryptMethod)); |
| 74 | + }); |
| 75 | + |
| 76 | + // Decrypt string |
| 77 | + String decryptedString = decryptString(encryptedData, password, ivArray, iterationCount); |
| 78 | + methodNode.instructions.insert(matchCtx.insn(), new LdcInsnNode(decryptedString)); |
| 79 | + matchCtx.removeAll(); |
| 80 | + |
| 81 | + markChange(); |
| 82 | + }); |
| 83 | + })); |
| 84 | + |
| 85 | + // Cleanup |
| 86 | + ivArrays.keySet().forEach(fieldRef -> context().removeField(fieldRef)); |
| 87 | + iterationCounts.keySet().forEach(methodRef -> context().removeMethod(methodRef)); |
| 88 | + initIVArrayMethods.forEach(methodRef -> { |
| 89 | + context().removeMethod(methodRef); |
| 90 | + // Remove invocation from <clinit> |
| 91 | + ClassWrapper classWrapper = context().getClassesMap().get(methodRef.owner()); |
| 92 | + classWrapper.findClInit().ifPresent(clinit -> { |
| 93 | + for (AbstractInsnNode insn : clinit.instructions.toArray()) { |
| 94 | + if (insn.getOpcode() == INVOKESTATIC && insn instanceof MethodInsnNode methodInsn && |
| 95 | + methodInsn.name.equals(methodRef.name()) && methodInsn.desc.equals(methodRef.desc()) && |
| 96 | + methodInsn.owner.equals(classWrapper.name()) |
| 97 | + ) { |
| 98 | + // Remove invocation |
| 99 | + clinit.instructions.remove(insn); |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + private int extractIterationCount(MethodContext decryptMethod) { |
| 107 | + /* |
| 108 | + sipush 1838 // iteration count |
| 109 | + sipush 256 |
| 110 | + invokespecial javax/crypto/spec/PBEKeySpec.<init> ([C[BII)V |
| 111 | + */ |
| 112 | + Match iterationCountMatch = MethodMatch.invokeSpecial().owner("javax/crypto/spec/PBEKeySpec").name("<init>").desc("([C[BII)V") |
| 113 | + .and(FrameMatch.stack(0, NumberMatch.of())) |
| 114 | + .and(FrameMatch.stack(1, NumberMatch.of().capture("iteration-count"))); |
| 115 | + |
| 116 | + MatchContext matchCtx = iterationCountMatch.findFirstMatch(decryptMethod); |
| 117 | + if (matchCtx == null) { |
| 118 | + throw new IllegalStateException("Could not find iteration count"); |
| 119 | + } |
| 120 | + |
| 121 | + // Get the iteration count from the match context |
| 122 | + return matchCtx.captures().get("iteration-count").insn().asInteger(); |
| 123 | + } |
| 124 | + |
| 125 | + private byte @Nullable [] extractIvArray(ClassWrapper classWrapper, FieldRef ivArrayFieldRef) { |
| 126 | + Match ivArrayMethodMatch = FieldMatch.putStatic().fieldRef(ivArrayFieldRef) |
| 127 | + .and(FrameMatch.stack(0, |
| 128 | + OpcodeMatch.of(NEWARRAY).and(Match.of(ctx -> ((IntInsnNode) ctx.insn()).operand == T_BYTE)) |
| 129 | + .and(FrameMatch.stack(0, NumberMatch.of().capture("size"))))); |
| 130 | + |
| 131 | + for (MethodNode methodNode : classWrapper.methods()) { |
| 132 | + // Find match |
| 133 | + MethodContext methodContext = MethodContext.of(classWrapper, methodNode); |
| 134 | + MatchContext ivArrayMatchCtx = ivArrayMethodMatch.findFirstMatch(methodContext); |
| 135 | + |
| 136 | + if (ivArrayMatchCtx == null) continue; |
| 137 | + |
| 138 | + int size = ivArrayMatchCtx.captures().get("size").insn().asInteger(); |
| 139 | + |
| 140 | + Number[] ivArrayObj = UniversalNumberPoolTransformer.getNumberPool(methodContext, size, ivArrayFieldRef); |
| 141 | + byte[] ivArray = new byte[size]; |
| 142 | + for (int i = 0; i < size; i++) { |
| 143 | + // Convert Number to byte |
| 144 | + ivArray[i] = ivArrayObj[i].byteValue(); |
| 145 | + } |
| 146 | + |
| 147 | + initIVArrayMethods.add(MethodRef.of(classWrapper.classNode(), methodNode)); |
| 148 | + |
| 149 | + return ivArray; |
| 150 | + } |
| 151 | + |
| 152 | + // Not found |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Decrypts a Base64 encoded string using AES encryption with a password-based key derivation function (PBKDF2). |
| 158 | + */ |
| 159 | + private String decryptString(String base64EncryptedData, String password, byte[] ivArray, int iterationCount) { |
| 160 | + try { |
| 161 | + // Decode the Base64 encoded input string |
| 162 | + byte[] decodedData = Base64.getDecoder().decode(base64EncryptedData); |
| 163 | + |
| 164 | + // Initialize salt array. This will be overwritten by the first 16 bytes of the decoded data. |
| 165 | + // The initial values here seem to be placeholders or defaults that are immediately replaced. |
| 166 | + //byte[] salt = new byte[]{124, 26, -30, -113, 87, 0, -111, -97, -126, 91, -12, 50, 77, 75, 6, -4}; // Dynamic |
| 167 | + byte[] salt = new byte[16]; |
| 168 | + |
| 169 | + // The actual encrypted content is after the first 32 bytes of the decoded data. |
| 170 | + // The first 16 bytes are used as the salt, and bytes 17-32 are skipped/unused. |
| 171 | + byte[] encryptedContent = new byte[decodedData.length - 32]; |
| 172 | + |
| 173 | + // Extract the salt from the first 16 bytes of the decoded data |
| 174 | + System.arraycopy(decodedData, 0, salt, 0, 16); |
| 175 | + // Extract the encrypted content, skipping the first 32 bytes (16 for salt, 16 unused) |
| 176 | + System.arraycopy(decodedData, 32, encryptedContent, 0, decodedData.length - 32); |
| 177 | + |
| 178 | + // Configure the Password-Based Key Derivation Function (PBKDF2) |
| 179 | + // Uses the provided password, extracted salt, an iteration count of 1278, and a key length of 256 bits. |
| 180 | + PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, 256); // Dynamic - iteration count |
| 181 | + SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 182 | + |
| 183 | + // Generate the secret key from the PBEKeySpec |
| 184 | + byte[] derivedKey = secretKeyFactory.generateSecret(pbeKeySpec).getEncoded(); |
| 185 | + |
| 186 | + // Create a SecretKeySpec for AES using the derived key |
| 187 | + SecretKeySpec secretKeySpec = new SecretKeySpec(derivedKey, "AES"); |
| 188 | + |
| 189 | + // Initialize the Cipher for AES decryption in CBC mode with PKCS5Padding |
| 190 | + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 191 | + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(ivArray)); |
| 192 | + |
| 193 | + // Perform the decryption |
| 194 | + byte[] decryptedBytes = cipher.doFinal(encryptedContent); |
| 195 | + |
| 196 | + // Convert the decrypted bytes to a String using UTF-8 encoding |
| 197 | + return new String(decryptedBytes, StandardCharsets.UTF_8); |
| 198 | + } catch (Exception e) { |
| 199 | + throw new RuntimeException(e); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments