From 295eceece371b516e771de93b6127bf728999483 Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 26 Jul 2014 19:24:00 -0400 Subject: initial commit so far source analysis is working. =) --- src/cuchaz/enigma/Deobfuscator.java | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/cuchaz/enigma/Deobfuscator.java (limited to 'src/cuchaz/enigma/Deobfuscator.java') diff --git a/src/cuchaz/enigma/Deobfuscator.java b/src/cuchaz/enigma/Deobfuscator.java new file mode 100644 index 0000000..83a2171 --- /dev/null +++ b/src/cuchaz/enigma/Deobfuscator.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * 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.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import com.strobel.assembler.metadata.JarTypeLoader; +import com.strobel.decompiler.Decompiler; +import com.strobel.decompiler.DecompilerSettings; +import com.strobel.decompiler.PlainTextOutput; + +public class Deobfuscator +{ + private File m_file; + private JarFile m_jar; + private DecompilerSettings m_settings; + + private static Comparator m_obfuscatedClassSorter; + + static + { + m_obfuscatedClassSorter = new Comparator( ) + { + @Override + public int compare( ClassFile a, ClassFile b ) + { + if( a.getName().length() != b.getName().length() ) + { + return a.getName().length() - b.getName().length(); + } + + return a.getName().compareTo( b.getName() ); + } + }; + } + + public Deobfuscator( File file ) + throws IOException + { + m_file = file; + m_jar = new JarFile( m_file ); + m_settings = DecompilerSettings.javaDefaults(); + m_settings.setTypeLoader( new JarTypeLoader( m_jar ) ); + m_settings.setForceExplicitImports( true ); + m_settings.setShowSyntheticMembers( true ); + } + + public List getObfuscatedClasses( ) + { + List classes = new ArrayList(); + Enumeration entries = m_jar.entries(); + while( entries.hasMoreElements() ) + { + JarEntry entry = entries.nextElement(); + + // get the class name + String className = toClassName( entry.getName() ); + if( className == null ) + { + continue; + } + + ClassFile classFile = new ClassFile( className ); + if( classFile.isObfuscated() ) + { + classes.add( classFile ); + } + } + Collections.sort( classes, m_obfuscatedClassSorter ); + return classes; + } + + // TODO: could go somewhere more generic + private static String toClassName( String fileName ) + { + final String suffix = ".class"; + + if( !fileName.endsWith( suffix ) ) + { + return null; + } + + return fileName.substring( 0, fileName.length() - suffix.length() ).replace( "/", "." ); + } + + public String getSource( final ClassFile classFile ) + { + StringWriter buf = new StringWriter(); + Decompiler.decompile( classFile.getName(), new PlainTextOutput( buf ), m_settings ); + return buf.toString(); + } +} -- cgit v1.2.3