From ffcf6dafc29f3af4f730488744e2faca60a8d9f2 Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Wed, 14 Feb 2024 13:17:04 -0500 Subject: [PATCH] commit so I can work somewhere else lol --- platform/cert.c | 139 +++++++++++++++++++++++++++++++++++++++++++ platform/cert.h | 14 +++++ platform/config.c | 17 ++++++ platform/config.h | 2 + platform/meson.build | 2 + platform/platform.c | 7 +++ platform/platform.h | 2 + sivahook/dllmain.c | 1 + sivahook/unity.c | 4 ++ 9 files changed, 188 insertions(+) create mode 100644 platform/cert.c create mode 100644 platform/cert.h diff --git a/platform/cert.c b/platform/cert.c new file mode 100644 index 0000000..b1dae2b --- /dev/null +++ b/platform/cert.c @@ -0,0 +1,139 @@ +#include +#include + +#include +#include +#include + +#include "hook/table.h" + +#include "platform/cert.h" +#include "hook/procaddr.h" + +#include "util/dprintf.h" +#include "util/str.h" + +static CRITICAL_SECTION cert_lock; +static wchar_t path[MAX_PATH]; + +HCERTSTORE WINAPI hook_CertOpenStore( + LPCSTR lpszStoreProvider, + DWORD dwEncodingType, + HCRYPTPROV_LEGACY hCryptProv, + DWORD dwFlags, + const void *pvPara +); + +PCCERT_CONTEXT WINAPI hook_CertFindCertificateInStore( + HCERTSTORE hCertStore, + DWORD dwCertEncodingType, + DWORD dwFindFlags, + DWORD dwFindType, + const void *pvFindPara, + PCCERT_CONTEXT pPrevCertContext +); + +BOOL WINAPI hook_CertCloseStore( + HCERTSTORE hCertStore, + DWORD dwFlags +); + +HCERTSTORE (WINAPI *next_CertOpenStore)( + LPCSTR lpszStoreProvider, + DWORD dwEncodingType, + HCRYPTPROV_LEGACY hCryptProv, + DWORD dwFlags, + const void *pvPara +); + +PCCERT_CONTEXT (WINAPI *next_CertFindCertificateInStore)( + HCERTSTORE hCertStore, + DWORD dwCertEncodingType, + DWORD dwFindFlags, + DWORD dwFindType, + const void *pvFindPara, + PCCERT_CONTEXT pPrevCertContext +); + +BOOL (WINAPI *next_CertCloseStore)( + HCERTSTORE hCertStore, + DWORD dwFlags +); + +static const struct hook_symbol cert_syms[] = { + { + .name = "CertOpenStore", + .patch = hook_CertOpenStore, + .link = (void **) &next_CertOpenStore, + }, { + .name = "CertFindCertificateInStore", + .patch = hook_CertFindCertificateInStore, + .link = (void **) &next_CertFindCertificateInStore, + }, { + .name = "CertCloseStore", + .patch = hook_CertCloseStore, + .link = (void **) &next_CertCloseStore, + }, +}; + +HRESULT cert_hook_init(const struct cert_config *cfg) +{ + assert(cfg != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + + dprintf("Cert hook init\n"); + wcscpy_s(path, MAX_PATH, cfg->path); + InitializeCriticalSection(&cert_lock); + cert_hook_insert_hooks(NULL); + + proc_addr_table_push( + NULL, + "crypt32.dll", + (struct hook_symbol *) cert_syms, + _countof(cert_syms)); + + return S_OK; +} + +void cert_hook_insert_hooks(HMODULE target) +{ + hook_table_apply( + target, + "crypt32.dll", + cert_syms, + _countof(cert_syms)); +} + +HCERTSTORE WINAPI hook_CertOpenStore( + LPCSTR lpszStoreProvider, + DWORD dwEncodingType, + HCRYPTPROV_LEGACY hCryptProv, + DWORD dwFlags, + const void *pvPara +) +{ + +} + +PCCERT_CONTEXT WINAPI hook_CertFindCertificateInStore( + HCERTSTORE hCertStore, + DWORD dwCertEncodingType, + DWORD dwFindFlags, + DWORD dwFindType, + const void *pvFindPara, + PCCERT_CONTEXT pPrevCertContext +) +{ + +} + +BOOL WINAPI hook_CertCloseStore( + HCERTSTORE hCertStore, + DWORD dwFlags +) +{ + +} diff --git a/platform/cert.h b/platform/cert.h new file mode 100644 index 0000000..c8e22a4 --- /dev/null +++ b/platform/cert.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#include +#include + +struct cert_config { + bool enable; + wchar_t path[MAX_PATH]; +}; + +HRESULT cert_hook_init(const struct cert_config *cfg); +void cert_hook_insert_hooks(HMODULE target); \ No newline at end of file diff --git a/platform/config.c b/platform/config.c index 86cb9c8..4dfa277 100644 --- a/platform/config.c +++ b/platform/config.c @@ -9,6 +9,7 @@ #include #include +#include "platform/cert.h" #include "platform/clock.h" #include "platform/config.h" #include "platform/dns.h" @@ -24,6 +25,7 @@ void platform_config_load(struct platform_config *cfg, const wchar_t *filename) assert(cfg != NULL); assert(filename != NULL); + cert_config_load(&cfg->cert, filename); clock_config_load(&cfg->clock, filename); dns_config_load(&cfg->dns, filename); misc_config_load(&cfg->misc, filename); @@ -33,6 +35,21 @@ void platform_config_load(struct platform_config *cfg, const wchar_t *filename) syscfg_config_load(&cfg->syscfg, filename); } +void cert_config_load(struct cert_config *cfg, const wchar_t *filename) +{ + assert(cfg != NULL); + assert(filename != NULL); + + cfg->enable = GetPrivateProfileIntW(L"cert", L"enable", 1, filename); + GetPrivateProfileStringW( + L"cert", + L"path", + L"cert", + cfg->path, + _countof(cfg->path), + filename); +} + void clock_config_load(struct clock_config *cfg, const wchar_t *filename) { assert(cfg != NULL); diff --git a/platform/config.h b/platform/config.h index c22a552..e7a03b5 100644 --- a/platform/config.h +++ b/platform/config.h @@ -6,6 +6,7 @@ #include #include +#include "platform/cert.h" #include "platform/clock.h" #include "platform/dns.h" #include "platform/misc.h" @@ -19,6 +20,7 @@ void platform_config_load( struct platform_config *cfg, const wchar_t *filename); +void cert_config_load(struct cert_config *cfg, const wchar_t *filename); void clock_config_load(struct clock_config *cfg, const wchar_t *filename); void dns_config_load(struct dns_config *cfg, const wchar_t *filename); void misc_config_load(struct misc_config *cfg, const wchar_t *filename); diff --git a/platform/meson.build b/platform/meson.build index 368c5af..8a8dd3c 100644 --- a/platform/meson.build +++ b/platform/meson.build @@ -8,6 +8,8 @@ platform_lib = static_library( shlwapi_lib, ], sources : [ + 'cert.c', + 'cert.h', 'clock.c', 'clock.h', 'config.c', diff --git a/platform/platform.c b/platform/platform.c index bc3d20d..981666c 100644 --- a/platform/platform.c +++ b/platform/platform.c @@ -2,6 +2,7 @@ #include +#include "platform/cert.h" #include "platform/clock.h" #include "platform/dns.h" #include "platform/misc.h" @@ -22,6 +23,12 @@ HRESULT platform_hook_init( assert(game_id != 0); assert(redir_mod != NULL); + hr = cert_hook_init(&cfg->cert); + + if (FAILED(hr)) { + return hr; + } + hr = clock_hook_init(&cfg->clock); if (FAILED(hr)) { diff --git a/platform/platform.h b/platform/platform.h index 4ce9e7d..31e76cf 100644 --- a/platform/platform.h +++ b/platform/platform.h @@ -2,6 +2,7 @@ #include +#include "platform/cert.h" #include "platform/clock.h" #include "platform/dns.h" #include "platform/misc.h" @@ -11,6 +12,7 @@ #include "platform/syscfg.h" struct platform_config { + struct cert_config cert; struct clock_config clock; struct dns_config dns; struct misc_config misc; diff --git a/sivahook/dllmain.c b/sivahook/dllmain.c index 7478107..8ad1e2e 100644 --- a/sivahook/dllmain.c +++ b/sivahook/dllmain.c @@ -76,6 +76,7 @@ BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx) HRESULT hr; if (cause != DLL_PROCESS_ATTACH) { + dprintf("Connected\n"); return TRUE; } diff --git a/sivahook/unity.c b/sivahook/unity.c index 5c719be..2153e16 100644 --- a/sivahook/unity.c +++ b/sivahook/unity.c @@ -11,6 +11,8 @@ #include "hooklib/reg.h" #include "hook/procaddr.h" +#include "platform/cert.h" + #include "util/dprintf.h" static void dll_hook_insert_hooks(HMODULE target); @@ -104,6 +106,7 @@ static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name) path_hook_insert_hooks(result); reg_hook_insert_hooks(result); proc_addr_insert_hooks(result); + cert_hook_insert_hooks(result); } for (size_t i = 0; i < dep_hooks_len; i++) { @@ -115,6 +118,7 @@ static HMODULE WINAPI my_LoadLibraryW(const wchar_t *name) iohook_apply_hooks(dep_mod); serial_hook_apply_hooks(dep_mod); reg_hook_insert_hooks(dep_mod); + cert_hook_insert_hooks(dep_mod); } } }