【问题标题】:C++ Code Injection crashes injected applicationC++ 代码注入使注入的应用程序崩溃
【发布时间】:2014-09-30 09:57:55
【问题描述】:

我正在尝试将一个简单的可执行文件注入到我制作的另一个可执行文件中,不幸的是,每当我将代码注入可执行文件时,它都会显示“simpleinjected.exe 已停止工作”,然后它会关闭。我正在使用CreateRemoteThread 注入代码。这是我到目前为止所做的。

Injector.exe // 注入代码的文件

#include <stdio.h>
#include <windows.h>

#define procId 2844
#define executable "executable.exe"    // located in same directory

int main()
{
    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, procId);
    LPVOID allocated = (LPVOID)VirtualAllocEx(hProc, NULL, strlen(executable), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
    WriteProcessMemory(hProc, (LPVOID)allocated, executable, strlen(executable), NULL);
    LPVOID libaddr = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
    CreateRemoteThread(hProc, NULL, NULL, (LPTHREAD_START_ROUTINE)libaddr, NULL, NULL);
    CloseHandle(hProc);
    return 0;
}

Simpleinjected.exe // 被注入的文件

#include <stdio.h>

int main()
{
    printf("Hello");
    return 0;
}

executable.exe // 注入simpleinjected的可执行文件

#include <windows.h>

int main()
{
    MessageBox(NULL, "Injected successfully", "Code Injection", MB_OK);
    return 0;
}

消息未显示,simpleinjected.exe 崩溃。崩溃显示代码已插入,但我不明白为什么它会崩溃。

使用 DLL 和上述相同技术时,dll 在“simpleinjected.exe”中执行,但在注入 Firefox 时不起作用。 dll代码如下。它在自定义应用程序中执行,而不是在 Firefox 中执行,即使它已成功注入。

dllinject.dll

#include <windows.h>

int message(const char *msg)
{
    MessageBox(NULL, msg, "Message from Dll", MB_OK);
    return 0;
}

BOOL WINAPI DLLMain(HINSTANCE hInstDll, DWORD ulReason, LPVOID lpReserved)
{
    switch(ulReason)
    {
        case DLL_PROCESS_ATTACH:
            message("process attach");
            break;
        case DLL_THREAD_ATTACH:
            message("thread attach");
            break;
        case DLL_PROCESS_DETACH:
            message("process detach");
            break;
        case DLL_THREAD_DETACH:
            message("thread detach");
            break;
    }
    return true;
}

【问题讨论】:

  • 将一个 DLL 注入其他进程还不够困难,更不用说另一个 executable 了吗?
  • 所以不可能?
  • 如果您问CreateRemoteThread/LoadLibrary 技术是否适用于 EXE 而不是 DLL,I doubt it,但我从未尝试过。我可以告诉你你的VirtualAlloc 区域不够大,因为strlen(executable) 不包括你的终结符字符的空间(顺便说一句,这也需要编写)。祝你好运,顺便说一句。
  • 那么我该如何将代码注入另一个进程呢?
  • 我对dll注入感兴趣,不是想解决任何问题,只是满足我的好奇心。

标签: c++ c firefox code-injection dll-injection


【解决方案1】:

Simpleinjected.exe 的修改代码如下。然后再次尝试将 dllinject.dll 注入 Simpleinjected.exe。

#include <stdio.h>

int main()
{
   while(true)
   {
      printf("Hello");
   }
   return 0;
}

你应该像 Simpleinjected.exe 一样修改下面的定义。

#define procId 2844 //process id of Simpleinjected.exe
#define executable "dllinject.dll"    // located in same directory

【讨论】:

  • 可以在simpleinjected.exe中注入,它可以工作,但在firefox中不能注入。
  • 问题的依赖注入标签错误。问题是关于注入 EXE。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多