Skip to content

Ghidra is a software reverse engineering (SRE) framework

Fixing MzLoader false positives (relates to #1054)

Ryan Kurtzaa186d306dfa
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/mz/DOSHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/mz/DOSHeader.java
index e7f406cc37..970c96df7d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/mz/DOSHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/mz/DOSHeader.java
@@ -23,6 +23,9 @@ import ghidra.app.util.bin.format.FactoryBundledWithBinaryReader;
import ghidra.app.util.bin.format.Writeable;
import ghidra.app.util.bin.format.ne.InvalidWindowsHeaderException;
import ghidra.app.util.bin.format.ne.WindowsHeader;
+import ghidra.app.util.bin.format.pe.InvalidNTHeaderException;
+import ghidra.app.util.bin.format.pe.NTHeader;
+import ghidra.app.util.bin.format.pe.PortableExecutable.SectionLayout;
import ghidra.program.model.data.*;
import ghidra.util.DataConverter;
import ghidra.util.exception.DuplicateNameException;
@@ -271,6 +274,27 @@ public class DOSHeader implements StructConverter, Writeable {
}
return false;
}
+
+ /**
+ * Returns true if a PE header exists.
+ * @return true if a PE header exists
+ */
+ public boolean hasPeHeader() {
+ if (e_lfanew >= 0 && e_lfanew <= 0x1000000) {
+ try {
+ NTHeader ntHeader =
+ NTHeader.createNTHeader(reader, e_lfanew, SectionLayout.FILE, false, false);
+ if (ntHeader != null && ntHeader.getOptionalHeader() != null) {
+ return true;
+ }
+ }
+ catch (InvalidNTHeaderException | IOException e) {
+ // Fall through and return false
+ }
+ }
+ return false;
+ }
+
/**
* Returns true if the DOS magic number is correct
* @return true if the DOS magic number is correct
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java
index a281102a3c..b7d1c29bcc 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java
@@ -71,7 +71,7 @@ public class MzLoader extends AbstractLibrarySupportLoader {
}
OldStyleExecutable ose = new OldStyleExecutable(RethrowContinuesFactory.INSTANCE, provider);
DOSHeader dos = ose.getDOSHeader();
- if (dos.isDosSignature() && !dos.hasNewExeHeader()) {
+ if (dos.isDosSignature() && !dos.hasNewExeHeader() && !dos.hasPeHeader()) {
List<QueryResult> results =
QueryOpinionService.query(getName(), "" + dos.e_magic(), null);
for (QueryResult result : results) {