OSDN Git Service

Fix bugs of hooking routines.
[ffftp/ffftp.git] / protectprocess.c
index f0669a8..b5a271e 100644 (file)
@@ -73,7 +73,7 @@ BOOL IsModuleTrusted(LPCWSTR Filename);
 // フック対象のコードを置換してフックを開始\r
 #define SET_HOOK_FUNCTION(name) HookFunctionInCode(p_##name, h_##name, &c_##name, FALSE)\r
 // フック対象を呼び出す前に対象のコードを復元\r
-#define START_HOOK_FUNCTION(name) HookFunctionInCode(p_##name, h_##name, &c_##name, TRUE)\r
+#define BEGIN_HOOK_FUNCTION(name) HookFunctionInCode(p_##name, h_##name, &c_##name, TRUE)\r
 // フック対象を呼び出した後に対象のコードを置換\r
 #define END_HOOK_FUNCTION(name) HookFunctionInCode(p_##name, h_##name, NULL, FALSE)\r
 \r
@@ -103,7 +103,7 @@ BYTE g_TrustedMD5HashTable[MAX_TRUSTED_MD5_HASH_TABLE][20];
 WNDPROC g_PasswordEditControlProc;\r
 \r
 // 以下フック関数\r
-// フック対象を呼び出す場合は前後でSTART_HOOK_FUNCTIONとEND_HOOK_FUNCTIONを実行する必要がある\r
+// フック対象を呼び出す場合は前後でBEGIN_HOOK_FUNCTIONとEND_HOOK_FUNCTIONを実行する必要がある\r
 \r
 HMODULE WINAPI h_LoadLibraryA(LPCSTR lpLibFileName)\r
 {\r
@@ -248,57 +248,62 @@ BOOL UnlockThreadLock()
 BOOL HookFunctionInCode(void* pOriginal, void* pNew, void* pBackupCode, BOOL bRestore)\r
 {\r
        BOOL bResult;\r
-       DWORD Protect;\r
-#if defined(_X86_)\r
-       BYTE JumpCode[HOOK_JUMP_CODE_LENGTH] = {0xe9, 0x00, 0x00, 0x00, 0x00};\r
-       size_t Relative;\r
-       Relative = (size_t)pNew - (size_t)pOriginal - HOOK_JUMP_CODE_LENGTH;\r
-       memcpy(&JumpCode[1], &Relative, 4);\r
        bResult = FALSE;\r
-       if(bRestore)\r
+#if defined(_X86_)\r
        {\r
-               if(VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, PAGE_EXECUTE_READWRITE, &Protect))\r
+               BYTE JumpCode[HOOK_JUMP_CODE_LENGTH] = {0xe9, 0x00, 0x00, 0x00, 0x00};\r
+               size_t Relative;\r
+               DWORD Protect;\r
+               Relative = (size_t)pNew - (size_t)pOriginal - HOOK_JUMP_CODE_LENGTH;\r
+               memcpy(&JumpCode[1], &Relative, 4);\r
+               if(bRestore)\r
                {\r
-                       memcpy(pOriginal, pBackupCode, HOOK_JUMP_CODE_LENGTH);\r
-                       VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, Protect, &Protect);\r
-                       bResult = TRUE;\r
+                       if(VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, PAGE_EXECUTE_READWRITE, &Protect))\r
+                       {\r
+                               memcpy(pOriginal, pBackupCode, HOOK_JUMP_CODE_LENGTH);\r
+                               VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, Protect, &Protect);\r
+                               bResult = TRUE;\r
+                       }\r
                }\r
