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
|
/*******************************************************************************
* Copyright (c) 2014 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.analysis;
import java.lang.reflect.Modifier;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javassist.CannotCompileException;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
import javassist.bytecode.AccessFlag;
import javassist.bytecode.Descriptor;
import javassist.bytecode.FieldInfo;
import javassist.expr.ConstructorCall;
import javassist.expr.ExprEditor;
import javassist.expr.FieldAccess;
import javassist.expr.MethodCall;
import javassist.expr.NewExpr;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.Sets;
import cuchaz.enigma.mapping.ArgumentEntry;
import cuchaz.enigma.mapping.BehaviorEntry;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.ConstructorEntry;
import cuchaz.enigma.mapping.Entry;
import cuchaz.enigma.mapping.FieldEntry;
import cuchaz.enigma.mapping.MethodEntry;
import cuchaz.enigma.mapping.Translator;
public class JarIndex
{
private Set<String> m_obfClassNames;
private Ancestries m_ancestries;
private Multimap<String,MethodEntry> m_methodImplementations;
private Multimap<BehaviorEntry,EntryReference<BehaviorEntry,BehaviorEntry>> m_behaviorReferences;
private Multimap<FieldEntry,EntryReference<FieldEntry,BehaviorEntry>> m_fieldReferences;
private Multimap<String,String> m_innerClasses;
private Map<String,String> m_outerClasses;
private Set<String> m_anonymousClasses;
public JarIndex( )
{
m_obfClassNames = Sets.newHashSet();
m_ancestries = new Ancestries();
m_methodImplementations = HashMultimap.create();
m_behaviorReferences = HashMultimap.create();
m_fieldReferences = HashMultimap.create();
m_innerClasses = HashMultimap.create();
m_outerClasses = Maps.newHashMap();
m_anonymousClasses = Sets.newHashSet();
}
public void indexJar( JarFile jar )
{
// pass 1: read the class names
for( JarEntry entry : JarClassIterator.getClassEntries( jar ) )
{
String className = entry.getName().substring( 0, entry.getName().length() - 6 );
m_obfClassNames.add( Descriptor.toJvmName( className ) );
}
// pass 2: index the types, methods
for( CtClass c : JarClassIterator.classes( jar ) )
{
m_ancestries.addSuperclass( c.getName(), c.getClassFile().getSuperclass() );
for( CtBehavior behavior : c.getDeclaredBehaviors() )
{
indexBehavior( behavior );
}
}
// pass 2: index inner classes and anonymous classes
for( CtClass c : JarClassIterator.classes( jar ) )
{
String outerClassName = findOuterClass( c );
if( outerClassName != null )// /* TEMP */ && false )
{
String innerClassName = Descriptor.toJvmName( c.getName() );
m_innerClasses.put( outerClassName, innerClassName );
m_outerClasses.put( innerClassName, outerClassName );
if( isAnonymousClass( c, outerClassName ) )
{
m_anonymousClasses.add( innerClassName );
// DEBUG
//System.out.println( "ANONYMOUS: " + outerClassName + "$" + innerClassName );
}
else
{
// DEBUG
//System.out.println( "INNER: " + outerClassName + "$" + innerClassName );
}
}
}
// step 3: update other indicies with inner class info
Map<String,String> renames = Maps.newHashMap();
for( Map.Entry<String,String> entry : m_outerClasses.entrySet() )
{
renames.put( entry.getKey(), entry.getValue() + "$" + entry.getKey() );
}
renameClasses( renames );
}
private void indexBehavior( CtBehavior behavior )
{
// get the method entry
String className = Descriptor.toJvmName( behavior.getDeclaringClass().getName() );
final BehaviorEntry thisEntry;
if( behavior instanceof CtMethod )
{
MethodEntry methodEntry = new MethodEntry(
new ClassEntry( className ),
behavior.getName(),
behavior.getSignature()
);
thisEntry = methodEntry;
// index implementation
m_methodImplementations.put( className, methodEntry );
}
else if( behavior instanceof CtConstructor )
{
boolean isStatic = behavior.getName().equals( "<clinit>" );
if( isStatic )
{
thisEntry = new ConstructorEntry( new ClassEntry( className ) );
}
else
{
thisEntry = new ConstructorEntry( new ClassEntry( className ), behavior.getSignature() );
}
}
else
{
throw new IllegalArgumentException( "behavior must be a method or a constructor!" );
}
// index method calls
try
{
behavior.instrument( new ExprEditor( )
{
@Override
public void edit( MethodCall call )
{
String className = Descriptor.toJvmName( call.getClassName() );
MethodEntry calledMethodEntry = new MethodEntry(
new ClassEntry( className ),
call.getMethodName(),
call.getSignature()
);
EntryReference<BehaviorEntry,BehaviorEntry> reference = new EntryReference<BehaviorEntry,BehaviorEntry>(
calledMethodEntry,
thisEntry
);
m_behaviorReferences.put( calledMethodEntry, reference );
}
@Override
public void edit( FieldAccess call )
{
String className = Descriptor.toJvmName( call.getClassName() );
FieldEntry calledFieldEntry = new FieldEntry(
new ClassEntry( className ),
call.getFieldName()
);
EntryReference<FieldEntry,BehaviorEntry> reference = new EntryReference<FieldEntry,BehaviorEntry>(
calledFieldEntry,
thisEntry
);
m_fieldReferences.put( calledFieldEntry, reference );
}
@Override
public void edit( ConstructorCall call )
{
// TODO: save isSuper in the reference somehow
boolean isSuper = call.getMethodName().equals( "super" );
String className = Descriptor.toJvmName( call.getClassName() );
ConstructorEntry calledConstructorEntry = new ConstructorEntry(
new ClassEntry( className ),
call.getSignature()
);
EntryReference<BehaviorEntry,BehaviorEntry> reference = new EntryReference<BehaviorEntry,BehaviorEntry>(
calledConstructorEntry,
thisEntry
);
m_behaviorReferences.put( calledConstructorEntry, reference );
}
@Override
public void edit( NewExpr call )
{
String className = Descriptor.toJvmName( call.getClassName() );
ConstructorEntry calledConstructorEntry = new ConstructorEntry(
new ClassEntry( className ),
call.getSignature()
);
EntryReference<BehaviorEntry,BehaviorEntry> reference = new EntryReference<BehaviorEntry,BehaviorEntry>(
calledConstructorEntry,
thisEntry
);
m_behaviorReferences.put( calledConstructorEntry, reference );
}
} );
}
catch( CannotCompileException ex )
{
throw new Error( ex );
}
}
private String findOuterClass( CtClass c )
{
// inner classes:
// have constructors that can (illegally) set synthetic fields
// the outer class is the only class that calls constructors
// use the synthetic fields to find the synthetic constructors
for( CtConstructor constructor : c.getDeclaredConstructors() )
{
if( !isIllegalConstructor( constructor ) )
{
continue;
}
// who calls this constructor?
Set<ClassEntry> callerClasses = Sets.newHashSet();
ConstructorEntry constructorEntry = new ConstructorEntry(
new ClassEntry( Descriptor.toJvmName( c.getName() ) ),
constructor.getMethodInfo().getDescriptor()
);
for( EntryReference<BehaviorEntry,BehaviorEntry> reference : getBehaviorReferences( constructorEntry ) )
{
callerClasses.add( reference.context.getClassEntry() );
}
// is this called by exactly one class?
if( callerClasses.size() == 1 )
{
return callerClasses.iterator().next().getName();
}
else if( callerClasses.size() > 1 )
{
// TEMP
System.out.println( "WARNING: Illegal class called by more than one class!" + callerClasses );
}
}
return null;
}
@SuppressWarnings( "unchecked" )
private boolean isIllegalConstructor( CtConstructor constructor )
{
// illegal constructors only set synthetic member fields, then call super()
String className = constructor.getDeclaringClass().getName();
// collect all the field accesses, constructor calls, and method calls
final List<FieldAccess> illegalFieldWrites = Lists.newArrayList();
final List<ConstructorCall> constructorCalls = Lists.newArrayList();
final List<MethodCall> methodCalls = Lists.newArrayList();
try
{
constructor.instrument( new ExprEditor( )
{
@Override
public void edit( FieldAccess fieldAccess )
{
if( fieldAccess.isWriter() && constructorCalls.isEmpty() )
{
illegalFieldWrites.add( fieldAccess );
}
}
@Override
public void edit( ConstructorCall constructorCall )
{
constructorCalls.add( constructorCall );
}
@Override
public void edit( MethodCall methodCall )
{
methodCalls.add( methodCall );
}
} );
}
catch( CannotCompileException ex )
{
// we're not compiling anything... this is stupid
throw new Error( ex );
}
// method calls are not allowed
if( !methodCalls.isEmpty() )
{
return false;
}
// is there only one constructor call?
if( constructorCalls.size() != 1 )
{
return false;
}
// is the call to super?
ConstructorCall constructorCall = constructorCalls.get( 0 );
if( !constructorCall.getMethodName().equals( "super" ) )
{
return false;
}
// are there any illegal field writes?
if( illegalFieldWrites.isEmpty() )
{
return false;
}
// are all the writes to synthetic fields?
for( FieldAccess fieldWrite : illegalFieldWrites )
{
// all illegal writes have to be to the local class
if( !fieldWrite.getClassName().equals( className ) )
{
System.err.println( String.format( "WARNING: illegal write to non-member field %s.%s", fieldWrite.getClassName(), fieldWrite.getFieldName() ) );
return false;
}
// find the field
FieldInfo fieldInfo = null;
for( FieldInfo info : (List<FieldInfo>)constructor.getDeclaringClass().getClassFile().getFields() )
{
if( info.getName().equals( fieldWrite.getFieldName() ) )
{
fieldInfo = info;
break;
}
}
if( fieldInfo == null )
{
// field is in a superclass or something, can't be a local synthetic member
return false;
}
// is this field synthetic?
boolean isSynthetic = (fieldInfo.getAccessFlags() & AccessFlag.SYNTHETIC) != 0;
if( !isSynthetic )
{
System.err.println( String.format( "WARNING: illegal write to non synthetic field %s.%s", className, fieldInfo.getName() ) );
return false;
}
}
// we passed all the tests!
return true;
}
private boolean isAnonymousClass( CtClass c, String outerClassName )
{
String innerClassName = Descriptor.toJvmName( c.getName() );
// anonymous classes:
// can't be abstract
// have only one constructor
// it's called exactly once by the outer class
// type of inner class not referenced anywhere in outer class
// is absract?
if( Modifier.isAbstract( c.getModifiers() ) )
{
return false;
}
// is there exactly one constructor?
if( c.getDeclaredConstructors().length != 1 )
{
return false;
}
CtConstructor constructor = c.getDeclaredConstructors()[0];
// is this constructor called exactly once?
ConstructorEntry constructorEntry = new ConstructorEntry(
new ClassEntry( innerClassName ),
constructor.getMethodInfo().getDescriptor()
);
if( getBehaviorReferences( constructorEntry ).size() != 1 )
{
return false;
}
// TODO: check outer class doesn't reference type
// except this is hard because we can't just load the outer class now
// we'd have to pre-index those references in the JarIndex
return true;
}
public Set<String> getObfClassNames( )
{
return m_obfClassNames;
}
public Ancestries getAncestries( )
{
return m_ancestries;
}
public boolean isMethodImplemented( MethodEntry methodEntry )
{
Collection<MethodEntry> implementations = m_methodImplementations.get( methodEntry.getClassName() );
if( implementations == null )
{
return false;
}
return implementations.contains( methodEntry );
}
public ClassInheritanceTreeNode getClassInheritance( Translator deobfuscatingTranslator, ClassEntry obfClassEntry )
{
// get the root node
List<String> ancestry = Lists.newArrayList();
ancestry.add( obfClassEntry.getName() );
ancestry.addAll( m_ancestries.getAncestry( obfClassEntry.getName() ) );
ClassInheritanceTreeNode rootNode = new ClassInheritanceTreeNode( deobfuscatingTranslator, ancestry.get( ancestry.size() - 1 ) );
// expand all children recursively
rootNode.load( m_ancestries, true );
return rootNode;
}
public MethodInheritanceTreeNode getMethodInheritance( Translator deobfuscatingTranslator, MethodEntry obfMethodEntry )
{
// travel to the ancestor implementation
String baseImplementationClassName = obfMethodEntry.getClassName();
for( String ancestorClassName : m_ancestries.getAncestry( obfMethodEntry.getClassName() ) )
{
MethodEntry ancestorMethodEntry = new MethodEntry(
new ClassEntry( ancestorClassName ),
obfMethodEntry.getName(),
obfMethodEntry.getSignature()
);
if( isMethodImplemented( ancestorMethodEntry ) )
{
baseImplementationClassName = ancestorClassName;
}
}
// make a root node at the base
MethodEntry methodEntry = new MethodEntry(
new ClassEntry( baseImplementationClassName ),
obfMethodEntry.getName(),
obfMethodEntry.getSignature()
);
MethodInheritanceTreeNode rootNode = new MethodInheritanceTreeNode(
deobfuscatingTranslator,
methodEntry,
isMethodImplemented( methodEntry )
);
// expand the full tree
rootNode.load( this, true );
return rootNode;
}
public Collection<EntryReference<FieldEntry,BehaviorEntry>> getFieldReferences( FieldEntry fieldEntry )
{
return m_fieldReferences.get( fieldEntry );
}
public Collection<EntryReference<BehaviorEntry,BehaviorEntry>> getBehaviorReferences( BehaviorEntry behaviorEntry )
{
return m_behaviorReferences.get( behaviorEntry );
}
public Collection<String> getInnerClasses( String obfOuterClassName )
{
return m_innerClasses.get( obfOuterClassName );
}
public String getOuterClass( String obfInnerClassName )
{
return m_outerClasses.get( obfInnerClassName );
}
public boolean isAnonymousClass( String obfInnerClassName )
{
return m_anonymousClasses.contains( obfInnerClassName );
}
private void renameClasses( Map<String,String> renames )
{
m_ancestries.renameClasses( renames );
renameMultimap( renames, m_methodImplementations );
renameMultimap( renames, m_behaviorReferences );
renameMultimap( renames, m_fieldReferences );
}
private <Key,Val> void renameMultimap( Map<String,String> renames, Multimap<Key,Val> map )
{
// for each key/value pair...
Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet();
Iterator<Map.Entry<Key,Val>> iter = map.entries().iterator();
while( iter.hasNext() )
{
Map.Entry<Key,Val> entry = iter.next();
iter.remove();
entriesToAdd.add( new AbstractMap.SimpleEntry<Key,Val>(
renameThing( renames, entry.getKey() ),
renameThing( renames, entry.getValue() )
) );
}
for( Map.Entry<Key,Val> entry : entriesToAdd )
{
map.put( entry.getKey(), entry.getValue() );
}
}
@SuppressWarnings( "unchecked" )
private <T> T renameThing( Map<String,String> renames, T thing )
{
if( thing instanceof String )
{
String stringEntry = (String)thing;
if( renames.containsKey( stringEntry ) )
{
return (T)renames.get( stringEntry );
}
}
else if( thing instanceof ClassEntry )
{
ClassEntry classEntry = (ClassEntry)thing;
return (T)new ClassEntry( renameThing( renames, classEntry.getClassName() ) );
}
else if( thing instanceof FieldEntry )
{
FieldEntry fieldEntry = (FieldEntry)thing;
return (T)new FieldEntry(
renameThing( renames, fieldEntry.getClassEntry() ),
fieldEntry.getName()
);
}
else if( thing instanceof ConstructorEntry )
{
ConstructorEntry constructorEntry = (ConstructorEntry)thing;
return (T)new ConstructorEntry(
renameThing( renames, constructorEntry.getClassEntry() ),
constructorEntry.getSignature()
);
}
else if( thing instanceof MethodEntry )
{
MethodEntry methodEntry = (MethodEntry)thing;
return (T)new MethodEntry(
renameThing( renames, methodEntry.getClassEntry() ),
methodEntry.getName(),
methodEntry.getSignature()
);
}
else if( thing instanceof ArgumentEntry )
{
ArgumentEntry argumentEntry = (ArgumentEntry)thing;
return (T)new ArgumentEntry(
renameThing( renames, argumentEntry.getMethodEntry() ),
argumentEntry.getIndex(),
argumentEntry.getName()
);
}
else if( thing instanceof EntryReference )
{
EntryReference<Entry,Entry> reference = (EntryReference<Entry,Entry>)thing;
reference.entry = renameThing( renames, reference.entry );
reference.context = renameThing( renames, reference.context );
return thing;
}
else
{
throw new Error( "Not an entry: " + thing );
}
return thing;
}
}
|