Skip to content

Instantly share code, notes, and snippets.

@lucko
Created Mar 2, 2018
Embed
What would you like to do?
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodNode;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class StringFinder {
public static void main(String[] args) throws IOException {
List<Map.Entry<String, String>> results = new ArrayList<>();
try (JarFile in = new JarFile(new File("input.jar"))) {
for (Enumeration<JarEntry> entries = in.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
continue;
}
try (InputStream entryIn = in.getInputStream(entry)) {
ClassReader cr = new ClassReader(entryIn);
ClassNode classNode = new ClassNode();
cr.accept(classNode, 0);
for (MethodNode methodNode : classNode.methods) {
Iterator<AbstractInsnNode> insnIterator = methodNode.instructions.iterator();
while (insnIterator.hasNext()) {
AbstractInsnNode insnNode = insnIterator.next();
if (insnNode.getType() != AbstractInsnNode.LDC_INSN) {
continue;
}
Object constant = ((LdcInsnNode) insnNode).cst;
if (constant instanceof String) {
String str = ((String) constant);
results.add(new SimpleEntry<>(classNode.name + "." + methodNode.name, str));
}
}
}
}
}
}
System.out.println("Found " + results.size() + " results");
for (Map.Entry<String, String> entry : results) {
System.out.println(entry.getKey() + ": '" + entry.getValue() + "'");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment