Solved Calling entry point without exit the process

Hexui Undetected CSGO Cheats PUBG Accounts

Compeador

Dank Tier Donator
Full Member
May 26, 2018
22
513
1
How long you been coding/hacking?
5y+
I'm trying to start an exe by "calling" its entry point, aka run PE in memory; so far I've managed to map the whole PE stuff (with this guide!) into memory and the PE runs successfully without errors, but the caller (main process) also terminated along with the callee (the mapped exe's entry point)...... that's not how a function call should be right!?

main code snippet:
parseHeader32();
if(_reloced){ applyRelocation32(); }
parseImport32();
parseTLS32();
uintptr_t entry_addr = INH32.OptionalHeader.ImageBase + INH32.OptionalHeader.AddressOfEntryPoint;
std::cout << "Entry addr: " << (void*)entry_addr << '\n';
((void(*)()) entry_addr)(); // PE starts up nicely
// (int(*)()) entry_addr)(); // will not "return to parent" too

// never reaches here :(
std::cout << "This line will not execute\n";
Full source: ken1882/Mem-PE-Exec
(Compile and enter path to exe desire to make it run in memory)

And the exe I mapped:
C++:
#include <windows.h>

int main(void){
    MessageBoxA(NULL, "Test Message", "TEST", 0x0);
    return 0;
}
what am I missing here?

EDIT: Solved myself, see post below
 
Last edited:
  • Like
Reactions: Sppechy

Compeador

Dank Tier Donator
Full Member
May 26, 2018
22
513
1
after some more research turns out when Mingw linking the main function it'll call `ExitProcess` right after the main function returns.
So a workaround is to modify the main function to call, abort the thread before it does:
C++:
#include <windows.h>
int main(){
  // ...
  ExitThread(0);
  return 0;
}
thus the caller will need to start a separate thread to start the call routine:
C++:
uintptr_t entry_address = PE_ADDRESS_OF_ENTRY_POINT; 
HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)entry_address,NULL,0,NULL); 
if(hThread == NULL){
  std::cout << "Failed to create thread: " << GetLastError() << '\n';
  return ; 
}
WaitForSingleObject(hThread,INFINITE);
DWORD tReturn = -1;
if(!GetExitCodeThread(hThread, &tReturn)){
  std::cout << "Failed to get thread return code: " << GetLastError() << '\n';
  return ;
}
std::cout << "Thread termianted with return code: " << tReturn << '\n';
CloseHandle(hThread);
Async version:
while(true){
  // do other things
  // ...
  DWORD tResult = WaitForSingleObject(hThread,0);
  if(tResult == WAIT_OBJECT_0){
    DWORD tReturn = -1;
    if(!GetExitCodeThread(hThread, &tReturn)){
      std::cout << "Failed to get thread return code: " << GetLastError() << '\n';
      return ;
    }
    std::cout << "Thread termianted with return code: " << tReturn << '\n';
    CloseHandle(hThread);
    break;
  }
  else{ std::cout << "Thread is still running!\n"; }
  Sleep(1000); // check after 1 sec
}
 
Community Mods