有没有安全的方法来识别 MS-DOS 可执行文件?

Is there a safe way to identify MS-DOS executable?

我正在尝试从我拥有的可执行文件中识别并过滤掉所有 MS-DOS 可执行文件。

据我所知,PE 与 MS-DOS 的不同之处在于他有 headers 而 MS-DOS 没有,但出于某种原因,我的一些样本是被 TrID 识别为 MS-DOS,即使它们是 PE。

我找不到关于这个主题的任何文档,我搜索了很多。

谢谢!

识别 MS-DOS 可执行文件的问题是技术上 Windows PECOFF 可执行文件也是有效的 MS-DOS 可执行文件。 PECOFF 可执行文件以 "MS-DOS Stub" 为前缀,这是一个完整的 MS-DOS 程序,在大多数可执行文件中打印一条消息,如 "This program cannot be run in DOS mode".

所以要做的第一件事就是查看 MS-DOS 可执行文件 header,看看它是否有效。它看起来像这样(来自 Ralf Brown 的中断列表):

 00h  2 BYTEs .EXE signature, either "MZ" or "ZM" (5A4Dh or 4D5Ah)
        (see also #01593)
 02h  WORD    number of bytes in last 512-byte page of executable
 04h  WORD    total number of 512-byte pages in executable (includes any
      partial last page)
 06h  WORD    number of relocation entries
 08h  WORD    header size in paragraphs
 0Ah  WORD    minimum paragraphs of memory required to allocate in addition
        to executable's size
 0Ch  WORD    maximum paragraphs to allocate in addition to executable's size
 0Eh  WORD    initial SS relative to start of executable
 10h  WORD    initial SP
 12h  WORD    checksum (one's complement of sum of all words in executable)
 14h  DWORD   initial CS:IP relative to start of executable
 18h  WORD    offset within header of relocation table
      40h or greater for new-format (NE,LE,LX,W3,PE,etc.) executable
 1Ah  WORD    overlay number (normally 0000h = main program)

要检查的键值位于偏移量 00h 和 18h 处。文件开头的两个字节,签名,必须是"MZ"或54ADh。虽然 "ZM" 也适用于 MS-DOS 程序,但 Windows 要求 PECOFF 可执行文件使用更常见的 "MZ" 签名。接下来要检查的是偏移量 18h 处的 16 位值。它需要大于或等于 40h 才能成为 PECOFF 可执行文件。

如果检查出偏移量 00h 和 18h 处的值,那么下一步要做的是读取偏移量 3Ch 处的 32 位值。这包含实际 PECOFF header 的偏移量。然后,您需要检查 header 个带有签名 "PE[=27=][=27=]" 的星星,即两个字符 "P" 和 "E",后跟两个 0 字节。

请注意,可以在偏移量 3Ch 的位置找到其他字母,例如 "NE"、"LE"、"LX" 用于 16 位 Windows 可执行文件、VxD 和 32 位 OS/2 可执行文件。这些其他可执行格式也有 MS-DOS 存根,并以相同的方式定位它们真正的 header。