Skip to content

Commit

Permalink
Version 1.0.0
Browse files Browse the repository at this point in the history
Added I18n.java as well as the ability to translate messages into other languages
Tool#getName and Tool#getDescription have been renamed to Tool#name and Tool#description respectively
Added FixTool. This tool tries to fix .class and .jar files that can't be dedecompiled
Cleaned up some of the code
Added TODO comments
StringsTool now handles classes that have fields and/or methods properly
Added FilesCodeAnalyzer
Added JavassistAnalyzer
  • Loading branch information
OpticFusion1 committed Aug 5, 2022
1 parent 287d7f2 commit 95b32e5
Show file tree
Hide file tree
Showing 31 changed files with 1,062 additions and 140 deletions.
31 changes: 30 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>optic_fusion1</groupId>
<artifactId>Kitsune</artifactId>
<version>0.12.0</version>
<version>1.0.0</version>

<build>
<resources>
Expand Down Expand Up @@ -67,7 +67,24 @@
</plugins>
</build>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
Expand Down Expand Up @@ -104,6 +121,18 @@
<version>1.15</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.github.Col-E</groupId>
<artifactId>CAFED00D</artifactId>
<version>1.10.2</version>
<type>jar</type>
</dependency>
</dependencies>

<properties>
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/optic_fusion1/kitsune/Kitsune.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import optic_fusion1.kitsune.logging.CustomLogger;
import optic_fusion1.kitsune.shellparser.ParseException;
import optic_fusion1.kitsune.shellparser.ShellParser;
import optic_fusion1.kitsune.tool.impl.FixTool;
import optic_fusion1.kitsune.util.I18n;
import static optic_fusion1.kitsune.util.I18n.tl;

