Ghidra is a software reverse engineering (SRE) framework
GP-51: Improving COFF header checks
Ryan Kurtz6a0037b69b46
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/formats/CoffBinaryAnalysisCommand.java b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/formats/CoffBinaryAnalysisCommand.javaindex 4e2f8e1932..5946e7e801 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/formats/CoffBinaryAnalysisCommand.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/formats/CoffBinaryAnalysisCommand.java@@ -30,7 +30,6 @@ import ghidra.program.flatapi.FlatProgramAPI;import ghidra.program.model.address.Address;import ghidra.program.model.data.*;import ghidra.program.model.listing.*;-import ghidra.program.model.mem.Memory;import ghidra.util.exception.CancelledException;import ghidra.util.exception.NotEmptyException;import ghidra.util.task.TaskMonitor;@@ -52,10 +51,10 @@ public class CoffBinaryAnalysisCommand extends FlatProgramAPIif (!BinaryLoader.BINARY_NAME.equals(format)) {return false;}- Memory memory = program.getMemory();- short magic =- memory.getShort(program.getAddressFactory().getDefaultAddressSpace().getAddress(0));- return CoffMachineType.isMachineTypeDefined(magic);++ ByteProvider provider =+ MemoryByteProvider.createDefaultAddressSpaceByteProvider(program, false);+ return new CoffFileHeader(provider).isValid(provider);}catch (Exception e) {return false;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffFileHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffFileHeader.javaindex 94abaa44ce..eeb14911fc 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffFileHeader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffFileHeader.java@@ -271,6 +271,39 @@ public class CoffFileHeader implements StructConverter {return _aoutHeader;}+ /**+ * Tests if this {@link CoffFileHeader} is valid.+ * <p>+ * To avoid false positives when the machine type is+ * {@link CoffMachineType#IMAGE_FILE_MACHINE_UNKNOWN}, we do an additional check on some extra+ * bytes at the beginning of the given {@link ByteProvider} to make sure the entire file isn't+ * all 0's.+ *+ * @param provider The {@link ByteProvider} that this {@link CoffFileHeader} was created from+ * @return True if this is a is a valid {@link CoffFileHeader}; otherwise, false+ * @throws IOException if there was an IO-related issue+ */+ public boolean isValid(ByteProvider provider) throws IOException {+ final int COFF_NULL_SANITY_CHECK_LEN = 64;++ if (getMagic() == CoffMachineType.IMAGE_FILE_MACHINE_UNKNOWN /* ie. == 0 */ &&+ provider.length() > COFF_NULL_SANITY_CHECK_LEN) {+ byte[] headerBytes = provider.readBytes(0, COFF_NULL_SANITY_CHECK_LEN);+ boolean allZeros = true;+ for (byte b : headerBytes) {+ allZeros = (b == 0);+ if (!allZeros) {+ break;+ }+ }+ if (allZeros) {+ return false;+ }+ }++ return CoffMachineType.isMachineTypeDefined(getMagic());+ }+@Overridepublic DataType toDataType() throws DuplicateNameException, IOException {Structure struct = new StructureDataType(StructConverterUtil.parseName(getClass()), 0);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/CoffLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/CoffLoader.javaindex 0c69fc83bb..f2a28e774a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/CoffLoader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/CoffLoader.java@@ -46,7 +46,6 @@ public class CoffLoader extends AbstractLibrarySupportLoader {public final static String COFF_NAME = "Common Object File Format (COFF)";public static final String FAKE_LINK_OPTION_NAME = "Attempt to link sections located at 0x0";static final boolean FAKE_LINK_OPTION_DEFAULT = true;- private static final int COFF_NULL_SANITY_CHECK_LEN = 64;// where do sections start if they're all zero??? this affects object files// and if we're high enough (!!!) the scalar operand analyzer will work@@ -103,25 +102,7 @@ public class CoffLoader extends AbstractLibrarySupportLoader {CoffFileHeader header = new CoffFileHeader(provider);- // Check to prevent false positives when the file is full of '\0' bytes.- // If the machine type is unknown (0), check the first 64 bytes of the file and bail if- // they are also all 0.- if (header.getMagic() == CoffMachineType.IMAGE_FILE_MACHINE_UNKNOWN /* ie. == 0 */ &&- provider.length() > COFF_NULL_SANITY_CHECK_LEN) {- byte[] headerBytes = provider.readBytes(0, COFF_NULL_SANITY_CHECK_LEN);- boolean allZeros = true;- for (byte b : headerBytes) {- allZeros = (b == 0);- if (!allZeros) {- break;- }- }- if (allZeros) {- return loadSpecs;- }- }-- if (CoffMachineType.isMachineTypeDefined(header.getMagic())) {+ if (header.isValid(provider)) {header.parseSectionHeaders(provider);if (isVisualStudio(header) != isMicrosoftFormat()) {