如何将带有ReadFile的文件读入NASM x86组件中的堆栈? [英] How do I read a file with ReadFile onto the stack in NASM x86 assembly?

查看:70
本文介绍了如何将带有ReadFile的文件读入NASM x86组件中的堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用OpenFile打开了一个文件,并用GetFileSize得到了它的大小.我希望使用ReadFile并将堆栈用作其所需的缓冲区,并在堆栈上分配足够的空间,以及从GetFileSize返回的文件的大小.当我运行此程序时,没有任何输出.

I have opened a file with OpenFile, and gotten its size with GetFileSize. I wish to use ReadFile and use the stack as the buffer it requires, allocating enough room on the stack with the size of the file returned from GetFileSize. When I run this I get no output.

这是我的代码...

extern GetStdHandle
extern GetModuleFileNameA
extern OpenFile
extern ReadFile
extern WriteFile
extern CloseHandle
extern GetFileSize
extern ExitProcess

import GetStdHandle kernel32.dll
import GetModuleFileNameA kernel32.dll
import OpenFile kernel32.dll
import ReadFile kernel32.dll
import WriteFile kernel32.dll
import CloseHandle kernel32.dll
import GetFileSize kernel32.dll
import ExitProcess kernel32.dll

global ..start

segment .code USE32

..start:

;Setting up the stack...
push ebp
mov ebp, esp

;Get standard output to console
push dword -11
call [GetStdHandle]
mov dword [hStdOut], eax

;Get filepath...
push dword filepath
push dword 0
call [GetModuleFileNameA]

;Outputting filepath
;Doesn't show up on the console but if you dump it to a file
;and edit it it is there...
;push dword 0
;push dword bytesRead
;push dword 128 ;Maximum path size for OpenFile...
;push dword filepath
;push dword [hStdOut]
;call [WriteFile]

;Opening the file and getting the handle...
push dword 0
push dword ofstruct
push dword filepath
mov dword [hSelfFile]

;Getting the file size...
push dword 0
push dword hSelfFile
call [GetFileSize]
mov dword [fSize], eax

;Allocating data on the stack...
sub esp, fSize

;Reading the file...
push dword 0
push dword bytesRead
push dword fSize
push dword ebp ;ebp or esp?
push dword [hSelfFile]
call [ReadFile]

;Outputting the read file...
push dword 0
push dword bytesRead
push dword fSize
push dword ebp ;ebp or esp?
push dword [hStdOut]
call [WriteFile]

;Closing the file handle...
push dword hSelfFile
call [CloseHandle]

;Cleaning up the stack...
mov esp, ebp
pop ebp

xor eax, eax
push eax
call [ExitProcess]

segment .data

segment .bss

hStdOut resd 1
hSelfFile resd 1
bytesRead resd 1
ofstruct resb 136 ;The size in bytes of ofstruct
fSize resd 1
filepath resb 128 ;Maximum path OpenFile will allow

我在做什么错了?

推荐答案

malloc?当Windows中包含内存功能时,为什么要链接到C库?

malloc? Why would you link to the C library when there are memory functions that are part of Windows?

除了代码中的所有错误外,您是否意识到自己正在尝试打开可执行文件并将内容打印到控制台?因为exe包含不可打印的字符,所以您不能这样做(虽然可以,但是您不会得到任何漂亮的东西),您会在屏幕上看到很多垃圾.

Besides all of the mistakes in your code, you do realize that you are trying to open an executable file and print the contents to the console? You cannot do that (well you can but you won't get anything pretty) since exes contain non printable characters, you will see a lot of garbage on the screen.

下面是一些示例代码,这些代码将在exes目录中打开一个文本文件,获取大小,分配一些内存来保存文件,读取文件,并将内容显示到控制台.

Here is some example code that will open a text file in the exes directory, get the size, allocate some memory to hold file, read file, and display the contents to the console.

这里绝对不涉及错误检查,您当然会在普通应用中添加错误检查.

There is absolutely no error checking involved here, you would of course add error checking in a normal app.

创建一个名为test.txt的文本文件,并将其保存在exes目录中.您可以使用此测试所需的任何文本填充该文件.我选择使用培根lorem ipsum生成器,因为我喜欢培根:-)

Create a text file called test.txt and save it in the exes directory. You can fill this file with whatever text you want for this test. I chose to use a bacon lorem ipsum generator, since I like bacon :-)

%define STD_OUTPUT_HANDLE -11
%define OPEN_EXISTING 3
%define NULL 0
%define HEAP_ZERO_MEMORY 8
%define FILE_READ_DATA 1

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
section .bss
hConOut         resd 1
lpBytesWritten  resd 1
hSelfFile       resd 1
hHeap           resd 1
lpFileSize      resd 1
hReadBuf        resd 1

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
section .data
szTestFile      db 'test.txt', 0
szCRLF          db 13, 10 

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global Start
section .text
Start:    
    call    GetProcessHeap                  ; get handle to apps heap
    mov     [hHeap], eax        

    push    STD_OUTPUT_HANDLE               ; um, self explanitory
    call    GetStdHandle
    mov     [hConOut], eax

    push    NULL
    push    NULL
    push    OPEN_EXISTING
    push    NULL
    push    0
    push    FILE_READ_DATA
    push    szTestFile
    call    CreateFileA                     ; Let's open the damn file
    mov     [hSelfFile], eax   

    push    NULL
    push    eax
    call    GetFileSize                     ; well, get the size of file to read
    add     eax, 1
    mov     [lpFileSize], eax

    push    eax
    push    HEAP_ZERO_MEMORY
    push    dword [hHeap]
    call    HeapAlloc                       ; now allocate some memory to hold contents
    mov     [hReadBuf], eax

    push    NULL
    push    lpBytesWritten
    push    dword [lpFileSize]
    push    dword [hReadBuf]
    push    dword [hSelfFile]
    call    ReadFile                        ; slurp it into memory

    push    dword [hSelfFile]
    call    CloseHandle                     ; don't need anymore

    push    NULL
    push    lpBytesWritten 
    push    dword [lpFileSize]
    push    dword [hReadBuf]
    push    dword [hConOut]
    call    WriteFile                       ; print to console

    push    NULL
    push    lpBytesWritten 
    push    2
    push    szCRLF                          
    push    dword [hConOut]
    call    WriteFile                       ; "display carrage return/linefeed"

    push    NULL
    push    hReadBuf
    call    HeapFree                        ; free the our buffer memory

    push    0
    call    ExitProcess

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
extern GetStdHandle
extern CreateFileA
extern ReadFile
extern WriteFile
extern CloseHandle
extern GetFileSize
extern ExitProcess
extern GetProcessHeap
extern HeapAlloc
extern HeapFree

这篇关于如何将带有ReadFile的文件读入NASM x86组件中的堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