1
0
mirror of synced 2024-11-12 00:40:49 +01:00

misc: add system version hook

This commit is contained in:
Kevin Trocolli 2023-09-18 03:56:15 -04:00
parent 4de39f2682
commit a0050a5fac
4 changed files with 152 additions and 8 deletions

View File

@ -43,6 +43,11 @@ static LSTATUS reg_hook_query_val_locked(
void *bytes,
uint32_t *nbytes);
static LSTATUS reg_hook_open_locked_a(
HKEY parent,
const char *name,
HKEY *out);
/* API hooks */
static LSTATUS WINAPI hook_RegOpenKeyExW(
@ -52,6 +57,13 @@ static LSTATUS WINAPI hook_RegOpenKeyExW(
uint32_t access,
HKEY *out);
static LSTATUS WINAPI hook_RegOpenKeyExA(
HKEY parent,
const char *name,
uint32_t flags,
uint32_t access,
HKEY *out);
static LSTATUS WINAPI hook_RegCreateKeyExW(
HKEY parent,
const wchar_t *name,
@ -108,6 +120,13 @@ static LSTATUS (WINAPI *next_RegOpenKeyExW)(
uint32_t access,
HKEY *out);
static LSTATUS (WINAPI *next_RegOpenKeyExA)(
HKEY parent,
const char *name,
uint32_t flags,
uint32_t access,
HKEY *out);
static LSTATUS (WINAPI *next_RegCreateKeyExW)(
HKEY parent,
const wchar_t *name,
@ -160,6 +179,10 @@ static const struct hook_symbol reg_hook_syms[] = {
.name = "RegOpenKeyExW",
.patch = hook_RegOpenKeyExW,
.link = (void **) &next_RegOpenKeyExW,
},{
.name = "RegOpenKeyExA",
.patch = hook_RegOpenKeyExA,
.link = (void **) &next_RegOpenKeyExA,
}, {
.name = "RegCreateKeyExW",
.patch = hook_RegCreateKeyExW,
@ -369,6 +392,70 @@ static LSTATUS reg_hook_open_locked(
return err;
}
static LSTATUS reg_hook_open_locked_a(
HKEY parent,
const char *name,
HKEY *out)
{
struct reg_hook_key *key;
LSTATUS err;
size_t i;
wchar_t *name_w;
size_t name_c;
*out = NULL;
mbstowcs_s(&name_c, NULL, 0, name, 0);
name_w = malloc(name_c * sizeof(wchar_t));
if (name_w == NULL) {
return ERROR_OUTOFMEMORY;
}
mbstowcs_s(NULL, name_w, name_c, name, name_c - 1);
for (i = 0 ; i < reg_hook_nkeys ; i++) {
/* Assume reg keys are referenced from a root key and not from some
intermediary key */
key = &reg_hook_keys[i];
if (key->root == parent && wstr_ieq(key->name, name_w)) {
break;
}
}
/* (Bail out if we didn't find anything; this causes the open/create call
to be passed onward down the hook chain) */
if (i >= reg_hook_nkeys) {
return ERROR_SUCCESS;
}
/* Assume only one handle will be open at a time */
if (key->handle != NULL) {
return ERROR_SHARING_VIOLATION;
}
/* Open a unique HKEY handle that we can use to identify accesses to
this virtual registry key. We open a read-only handle to an arbitrary
registry key that we can reliably assume exists and isn't one of the
hardcoded root handles. HKLM\SOFTWARE will suffice for this purpose. */
err = next_RegOpenKeyExA(
HKEY_LOCAL_MACHINE,
"SOFTWARE",
0,
KEY_READ,
out);
if (err == ERROR_SUCCESS) {
key->handle = *out;
}
return err;
}
static LSTATUS WINAPI hook_RegOpenKeyExW(
HKEY parent,
const wchar_t *name,
@ -397,6 +484,34 @@ static LSTATUS WINAPI hook_RegOpenKeyExW(
return err;
}
static LSTATUS WINAPI hook_RegOpenKeyExA(
HKEY parent,
const char *name,
uint32_t flags,
uint32_t access,
HKEY *out)
{
LSTATUS err;
if (out == NULL) {
return ERROR_INVALID_PARAMETER;
}
EnterCriticalSection(&reg_hook_lock);
err = reg_hook_open_locked_a(parent, name, out);
LeaveCriticalSection(&reg_hook_lock);
if (err == ERROR_SUCCESS) {
if (*out != NULL) {
//dprintf("Registry: Opened virtual key %s\n", name);
} else {
err = next_RegOpenKeyExA(parent, name, flags, access, out);
}
}
return err;
}
static LSTATUS WINAPI hook_RegCreateKeyExW(
HKEY parent,
const wchar_t *name,

View File

@ -214,6 +214,13 @@ void misc_config_load(
cfg->block_input_hook = GetPrivateProfileIntW(L"misc", L"blockInputHook", 1, filename);
cfg->show_cursor_hook = GetPrivateProfileIntW(L"misc", L"showCursorHook", 1, filename);
GetPrivateProfileStringW(
L"misc",
L"systemVersion",
L"XXX100-1-NA-SYS0-A00",
cfg->system_version,
_countof(cfg->system_version),
filename);
}
void es3sec_config_load(struct es3sec_config *cfg, const wchar_t *filename)

View File

@ -5,6 +5,7 @@
#include <stdint.h>
#include "hook/table.h"
#include "hooklib/reg.h"
#include "platform/misc.h"
@ -16,13 +17,14 @@ static BOOL WINAPI my_BlockInput(BOOL fBlockIt);
static int WINAPI my_ShowCursor(BOOL bShow);
static BOOL WINAPI my_GetCursorInfo(PCURSORINFO pci);
static HRESULT reg_read_sys_ver(void *bytes, uint32_t *nbytes);
static BOOL (WINAPI *next_BlockInput)(BOOL fBlockIt);
static int (WINAPI *next_ShowCursor)(BOOL bShow);
static BOOL (WINAPI *next_GetCursorInfo)(PCURSORINFO pci);
BOOL block_input_hook = true;
BOOL show_cursor_hook = true;
int real_cursor_state = 0;
static struct misc_config config;
static int real_cursor_state = 0;
static const struct hook_symbol misc_hook_syms[] = {
{
@ -40,12 +42,19 @@ static const struct hook_symbol misc_hook_syms[] = {
}
};
static const struct reg_hook_val nbgi_reg[] = {
{
.name = L"SystemVersion",
.type = REG_SZ,
.read = reg_read_sys_ver
}
};
HRESULT misc_hook_init(const struct misc_config *cfg)
{
assert(cfg != NULL);
show_cursor_hook = cfg->show_cursor_hook;
block_input_hook = cfg->block_input_hook;
memcpy(&config, cfg, sizeof(*cfg));
dprintf("Misc: init\n");
@ -55,12 +64,18 @@ HRESULT misc_hook_init(const struct misc_config *cfg)
misc_hook_syms,
_countof(misc_hook_syms));
reg_hook_push_key(
HKEY_LOCAL_MACHINE,
L"SOFTWARE\\NBGI",
nbgi_reg,
_countof(nbgi_reg));
return S_OK;
}
static BOOL WINAPI my_BlockInput(BOOL fBlockIt)
{
if (!block_input_hook) {
if (!config.block_input_hook) {
return next_BlockInput(fBlockIt);
}
@ -71,7 +86,7 @@ static BOOL WINAPI my_BlockInput(BOOL fBlockIt)
static int WINAPI my_ShowCursor(BOOL bShow)
{
if (!show_cursor_hook) {
if (!config.show_cursor_hook) {
return next_ShowCursor(bShow);
}
@ -87,7 +102,7 @@ static int WINAPI my_ShowCursor(BOOL bShow)
static BOOL WINAPI my_GetCursorInfo(PCURSORINFO pci)
{
if (!show_cursor_hook) {
if (!config.show_cursor_hook) {
return next_GetCursorInfo(pci);
}
@ -103,4 +118,10 @@ static BOOL WINAPI my_GetCursorInfo(PCURSORINFO pci)
}
return true;
}
static HRESULT reg_read_sys_ver(void *bytes, uint32_t *nbytes)
{
dprintf("Misc: Get system version\n");
return reg_hook_read_wstr(bytes, nbytes, config.system_version);
}

View File

@ -7,6 +7,7 @@
struct misc_config {
bool block_input_hook;
bool show_cursor_hook;
wchar_t system_version[256];
};
HRESULT misc_hook_init(const struct misc_config *cfg);