/
PhantomResolvingJarDumper.java
259 lines (227 loc) · 7.99 KB
/
PhantomResolvingJarDumper.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package dev.skidfuscator.obfuscator.phantom.jphantom;
import com.google.common.collect.Lists;
import dev.skidfuscator.obfuscator.Skidfuscator;
import dev.skidfuscator.obfuscator.skidasm.SkidClassNode;
import dev.skidfuscator.obfuscator.util.ProgressUtil;
import me.tongfei.progressbar.ProgressBar;
import org.mapleir.app.service.ApplicationClassSource;
import org.mapleir.app.service.ClassTree;
import org.mapleir.asm.ClassHelper;
import org.mapleir.asm.ClassNode;
import org.mapleir.asm.MethodNode;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.topdank.byteengineer.commons.data.JarClassData;
import org.topdank.byteengineer.commons.data.JarContents;
import org.topdank.byteengineer.commons.data.JarResource;
import org.topdank.byteio.out.JarDumper;
import org.topdank.byteio.util.Debug;
import javax.crypto.BadPaddingException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipError;
import java.util.zip.ZipException;
/**
* Dumps ClassNodes and JarResources back into a file on the local system.
* Todo: make this extend CompleteJarDumper?
*
* @author Bibl
*/
public class PhantomResolvingJarDumper implements JarDumper {
private final Skidfuscator skidfuscator;
private final JarContents contents;
private final ApplicationClassSource source;
/**
* Creates a new JarDumper.
*
* @param contents Contents of jar.
*/
public PhantomResolvingJarDumper(Skidfuscator skidfuscator, JarContents contents, ApplicationClassSource source) {
this.skidfuscator = skidfuscator;
this.contents = contents;
this.source = source;
}
/**
* Dumps the jars contents.
*
* @param file File to dump it to.
*/
@Override
public void dump(File file) throws IOException {
if (file.exists())
file.delete();
file.createNewFile();
JarOutputStream jos = new JarOutputStream(new FileOutputStream(file));
int classesDumped = 0;
int resourcesDumped = 0;
try (ProgressBar progressBar = ProgressUtil.progress(contents.getClassContents().size() + contents.getResourceContents().size())) {
for (JarClassData cn : contents.getClassContents()) {
try {
classesDumped += dumpClass(jos, cn);
} catch (ZipException e) {
System.out.println("\r[!] Failed to dump " + cn.getName() + "!\n");
throw e;
}
progressBar.step();
}
for (JarResource res : contents.getResourceContents()) {
resourcesDumped += dumpResource(jos, res.getName(), res.getData());
progressBar.step();
}
}
if(!Debug.debugging)
System.out.println("Dumped " + classesDumped + " classes and " + resourcesDumped + " resources to " + file.getAbsolutePath());
jos.close();
}
/**
* Writes the {@link ClassNode} to the Jar.
*
* @param out The {@link JarOutputStream}.
* @param classData The {@link JarClassData}
* @throws IOException If there is a write error.
* @return The amount of things dumped, 1 or if you're not dumping it 0.
*/
@Override
public int dumpClass(JarOutputStream out, JarClassData classData) throws IOException {
ClassNode cn = classData.getClassNode();
JarEntry entry = new JarEntry(cn.getName());
out.putNextEntry(entry);
if (skidfuscator.getExemptAnalysis().isExempt(cn)) {
out.write(
skidfuscator
.getJarDownloader()
.getJarContents()
.getClassContents()
.namedMap()
.get(classData.getName())
.getData()
);
return 1;
}
ClassTree tree = source.getClassTree();
for(MethodNode m : cn.getMethods()) {
if(m.node.instructions.size() > 10000) {
System.out.println("large method: " + m + " @" + m.node.instructions.size());
}
}
try {
try {
ClassWriter writer = this.buildClassWriter(tree, ClassWriter.COMPUTE_FRAMES);
cn.node.accept(writer); // must use custom writer which overrides getCommonSuperclass
out.write(writer.toByteArray());
} catch (Exception e) {
e.printStackTrace();
ClassWriter writer = this.buildClassWriter(tree, ClassWriter.COMPUTE_MAXS);
cn.node.accept(writer); // must use custom writer which overrides getCommonSuperclass
out.write(writer.toByteArray());
System.err.println("\rFailed to write " + cn.getName() + "! Writing with COMPUTE_MAXS, " +
"which may cause runtime abnormalities\n");
}
} catch (Exception e) {
System.err.println("Failed to write " + cn.getName() + "! Skipping class...");
e.printStackTrace();
}
return 1;
}
public ClassWriter buildClassWriter(ClassTree tree, int flags) {
return new ClassWriter(flags) {
// this method in ClassWriter uses the systemclassloader as
// a stream location to load the super class, however, most of
// the time the class is loaded/read and parsed by us so it
// isn't defined in the system classloader. in certain cases
// we may not even want it to be loaded/resolved and we can
// bypass this by implementing the hierarchy scanning algorithm
// with ClassNodes rather than Classes.
@Override
protected String getCommonSuperClass(String type1, String type2) {
ClassNode ccn = source.findClassNode(type1);
ClassNode dcn = source.findClassNode(type2);
boolean debug = false;
if(ccn == null) {
ClassNode c;
try {
final ClassReader reader = new ClassReader(type1);
final org.objectweb.asm.tree.ClassNode node = new org.objectweb.asm.tree.ClassNode();
reader.accept(node, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
c = new SkidClassNode(node, skidfuscator);
skidfuscator.getClassSource().getClassTree().addVertex(c);
} catch (IOException e) {
System.err.println("[FATAL] Failed to find common superclass due to failed " + type1);
return "java/lang/Object";
}
ccn = c;
}
if(dcn == null) {
ClassNode c;
try {
final ClassReader reader = new ClassReader(type1);
final org.objectweb.asm.tree.ClassNode node = new org.objectweb.asm.tree.ClassNode();
reader.accept(node, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
c = new SkidClassNode(node, skidfuscator);
skidfuscator.getClassSource().getClassTree().addVertex(c);
} catch (IOException e) {
System.err.println("[FATAL] Failed to find common superclass due to failed " + type1);
return "java/lang/Object";
}
dcn = c;
// classTree.build(c);
// return getCommonSuperClass(type1, type2);
}
if (debug) {
Collection<ClassNode> c = tree.getAllParents(ccn);
Collection<ClassNode> d = tree.getAllParents(dcn);
for (ClassNode classNode : c) {
System.out.println("1: " + classNode.getDisplayName());
}
for (ClassNode classNode : d) {
System.out.println("2: " + classNode.getDisplayName());
}
}
if (true)
return skidfuscator
.getClassSource()
.getClassTree()
.getCommonAncestor(Arrays.asList(ccn, dcn))
.iterator()
.next()
.getName();
{
throw new IllegalStateException("Could not find common class type between " + Arrays.toString(new Object[]{ccn.getDisplayName(), dcn.getDisplayName()}));
}
/*if(Modifier.isInterface(ccn.node.access) || Modifier.isInterface(dcn.node.access)) {
// enums as well?
return "java/lang/Object";
} else {
do {
ClassNode nccn = source.findClassNode(ccn.node.superName);
if(nccn == null)
break;
ccn = nccn;
c = tree.getAllParents(ccn);
} while(!c.contains(dcn));
return ccn.getName();
}*/
}
};
}
/**
* Writes a resource to the Jar.
*
* @param out The {@link JarOutputStream}.
* @param name The name of the file.
* @param file File as a byte[].
* @throws IOException If there is a write error.
* @return The amount of things dumped, 1 or if you're not dumping it 0.
*/
@Override
public int dumpResource(JarOutputStream out, String name, byte[] file) throws IOException {
JarEntry entry = new JarEntry(name);
out.putNextEntry(entry);
out.write(file);
return 1;
}
}