dllmain.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // dllmain.cpp : 定义 DLL 应用程序的入口点。
  2. #include "stdafx.h"
  3. HMODULE g_hModule = NULL;
  4. /************************************************************************/
  5. /* 函数:WriteTextLog[7/28/2016 IT];
  6. /* 描述:写文本日志;
  7. /* 参数:;
  8. /* [IN] :;
  9. /* 返回:void;
  10. /* 注意:;
  11. /* 示例:;
  12. /*
  13. /* 修改:;
  14. /* 日期:;
  15. /* 内容:;
  16. /************************************************************************/
  17. void WriteTextLog(const TCHAR *format, ...)
  18. {
  19. // 解析出日志路径;
  20. TCHAR szlogpath[MAX_PATH] = { 0 };
  21. static TCHAR szModulePath[MAX_PATH] = { 0 };
  22. static TCHAR szFna[MAX_PATH] = { 0 };
  23. if (szModulePath[0] == _T('\0'))
  24. {
  25. TCHAR szDrive[MAX_PATH] = { 0 };
  26. TCHAR szDir[MAX_PATH] = { 0 };
  27. TCHAR szExt[MAX_PATH] = { 0 };
  28. ::GetModuleFileName(g_hModule, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  29. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  30. _tcscpy_s(szModulePath, szDrive);
  31. _tcscat_s(szModulePath, szDir);
  32. }
  33. _stprintf_s(szlogpath, _T("%s%s.txt"), szModulePath, szFna);
  34. // 打开或创建文件;
  35. FILE *fp = NULL;
  36. if (_taccess(szlogpath, 0) != -1)
  37. {// 存在;
  38. fp = _tfopen(szlogpath, _T("a+"));
  39. // 移动到末尾;
  40. fseek(fp, 0, SEEK_END);
  41. }
  42. else
  43. {// 不存在;
  44. fp = _tfopen(szlogpath, _T("w+"));
  45. }
  46. if ( fp == NULL )
  47. return;
  48. // 格式化前设置语言区域;
  49. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  50. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  51. // 格式化日志内容;
  52. va_list args = NULL;
  53. int len = 0;
  54. TCHAR *buffer = NULL;
  55. va_start(args, format);
  56. // _vscprintf doesn't count. terminating '\0'
  57. len = _vsctprintf(format, args) + 1;
  58. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  59. _vstprintf_s(buffer, len, format, args);
  60. // 将日志内容输入到文件中;
  61. // 获取今年年份;
  62. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  63. struct tm gmtm = {0};
  64. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  65. _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year+1990, gmtm.tm_mon+1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
  66. // 关闭文件,释放资源并设置回原语言区域;
  67. free(buffer);
  68. fclose(fp);
  69. _tsetlocale(LC_CTYPE, old_locale);
  70. free(old_locale);//还原区域设定;
  71. }
  72. // 本函数使用的都是未公开的Win API
  73. // 即:以后可能会变化的函数;
  74. void killWeChatMutex()
  75. {
  76. }
  77. NTSTATUS GetProcessModules(HANDLE hProcess, LPCTSTR lpTypName, LPCTSTR lpName)
  78. {
  79. NTSTATUS Status = 0;
  80. pfZwQueryInformationProcess ZwQueryInformationProcess = NULL;
  81. PROCESS_BASIC_INFORMATION ProcessInfo;
  82. PPEB pPeb;
  83. ZwQueryInformationProcess = (pfZwQueryInformationProcess)GetProcAddress(GetModuleHandle(L"ntdll.dll"), "ZwQueryInformationProcess");
  84. if (ZwQueryInformationProcess == NULL)
  85. {
  86. WriteTextLog(_T("查找进程模块名称失败"));
  87. return Status;
  88. }
  89. Status = ZwQueryInformationProcess(hProcess, ProcessBasicInformation, &ProcessInfo, sizeof(ProcessInfo), NULL);
  90. if (NT_SUCCESS(Status))
  91. {
  92. pPeb = (PPEB)ProcessInfo.PebBaseAddress;
  93. for (PLIST_ENTRY pListEntry = pPeb->Ldr->InMemoryOrderModuleList.Flink;pListEntry != &pPeb->Ldr->InMemoryOrderModuleList;pListEntry = pListEntry->Flink)
  94. {
  95. //PLDR_DATA_TABLE_ENTRY pEntry = CONTAINING_RECORD(pListEntry, LDR_DATA_TABLE_ENTRY, pListEntry);
  96. //wprintf(L"%s\n", pEntry->FullDllName.Buffer);
  97. WriteTextLog(_T("模块名称"));
  98. }
  99. }
  100. else
  101. WriteTextLog(_T("查找进程模块名称失败1"));
  102. CloseHandle(hProcess);
  103. return Status;
  104. }
  105. BOOL APIENTRY DllMain( HMODULE hModule,
  106. DWORD ul_reason_for_call,
  107. LPVOID lpReserved
  108. )
  109. {
  110. g_hModule = hModule;
  111. switch (ul_reason_for_call)
  112. {
  113. case DLL_PROCESS_ATTACH:
  114. WriteTextLog(_T("dll已成功注入"));
  115. GetProcessModules(::GetCurrentProcess(), _T("Mutant"), _T("_WeChat_Instance_Identity_Mutex_Name"));
  116. break;
  117. case DLL_THREAD_ATTACH:
  118. break;
  119. case DLL_THREAD_DETACH:
  120. break;
  121. case DLL_PROCESS_DETACH:
  122. WriteTextLog(_T("dll已成功卸载"));
  123. break;
  124. default:
  125. break;
  126. }
  127. return TRUE;
  128. }