From 9474da083df3331c1b65175fb5a5ab0dc3be9d0b Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Sun, 10 Dec 2023 18:47:17 -0500 Subject: [PATCH] path: add delete and move file hooks --- hooklib/path.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/hooklib/path.c b/hooklib/path.c index 0c6fe42..07647f7 100644 --- a/hooklib/path.c +++ b/hooklib/path.c @@ -97,6 +97,11 @@ static BOOL WINAPI hook_RemoveDirectoryA(const char *lpFileName); static BOOL WINAPI hook_RemoveDirectoryW(const wchar_t *lpFileName); +BOOL WINAPI hook_DeleteFileA(LPCSTR lpFileName); +BOOL WINAPI hook_DeleteFileW(LPWSTR lpFileName); +BOOL WINAPI hook_MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName); +BOOL WINAPI hook_MoveFileW(LPWSTR lpFileName, LPWSTR lpNewFileName); + /* Link pointers */ static BOOL (WINAPI *next_CreateDirectoryA)( @@ -177,6 +182,11 @@ static BOOL (WINAPI *next_RemoveDirectoryA)(const char *lpFileName); static BOOL (WINAPI *next_RemoveDirectoryW)(const wchar_t *lpFileName); +BOOL (WINAPI *next_DeleteFileA)(LPCSTR lpFileName); +BOOL (WINAPI *next_DeleteFileW)(LPWSTR lpFileName); +BOOL (WINAPI *next_MoveFileA)(LPCSTR lpExistingFileName, LPCSTR lpNewFileName); +BOOL (WINAPI *next_MoveFileW)(LPWSTR lpFileName, LPWSTR lpNewFileName); + /* Hook table */ static const struct hook_symbol path_hook_syms[] = { @@ -244,6 +254,22 @@ static const struct hook_symbol path_hook_syms[] = { .name = "RemoveDirectoryW", .patch = hook_RemoveDirectoryW, .link = (void **) &next_RemoveDirectoryW, + }, { + .name = "DeleteFileA", + .patch = hook_DeleteFileA, + .link = (void **) &next_DeleteFileA, + }, { + .name = "DeleteFileW", + .patch = hook_DeleteFileW, + .link = (void **) &next_DeleteFileW, + }, { + .name = "MoveFileA", + .patch = hook_MoveFileA, + .link = (void **) &next_MoveFileA, + }, { + .name = "MoveFileW", + .patch = hook_MoveFileW, + .link = (void **) &next_MoveFileW, } }; @@ -854,3 +880,81 @@ static BOOL WINAPI hook_RemoveDirectoryW(const wchar_t *lpFileName) return ok; } + +BOOL WINAPI hook_DeleteFileA(LPCSTR lpFileName) +{ + char *trans; + BOOL ok; + + ok = path_transform_a(&trans, lpFileName); + + if (!ok) { + return FALSE; + } + + ok = next_DeleteFileA(trans ? trans : lpFileName); + + free(trans); + + return ok; +} + +BOOL WINAPI hook_DeleteFileW(LPWSTR lpFileName) +{ + wchar_t *trans; + BOOL ok; + + ok = path_transform_w(&trans, lpFileName); + + if (!ok) { + return FALSE; + } + + ok = next_DeleteFileW(trans ? trans : lpFileName); + + free(trans); + + return ok; +} + +BOOL WINAPI hook_MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName) +{ + char *trans1; + char *trans2; + BOOL ok; + + ok = path_transform_a(&trans1, lpExistingFileName); + ok = path_transform_a(&trans2, lpNewFileName); + + if (!ok) { + return FALSE; + } + + ok = next_MoveFileA(trans1 ? trans1 : lpExistingFileName, trans2 ? trans2 : lpNewFileName); + + free(trans1); + free(trans2); + + return ok; +} + +BOOL WINAPI hook_MoveFileW(LPWSTR lpExistingFileName, LPWSTR lpNewFileName) +{ + wchar_t *trans1; + wchar_t *trans2; + BOOL ok; + + ok = path_transform_w(&trans1, lpExistingFileName); + ok = path_transform_w(&trans2, lpNewFileName); + + if (!ok) { + return FALSE; + } + + ok = next_MoveFileW(trans1 ? trans1 : lpExistingFileName, trans2 ? trans2 : lpNewFileName); + + free(trans1); + free(trans2); + + return ok; +}