Issue with hooking CreateFileA

Just starting out? Need help? Post your questions and find answers here.
Opcode
Enthusiast
Enthusiast
Posts: 137
Joined: Thu Jul 18, 2013 4:58 am

Issue with hooking CreateFileA

Post by Opcode »

I'm hooking CreateFileA within a process using the hooking functions here. I've hooked other functions without issue.

Code: Select all

Prototype.l OriginalCreateFileA(lpFileName.s, dwDesiredAccess.i, dwShareMode.i, *lpSecurityAttributes.SECURITY_ATTRIBUTES, dwCreationDisposition.i, dwFlagsAndAttributes.i, hTemplateFile.i)

Global o_CreateFileA.OriginalCreateFileA

Procedure.l MyCreateFileA(lpFileName.s, dwDesiredAccess.i, dwShareMode.i, *lpSecurityAttributes.SECURITY_ATTRIBUTES, dwCreationDisposition.i, dwFlagsAndAttributes.i, hTemplateFile.i)
  
  ProcedureReturn o_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, *lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile)
  
EndProcedure

Code: Select all

InlineHook("Kernel32.dll", "CreateFileA",  @MyCreateFileA(),  @o_CreateFileA)
The problematic line seems to be:

Code: Select all

ProcedureReturn o_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, *lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile)
Calling GetLastError_() returns 6 (ERROR_INVALID_HANDLE).

Now if I change it up and use a unhook/rehook approach with something like this and importing/calling CreateFileA_() unhooked and rehooking before returning, everything works fine...