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
|
/*******************************************************************************
* 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.bytecode;
import java.util.Set;
import javassist.ClassMap;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import javassist.bytecode.InnerClassesAttribute;
import com.beust.jcommander.internal.Sets;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.FieldEntry;
import cuchaz.enigma.mapping.MethodEntry;
import cuchaz.enigma.mapping.Translator;
public class ClassTranslator
{
private Translator m_translator;
public ClassTranslator( Translator translator )
{
m_translator = translator;
}
public void translate( CtClass c )
{
// NOTE: the order of these translations is very important
// translate all the field and method references in the code by editing the constant pool
ConstPool constants = c.getClassFile().getConstPool();
ConstPoolEditor editor = new ConstPoolEditor( constants );
for( int i=1; i<constants.getSize(); i++ )
{
switch( constants.getTag( i ) )
{
case ConstPool.CONST_Fieldref:
{
// translate the name
FieldEntry entry = new FieldEntry(
new ClassEntry( Descriptor.toJvmName( constants.getFieldrefClassName( i ) ) ),
constants.getFieldrefName( i )
);
String translatedName = m_translator.translate( entry );
if( translatedName == null )
{
translatedName = entry.getName();
}
// translate the type
String type = constants.getFieldrefType( i );
String translatedType = m_translator.translateSignature( type );
if( !entry.getName().equals( translatedName ) || !type.equals( translatedType ) )
{
editor.changeMemberrefNameAndType( i, translatedName, translatedType );
}
}
break;
case ConstPool.CONST_Methodref:
case ConstPool.CONST_InterfaceMethodref:
{
// translate the name and type
MethodEntry entry = new MethodEntry(
new ClassEntry( Descriptor.toJvmName( editor.getMemberrefClassname( i ) ) ),
editor.getMemberrefName( i ),
editor.getMemberrefType( i )
);
String translatedName = m_translator.translate( entry );
if( translatedName == null )
{
translatedName = entry.getName();
}
String translatedSignature = m_translator.translateSignature( entry.getSignature() );
if( !entry.getName().equals( translatedName ) || !entry.getSignature().equals( translatedSignature ) )
{
editor.changeMemberrefNameAndType( i, translatedName, translatedSignature );
}
}
break;
}
}
ClassEntry classEntry = new ClassEntry( Descriptor.toJvmName( c.getName() ) );
// translate all the fields
for( CtField field : c.getDeclaredFields() )
{
// translate the name
FieldEntry entry = new FieldEntry( classEntry, field.getName() );
String translatedName = m_translator.translate( entry );
if( translatedName != null )
{
field.setName( translatedName );
}
// translate the type
String translatedType = m_translator.translateSignature( field.getFieldInfo().getDescriptor() );
field.getFieldInfo().setDescriptor( translatedType );
}
// translate all the methods and constructors
for( CtBehavior behavior : c.getDeclaredBehaviors() )
{
// translate the name
MethodEntry entry = new MethodEntry( classEntry, behavior.getName(), behavior.getSignature() );
String translatedName = m_translator.translate( entry );
if( translatedName != null )
{
if( behavior instanceof CtMethod )
{
((CtMethod)behavior).setName( translatedName );
}
}
// translate the type
String translatedSignature = m_translator.translateSignature( behavior.getMethodInfo().getDescriptor() );
behavior.getMethodInfo().setDescriptor( translatedSignature );
}
// translate all the class names referenced in the code
// the above code only changed method/field/reference names and types, but not the class names themselves
Set<ClassEntry> classEntries = getAllClassEntries( c );
ClassMap map = new ClassMap();
for( ClassEntry obfClassEntry : classEntries )
{
map.put( obfClassEntry.getName(), m_translator.translateEntry( obfClassEntry ).getName() );
}
c.replaceClassName( map );
// translate the names in the InnerClasses attribute
InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute( InnerClassesAttribute.tag );
if( attr != null )
{
for( int i=0; i<attr.tableLength(); i++ )
{
ClassEntry obfClassEntry = new ClassEntry( Descriptor.toJvmName( attr.innerClass( i ) ) );
ClassEntry deobfClassEntry = m_translator.translateEntry( obfClassEntry );
attr.setInnerClassIndex( i, constants.addClassInfo( deobfClassEntry.getName() ) );
if( attr.outerClassIndex( i ) != 0 )
{
attr.setOuterClassIndex( i, constants.addClassInfo( deobfClassEntry.getOuterClassName() ) );
}
if( attr.innerNameIndex( i ) != 0 )
{
attr.setInnerNameIndex( i, constants.addUtf8Info( deobfClassEntry.getInnerClassName() ) );
}
/* DEBUG
System.out.println( String.format( "\tOBF: %s DEOBF: %s-> ATTR: %s,%s,%s",
obfClassEntry, deobfClassEntry,
attr.outerClass( i ),
attr.innerClass( i ),
attr.innerName( i )
) );
*/
}
}
}
private Set<ClassEntry> getAllClassEntries( CtClass c )
{
final Set<ClassEntry> entries = Sets.newHashSet();
ClassMap map = new ClassMap( )
{
@Override
public Object get( Object obj )
{
if( obj instanceof String )
{
entries.add( new ClassEntry( (String)obj ) );
}
return null;
}
private static final long serialVersionUID = -202160293602070641L;
};
c.replaceClassName( map );
return entries;
}
}
|