-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPGSharpStrings.java
More file actions
154 lines (128 loc) · 4.46 KB
/
PGSharpStrings.java
File metadata and controls
154 lines (128 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package jadx.core.dex.visitors.deobf;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.core.codegen.TypeGen;
import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.ConstStringNode;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.InvokeNode;
import jadx.core.dex.instructions.PhiInsn;
import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.InsnWrapArg;
import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.instructions.args.LiteralArg;
import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.visitors.AbstractVisitor;
import jadx.core.utils.InsnList;
import jadx.core.utils.InsnRemover;
import jadx.core.utils.exceptions.JadxRuntimeException;
import java.util.Base64;
public class PGSharpStrings extends AbstractVisitor {
// To be adjusted with the PGSharp version
private static final String ENCODING_METH = new String("h.a.a.r3");
private static final String ENCODING_KEY = new String("vqGqQWCVnDRrNXTR");
private static final Logger LOG = LoggerFactory.getLogger(PGSharp.class);
public static byte[] decode(byte[] bArr) {
char[] KEY = ENCODING_KEY.toCharArray();
byte[] bArr2 = new byte[bArr.length];
for (int i = 0; i < bArr.length; i++) {
byte c = (byte)(KEY[i % ENCODING_KEY.length()] & 0xFF);
bArr2[i] = (byte) (bArr[i] ^ c);
}
return bArr2;
}
public static String decodeStr(String str) {
byte[] decodedBytes = Base64.getDecoder().decode(str);
return new String(decode(decodedBytes));
}
@Override
public void visit(MethodNode mth) {
LOG.info("Process: {}", mth.toString());
if (mth.isNoCode()) {
return;
}
for (BlockNode block : mth.getBasicBlocks()) {
simplify(mth, block);
}
}
private static String getConstString(InsnArg arg) {
if (arg.isLiteral()) {
return TypeGen.literalToRawString((LiteralArg) arg);
}
if (arg.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
if (wrapInsn instanceof ConstStringNode) {
return ((ConstStringNode) wrapInsn).getString();
}
}
return null;
}
private static InsnNode simplify(MethodNode mth, InsnNode insn) {
boolean changed = false;
for (InsnArg arg : insn.getArguments()) {
if (arg.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
InsnNode ni = simplify(mth, wrapInsn);
if (ni != null) {
arg.wrapInstruction(mth, ni);
InsnRemover.unbindInsn(mth, wrapInsn);
changed = true;
}
}
}
if (changed) {
insn.rebindArgs();
}
if (insn.getType() != InsnType.INVOKE) {
return null;
}
MethodInfo callMth = ((InvokeNode) insn).getCallMth();
if (callMth.getDeclClass().getFullName().equals(ENCODING_METH) && callMth.getName().equals("a")) {
InsnArg arg0 = insn.getArg(0);
LOG.info("simplify({})", arg0.toString());
if (arg0.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg0).getWrapInsn();
String encoded = getConstString(arg0);
if (encoded == null) {
LOG.info("Can't get string object from {}", wrapInsn.toString());
return null;
}
String decoded = decodeStr(encoded);
LOG.info("{} --> {}", encoded, decoded);
ConstStringNode constStrInsn = new ConstStringNode(decodeStr(encoded));
return constStrInsn;
}
if (arg0.isRegister()) {
LOG.info("Reg is not supported for {}", arg0.toString());
return null;
}
return null;
}
return null;
}
private static void simplify(MethodNode mth, BlockNode block) {
List<InsnNode> insns = block.getInstructions();
int size = insns.size();
for (int i = 0; i < size; i++) {
InsnNode insn = insns.get(i);
InsnNode modInsn = simplify(mth, insn);
if (modInsn != null) {
modInsn.setResult(insn.getResult());
modInsn.rebindArgs();
LOG.info("Simplify {} -> {}", insn.toString(), modInsn.toString());
if (i < insns.size() && insns.get(i) == insn) {
insns.set(i, modInsn);
} else {
int idx = InsnList.getIndex(insns, insn);
if (idx == -1) {
throw new JadxRuntimeException("Failed to replace insn");
}
insns.set(idx, modInsn);
}
}
}
}
}