forked from TellowKrinkle/DInput8HookingExample
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hook for overriding user account's name by reading a file called …
…"Driver"
- Loading branch information
Showing
1 changed file
with
68 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|