(FASM)我无法看到文件的内容

时间:2014-08-20 22:54:15

标签: file winapi buffer fasm

我的问题如下:我在Assembler中编写软件。它读取Windows可执行文件,并比较前两个字节,如果它们等于" MZ"。

ReadFile API在其工作中取得了成功,但是当我尝试将缓冲区的前两个字节与MZ符号进行比较时,它却失败了。

我不知道如何访问缓冲区中API函数存储的数据。如果找到MZ标志,程序应跳转到特殊程序。但是,我使用真正的可执行文件测试代码,但它不会跳转到特殊例程。

这是我的源代码:(短,最小,并重现我的错误):

;I use the 4 for the open mode because it means "Read/Write".
;I open the file and Get its filesize for later use.
;The handle was stored in eax by the API
invoke CreateFile, cFileName, 4, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0
mov [file_handle], eax

;Read 8000 bytes from the file
invoke  ReadFile, [file_handle], buffer, 8000, bytesread, 0 ; Now read the full file

;The error is here.
;The program should compare the bytes, but when I test it
;with a real executable, it jumps to the wrong routine.
cmp buffer, "MZ" ;Check if the executable contains the MZ sign (4d5a) in hex
jne bad_executable
jmp good_executable

请帮帮我。谢谢你的回答

2 个答案:

答案 0 :(得分:2)

我认为你有一个字节顺序问题。来自FASM manual

  

引用字符串,当在表达式中遇到时,将被转换为数字 - 第一个字符将成为数字的最低有效字节

所以“MZ”将变为0x5A4D。

答案 1 :(得分:1)

错误在CreateFile函数中。 API期望正确的开放模式。显然,开放模式很糟糕,因此我的功能不起作用。只需使用有效的开放模式,如“只读”或“只写”,它就可以工作。