Skip to content

Commit

Permalink
Add hook for overriding user account's name by reading a file called …
Browse files Browse the repository at this point in the history
…"Driver"
  • Loading branch information
pathartl committed Feb 13, 2024
1 parent 133aeb3 commit 9991154
Showing 1 changed file with 68 additions and 70 deletions.
138 changes: 68 additions & 70 deletions MinimalDInput8Hook/CustomHooks.cpp
Original file line number Diff line number Diff line change
@@ -1,85 +1,83 @@
#include "stdafx.h"
#include "CustomHooks.h"

typedef HANDLE(WINAPI*CreateFileA_t)(
LPCSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile);

typedef HANDLE(WINAPI*CreateFileW_t)(
LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile);

CreateFileA_t OriginalCreateFileA;
CreateFileW_t OriginalCreateFileW;

HANDLE WINAPI CreateFileA_Wrapper(
LPCSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
)
{
// Do our custom stuff and parameter rewriting
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), lpFileName, (DWORD)strlen(lpFileName), nullptr, nullptr);
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), "\n", 1, nullptr, nullptr);

// Call the original CreateFileA function
return OriginalCreateFileA(
lpFileName,
dwDesiredAccess,
dwShareMode,
lpSecurityAttributes,
dwCreationDisposition,
dwFlagsAndAttributes,
hTemplateFile);
}
typedef BOOL(WINAPI*GetUserNameA_t)(
LPSTR lpBuffer,
LPDWORD pcbBuffer);

HANDLE WINAPI CreateFileW_Wrapper(
LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
GetUserNameA_t OriginalGetUserNameA;

BOOL WINAPI GetUserNameA_Wrapper(
LPSTR lpBuffer,
LPDWORD pcbBuffer
)
{
// Do our custom stuff and parameter rewriting
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), lpFileName, (DWORD)wcslen(lpFileName), nullptr, nullptr);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"\n", 1, nullptr, nullptr);

// Call the original CreateFileW function
return OriginalCreateFileW(
lpFileName,
dwDesiredAccess,
dwShareMode,
lpSecurityAttributes,
dwCreationDisposition,
dwFlagsAndAttributes,
hTemplateFile);
HANDLE hFile = CreateFile(
L"Driver",
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);

try {
if (hFile == INVALID_HANDLE_VALUE)
throw FALSE;

DWORD fileSize = GetFileSize(hFile, NULL);

if (fileSize == INVALID_FILE_SIZE) {
CloseHandle(hFile);
throw FALSE;
}

const DWORD bufferSize = 16; // Max amount of characters supported
char* buffer = new char[bufferSize + 1];

DWORD bytesRead;
if (ReadFile(hFile, buffer, bufferSize, &bytesRead, NULL) == FALSE) {
CloseHandle(hFile);
throw FALSE;
}

buffer[bytesRead] = '\0';

CloseHandle(hFile);

if (*pcbBuffer < bytesRead + 1) {
throw FALSE;
}

strcpy_s(lpBuffer, *pcbBuffer, buffer);

*pcbBuffer = static_cast<DWORD>(bytesRead);

return TRUE;

} catch (bool result) {
// Use default driver name on any error
const char* defaultUsername = "Driver";
size_t defaultUsernameLength = strlen(defaultUsername);

if (*pcbBuffer < defaultUsernameLength + 1)
return false;

strcpy_s(lpBuffer, *pcbBuffer, defaultUsername);

*pcbBuffer = static_cast<DWORD>(defaultUsernameLength);

return TRUE;
}
}

void SetupHooks()
{
// Create a console for Debug output
AllocConsole();
// AllocConsole();

// Setup hooks here, see examples below

OriginalCreateFileA = HookFunction("KERNEL32.dll", "CreateFileA", &CreateFileA_Wrapper);
OriginalCreateFileW = HookFunction("KERNEL32.dll", "CreateFileW", &CreateFileW_Wrapper);
OriginalGetUserNameA = HookFunction("ADVAPI32.dll", "GetUserNameA", &GetUserNameA_Wrapper);
}

0 comments on commit 9991154

Please sign in to comment.