Parcourir la source

Debug打印封装成函数

sat23 il y a 3 ans
Parent
commit
d5e20139af

+ 8 - 7
DLLInjection/RegistryInjection/InjectionDLL/InjectionDLL/PipeClient.cpp

@@ -1,5 +1,6 @@
 #include "StdAfx.h"
 #include "PipeClient.h"
+#include "Utility.h"
 
 #define BUFSIZE 512
 
@@ -54,14 +55,14 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
         if ( pInstance->m_hPipeInst != INVALID_HANDLE_VALUE ) {
             // 1分钟检测;
             Sleep(60000);
-            OutputDebugString(_T("<Injecter> m_hPipeInst 已存在\n"));
+            Utility::dprintf(_T("m_hPipeInst 已存在\n"));
             continue;
         }
 
         // 等待10秒;
         if ( !WaitNamedPipe(pInstance->m_szPipeName, 10000) )
         {// 如果管道不存在,会立即返回而不考虑超时值,所以此处仍要Sleep;
-            OutputDebugString(_T("<Injecter> WaitNamedPipe 失败\n"));
+            Utility::dprintf(_T("<%ld> WaitNamedPipe 失败\n"), Utility::g_WndInfo.dwProcessId);
             Sleep(10000);
             continue;
         }
@@ -86,7 +87,7 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
                 NULL);                      // don't set maximum time 
 
             if (!fSuccess) {
-                _tprintf( TEXT("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() ); 
+                Utility::dprintf(_T("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() ); 
                 CloseHandle(pInstance->m_hPipeInst);
             }
         }
@@ -95,12 +96,12 @@ DWORD CPipeClient::ConnectThread(LPVOID lpParam)
             // Exit if an error other than ERROR_PIPE_BUSY occurs. 
             if ( GetLastError() != ERROR_PIPE_BUSY ) 
             {
-                _tprintf( TEXT("Could not open pipe. GLE=%d\n"), GetLastError() ); 
+                Utility::dprintf(_T("Could not open pipe. GLE=%d\n"), GetLastError() ); 
             }
         }
     }
 
-    OutputDebugString(_T("<Injecter> ConnectThread 退出\n"));
+    Utility::dprintf(_T("<%ld> ConnectThread 退出\n"), Utility::g_WndInfo.dwProcessId);
 
     return 0;
 }
@@ -151,7 +152,7 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
         }
         else
         {
-            _tprintf( TEXT("ReadFile from pipe failed. GLE=%d\n"), dwError );
+            Utility::dprintf(_T("ReadFile from pipe failed. GLE=%d\n"), dwError );
             if ( dwError == ERROR_PIPE_NOT_CONNECTED )
             {
                 CloseHandle(pInstance->m_hPipeInst);
@@ -160,7 +161,7 @@ DWORD CPipeClient::ReadMsgThread(LPVOID lpParam)
         }
     }
 
-    OutputDebugString(_T("<Injecter> ReadMsgThread 退出\n"));
+    Utility::dprintf(_T("<%ld> ReadMsgThread 退出\n"),Utility::g_WndInfo.dwProcessId);
 
     return 0;
 }

+ 24 - 0
DLLInjection/RegistryInjection/InjectionDLL/InjectionDLL/Utility.cpp

@@ -1,5 +1,8 @@
 #include "StdAfx.h"
 #include "Utility.h"
+#include <time.h>
+
+#define MAX_SIZE 8192
 
 namespace Utility
 {
@@ -53,4 +56,25 @@ namespace Utility
         if ( g_hDLLModule )
             FreeLibraryAndExitThread(g_hDLLModule, 0);
     }
+
+    void dprintf(TCHAR* pszStr, ...)
+    {
+        TCHAR szData[MAX_SIZE] = {0};
+        // 获取今年年份;
+        __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
+        struct tm gmtm = { 0 };
+        localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
+#if _MSC_VER >= 1200 && _MSC_VER < 1500
+        sprintf(szData, _T("%s %s "), _T("[Assist] "), gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec);
+#else
+        _stprintf_s(szData, _T("%s %04d-%02d-%02d %02d:%02d:%02d "), _T("[Assist] "), gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec);
+#endif
+        int len = _tcslen(szData)*sizeof(TCHAR);
+        va_list args;
+        va_start(args, pszStr);
+        _vsntprintf_s(szData + len, MAX_SIZE - len, MAX_SIZE - len, pszStr, args);
+        va_end(args);
+        _tcscat_s(szData, "\n");
+        OutputDebugString(szData);
+    }
 };

+ 5 - 0
DLLInjection/RegistryInjection/InjectionDLL/InjectionDLL/Utility.h

@@ -42,10 +42,13 @@ namespace Utility
         HWND hWnd;
         // 窗口名称;
         TCHAR szWindowTitle[MAX_PATH];
+        // DLL所在进程ID;
+        DWORD dwProcessId;
 
         __WND_INFO__()
         {
             hWnd = NULL;
+            dwProcessId = 0;
             memset(szWindowTitle, 0, MAX_PATH*sizeof(TCHAR));
         }
     }WndInfo, *pWndInfo;
@@ -60,4 +63,6 @@ namespace Utility
     void GetConfigContent();
     // 卸载DLL;
     void FreeLibraryAndExit();
+    // Debug日志输出;
+    void dprintf(TCHAR* pszStr, ...);
 };

