I'm trying to write to a file using x64 assembly with MASM on Windows, but the content is not being written correctly. I've included my code below, but I'm not sure where the issue is. I'm using the CreateFileA and WriteFile functions. Can someone please help me identify and fix the problem?
Assembler: MASM Architecture: x64 Platform: Windows
Code snippet:
INCLUDELIB kernel32.lib
ExitProcess PROTO
CreateFileA PROTO
WriteFile PROTO
clearRegisters MACRO
XOR RAX, RAX
XOR RDX, RDX
XOR RCX, RCX
XOR R8, R8
XOR R9, R9
ENDM
GENERIC_WRITE EQU 04000000h ;Solution 040000000h
FILE_SHARE_READ EQU 1
FILE_SHARE_WRITE EQU 2
OPEN_ALWAYS EQU 4
FILE_ATTRIBUTE_NORMAL EQU 128
.DATA
filePath BYTE "D:/thanks/ASSEMBLY.txt", 0
fileHandle QWORD ?
txt BYTE "Learning Assembly for shellcode development.", 0
num DWORD ?
.CODE
main PROC
clearRegisters
SUB RSP, 64
LEA RCX, filePath
MOV RDX, GENERIC_WRITE
MOV R8, FILE_SHARE_READ OR FILE_SHARE_WRITE
MOV R9, 0
MOV QWORD PTR [RSP+32], OPEN_ALWAYS
MOV QWORD PTR [RSP+40], FILE_ATTRIBUTE_NORMAL
MOV QWORD PTR [RSP+48], 0
CALL CreateFileA
MOV fileHandle, RAX
clearRegisters
MOV RCX, fileHandle
LEA RDX, txt
MOV R8, LENGTHOF txt
LEA R9, num
MOV QWORD PTR [ RSP+32 ], 0
CALL WriteFile
TEST RAX, RAX
CALL ExitProcess
main ENDP
END
i have implement correctly agruments of CreateFileA and Writefile but after wirtefile call, rax content zero(0) which indicate it's fail.
This works on my computer.
I added CreateDirectoryA
.
GENERIC_WRITE
was 04000000h
in Your code and should be 40000000h
.
https://learn.microsoft.com/en-us/windows/win32/secauthz/generic-access-rights
includelib "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\lib\x64\msvcmrt.lib"
includelib "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\um\x64\kernel32.lib"
includelib "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\lib\x64\vcruntime.lib"
includelib "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\ucrt\x64\ucrt.lib"
ExitProcess PROTO
CreateFileA PROTO
CreateDirectoryA PROTO
WriteFile PROTO
clearRegisters MACRO
XOR RAX, RAX
XOR RDX, RDX
XOR RCX, RCX
XOR R8, R8
XOR R9, R9
ENDM
GENERIC_WRITE EQU 40000000h
FILE_SHARE_READ EQU 1
FILE_SHARE_WRITE EQU 2
OPEN_ALWAYS EQU 4
FILE_ATTRIBUTE_NORMAL EQU 128
.DATA
pathName byte "D:\thanks",0
fileName byte "D:\thanks\ASSEMBLY.txt", 0
fileHandle QWORD ?
txt BYTE "Learning Assembly for shellcode development.", 0
num DWORD ?
.CODE
main PROC
sub RSP, 56
clearRegisters
;----------------------------------------------------------------------
; Create Directory
LEA RCX,pathName
MOV RDX,0
CALL CreateDirectoryA
;----------------------------------------------------------------------
; Create File
LEA RCX, fileName
MOV RDX, GENERIC_WRITE
MOV R8, FILE_SHARE_READ OR FILE_SHARE_WRITE
MOV R9, 0
MOV QWORD PTR [RSP+32], OPEN_ALWAYS
MOV QWORD PTR [RSP+40], FILE_ATTRIBUTE_NORMAL
MOV QWORD PTR [RSP+48], 0
CALL CreateFileA
MOV fileHandle, RAX
clearRegisters
;----------------------------------------------------------------------
; Write to the File
MOV RCX, fileHandle
LEA RDX, txt
MOV R8, LENGTHOF txt
LEA R9, num
MOV QWORD PTR [RSP+32], 0
CALL WriteFile
TEST RAX, RAX
CALL ExitProcess
main ENDP
END