Beispiel #1
0
        public bool Verify(OptionalHeader header, byte * pFilePointer, uint fileSize, PEHeader * pPeHeader)
        {
            //1. The name is not empty.
            if (GetNameAsString().Trim() == "") {
                return false;
            }

            //2. VirtualAddress is a multiple of the section alignment.
            if (VirtualAddress == 0 || (VirtualAddress % header.SectionAlignment) != 0) {
                return false;
            }

            //3. SizeOfRawData is a multiple of the file alignment.
            if ((SizeOfRawData % header.FileAlignment) != 0) {
                return false;
            }

            //4. PointerToRawData is a multiple of the file alignment
            if ((PointerToRawData % header.FileAlignment) != 0) {
                return false;
            }

            //5. PointerToRawData == 0 <-> SizeOfRawData == 0
            if ((PointerToRawData == 0) != (SizeOfRawData == 0)) {
                return false;
            }

            //6. If PointerToRawData is not null, it must fit inside the file.
            try {
                if (PointerToRawData > fileSize || checked(PointerToRawData + SizeOfRawData) > fileSize) {
                    return false;
                }
            } catch (OverflowException) {
                return false;
            }

            //7. PointerToRelocations must be 0.
            if (PointerToRelocations != 0) {
                return false;
            }

            //8. PointerToLineNumbers must be 0.
            if (PointerToLineNumbers != 0) {
                return false;
            }

            //9. NumberOfRelocations must be 0.
            if (NumberOfRelocations != 0) {
                return false;
            }

            //10. NumberOfLineNumbers must be 0.
            if (NumberOfLineNumbers != 0) {
                return false;
            }

            //11. The characteristics must be valid. We seperate out the check for
            //bits marked as reserved from the check for defined bits that we don't support.
            if ((Characteristics & SectionCharacteristics.Reserved) != 0) {
                return false;
            }

            if ((Characteristics & SectionCharacteristics.DisallowedFlags) != 0) {
                return false;
            }

            return true;
        }
Beispiel #2
0
        public bool Verify(uint fileSize, PEHeader * pPEHeader)
        {
            FluentAsserts.AssumeNotNull((void *)pPEHeader);
            fileSize.AssumeGT(0U);

            if (CodeSize > ImageSize) {
                return false;
            }

            if (InitializedDataSize > ImageSize) {
                return false;
            }

            if (UninitializedDataSize > ImageSize) {
                return false;
            }

            try {
                if (checked(CodeSize + InitializedDataSize + UninitializedDataSize) > ImageSize) {
                    return false;
                }
            }
            catch (OverflowException) {
                return false;
            }

            if (AddressOfEntryPoint != 0 && AddressOfEntryPoint < BaseOfCode) {
                return false;
            }

            if (SectionAlignment < FileAlignment) {
                return false;
            }

            if (FileAlignment < 512 || FileAlignment > 0x10000) {
                return false;
            }

            //Is file alignment a power of 2?
            if (((FileAlignment) & (FileAlignment - 1)) != 0) {
                return false;
            }

            if (Win32VersionValue != 0) {
                return false;
            }

            //Is the header size a multiple of the file alignment?
            if ((HeaderSize & (FileAlignment - 1)) != 0) {
                return false;
            }

            if (HeaderSize > fileSize) {
                return false;
            }

            try {
                //Is the reported header large enough to actually hold all the headers?
                if (
                    HeaderSize < checked(
                        LAYOUT_SIZE
                        + (NumberOfDataDirectories * sizeof(RVAAndSize))
                        + sizeof(PEHeader)
                        + PEFile.MSDosStubSize
                        + (pPEHeader->NumberOfSections * sizeof(SectionHeader))
                    )
                ) {
                    return false;
                }
            }
            catch (OverflowException) {
                return false;
            }

            if (
                Subsystem != ImageSubsystem.IMAGE_SUBSYSTEM_WINDOWS_CUI
                && Subsystem != ImageSubsystem.IMAGE_SUBSYSTEM_WINDOWS_GUI
            ) {
                return false;
            }

            if ((DllCharacteristics.RESERVED_BITS & DllCharacteristics) != 0) {
                return false;
            }

            if ((DllCharacteristics & DllCharacteristics.IMAGE_DLLCHARACTERISTICS_WDM_DRIVER) != 0) {
                return false;
            }

            if (StackReserveSize < StackCommitSize) {
                return false;
            }

            StackReserveSize.AssumeLTE(SIZE_MAX);
            StackCommitSize.AssumeLTE(SIZE_MAX);

            if (HeapReserveSize < HeapCommitSize) {
                return false;
            }

            HeapReserveSize.AssumeLTE(SIZE_MAX);
            HeapCommitSize.AssumeLTE(SIZE_MAX);

            if (LoaderFlags != 0) {
                return false;
            }

            if (NumberOfDataDirectories < 16) {
                return false;
            }

            if (! DataDirectories[15].IsZero()) {
                return false;
            }

            if (CLRRuntimeHeader.IsZero()) {
                return false;
            }

            if (CLRRuntimeHeader.Size < sizeof(CLRHeader)) {
                return false;
            }

            for (int i = 0; i < NumberOfDataDirectories; ++i) {
                if (DataDirectories[i].RVA > ImageSize) {
                    return false;
                }

                if (! DataDirectories[i].IsConsistent()) {
                    return false;
                }
            }

            return true;
        }