Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions nl_luainit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ ffi.cdef[[
DWORD64 nl_BaseOfDll;
LONG nl_attach(PVOID *ppPointer, PVOID pDetour);
LONG nl_detach(PVOID *ppPointer, PVOID pDetour);
DWORD tempPrintLastError();
typedef void CxxClass;
]]

Expand Down Expand Up @@ -399,19 +400,23 @@ end

-- Find a symbol index.
local function nl_symbol(name)
print("calling nl_symbol(\""..name.."\")")
local si = new("SYMBOL_INFO[1]")
si[0].SizeOfStruct = sizeof("SYMBOL_INFO")
si[0].MaxNameLen = 0
local ok = Dbghelp.SymFromName(NLAPI.nl_hProcess, name, si)
if ok then return si[0] end
local ret = Dbghelp.SymFromName(NLAPI.nl_hProcess, name, si)
if ret == 1 then return si[0] end
NLAPI.tempPrintLastError()
end

local function nl_type(name)
print("calling nl_type(\""..name.."\")")
local si = new("SYMBOL_INFO[1]")
si[0].SizeOfStruct = sizeof("SYMBOL_INFO")
si[0].MaxNameLen = 0
local ok = Dbghelp.SymGetTypeFromName(NLAPI.nl_hProcess, NLAPI.nl_BaseOfDll, name, si)
if ok then return si[0] end
local ret = Dbghelp.SymGetTypeFromName(NLAPI.nl_hProcess, NLAPI.nl_BaseOfDll, name, si)
if ret == 1 then return si[0] end
NLAPI.tempPrintLastError()
end

-- Get the value of a symbol.
Expand Down
26 changes: 26 additions & 0 deletions nl_payload.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ __declspec(dllexport) LONG nl_detach(PVOID *ppPointer, PVOID pDetour)
return ret;
}

// idk how to thread this through to Lua; this is probably not the best
// way but it works
__declspec(dllexport) DWORD tempPrintLastError()
{
// adapted from https://docs.microsoft.com/en-us/windows/win32/debug/retrieving-the-last-error-code

LPVOID lpMsgBuf;
DWORD code = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

printf("Error %d: %s", code, (char *)lpMsgBuf);

LocalFree(lpMsgBuf);

return 0; // returning b/c declaring the function as VOID instead of DWORD caused mysterious issues
}

static BOOL nlP_filexists(LPCTSTR szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);
Expand Down