+ 10 - 24
DLLInjection/RegistryInjection/InjectionDLL/InjectionDLL/dllmain.cpp

@@ -11,12 +11,7 @@ BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lParam)
 	// 根据进程ID、窗口,获取当前窗口所在线程ID;
 	DWORD dwWindProcessId = 0;
 	DWORD dwThreadId = GetWindowThreadProcessId(hwnd, &dwWindProcessId);
-#if _MSC_VER >= 1200 && _MSC_VER < 1500
-	sprintf(szLog, _T("<Injecter> 进程ID=%ld, 窗口进程ID=%ld\n"), dwCurrentProcessId, dwThreadId);
-#else
-	_stprintf_s(szLog, _T("<Injecter> 进程ID=%ld, 窗口进程ID=%ld\n"), dwCurrentProcessId, dwThreadId);
-#endif
-	OutputDebugString(szLog);
+	Utility::dprintf(_T("进程ID=%ld, 窗口进程ID=%ld\n"), dwCurrentProcessId, dwThreadId);
 	if ( dwCurrentProcessId == dwWindProcessId && GetParent(hwnd) == NULL )   // GetParent==Null表示主窗口,本来就是找顶级的,有点多余?;
 	{
 		*((HWND*)lParam) = hwnd;
@@ -36,6 +31,7 @@ HWND GetMainWnd()
 	TCHAR szLog[MAX_PATH] = {0};
 	// 获取当前DLL所在进程ID;
 	DWORD dwCurrentProcessId = GetCurrentProcessId();
+    Utility::g_WndInfo.dwProcessId = dwCurrentProcessId;
 	// 枚举该进程窗口;
 	EnumWindows(EnumWndProc, (LPARAM)&dwCurrentProcessId);
 	DWORD dwError = GetLastError();
@@ -45,12 +41,7 @@ HWND GetMainWnd()
 		return (HWND)dwCurrentProcessId;
 	}
 
-#if _MSC_VER >= 1200 && _MSC_VER < 1500
-	sprintf(szLog, _T("<Injecter> 进程ID=%ld, 没找到窗口,GetLastError=%ld\n"), dwCurrentProcessId, dwError);
-#else
-	_stprintf_s(szLog, _T("<Injecter> 进程ID=%ld, 没找到窗口,GetLastError=%ld\n"), dwCurrentProcessId, dwError);
-#endif
-	OutputDebugString(szLog);
+    Utility::dprintf(_T("进程ID=%ld, 没找到窗口,GetLastError=%ld\n"), dwCurrentProcessId, dwError);
 
 	return NULL;
 }
@@ -65,23 +56,19 @@ DWORD WINAPI WorkThreadProc(LPVOID lParam)
 	Utility::g_WndInfo.hWnd = GetMainWnd();
 	if ( Utility::g_WndInfo.hWnd == NULL )
 	{
-		OutputDebugString(_T("<Injecter> 没有找到窗口句柄"));
+		Utility::dprintf(_T("没有找到窗口句柄"));
 		Utility::FreeLibraryAndExit();
 		return 0;
 	}
 
 	// 得到窗口名称
 	GetWindowText(Utility::g_WndInfo.hWnd, Utility::g_WndInfo.szWindowTitle, sizeof(Utility::g_WndInfo.szWindowTitle));
-#if _MSC_VER >= 1200 && _MSC_VER < 1500
-	sprintf(szLog, _T("找到窗口名称:%s\n"),  Utility::g_WndInfo.szWindowTitle);
-#else
-	_stprintf_s(szLog, _T("找到窗口名称:%s\n"),  Utility::g_WndInfo.szWindowTitle);
-#endif
-	OutputDebugString(szLog);
+	Utility::dprintf(_T("找到窗口名称:%s\n"),  Utility::g_WndInfo.szWindowTitle);
+
 	//是否名称是计算器
     if( _tcsstr(Utility::g_WndInfo.szWindowTitle, Utility::g_cfgCtx.szWindowTitel) )
 	{
-		OutputDebugString(_T("<Injecter> 找到指定窗口!!!"));
+		Utility::dprintf(_T("找到指定窗口!!!"));
 		// 2、创建后台线程;
 		MessageBox(NULL, Utility::g_WndInfo.szWindowTitle, Utility::g_cfgCtx.szWindowTitel, MB_OK);
         if ( Utility::g_pPipeClient == NULL )
@@ -92,7 +79,7 @@ DWORD WINAPI WorkThreadProc(LPVOID lParam)
 	}
 	else
 	{
-		OutputDebugString(_T("<Injecter> 不满足条件,DLL自我卸载!!!"));
+		Utility::dprintf(_T("不满足条件,DLL自我卸载!!!"));
 		// 如果不满足条件,DLL自我卸载;
 		Utility::FreeLibraryAndExit();
 	}
@@ -103,13 +90,12 @@ DWORD WINAPI WorkThreadProc(LPVOID lParam)
 
 BOOL APIENTRY DllMain( HMODULE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)
 {
-    Utility::g_hDLLModule = hModule;
-    Utility::GetConfigContent();
-
 	switch (ul_reason_for_call)
 	{
 	case DLL_PROCESS_ATTACH:
 		{
+            Utility::g_hDLLModule = hModule;
+            Utility::GetConfigContent();
 			HANDLE hThreadProc = CreateThread(NULL, 0, WorkThreadProc, NULL, 0, NULL);
 			CloseHandle(hThreadProc);
 		}