C ++ Code Injection dumps the injected application

I am trying to embed a simple executable in another executable which I made unfortunately whenever I paste code into the executable it says "simpleinjected.exe stops working" then it closes. I am using CreateRemoteThread

to enter code. This is what I have done so far.

Injector.exe // file that injects the code

#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 // file to insert

#include <stdio.h>

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

      

executable.exe // executable file that is injected into simpleinjected

#include <windows.h>

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

      

The message is not displayed, but simpleinjected.exe

a failure. The crash shows that the code has been pasted, but I don't understand why it is crashing.

When using the DLL and the same method above, the dll is executed in "simpleinjected.exe" but does not work when typed in Firefox. Below is the dll code. It runs in a custom application, but not in Firefox, even if entered successfully.

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;
}

      

+3


source to share


1 answer


modified Simpleinjected.exe code as shown below. and then try adding dllinject.dll to Simpleinjected.exe again.

#include <stdio.h>

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

      



you have to change the definitions below the same as Simpleinjected.exe.

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

      

+1


source







All Articles