[Ttssh2-commit] [6793] SetDllDirectory(), SetDefaultDllDirectories() が使える場合は呼ぶようにした。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2017年 6月 9日 (金) 23:32:37 JST


Revision: 6793
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/6793
Author:   doda
Date:     2017-06-09 23:32:37 +0900 (Fri, 09 Jun 2017)
Log Message:
-----------
SetDllDirectory(), SetDefaultDllDirectories() が使える場合は呼ぶようにした。

DLL Hijacking 対策。
現行 OS 向け。古い OS では効果ないけれど。

DLL 検索パスからプログラムディレクトリを外したが、それでもプログラムディレクトリはセキュアである必要がある。
なぜなら、TTXなんちゃら.DLL という DLL を作成されたら……

Modified Paths:
--------------
    trunk/teraterm/keycode/keycode.c
    trunk/teraterm/teraterm/teraterm.cpp
    trunk/teraterm/ttpmacro/ttmacro.cpp
    trunk/ttpmenu/ttpmenu.cpp

-------------- next part --------------
Modified: trunk/teraterm/keycode/keycode.c
===================================================================
--- trunk/teraterm/keycode/keycode.c	2017-06-09 14:32:34 UTC (rev 6792)
+++ trunk/teraterm/keycode/keycode.c	2017-06-09 14:32:37 UTC (rev 6793)
@@ -27,12 +27,30 @@
                    LPSTR lpszCmdLine,
                    int nCmdShow)
 {
+	typedef BOOL (WINAPI *pSetDllDir)(LPCSTR);
+	typedef BOOL (WINAPI *pSetDefDllDir)(DWORD);
+
 	WNDCLASS wc;
 	MSG msg;
 	HWND hWnd;
+	HMODULE module;
+	pSetDllDir setDllDir;
+	pSetDefDllDir setDefDllDir;
 
 	DoCover_IsDebuggerPresent();
 
+	if ((module = GetModuleHandle("kernel32.dll")) != NULL) {
+		if ((setDefDllDir = (pSetDefDllDir)GetProcAddress(module, "SetDefaultDllDirectories")) != NULL) {
+			// SetDefaultDllDirectories() が使える場合は、検索パスを %WINDOWS%\system32 のみに設定する
+			(*setDefDllDir)((DWORD)0x00000800); // LOAD_LIBRARY_SEARCH_SYSTEM32
+		}
+		else if ((setDllDir = (pSetDllDir)GetProcAddress(module, "SetDllDirectoryA")) != NULL) {
+			// SetDefaultDllDirectories() が使えなくても、SetDllDirectory() が使える場合は
+			// カレントディレクトリだけでも検索パスからはずしておく。
+			(*setDllDir)("");
+		}
+	}
+
 	if(!hPrevInstance) {
 		wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
 		wc.lpfnWndProc = MainWndProc;

Modified: trunk/teraterm/teraterm/teraterm.cpp
===================================================================
--- trunk/teraterm/teraterm/teraterm.cpp	2017-06-09 14:32:34 UTC (rev 6792)
+++ trunk/teraterm/teraterm/teraterm.cpp	2017-06-09 14:32:37 UTC (rev 6793)
@@ -38,6 +38,24 @@
 
 CTeraApp::CTeraApp()
 {
+	typedef BOOL (WINAPI *pSetDllDir)(LPCSTR);
+	typedef BOOL (WINAPI *pSetDefDllDir)(DWORD);
+
+	HMODULE module;
+	pSetDllDir setDllDir;
+	pSetDefDllDir setDefDllDir;
+
+	if ((module = GetModuleHandle("kernel32.dll")) != NULL) {
+		if ((setDefDllDir = (pSetDefDllDir)GetProcAddress(module, "SetDefaultDllDirectories")) != NULL) {
+			// SetDefaultDllDirectories() \x82\xAA\x8Eg\x82\xA6\x82\xE9\x8Fꍇ\x82́A\x8C\x9F\x8D\xF5\x83p\x83X\x82\xF0 %WINDOWS%\system32 \x82݂̂ɐݒ肷\x82\xE9
+			(*setDefDllDir)((DWORD)0x00000800); // LOAD_LIBRARY_SEARCH_SYSTEM32
+		}
+		else if ((setDllDir = (pSetDllDir)GetProcAddress(module, "SetDllDirectoryA")) != NULL) {
+			// SetDefaultDllDirectories() \x82\xAA\x8Eg\x82\xA6\x82Ȃ\xAD\x82Ă\xE0\x81ASetDllDirectory() \x82\xAA\x8Eg\x82\xA6\x82\xE9\x8Fꍇ\x82\xCD
+			// \x83J\x83\x8C\x83\x93\x83g\x83f\x83B\x83\x8C\x83N\x83g\x83\x8A\x82\xBE\x82\xAF\x82ł\xE0\x8C\x9F\x8D\xF5\x83p\x83X\x82\xA9\x82\xE7\x82͂\xB8\x82\xB5\x82Ă\xA8\x82\xAD\x81B
+			(*setDllDir)("");
+		}
+	}
 }
 
 // CTeraApp instance

Modified: trunk/teraterm/ttpmacro/ttmacro.cpp
===================================================================
--- trunk/teraterm/ttpmacro/ttmacro.cpp	2017-06-09 14:32:34 UTC (rev 6792)
+++ trunk/teraterm/ttpmacro/ttmacro.cpp	2017-06-09 14:32:37 UTC (rev 6793)
@@ -35,6 +35,24 @@
 
 CCtrlApp::CCtrlApp()
 {
+	typedef BOOL (WINAPI *pSetDllDir)(LPCSTR);
+	typedef BOOL (WINAPI *pSetDefDllDir)(DWORD);
+
+	HMODULE module;
+	pSetDllDir setDllDir;
+	pSetDefDllDir setDefDllDir;
+
+	if ((module = GetModuleHandle("kernel32.dll")) != NULL) {
+		if ((setDefDllDir = (pSetDefDllDir)GetProcAddress(module, "SetDefaultDllDirectories")) != NULL) {
+			// SetDefaultDllDirectories() が使える場合は、検索パスを %WINDOWS%\system32 のみに設定する
+			(*setDefDllDir)((DWORD)0x00000800); // LOAD_LIBRARY_SEARCH_SYSTEM32
+		}
+		else if ((setDllDir = (pSetDllDir)GetProcAddress(module, "SetDllDirectoryA")) != NULL) {
+			// SetDefaultDllDirectories() が使えなくても、SetDllDirectory() が使える場合は
+			// カレントディレクトリだけでも検索パスからはずしておく。
+			(*setDllDir)("");
+		}
+	}
 }
 
 /////////////////////////////////////////////////////////////////////////////

Modified: trunk/ttpmenu/ttpmenu.cpp
===================================================================
--- trunk/ttpmenu/ttpmenu.cpp	2017-06-09 14:32:34 UTC (rev 6792)
+++ trunk/ttpmenu/ttpmenu.cpp	2017-06-09 14:32:37 UTC (rev 6793)
@@ -2632,6 +2632,9 @@
    ======1=========2=========3=========4=========5=========6=========7======= */
 int WINAPI WinMain(HINSTANCE hI, HINSTANCE, LPSTR nCmdLine, int nCmdShow)
 {
+	typedef BOOL (WINAPI *pSetDllDir)(LPCSTR);
+	typedef BOOL (WINAPI *pSetDefDllDir)(DWORD);
+
 	MSG			msg;
 	HWND		hWnd;
 	WNDCLASS	winClass;
@@ -2638,7 +2641,22 @@
 	char		uimsg[MAX_UIMSG];
 	DWORD		dwErr;
 	int			fuLoad = LR_DEFAULTCOLOR;
+	HMODULE		module;
+	pSetDllDir	setDllDir;
+	pSetDefDllDir	setDefDllDir;
 
+	if ((module = GetModuleHandle("kernel32.dll")) != NULL) {
+		if ((setDefDllDir = (pSetDefDllDir)GetProcAddress(module, "SetDefaultDllDirectories")) != NULL) {
+			// SetDefaultDllDirectories() \x82\xAA\x8Eg\x82\xA6\x82\xE9\x8Fꍇ\x82́A\x8C\x9F\x8D\xF5\x83p\x83X\x82\xF0 %WINDOWS%\system32 \x82݂̂ɐݒ肷\x82\xE9
+			(*setDefDllDir)((DWORD)0x00000800); // LOAD_LIBRARY_SEARCH_SYSTEM32
+		}
+		else if ((setDllDir = (pSetDllDir)GetProcAddress(module, "SetDllDirectoryA")) != NULL) {
+			// SetDefaultDllDirectories() \x82\xAA\x8Eg\x82\xA6\x82Ȃ\xAD\x82Ă\xE0\x81ASetDllDirectory() \x82\xAA\x8Eg\x82\xA6\x82\xE9\x8Fꍇ\x82\xCD
+			// \x83J\x83\x8C\x83\x93\x83g\x83f\x83B\x83\x8C\x83N\x83g\x83\x8A\x82\xBE\x82\xAF\x82ł\xE0\x8C\x9F\x8D\xF5\x83p\x83X\x82\xA9\x82\xE7\x82͂\xB8\x82\xB5\x82Ă\xA8\x82\xAD\x81B
+			(*setDllDir)("");
+		}
+	}
+
 	checkIniFile();		//INI\x83t\x83@\x83C\x83\x8B/\x83\x8C\x83W\x83X\x83g\x83\x8A\x90ؑ\xD6
 
 	GetUILanguageFile(UILanguageFile, sizeof(UILanguageFile));



Ttssh2-commit メーリングリストの案内
Back to archive index