public class Kitsune implements Runnable {

Expand All @@ -36,19 +39,22 @@ public class Kitsune implements Runnable {
@Override
public void run() {
init();
LOGGER.info(tl("kitsune_program_loaded"));
handleCLI();
}

private void handleCLI() {
while (running) {
try {
List<String> args = ShellParser.parseString(SCANNER.nextLine());
if (args.isEmpty()) {
LOGGER.warn("You did not enter any arguments");
LOGGER.warn(tl("no_args_entered"));
continue;
}
Tool tool = TOOL_MANAGER.getTool(args.get(0));
if (tool == null) {
LOGGER.warn(args.get(0) + " is not a valid tool");
// TODO: Look into suggesting the closest tool to what's provided
LOGGER.warn(tl("invalid_tool", args.get(0)));
continue;
}
args.remove(0);
Expand All @@ -61,9 +67,14 @@ private void handleCLI() {

private void init() {
running = true;
registerTools(new StringsTool(), new AnalyzeTool());
LOGGER.info("Program loaded. Enter a command:");
handleCLI();
loadI18n();
registerTools(new StringsTool(), new AnalyzeTool(), new FixTool());
}

private void loadI18n() {
I18n I18n = new I18n();
// TODO: Add other lang files
I18n.updateLocale("en");
}

private void registerTools(Tool... tools) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

import java.util.List;
import optic_fusion1.kitsune.shellparser.ParseException;
import static optic_fusion1.kitsune.util.I18n.tl;

public class EscapeState extends State {

@Override
public List<String> parse(String parsing, String accumulator, List<String> parsed, State referrer) throws ParseException {
if (parsing.isEmpty()) {
throw new ParseException("Unexpected end of string after escape character");
throw new ParseException(tl("es_parse_exception"));
}
return referrer.parse(parsing.substring(1), accumulator + (char) parsing.getBytes()[0], parsed, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.List;
import optic_fusion1.kitsune.shellparser.ParseException;
import static optic_fusion1.kitsune.util.I18n.tl;

public class QuoteState extends State {

Expand All @@ -30,7 +31,7 @@ public QuoteState(char quote) {
@Override
public List<String> parse(final String parsing, final String accumulator, final List<String> parsed, final State referrer) throws ParseException {
if (parsing.isEmpty()) {
throw new ParseException("Mismatched quote character: " + this.quote);
throw new ParseException(tl("qs_parse_exception", this.quote));
}
final char c = (char) parsing.getBytes()[0];
if (c == '\\') {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/optic_fusion1/kitsune/tool/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public Tool(String name, String description) {

public abstract void run(List<String> args);

public String getName() {
public String name() {
return name;
}

public String getDescription() {
public String description() {
return description;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/optic_fusion1/kitsune/tool/ToolManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void addTool(Tool tool) {
if (tool == null) {
return;
}
TOOLS.putIfAbsent(tool.getName(), tool);
TOOLS.putIfAbsent(tool.name(), tool);
}

}
127 changes: 127 additions & 0 deletions src/main/java/optic_fusion1/kitsune/tool/impl/FixTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package optic_fusion1.kitsune.tool.impl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import me.coley.cafedude.InvalidClassException;
import static optic_fusion1.kitsune.Kitsune.LOGGER;
import optic_fusion1.kitsune.tool.Tool;
import static optic_fusion1.kitsune.util.I18n.tl;
import optic_fusion1.kitsune.util.Utils;
import static optic_fusion1.kitsune.util.Utils.checkFileExists;
import static optic_fusion1.kitsune.util.Utils.writeToFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.objectweb.asm.ClassWriter;

public class FixTool extends Tool {

public FixTool() {
super("fix", "Attempts to fix .class or .jar files to make them decompilable");
}

@Override
public void run(List<String> args) {
File input = new File(args.get(0));
if (!checkFileExists(input)) {
LOGGER.info(tl("file_does_not_exist", input.toPath()));
return;
}
if (!input.getName().endsWith(".jar") && !input.getName().endsWith(".class")) {
LOGGER.info(tl("file_invalid_extension", input.toPath()));
return;
}
if (input.isDirectory()) {
LOGGER.info(tl("file_is_directory", input.toPath()));
return;
}
if (input.getName().endsWith(".jar")) {
File output = new File(args.get(0) + "-fixed.jar");
if (!output.exists()) {
try {
output.createNewFile();
} catch (IOException ex) {
Logger.getLogger(FixTool.class.getName()).log(Level.SEVERE, null, ex);
}
}
handleJarFile(input, output);
return;
}
handleClassFile(input);
}

// TODO: Clean these methods up
private void handleClassFile(File inputFile) {
if (inputFile.getName().endsWith(".class/")) {
try {
FileUtils.copyFile(inputFile, new File(inputFile.getName().replace(".class/", ".class")));
} catch (IOException ex) {
Logger.getLogger(FixTool.class.getName()).log(Level.SEVERE, null, ex);
}
return;
}
File output = new File(inputFile.getName().replace(".class", "-fixed.class"));
try (FileInputStream inputStream = new FileInputStream(inputFile)) {
byte[] data = IOUtils.toByteArray(inputStream);
ClassWriter classWriter = Utils.stripIllegalAttributesAndData(data);
LOGGER.info(tl("writing_to_jar_file", output));
try (FileOutputStream outputStream = new FileOutputStream(output)) {
writeToFile(outputStream, new ByteArrayInputStream(classWriter.toByteArray()));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(StringsTool.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(StringsTool.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidClassException ex) {
Logger.getLogger(FixTool.class.getName()).log(Level.SEVERE, null, ex);
} catch (Throwable ex) {
Logger.getLogger(FixTool.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void handleJarFile(File inputFile, File outputFile) {
ZipOutputStream out = null;
try (ZipFile zipFile = new ZipFile(inputFile)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
out = new ZipOutputStream(new FileOutputStream(outputFile));
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (entry.getName().endsWith(".class/")) {
LOGGER.info(tl("invalid_extension", entry.getName()));
ZipEntry outEntry = new ZipEntry(entry.getName().replace(".class/", ".class"));
out.putNextEntry(outEntry);
LOGGER.info(tl("writing_to_jar_file", entry.getName()));
writeToFile(out, zipFile.getInputStream(entry));
continue;
}
if (entry.getName().endsWith(".class")) {
byte[] data = IOUtils.toByteArray(zipFile.getInputStream(entry));
ClassWriter classWriter = Utils.stripIllegalAttributesAndData(data);
ZipEntry newEntry = new ZipEntry(entry.getName());
out.putNextEntry(newEntry);
LOGGER.info(tl("writing_to_jar_file", entry.getName()));
writeToFile(out, new ByteArrayInputStream(classWriter.toByteArray()));
continue;
}
out.putNextEntry(entry);
LOGGER.info(tl("writing_to_jar_file", entry.getName()));
writeToFile(out, zipFile.getInputStream(entry));
}
out.close();
} catch (IOException ex) {
Logger.getLogger(FixTool.class.getName()).log(Level.SEVERE, null, ex);
} catch (Throwable ex) {
Logger.getLogger(FixTool.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Loading

0 comments on commit 95b32e5

Please sign in to comment.