|
| 1 | +package uwu.narumi.deobfuscator.core.other.impl.qprotect; |
| 2 | + |
| 3 | +import org.objectweb.asm.tree.LdcInsnNode; |
| 4 | +import org.objectweb.asm.tree.MethodInsnNode; |
| 5 | +import org.objectweb.asm.tree.MethodNode; |
| 6 | +import org.objectweb.asm.tree.TableSwitchInsnNode; |
| 7 | +import uwu.narumi.deobfuscator.api.asm.MethodContext; |
| 8 | +import uwu.narumi.deobfuscator.api.asm.MethodRef; |
| 9 | +import uwu.narumi.deobfuscator.api.asm.matcher.Match; |
| 10 | +import uwu.narumi.deobfuscator.api.asm.matcher.MatchContext; |
| 11 | +import uwu.narumi.deobfuscator.api.asm.matcher.group.SequenceMatch; |
| 12 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.*; |
| 13 | +import uwu.narumi.deobfuscator.api.transformer.Transformer; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.atomic.AtomicReference; |
| 20 | + |
| 21 | +/** |
| 22 | + * Transforms encrypted strings in qProtect obfuscated code. Example here: {@link qprotect.StringStrongEncryption} |
| 23 | + */ |
| 24 | +public class qProtectStrongStringTransformer extends Transformer { |
| 25 | + |
| 26 | + private static final Match DECRYPT_STRING_MATCH = MethodMatch.invokeStatic().desc("(II)Ljava/lang/String;") |
| 27 | + .and(FrameMatch.stack(0, NumberMatch.numInteger().capture("salt2"))) |
| 28 | + .and(FrameMatch.stack(1, NumberMatch.numInteger().capture("salt1"))); |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void transform() throws Exception { |
| 32 | + scopedClasses().forEach(classWrapper -> { |
| 33 | + AtomicReference<MethodRef> decryptionMethodRef = new AtomicReference<>(); |
| 34 | + |
| 35 | + Map<Integer, String> encryptedArray = new HashMap<>(); |
| 36 | + |
| 37 | + classWrapper.findClInit().ifPresent(clinit -> { |
| 38 | + if (clinit.instructions.size() > 3 && clinit.instructions.get(0).isNumber()) { |
| 39 | + MethodContext clinitContext = MethodContext.of(classWrapper, clinit); |
| 40 | + SequenceMatch.of(OpcodeMatch.of(DUP), NumberMatch.of().capture("array-index"), StringMatch.of().capture("encrypted-value"), OpcodeMatch.of(AASTORE)).findAllMatches(clinitContext).forEach(matchContext -> { |
| 41 | + encryptedArray.put(matchContext.captures().get("array-index").insn().asInteger(), matchContext.captures().get("encrypted-value").insn().asString()); |
| 42 | + matchContext.removeAll(); |
| 43 | + }); |
| 44 | + } |
| 45 | + }); |
| 46 | + if (encryptedArray.isEmpty()) return; |
| 47 | + |
| 48 | + String[] decryptedArray = new String[encryptedArray.size()]; |
| 49 | + |
| 50 | + classWrapper.methods().forEach(methodNode -> { |
| 51 | + DECRYPT_STRING_MATCH.findAllMatches(MethodContext.of(classWrapper, methodNode)).forEach(matchContext -> { |
| 52 | + int salt1 = matchContext.captures().get("salt1").insn().asInteger(); |
| 53 | + int salt2 = matchContext.captures().get("salt2").insn().asInteger(); |
| 54 | + |
| 55 | + MethodInsnNode decryptMethodInsn = (MethodInsnNode) matchContext.insn(); |
| 56 | + decryptionMethodRef.set(MethodRef.of(decryptMethodInsn)); |
| 57 | + |
| 58 | + MethodNode decryptionMethod = findMethod(classWrapper.classNode(), MethodRef.of(decryptMethodInsn)).orElseThrow(); |
| 59 | + |
| 60 | + MethodContext decryptedMethodContext = MethodContext.of(classWrapper, decryptionMethod); |
| 61 | + List<MatchContext> numbers = NumberMatch.of().findAllMatches(decryptedMethodContext); |
| 62 | + |
| 63 | + int swappedKey = -1; |
| 64 | + |
| 65 | + int[] numbersArray = new int[2]; |
| 66 | + for (int i = 0, hit = 0; i < numbers.size() && hit < 2; i++) { |
| 67 | + if (numbers.get(i).insn().asInteger() != -1) { |
| 68 | + numbersArray[hit] = numbers.get(i).insn().asInteger(); |
| 69 | + hit++; |
| 70 | + } |
| 71 | + } |
| 72 | + int number1 = numbersArray[0]; |
| 73 | + int number2 = numbersArray[1]; |
| 74 | + |
| 75 | + if (!encryptedArray.containsKey(swappedKey)) { |
| 76 | + int key = salt1; |
| 77 | + swappedKey = (~(key = (key | number1) & (~key | number2)) | 0xFFFF) - ~key; |
| 78 | + } |
| 79 | + if (!encryptedArray.containsKey(swappedKey)) { |
| 80 | + int key = salt1; |
| 81 | + swappedKey = (~(key = key & number1 | ~key & number2) | 0xFFFF) - ~key; |
| 82 | + } |
| 83 | + if (!encryptedArray.containsKey(swappedKey)) { |
| 84 | + int key = salt1; |
| 85 | + swappedKey = (~(key = (key | number1) & ~(key & number2)) | 0xFFFF) - ~key; |
| 86 | + } |
| 87 | + |
| 88 | + if (!encryptedArray.containsKey(swappedKey)) { |
| 89 | + int key = salt1; |
| 90 | + swappedKey = (~(key = (key | number1) - (key & number1)) | 0xFFFF) - ~key; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + if (!encryptedArray.containsKey(swappedKey)) { |
| 95 | + System.out.println(swappedKey); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + int n3 = encryptedArray.get(swappedKey).toCharArray()[0]; |
| 101 | + int switchCaseKey = (~n3 | 0xFF) - ~n3; |
| 102 | + int switchReturnKey; |
| 103 | + TableSwitchInsnNode table = (TableSwitchInsnNode) Match.of(ctx -> ctx.insn() instanceof TableSwitchInsnNode).findFirstMatch(decryptedMethodContext).insn(); |
| 104 | + if (switchCaseKey >= table.min && switchCaseKey <= table.max) { |
| 105 | + switchReturnKey = table.labels.get(switchCaseKey).getNext().asNumber().intValue(); |
| 106 | + } else { |
| 107 | + switchReturnKey = table.dflt.getNext().asNumber().intValue(); |
| 108 | + } |
| 109 | + |
| 110 | + String decryptedString = decrypt(encryptedArray.get(swappedKey), decryptedArray, salt1, salt2, swappedKey, switchReturnKey); |
| 111 | + methodNode.instructions.insert(matchContext.insn(), new LdcInsnNode(decryptedString)); |
| 112 | + matchContext.removeAll(); |
| 113 | + |
| 114 | + markChange(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + // Remove decryption method |
| 119 | + if (decryptionMethodRef.get() != null) { |
| 120 | + classWrapper.methods().removeIf(methodNode -> methodNode.name.equals(decryptionMethodRef.get().name()) && methodNode.desc.equals(decryptionMethodRef.get().desc())); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + /* |
| 125 | + * n pierwszy klucz |
| 126 | + * n2 drugi klucz |
| 127 | + * n4 klucz rozszyfrowany po pierwszej matematyce losowej |
| 128 | + * n5 zwrocony przez switch klucz |
| 129 | + * */ |
| 130 | + private static String decrypt(String encryptedString, String[] decryptedArray, int n, int n2, int n4, int n5) { |
| 131 | + int n3 = n; |
| 132 | + if (decryptedArray[n4] == null) { |
| 133 | + char[] cArray = encryptedString.toCharArray(); |
| 134 | + |
| 135 | + n3 = (short)n2; |
| 136 | + int n6 = (~n3 | 0xFF) - ~n3 + ~n5 + 1; |
| 137 | + if (n6 < 0) { |
| 138 | + n6 += 256; |
| 139 | + } |
| 140 | + n3 = (short)n2; |
| 141 | + int n7 = n5; |
| 142 | + int n8 = ((n3 = (~n3 | 0xFFFF) - ~n3 >>> 8) & ~n7) - (~n3 & n7); |
| 143 | + if (n8 < 0) { |
| 144 | + n8 += 256; |
| 145 | + } |
| 146 | + int n9 = 0; |
| 147 | + while (n9 < cArray.length) { |
| 148 | + int n10 = n9 % 2; |
| 149 | + int n11 = n9; |
| 150 | + char[] cArray2 = cArray; |
| 151 | + int n12 = cArray[n11]; |
| 152 | + if (n10 == 0) { |
| 153 | + n7 = n6; |
| 154 | + n3 = n12; |
| 155 | + cArray2[n11] = (char)(n3 & ~n7 | ~n3 & n7); |
| 156 | + n7 = n6 << 5; |
| 157 | + n3 = n6 >>> 3; |
| 158 | + int n13 = (n3 & ~n7) + n7; |
| 159 | + n7 = cArray[n9]; |
| 160 | + n3 = n13; |
| 161 | + n3 = (n3 | n7) & (~n3 | ~n7); |
| 162 | + n6 = (~n3 | 0xFF) - ~n3; |
| 163 | + } else { |
| 164 | + n7 = n8; |
| 165 | + n3 = n12; |
| 166 | + cArray2[n11] = (char)(n3 & ~n7 | ~n3 & n7); |
| 167 | + n7 = n8 << 5; |
| 168 | + n3 = n8 >>> 3; |
| 169 | + int n14 = (n3 & ~n7) + n7; |
| 170 | + n7 = cArray[n9]; |
| 171 | + n3 = n14; |
| 172 | + n3 = (n3 | n7) - (n3 & n7); |
| 173 | + n8 = (~n3 | 0xFF) - ~n3; |
| 174 | + } |
| 175 | + ++n9; |
| 176 | + } |
| 177 | + decryptedArray[n4] = new String(cArray).intern(); |
| 178 | + } |
| 179 | + return decryptedArray[n4]; |
| 180 | + } |
| 181 | +} |
0 commit comments