-       }\r
-       else\r
-       {\r
-               if(pBackupCode)\r
-                       memcpy(pBackupCode, pOriginal, HOOK_JUMP_CODE_LENGTH);\r
-               if(VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, PAGE_EXECUTE_READWRITE, &Protect))\r
+               else\r
                {\r
-                       memcpy(pOriginal, &JumpCode, HOOK_JUMP_CODE_LENGTH);\r
-                       VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, Protect, &Protect);\r
-                       bResult = TRUE;\r
+                       if(pBackupCode)\r
+                               memcpy(pBackupCode, pOriginal, HOOK_JUMP_CODE_LENGTH);\r
+                       if(VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, PAGE_EXECUTE_READWRITE, &Protect))\r
+                       {\r
+                               memcpy(pOriginal, &JumpCode, HOOK_JUMP_CODE_LENGTH);\r
+                               VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, Protect, &Protect);\r
+                               bResult = TRUE;\r
+                       }\r
                }\r
        }\r
 #elif defined(_AMD64_)\r
-       BYTE JumpCode[HOOK_JUMP_CODE_LENGTH] = {0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};\r
-       size_t Absolute;\r
-       Absolute = (size_t)pOriginal;\r
-       memcpy(&JumpCode[6], &Absolute, 8);\r
-       bResult = FALSE;\r
-       if(bRestore)\r
        {\r
-               if(VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, PAGE_EXECUTE_READWRITE, &Protect))\r
+               BYTE JumpCode[HOOK_JUMP_CODE_LENGTH] = {0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};\r
+               size_t Absolute;\r
+               DWORD Protect;\r
+               Absolute = (size_t)pNew;\r
+               memcpy(&JumpCode[6], &Absolute, 8);\r
+               bResult = FALSE;\r
+               if(bRestore)\r
                {\r
-                       memcpy(pOriginal, pBackupCode, HOOK_JUMP_CODE_LENGTH);\r
-                       VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, Protect, &Protect);\r
-                       bResult = TRUE;\r
+                       if(VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, PAGE_EXECUTE_READWRITE, &Protect))\r
+                       {\r
+                               memcpy(pOriginal, pBackupCode, HOOK_JUMP_CODE_LENGTH);\r
+                               VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, Protect, &Protect);\r
+                               bResult = TRUE;\r
+                       }\r
                }\r
-       }\r
-       else\r
-       {\r
-               if(pBackupCode)\r
-                       memcpy(pBackupCode, pOriginal, HOOK_JUMP_CODE_LENGTH);\r
-               if(VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, PAGE_EXECUTE_READWRITE, &Protect))\r
+               else\r
                {\r
-                       memcpy(pOriginal, &JumpCode, HOOK_JUMP_CODE_LENGTH);\r
-                       VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, Protect, &Protect);\r
-                       bResult = TRUE;\r
+                       if(pBackupCode)\r
+                               memcpy(pBackupCode, pOriginal, HOOK_JUMP_CODE_LENGTH);\r
+                       if(VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, PAGE_EXECUTE_READWRITE, &Protect))\r
+                       {\r
+                               memcpy(pOriginal, &JumpCode, HOOK_JUMP_CODE_LENGTH);\r
+                               VirtualProtect(pOriginal, HOOK_JUMP_CODE_LENGTH, Protect, &Protect);\r
+                               bResult = TRUE;\r
+                       }\r
                }\r
        }\r
 #endif\r
@@ -1012,13 +1017,13 @@ BOOL EnableLoadLibraryHook(BOOL bEnable)
        {\r
                bResult = TRUE;\r
 #ifdef USE_CODE_HOOK\r
-               if(!END_HOOK_FUNCTION(LoadLibraryA))\r
+               if(!BEGIN_HOOK_FUNCTION(LoadLibraryA))\r
                        bResult = FALSE;\r
-               if(!END_HOOK_FUNCTION(LoadLibraryW))\r
+               if(!BEGIN_HOOK_FUNCTION(LoadLibraryW))\r
                        bResult = FALSE;\r
-               if(!END_HOOK_FUNCTION(LoadLibraryExA))\r
+               if(!BEGIN_HOOK_FUNCTION(LoadLibraryExA))\r
                        bResult = FALSE;\r
-               if(!END_HOOK_FUNCTION(LoadLibraryExW))\r
+               if(!BEGIN_HOOK_FUNCTION(LoadLibraryExW))\r
                        bResult = FALSE;\r
 #endif\r
 #ifdef USE_IAT_HOOK\r