huettenhain / dhrake

The Delphi Hand Rake
147 stars 17 forks source link

DhrakeInit MalformedInputException when opening IDC file #3

Closed nmz787 closed 3 years ago

nmz787 commented 3 years ago

the IDC file (linked here: https://github.com/crypto2011/IDR/issues/63#issuecomment-737040633) was generated from the latest crypto2011 IDR (actually they provided the IDC as I couldn't get exporting it to work locally). When trying to open in Ghidra with your DhrakeInit script, I was getting "file not found" error, but with some debugging it turned out to actually be a MalformedInputException, here's the stack trace:

DhrakeInit.java> Running...
DhrakeInit.java> [Dhrake] file not found: /home/user/Documents/shared/Smi50.idc... error Input length = 1
DhrakeInit.java> [Dhrake] java.nio.charset.MalformedInputException: Input length = 1
    at java.base/java.nio.charset.CoderResult.throwException(CoderResult.java:274)
    at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
    at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
    at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
    at java.base/java.nio.file.Files.readAllLines(Files.java:3330)
    at DhrakeInit.importSymbolsFromIDC(DhrakeInit.java:226)
    at DhrakeInit.run(DhrakeInit.java:385)
    at ghidra.app.script.GhidraScript.executeNormal(GhidraScript.java:379)
    at ghidra.app.script.GhidraScript.doExecute(GhidraScript.java:234)
    at ghidra.app.script.GhidraScript.execute(GhidraScript.java:212)
    at ghidra.app.plugin.core.script.RunScriptTask.run(RunScriptTask.java:47)
    at ghidra.util.task.Task.monitoredRun(Task.java:124)
    at ghidra.util.task.TaskRunner.lambda$startTaskThread$1(TaskRunner.java:94)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

Here's the relevant chunk of script code:

try {
            List<String> stringList = Files.readAllLines(idc.toPath(), Charset.defaultCharset());
            lines = stringList.toArray(new String[]{});
        } catch (IOException e) {
            this.logMsg("file not found: %s... error %s", idc.getAbsolutePath(), e.getMessage());
            return false;
        }

which was changed to this, to get the debug and past the exception for this particular file:

try {
            List<String> stringList = Files.readAllLines(idc.toPath(), StandardCharsets.ISO_8859_1);
                        //Charset.defaultCharset());
            lines = stringList.toArray(new String[]{});
        } catch (IOException e) {
            this.logMsg("file not found: %s... error %s", idc.getAbsolutePath(), e.getMessage());
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String stackTrace = sw.toString();
                        this.logMsg("%s", stackTrace);
            return false;
        }

I tried to determine the text encoding, but not sure it helped much, as I ended up just trying StandardCharsets.UTF_8 and then StandardCharsets.ISO_8859_1.

user:~/Documents$ enca -L none shared/Smi50.idc 
7bit ASCII characters
  CRLF line terminators
huettenhain commented 3 years ago

Hey there! I get the same problem, but I would suggest a fairly straightforward solution instead:

$ cat Smi50.idc | grep MakeNameEx >> Smi50_2.idc

and then simply load Smi50_2.idc. Can you check if that works for you?

marceloalencar commented 3 years ago

Had the same issue. This works for me.