diff options
| author | 2014-07-26 19:24:00 -0400 | |
|---|---|---|
| committer | 2014-07-26 19:24:00 -0400 | |
| commit | 295eceece371b516e771de93b6127bf728999483 (patch) | |
| tree | 20ff6a35de79997fc338d922cae934b8872b11a9 /src/cuchaz/enigma/Deobfuscator.java | |
| download | enigma-fork-295eceece371b516e771de93b6127bf728999483.tar.gz enigma-fork-295eceece371b516e771de93b6127bf728999483.tar.xz enigma-fork-295eceece371b516e771de93b6127bf728999483.zip | |
initial commit
so far source analysis is working. =)
Diffstat (limited to 'src/cuchaz/enigma/Deobfuscator.java')
| -rw-r--r-- | src/cuchaz/enigma/Deobfuscator.java | 109 |
1 files changed, 109 insertions, 0 deletions
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 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2014 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Public License v3.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/gpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma; | ||
| 12 | |||
| 13 | import java.io.File; | ||
| 14 | import java.io.IOException; | ||
| 15 | import java.io.StringWriter; | ||
| 16 | import java.util.ArrayList; | ||
| 17 | import java.util.Collections; | ||
| 18 | import java.util.Comparator; | ||
| 19 | import java.util.Enumeration; | ||
| 20 | import java.util.List; | ||
| 21 | import java.util.jar.JarEntry; | ||
| 22 | import java.util.jar.JarFile; | ||
| 23 | |||
| 24 | import com.strobel.assembler.metadata.JarTypeLoader; | ||
| 25 | import com.strobel.decompiler.Decompiler; | ||
| 26 | import com.strobel.decompiler.DecompilerSettings; | ||
| 27 | import com.strobel.decompiler.PlainTextOutput; | ||
| 28 | |||
| 29 | public class Deobfuscator | ||
| 30 | { | ||
| 31 | private File m_file; | ||
| 32 | private JarFile m_jar; | ||
| 33 | private DecompilerSettings m_settings; | ||
| 34 | |||
| 35 | private static Comparator<ClassFile> m_obfuscatedClassSorter; | ||
| 36 | |||
| 37 | static | ||
| 38 | { | ||
| 39 | m_obfuscatedClassSorter = new Comparator<ClassFile>( ) | ||
| 40 | { | ||
| 41 | @Override | ||
| 42 | public int compare( ClassFile a, ClassFile b ) | ||
| 43 | { | ||
| 44 | if( a.getName().length() != b.getName().length() ) | ||
| 45 | { | ||
| 46 | return a.getName().length() - b.getName().length(); | ||
| 47 | } | ||
| 48 | |||
| 49 | return a.getName().compareTo( b.getName() ); | ||
| 50 | } | ||
| 51 | }; | ||
| 52 | } | ||
| 53 | |||
| 54 | public Deobfuscator( File file ) | ||
| 55 | throws IOException | ||
| 56 | { | ||
| 57 | m_file = file; | ||
| 58 | m_jar = new JarFile( m_file ); | ||
| 59 | m_settings = DecompilerSettings.javaDefaults(); | ||
| 60 | m_settings.setTypeLoader( new JarTypeLoader( m_jar ) ); | ||
| 61 | m_settings.setForceExplicitImports( true ); | ||
| 62 | m_settings.setShowSyntheticMembers( true ); | ||
| 63 | } | ||
| 64 | |||
| 65 | public List<ClassFile> getObfuscatedClasses( ) | ||
| 66 | { | ||
| 67 | List<ClassFile> classes = new ArrayList<ClassFile>(); | ||
| 68 | Enumeration<JarEntry> entries = m_jar.entries(); | ||
| 69 | while( entries.hasMoreElements() ) | ||
| 70 | { | ||
| 71 | JarEntry entry = entries.nextElement(); | ||
| 72 | |||
| 73 | // get the class name | ||
| 74 | String className = toClassName( entry.getName() ); | ||
| 75 | if( className == null ) | ||
| 76 | { | ||
| 77 | continue; | ||
| 78 | } | ||
| 79 | |||
| 80 | ClassFile classFile = new ClassFile( className ); | ||
| 81 | if( classFile.isObfuscated() ) | ||
| 82 | { | ||
| 83 | classes.add( classFile ); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | Collections.sort( classes, m_obfuscatedClassSorter ); | ||
| 87 | return classes; | ||
| 88 | } | ||
| 89 | |||
| 90 | // TODO: could go somewhere more generic | ||
| 91 | private static String toClassName( String fileName ) | ||
| 92 | { | ||
| 93 | final String suffix = ".class"; | ||
| 94 | |||
| 95 | if( !fileName.endsWith( suffix ) ) | ||
| 96 | { | ||
| 97 | return null; | ||
| 98 | } | ||
| 99 | |||
| 100 | return fileName.substring( 0, fileName.length() - suffix.length() ).replace( "/", "." ); | ||
| 101 | } | ||
| 102 | |||
| 103 | public String getSource( final ClassFile classFile ) | ||
| 104 | { | ||
| 105 | StringWriter buf = new StringWriter(); | ||
| 106 | Decompiler.decompile( classFile.getName(), new PlainTextOutput( buf ), m_settings ); | ||
| 107 | return buf.toString(); | ||
| 108 | } | ||
| 109 | } | ||