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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
package cuchaz.enigma.gui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.Multimap;
import com.strobel.decompiler.languages.java.ast.CompilationUnit;
import cuchaz.enigma.Constants;
import cuchaz.enigma.Deobfuscator;
import cuchaz.enigma.analysis.SourceIndex;
import cuchaz.enigma.analysis.Token;
import cuchaz.enigma.convert.ClassIdentifier;
import cuchaz.enigma.convert.ClassIdentity;
import cuchaz.enigma.convert.ClassMatch;
import cuchaz.enigma.convert.ClassMatching;
import cuchaz.enigma.convert.ClassNamer;
import cuchaz.enigma.convert.MappingsConverter;
import cuchaz.enigma.convert.Matches;
import cuchaz.enigma.gui.ClassSelector.ClassSelectionListener;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.Entry;
import de.sciss.syntaxpane.DefaultSyntaxKit;
public class MatchingGui {
private static enum SourceType {
Matched {
@Override
public Collection<ClassEntry> getSourceClasses(Matches matches) {
return matches.getUniqueMatches().keySet();
}
},
Unmatched {
@Override
public Collection<ClassEntry> getSourceClasses(Matches matches) {
return matches.getUnmatchedSourceClasses();
}
},
Ambiguous {
@Override
public Collection<ClassEntry> getSourceClasses(Matches matches) {
return matches.getAmbiguouslyMatchedSourceClasses();
}
};
public JRadioButton newRadio(ActionListener listener, ButtonGroup group) {
JRadioButton button = new JRadioButton(name(), this == getDefault());
button.setActionCommand(name());
button.addActionListener(listener);
group.add(button);
return button;
}
public abstract Collection<ClassEntry> getSourceClasses(Matches matches);
public static SourceType getDefault() {
return values()[0];
}
}
public static interface SaveListener {
public void save(Matches matches);
}
// controls
private JFrame m_frame;
private ClassSelector m_sourceClasses;
private ClassSelector m_destClasses;
private JEditorPane m_sourceReader;
private JEditorPane m_destReader;
private JLabel m_sourceClassLabel;
private JLabel m_destClassLabel;
private JButton m_matchButton;
private Map<SourceType,JRadioButton> m_sourceTypeButtons;
private JCheckBox m_advanceCheck;
private SelectionHighlightPainter m_selectionHighlightPainter;
private Matches m_matches;
private Deobfuscator m_sourceDeobfuscator;
private Deobfuscator m_destDeobfuscator;
private ClassEntry m_sourceClass;
private ClassEntry m_destClass;
private SourceType m_sourceType;
private SaveListener m_saveListener;
public MatchingGui(Matches matches, Deobfuscator sourceDeobfuscator, Deobfuscator destDeobfuscator) {
m_matches = matches;
m_sourceDeobfuscator = sourceDeobfuscator;
m_destDeobfuscator = destDeobfuscator;
// init frame
m_frame = new JFrame(Constants.Name);
final Container pane = m_frame.getContentPane();
pane.setLayout(new BorderLayout());
// init source side
JPanel sourcePanel = new JPanel();
sourcePanel.setLayout(new BoxLayout(sourcePanel, BoxLayout.PAGE_AXIS));
sourcePanel.setPreferredSize(new Dimension(200, 0));
pane.add(sourcePanel, BorderLayout.WEST);
sourcePanel.add(new JLabel("Source Classes"));
// init source type radios
JPanel sourceTypePanel = new JPanel();
sourcePanel.add(sourceTypePanel);
sourceTypePanel.setLayout(new BoxLayout(sourceTypePanel, BoxLayout.PAGE_AXIS));
ActionListener sourceTypeListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
setSourceType(SourceType.valueOf(event.getActionCommand()));
}
};
ButtonGroup sourceTypeButtons = new ButtonGroup();
m_sourceTypeButtons = Maps.newHashMap();
for (SourceType sourceType : SourceType.values()) {
JRadioButton button = sourceType.newRadio(sourceTypeListener, sourceTypeButtons);
m_sourceTypeButtons.put(sourceType, button);
sourceTypePanel.add(button);
}
m_sourceClasses = new ClassSelector(ClassSelector.DeobfuscatedClassEntryComparator);
m_sourceClasses.setListener(new ClassSelectionListener() {
@Override
public void onSelectClass(ClassEntry classEntry) {
setSourceClass(classEntry);
}
});
JScrollPane sourceScroller = new JScrollPane(m_sourceClasses);
sourcePanel.add(sourceScroller);
// init dest side
JPanel destPanel = new JPanel();
destPanel.setLayout(new BoxLayout(destPanel, BoxLayout.PAGE_AXIS));
destPanel.setPreferredSize(new Dimension(200, 0));
pane.add(destPanel, BorderLayout.WEST);
destPanel.add(new JLabel("Destination Classes"));
m_destClasses = new ClassSelector(ClassSelector.DeobfuscatedClassEntryComparator);
m_destClasses.setListener(new ClassSelectionListener() {
@Override
public void onSelectClass(ClassEntry classEntry) {
setDestClass(classEntry);
}
});
JScrollPane destScroller = new JScrollPane(m_destClasses);
destPanel.add(destScroller);
JButton autoMatchButton = new JButton("AutoMatch");
autoMatchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
autoMatch();
}
});
destPanel.add(autoMatchButton);
// init source panels
DefaultSyntaxKit.initKit();
m_sourceReader = makeReader();
m_destReader = makeReader();
// init all the splits
JSplitPane splitLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, sourcePanel, new JScrollPane(m_sourceReader));
splitLeft.setResizeWeight(0); // let the right side take all the slack
JSplitPane splitRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane(m_destReader), destPanel);
splitRight.setResizeWeight(1); // let the left side take all the slack
JSplitPane splitCenter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, splitLeft, splitRight);
splitCenter.setResizeWeight(0.5); // resize 50:50
pane.add(splitCenter, BorderLayout.CENTER);
splitCenter.resetToPreferredSizes();
// init bottom panel
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout());
m_sourceClassLabel = new JLabel();
m_sourceClassLabel.setHorizontalAlignment(SwingConstants.RIGHT);
m_sourceClassLabel.setPreferredSize(new Dimension(300, 24));
m_destClassLabel = new JLabel();
m_destClassLabel.setHorizontalAlignment(SwingConstants.LEFT);
m_destClassLabel.setPreferredSize(new Dimension(300, 24));
m_matchButton = new JButton();
m_matchButton.setPreferredSize(new Dimension(140, 24));
m_advanceCheck = new JCheckBox("Advance to next likely match");
m_advanceCheck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (m_advanceCheck.isSelected()) {
advance();
}
}
});
bottomPanel.add(m_sourceClassLabel);
bottomPanel.add(m_matchButton);
bottomPanel.add(m_destClassLabel);
bottomPanel.add(m_advanceCheck);
pane.add(bottomPanel, BorderLayout.SOUTH);
// show the frame
pane.doLayout();
m_frame.setSize(1024, 576);
m_frame.setMinimumSize(new Dimension(640, 480));
m_frame.setVisible(true);
m_frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
m_selectionHighlightPainter = new SelectionHighlightPainter();
// init state
updateDestMappings();
setSourceType(SourceType.getDefault());
updateMatchButton();
m_saveListener = null;
}
private JEditorPane makeReader() {
JEditorPane reader = new JEditorPane();
reader.setEditable(false);
reader.setContentType("text/java");
// turn off token highlighting (it's wrong most of the time anyway...)
DefaultSyntaxKit kit = (DefaultSyntaxKit)reader.getEditorKit();
kit.toggleComponent(reader, "de.sciss.syntaxpane.components.TokenMarker");
return reader;
}
public void setSaveListener(SaveListener val) {
m_saveListener = val;
}
private void updateDestMappings() {
m_destDeobfuscator.setMappings(MappingsConverter.newMappings(
m_matches,
m_sourceDeobfuscator.getMappings(),
m_sourceDeobfuscator,
m_destDeobfuscator
));
}
protected void setSourceType(SourceType val) {
// show the source classes
m_sourceType = val;
m_sourceClasses.setClasses(deobfuscateClasses(m_sourceType.getSourceClasses(m_matches), m_sourceDeobfuscator));
// update counts
for (SourceType sourceType : SourceType.values()) {
m_sourceTypeButtons.get(sourceType).setText(String.format("%s (%d)",
sourceType.name(),
sourceType.getSourceClasses(m_matches).size()
));
}
}
private Collection<ClassEntry> deobfuscateClasses(Collection<ClassEntry> in, Deobfuscator deobfuscator) {
List<ClassEntry> out = Lists.newArrayList();
for (ClassEntry entry : in) {
ClassEntry deobf = deobfuscator.deobfuscateEntry(entry);
// make sure we preserve any scores
if (entry instanceof ScoredClassEntry) {
deobf = new ScoredClassEntry(deobf, ((ScoredClassEntry)entry).getScore());
}
out.add(deobf);
}
return out;
}
protected void setSourceClass(ClassEntry classEntry) {
setSourceClass(classEntry, null);
}
protected void setSourceClass(ClassEntry classEntry, final Runnable onGetDestClasses) {
// update the current source class
m_sourceClass = classEntry;
m_sourceClassLabel.setText(m_sourceClass != null ? m_sourceClass.getName() : "");
if (m_sourceClass != null) {
// show the dest class(es)
ClassMatch match = m_matches.getMatchBySource(m_sourceDeobfuscator.obfuscateEntry(m_sourceClass));
assert(match != null);
if (match.destClasses.isEmpty()) {
m_destClasses.setClasses(null);
// run in a separate thread to keep ui responsive
new Thread() {
@Override
public void run() {
m_destClasses.setClasses(deobfuscateClasses(getLikelyMatches(m_sourceClass), m_destDeobfuscator));
m_destClasses.expandAll();
if (onGetDestClasses != null) {
onGetDestClasses.run();
}
}
}.start();
} else {
m_destClasses.setClasses(deobfuscateClasses(match.destClasses, m_destDeobfuscator));
m_destClasses.expandAll();
if (onGetDestClasses != null) {
onGetDestClasses.run();
}
}
}
setDestClass(null);
decompileClass(m_sourceClass, m_sourceDeobfuscator, m_sourceReader);
updateMatchButton();
}
private Collection<ClassEntry> getLikelyMatches(ClassEntry sourceClass) {
ClassEntry obfSourceClass = m_sourceDeobfuscator.obfuscateEntry(sourceClass);
// set up identifiers
ClassNamer namer = new ClassNamer(m_matches.getUniqueMatches());
ClassIdentifier sourceIdentifier = new ClassIdentifier(
m_sourceDeobfuscator.getJar(), m_sourceDeobfuscator.getJarIndex(),
namer.getSourceNamer(), true
);
ClassIdentifier destIdentifier = new ClassIdentifier(
m_destDeobfuscator.getJar(), m_destDeobfuscator.getJarIndex(),
namer.getDestNamer(), true
);
try {
// rank all the unmatched dest classes against the source class
ClassIdentity sourceIdentity = sourceIdentifier.identify(obfSourceClass);
List<ClassEntry> scoredDestClasses = Lists.newArrayList();
for (ClassEntry unmatchedDestClass : m_matches.getUnmatchedDestClasses()) {
ClassIdentity destIdentity = destIdentifier.identify(unmatchedDestClass);
float score = 100.0f*(sourceIdentity.getMatchScore(destIdentity) + destIdentity.getMatchScore(sourceIdentity))
/(sourceIdentity.getMaxMatchScore() + destIdentity.getMaxMatchScore());
scoredDestClasses.add(new ScoredClassEntry(unmatchedDestClass, score));
}
return scoredDestClasses;
} catch (ClassNotFoundException ex) {
throw new Error("Unable to find class " + ex.getMessage());
}
}
protected void setDestClass(ClassEntry classEntry) {
// update the current source class
m_destClass = classEntry;
m_destClassLabel.setText(m_destClass != null ? m_destClass.getName() : "");
decompileClass(m_destClass, m_destDeobfuscator, m_destReader);
updateMatchButton();
}
protected void decompileClass(final ClassEntry classEntry, final Deobfuscator deobfuscator, final JEditorPane reader) {
if (classEntry == null) {
reader.setText(null);
return;
}
reader.setText("(decompiling...)");
// run in a separate thread to keep ui responsive
new Thread() {
@Override
public void run() {
// get the outermost class
ClassEntry outermostClassEntry = classEntry;
while (outermostClassEntry.isInnerClass()) {
outermostClassEntry = outermostClassEntry.getOuterClassEntry();
}
// decompile it
CompilationUnit sourceTree = deobfuscator.getSourceTree(outermostClassEntry.getName());
String source = deobfuscator.getSource(sourceTree);
reader.setText(source);
SourceIndex sourceIndex = deobfuscator.getSourceIndex(sourceTree, source);
// navigate to the class declaration
Token token = sourceIndex.getDeclarationToken(classEntry);
if (token == null) {
// couldn't find the class declaration token, might be an anonymous class
// look for any declaration in that class instead
for (Entry entry : sourceIndex.declarations()) {
if (entry.getClassEntry().equals(classEntry)) {
token = sourceIndex.getDeclarationToken(entry);
break;
}
}
}
if (token != null) {
GuiTricks.navigateToToken(reader, token, m_selectionHighlightPainter);
} else {
// couldn't find anything =(
System.out.println("Unable to find declaration in source for " + classEntry);
}
}
}.start();
}
private void updateMatchButton() {
ClassEntry obfSource = m_sourceDeobfuscator.obfuscateEntry(m_sourceClass);
ClassEntry obfDest = m_destDeobfuscator.obfuscateEntry(m_destClass);
BiMap<ClassEntry,ClassEntry> uniqueMatches = m_matches.getUniqueMatches();
boolean twoSelected = m_sourceClass != null && m_destClass != null;
boolean isMatched = uniqueMatches.containsKey(obfSource) && uniqueMatches.containsValue(obfDest);
boolean canMatch = !uniqueMatches.containsKey(obfSource) && ! uniqueMatches.containsValue(obfDest);
deactivateButton(m_matchButton);
if (twoSelected) {
if (isMatched) {
activateButton(m_matchButton, "Unmatch", new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
onUnmatchClick();
}
});
} else if (canMatch) {
activateButton(m_matchButton, "Match", new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
onMatchClick();
}
});
}
}
}
private void deactivateButton(JButton button) {
button.setEnabled(false);
button.setText("");
for (ActionListener listener : Arrays.asList(button.getActionListeners())) {
button.removeActionListener(listener);
}
}
private void activateButton(JButton button, String text, ActionListener newListener) {
button.setText(text);
button.setEnabled(true);
for (ActionListener listener : Arrays.asList(button.getActionListeners())) {
button.removeActionListener(listener);
}
button.addActionListener(newListener);
}
private void onMatchClick() {
// precondition: source and dest classes are set correctly
ClassEntry obfSource = m_sourceDeobfuscator.obfuscateEntry(m_sourceClass);
ClassEntry obfDest = m_destDeobfuscator.obfuscateEntry(m_destClass);
// remove the classes from their match
m_matches.removeSource(obfSource);
m_matches.removeDest(obfDest);
// add them as matched classes
m_matches.add(new ClassMatch(obfSource, obfDest));
ClassEntry nextClass = null;
if (m_advanceCheck.isSelected()) {
nextClass = m_sourceClasses.getNextClass(m_sourceClass);
}
save();
updateMatches();
if (nextClass != null) {
advance(nextClass);
}
}
private void onUnmatchClick() {
// precondition: source and dest classes are set to a unique match
ClassEntry obfSource = m_sourceDeobfuscator.obfuscateEntry(m_sourceClass);
// remove the source to break the match, then add the source back as unmatched
m_matches.removeSource(obfSource);
m_matches.add(new ClassMatch(obfSource, null));
save();
updateMatches();
}
private void updateMatches() {
updateDestMappings();
setDestClass(null);
m_destClasses.setClasses(null);
updateMatchButton();
// remember where we were in the source tree
String packageName = m_sourceClasses.getSelectedPackage();
setSourceType(m_sourceType);
m_sourceClasses.expandPackage(packageName);
}
private void save() {
if (m_saveListener != null) {
m_saveListener.save(m_matches);
}
}
private void autoMatch() {
System.out.println("Automatching...");
// compute a new matching
ClassMatching matching = MappingsConverter.computeMatching(
m_sourceDeobfuscator.getJar(), m_sourceDeobfuscator.getJarIndex(),
m_destDeobfuscator.getJar(), m_destDeobfuscator.getJarIndex(),
m_matches.getUniqueMatches()
);
Matches newMatches = new Matches(matching.matches());
System.out.println(String.format("Automatch found %d new matches",
newMatches.getUniqueMatches().size() - m_matches.getUniqueMatches().size()
));
// update the current matches
m_matches = newMatches;
save();
updateMatches();
}
private void advance() {
advance(null);
}
private void advance(ClassEntry sourceClass) {
// make sure we have a source class
if (sourceClass == null) {
sourceClass = m_sourceClasses.getSelectedClass();
if (sourceClass != null) {
sourceClass = m_sourceClasses.getNextClass(sourceClass);
} else {
sourceClass = m_sourceClasses.getFirstClass();
}
}
// set the source class
setSourceClass(sourceClass, new Runnable() {
@Override
public void run() {
// then, pick the best dest class
ClassEntry firstClass = null;
ScoredClassEntry bestDestClass = null;
for (ClassSelectorPackageNode packageNode : m_destClasses.packageNodes()) {
for (ClassSelectorClassNode classNode : m_destClasses.classNodes(packageNode)) {
if (firstClass == null) {
firstClass = classNode.getClassEntry();
}
if (classNode.getClassEntry() instanceof ScoredClassEntry) {
ScoredClassEntry scoredClass = (ScoredClassEntry)classNode.getClassEntry();
if (bestDestClass == null || bestDestClass.getScore() < scoredClass.getScore()) {
bestDestClass = scoredClass;
}
}
}
}
// pick the entry to show
ClassEntry destClass = null;
if (bestDestClass != null) {
destClass = bestDestClass;
} else if (firstClass != null) {
destClass = firstClass;
}
setDestClass(destClass);
m_destClasses.setSelectionClass(destClass);
}
});
m_sourceClasses.setSelectionClass(sourceClass);
}
}
|