OSDN Git Service

Modify documents.
[ffftp/ffftp.git] / protectprocess.c
index a37fab2..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
@@ -100,9 +100,10 @@ DWORD g_ProcessProtectionLevel;
 DWORD g_LockedThread[MAX_LOCKED_THREAD];\r
 WCHAR* g_pTrustedFilenameTable[MAX_TRUSTED_FILENAME_TABLE];\r
 BYTE g_TrustedMD5HashTable[MAX_TRUSTED_MD5_HASH_TABLE][20];\r
+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
@@ -247,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
@@ -1011,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
@@ -1089,3 +1095,55 @@ BOOL RestartProtectedProcess(LPCTSTR Keyword)
        return bResult;\r
 }\r
 \r
+INT_PTR CALLBACK PasswordEditControlWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)\r
+{\r
+       switch(Msg)\r
+       {\r
+       case EM_GETPASSWORDCHAR:\r
+               break;\r
+       case EM_SETPASSWORDCHAR:\r
+               break;\r
+       default:\r
+               return CallWindowProcW(g_PasswordEditControlProc, hWnd, Msg, wParam, lParam);\r
+       }\r
+       return 0;\r
+}\r
+\r
+BOOL ProtectPasswordEditControl(HWND hWnd)\r
+{\r
+       BOOL bResult;\r
+       WCHAR ClassName[MAX_PATH];\r
+       WNDPROC Proc;\r
+       bResult = FALSE;\r
+       if(g_ProcessProtectionLevel & PROCESS_PROTECTION_PASSWORD_EDIT)\r
+       {\r
+               if(GetClassNameW(hWnd, ClassName, MAX_PATH) > 0)\r
+               {\r
+                       if(_wcsicmp(ClassName, WC_EDITW) == 0)\r
+                       {\r
+                               Proc = (WNDPROC)GetWindowLongPtrW(hWnd, GWLP_WNDPROC);\r
+                               if(Proc != (WNDPROC)PasswordEditControlWndProc)\r
+                               {\r
+                                       g_PasswordEditControlProc = Proc;\r
+                                       SetWindowLongPtrW(hWnd, GWLP_WNDPROC, (LONG_PTR)PasswordEditControlWndProc);\r
+                                       bResult = TRUE;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+       return bResult;\r
+}\r
+\r
+BOOL CALLBACK ProtectAllEditControlsEnumChildProc(HWND hwnd , LPARAM lParam)\r
+{\r
+       ProtectPasswordEditControl(hwnd);\r
+       return TRUE;\r
+}\r
+\r
+BOOL ProtectAllEditControls(HWND hWnd)\r
+{\r
+       if(g_ProcessProtectionLevel & PROCESS_PROTECTION_PASSWORD_EDIT)\r
+               EnumChildWindows(hWnd, ProtectAllEditControlsEnumChildProc, 0);\r
+       return TRUE;\r
+}\r
+\r