Skip to content

Commit aa186d3

Browse files
committed
Fixing MzLoader false positives (relates to NationalSecurityAgency#1054)
1 parent 083b5f6 commit aa186d3

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/mz/DOSHeader.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import ghidra.app.util.bin.format.Writeable;
2424
import ghidra.app.util.bin.format.ne.InvalidWindowsHeaderException;
2525
import ghidra.app.util.bin.format.ne.WindowsHeader;
26+
import ghidra.app.util.bin.format.pe.InvalidNTHeaderException;
27+
import ghidra.app.util.bin.format.pe.NTHeader;
28+
import ghidra.app.util.bin.format.pe.PortableExecutable.SectionLayout;
2629
import ghidra.program.model.data.*;
2730
import ghidra.util.DataConverter;
2831
import ghidra.util.exception.DuplicateNameException;
@@ -271,6 +274,27 @@ public boolean hasNewExeHeader() {
271274
}
272275
return false;
273276
}
277+
278+
/**
279+
* Returns true if a PE header exists.
280+
* @return true if a PE header exists
281+
*/
282+
public boolean hasPeHeader() {
283+
if (e_lfanew >= 0 && e_lfanew <= 0x1000000) {
284+
try {
285+
NTHeader ntHeader =
286+
NTHeader.createNTHeader(reader, e_lfanew, SectionLayout.FILE, false, false);
287+
if (ntHeader != null && ntHeader.getOptionalHeader() != null) {
288+
return true;
289+
}
290+
}
291+
catch (InvalidNTHeaderException | IOException e) {
292+
// Fall through and return false
293+
}
294+
}
295+
return false;
296+
}
297+
274298
/**
275299
* Returns true if the DOS magic number is correct
276300
* @return true if the DOS magic number is correct

Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws
7171
}
7272
OldStyleExecutable ose = new OldStyleExecutable(RethrowContinuesFactory.INSTANCE, provider);
7373
DOSHeader dos = ose.getDOSHeader();
74-
if (dos.isDosSignature() && !dos.hasNewExeHeader()) {
74+
if (dos.isDosSignature() && !dos.hasNewExeHeader() && !dos.hasPeHeader()) {
7575
List<QueryResult> results =
7676
QueryOpinionService.query(getName(), "" + dos.e_magic(), null);
7777
for (QueryResult result : results) {

0 commit comments

Comments
 (0)