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

标签 windows dos portable-executable

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

据我所知,PE 与 MS-DOS 的不同之处在于它具有 MS-DOS 没有的 header ,但出于某种原因,我拥有的一些样本被 TrID 识别为 MS-DOS,尽管它们是PE。

我找不到有关该主题的任何文档,并且我进行了很多搜索。

谢谢!

最佳答案

识别 MS-DOS 可执行文件的问题在于,从技术上讲,Windows PECOFF 可执行文件也是有效的 MS-DOS 可执行文件。 PECOFF 可执行文件以“MS-DOS stub ”为前缀,这是一个完整的 MS-DOS 程序,在大多数可执行文件中都会打印一条消息,例如“此程序无法在 DOS 模式下运行”。

所以要做的第一件事就是查看 MS-DOS 可执行文件头,看看它是否有效。它看起来像这样(来自 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 的偏移量。然后需要检查带有“PE\0\0”签名的 header 星号,即两个字符“P”和“E”,后面跟着两个0字节。

请注意,可以在偏移量 3Ch 给出的位置找到其他字母,例如用于 16 位 Windows 可执行文件、VxD 和 32 位 OS/2 的“NE”、“LE”、“LX”分别是可执行文件。这些其他可执行格式也具有 MS-DOS stub ,并以相同的方式定位其真实 header 。

关于windows - 有没有安全的方法来识别 MS-DOS 可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37432309/

相关文章:

windows - 如何调试 'Xaml.UnhandledExceptionEventArgs'异常?

c# - 当文件被锁定时,队列文件操作以备后用

powershell - 将 Dos 命令转换为 Powershell 命令

windows - 应用程序 list 和程序集 list 之间的区别

c# - 使用c#分析可移植可执行文件

c++ - 使 visual studio 项目可移植,问题

c++ - ifstream 在读取文件 3 次后中断

c++ - 有什么方法可以阻止 _popen 打开 DOS 窗口吗?

dos - 如何删除除 DOS 中的某些文件之外的所有文件/子目录?

ant - 如何从 ant exec 任务运行多个命令