【问题标题】:Detours DLL injection works only for specific applicationsDetours DLL 注入仅适用于特定应用程序
【发布时间】:2021-09-18 08:04:52
【问题描述】:

我尝试使用 Microsoft Detours 挂钩一些功能。我使用的方法是 CreateRemoteThread + LoadLibrary。

然而,我遇到过完全相同的代码在 notepad.exe、一些 chrome 进程等上工作,但在 wmplayer.exe(Windows 媒体播放器)、Calculator.exe 上却不工作。说这些应用程序可能试图阻止这种类型的 DLL 注入是否正确?我几乎想不出其他的可能性。

这些代码大部分是从Detours tutorial复制过来的

代码可以从this repository 看到和克隆,以防有人想试验它们。

  • DLL:

    INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
    {
        try {
            std::ofstream file("D:\\output.txt");
            file << "Hello!\n";
            file.close();
        }
        catch (...) {
            std::ofstream file("D:\\error.txt");
            file << "Hello!\n";
            file.close();
        }
    }
    
  • 喷油器:

      int main(void)
      {
          if (fileExists("D:\\output.txt"))
          {
              printf("Removing...\n");
              remove("D:\\output.txt");
          }
          PROCESSENTRY32 pe32;
          pe32.dwSize = sizeof(PROCESSENTRY32);
          HANDLE hTool32 = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
          if (Process32First(hTool32, &pe32))
          {
              while ((Process32Next(hTool32, &pe32)) == TRUE) {
                  char exeName[] = "Calculator.exe";
                  //char exeName[] = "notepad.exe";
                  if (strcmp(pe32.szExeFile, exeName) == 0)
                  {
                      printf("Found %s at %d\n", exeName, pe32.th32ProcessID);
                      char* DirPath = new char[MAX_PATH];
                      char* FullPath = new char[MAX_PATH];
                      GetCurrentDirectory(MAX_PATH, DirPath);
                      sprintf_s(FullPath, MAX_PATH, "%s\\..\\x64\\Debug\\TestDLL.dll", DirPath);
                      printf("%s File exists: %d\n", FullPath, fileExists(FullPath));
                      HANDLE hProcess = OpenProcess(
                          PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, pe32.th32ProcessID);
                      LPVOID LoadLibraryAddr = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
                      LPVOID LLParam = (LPVOID)VirtualAllocEx(hProcess, NULL, strlen(FullPath),
                          MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
                      BOOL status = WriteProcessMemory(hProcess, LLParam, FullPath, strlen(FullPath), NULL);
                      auto handle = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryAddr,
                          LLParam, NULL, NULL);
                      CloseHandle(hProcess);
                      delete[] DirPath;
                      delete[] FullPath;
                      std::cin.get();
                  }
    
              }
          }
          CloseHandle(hTool32);
          return 0;
      }
    

当变量exeName 设置为“notepad.exe”时,将创建文件“D:\output.txt”,而将变量设置为“Calculator.exe”则不会。

如果我的猜测是正确的,那么使用其他注入方法(例如 SetWindowsHookEx)是我可以使这些工作的唯一方法吗?

【问题讨论】:

    标签: windows dll hook dll-injection detours


    【解决方案1】:

    我个人认为 CreateRemoteThread + LoadLibrary 方法确实阻止了这些应用程序的注入,因为它是最基本的方法,但这种猜测需要进一步证明。切换到this repository中使用SetWindowsHookEX方法后,DllMain会调用成功。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多