summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/Deobfuscator.java
blob: 770172e399f5f84305d9cda8c2d4442fdc3f9766 (plain) (blame)
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
/*******************************************************************************
 * 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;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;
import java.util.jar.JarFile;

import com.strobel.assembler.metadata.MetadataSystem;
import com.strobel.assembler.metadata.TypeDefinition;
import com.strobel.decompiler.DecompilerContext;
import com.strobel.decompiler.DecompilerSettings;
import com.strobel.decompiler.PlainTextOutput;
import com.strobel.decompiler.languages.java.JavaOutputVisitor;
import com.strobel.decompiler.languages.java.ast.AstBuilder;
import com.strobel.decompiler.languages.java.ast.CompilationUnit;
import com.strobel.decompiler.languages.java.ast.InsertParenthesesVisitor;

import cuchaz.enigma.analysis.JarIndex;
import cuchaz.enigma.analysis.SourceIndex;
import cuchaz.enigma.analysis.SourceIndexVisitor;
import cuchaz.enigma.mapping.ArgumentEntry;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.ClassMapping;
import cuchaz.enigma.mapping.ConstructorEntry;
import cuchaz.enigma.mapping.Entry;
import cuchaz.enigma.mapping.FieldEntry;
import cuchaz.enigma.mapping.Mappings;
import cuchaz.enigma.mapping.MethodEntry;
import cuchaz.enigma.mapping.Renamer;
import cuchaz.enigma.mapping.TranslationDirection;
import cuchaz.enigma.mapping.Translator;

public class Deobfuscator
{
	private File m_file;
	private JarFile m_jar;
	private DecompilerSettings m_settings;
	private JarIndex m_jarIndex;
	private Mappings m_mappings;
	private Renamer m_renamer;
	
	public Deobfuscator( File file )
	throws IOException
	{
		m_file = file;
		m_jar = new JarFile( m_file );
		
		// build the ancestries
		InputStream jarIn = null;
		try
		{
			m_jarIndex = new JarIndex( m_jar );
			jarIn = new FileInputStream( m_file );
			m_jarIndex.indexJar( jarIn );
		}
		finally
		{
			Util.closeQuietly( jarIn );
		}
		
		// config the decompiler
		m_settings = DecompilerSettings.javaDefaults();
		m_settings.setShowSyntheticMembers( true );
		
		// init mappings
		setMappings( new Mappings() );
	}
	
	public String getJarName( )
	{
		return m_file.getName();
	}
	
	public JarIndex getJarIndex( )
	{
		return m_jarIndex;
	}
	
	public Mappings getMappings( )
	{
		return m_mappings;
	}
	public void setMappings( Mappings val )
	{
		if( val == null )
		{
			val = new Mappings();
		}
		m_mappings = val;
		m_renamer = new Renamer( m_jarIndex, m_mappings );
		
		// update decompiler options
		m_settings.setTypeLoader( new TranslatingTypeLoader(
			m_jar,
			getTranslator( TranslationDirection.Obfuscating ),
			getTranslator( TranslationDirection.Deobfuscating )
		) );
	}
	
	public Translator getTranslator( TranslationDirection direction )
	{
		return m_mappings.getTranslator( m_jarIndex.getAncestries(), direction );
	}
	
	public void getSeparatedClasses( List<String> obfClasses, List<String> deobfClasses )
	{
		for( String obfClassName : m_jarIndex.getObfClassNames() )
		{
			// separate the classes
			ClassMapping classMapping = m_mappings.getClassByObf( obfClassName );
			if( classMapping != null )
			{
				deobfClasses.add( classMapping.getDeobfName() );
			}
			else if( obfClassName.indexOf( '/' ) >= 0 )
			{
				// this class is in a package and therefore is not obfuscated
				deobfClasses.add( obfClassName );
			}
			else
			{
				obfClasses.add( obfClassName );
			}
		}
	}
	
	public SourceIndex getSource( String className )
	{
		// is this class deobfuscated?
		// we need to tell the decompiler the deobfuscated name so it doesn't get freaked out
		// the decompiler only sees the deobfuscated class, so we need to load it by the deobfuscated name
		ClassMapping classMapping = m_mappings.getClassByObf( className );
		if( classMapping != null )
		{
			className = classMapping.getDeobfName();
		}
		
		// decompile it!
		TypeDefinition resolvedType = new MetadataSystem( m_settings.getTypeLoader() ).lookupType( className ).resolve();
		DecompilerContext context = new DecompilerContext();
		context.setCurrentType( resolvedType );
		context.setSettings( m_settings );
		AstBuilder builder = new AstBuilder( context );
		builder.addType( resolvedType );
		builder.runTransformations( null );
		CompilationUnit root = builder.getCompilationUnit();
		
		// render the AST into source
		StringWriter buf = new StringWriter();
		root.acceptVisitor( new InsertParenthesesVisitor(), null );
		root.acceptVisitor( new JavaOutputVisitor( new PlainTextOutput( buf ), m_settings ), null );
		
		// build the source index
		SourceIndex index = new SourceIndex( buf.toString() );
		root.acceptVisitor( new SourceIndexVisitor(), index );
		
		return index;
	}
	
	// NOTE: these methods are a bit messy... oh well

	public void rename( Entry obfEntry, String newName )
	{
		if( obfEntry instanceof ClassEntry )
		{
			m_renamer.setClassName( (ClassEntry)obfEntry, newName );
		}
		else if( obfEntry instanceof FieldEntry )
		{
			m_renamer.setFieldName( (FieldEntry)obfEntry, newName );
		}
		else if( obfEntry instanceof MethodEntry )
		{
			m_renamer.setMethodTreeName( (MethodEntry)obfEntry, newName );
		}
		else if( obfEntry instanceof ConstructorEntry )
		{
			m_renamer.setClassName( obfEntry.getClassEntry(), newName );
		}
		else if( obfEntry instanceof ArgumentEntry )
		{
			m_renamer.setArgumentName( (ArgumentEntry)obfEntry, newName );
		}
		else
		{
			throw new Error( "Unknown entry type: " + obfEntry.getClass().getName() );
		}
	}
	
	public Entry obfuscateEntry( Entry deobfEntry )
	{
		Translator translator = getTranslator( TranslationDirection.Obfuscating );
		if( deobfEntry instanceof ClassEntry )
		{
			return translator.translateEntry( (ClassEntry)deobfEntry );
		}
		else if( deobfEntry instanceof FieldEntry )
		{
			return translator.translateEntry( (FieldEntry)deobfEntry );
		}
		else if( deobfEntry instanceof MethodEntry )
		{
			return translator.translateEntry( (MethodEntry)deobfEntry );
		}
		else if( deobfEntry instanceof ConstructorEntry )
		{
			return translator.translateEntry( (ConstructorEntry)deobfEntry );
		}
		else if( deobfEntry instanceof ArgumentEntry )
		{
			return translator.translateEntry( (ArgumentEntry)deobfEntry );
		}
		else
		{
			throw new Error( "Unknown entry type: " + deobfEntry.getClass().getName() );
		}
	}
	
	public Entry deobfuscateEntry( Entry obfEntry )
	{
		Translator translator = getTranslator( TranslationDirection.Deobfuscating );
		if( obfEntry instanceof ClassEntry )
		{
			return translator.translateEntry( (ClassEntry)obfEntry );
		}
		else if( obfEntry instanceof FieldEntry )
		{
			return translator.translateEntry( (FieldEntry)obfEntry );
		}
		else if( obfEntry instanceof MethodEntry )
		{
			return translator.translateEntry( (MethodEntry)obfEntry );
		}
		else if( obfEntry instanceof ConstructorEntry )
		{
			return translator.translateEntry( (ConstructorEntry)obfEntry );
		}
		else if( obfEntry instanceof ArgumentEntry )
		{
			return translator.translateEntry( (ArgumentEntry)obfEntry );
		}
		else
		{
			throw new Error( "Unknown entry type: " + obfEntry.getClass().getName() );
		}
	}
	
	public boolean hasMapping( Entry obfEntry )
	{
		Translator translator = getTranslator( TranslationDirection.Deobfuscating );
		if( obfEntry instanceof ClassEntry )
		{
			String deobfName = translator.translate( (ClassEntry)obfEntry );
			return deobfName != null && !deobfName.equals( obfEntry.getName() );
		}
		else if( obfEntry instanceof FieldEntry )
		{
			String deobfName = translator.translate( (FieldEntry)obfEntry );
			return deobfName != null && !deobfName.equals( obfEntry.getName() );
		}
		else if( obfEntry instanceof MethodEntry )
		{
			String deobfName = translator.translate( (MethodEntry)obfEntry );
			return deobfName != null && !deobfName.equals( obfEntry.getName() );
		}
		else if( obfEntry instanceof ConstructorEntry )
		{
			String deobfName = translator.translate( obfEntry.getClassEntry() );
			return deobfName != null && !deobfName.equals( obfEntry.getClassName() );
		}
		else if( obfEntry instanceof ArgumentEntry )
		{
			return translator.translate( (ArgumentEntry)obfEntry ) != null;
		}
		else
		{
			throw new Error( "Unknown entry type: " + obfEntry.getClass().getName() );
		}
	}

	public boolean entryIsObfuscatedIdenfitier( Entry obfEntry )
	{
		if( obfEntry instanceof ClassEntry )
		{
			// obf classes must be in the list
			return m_jarIndex.getObfClassNames().contains( obfEntry.getName() );
		}
		else if( obfEntry instanceof FieldEntry )
		{
			return m_jarIndex.getObfClassNames().contains( obfEntry.getClassName() );
		}
		else if( obfEntry instanceof MethodEntry )
		{
			return m_jarIndex.getObfClassNames().contains( obfEntry.getClassName() );
		}
		else if( obfEntry instanceof ConstructorEntry )
		{
			return m_jarIndex.getObfClassNames().contains( obfEntry.getClassName() );
		}
		else if( obfEntry instanceof ArgumentEntry )
		{
			// arguments only appear in method declarations
			// since we only show declarations for obf classes, these are always obfuscated
			return true;
		}
		
		// assume everything else is not obfuscated
		return false;
	}
}