pondělí 10. listopadu 2014

Dekompilace dekompilátoru - dekompilace .jar souborů

Info o práci s .jar soubory je na wiki Procyonu velice málo. Zkompilovaný procyon, ale toto dovede, takže někde musí být funkce main, která si s tím poradí =). Pro kontrolu jsem si vyzkoušel dekompilovat jar soubor samotného procyonu:

$ java -jar procyon-decompiler-0.5.27.jar -jar procyon-decompiler-0.5.27.jar -o out

Zabere to ale nějakou chvíli. Výsledek se uloží do složky "out". V této složce jsem dále spustil příkaz:


$ find | grep .java | while read soub; do echo $soub;cat $soub | grep "main("; done

Tento kód vypíše s názvem souboru, zda našel řetězec "main(", což by mělo jednoznačně specfikovat funkci main.

Funkce main se nachází v
./out/com/strobel/decompiler/DecompilerDriver.java

Podle kódu okolo mainu lze už práci s jar soubory nějak pochopit. V následujícím kódu demonstruju jednoduchý způsob kompletní dekompilace (výsledek je dekompilovaný kód vypsaný do terminálu) jar souboru.

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
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import com.strobel.assembler.InputTypeLoader;
import com.strobel.assembler.metadata.CompositeTypeLoader;
import com.strobel.assembler.metadata.ITypeLoader;
import com.strobel.assembler.metadata.JarTypeLoader;
import com.strobel.core.StringUtilities;
import com.strobel.decompiler.*;

public class test {
 public static void main(String[] args) { 
  

  final String internalName = "/path/file.jar";
  try {
   JarFile jarFile = new JarFile(internalName);
   JarTypeLoader jarFileloader = new JarTypeLoader(jarFile);
   PlainTextOutput output;
   
   DecompilerSettings settings = new DecompilerSettings();
   settings.setShowSyntheticMembers(false);
   settings.setTypeLoader(new CompositeTypeLoader(new ITypeLoader[] { jarFileloader, new InputTypeLoader() }));
   
   Enumeration<JarEntry> entries = jarFile.entries();

   while (entries.hasMoreElements()){
    JarEntry entry = entries.nextElement();
    System.out.println(entry.getName());
    if (entry.getName().endsWith(".class")){
     output = new PlainTextOutput();
     String name = StringUtilities.removeRight(entry.getName(), ".class");
     Decompiler.decompile(name, output, settings);
     System.out.print(output);
    }
   }
   
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

Zdroje:
https://bitbucket.org/mstrobel/procyon/downloads
https://bitbucket.org/mstrobel/procyon/wiki/Java%20Decompiler

Žádné komentáře:

Okomentovat