class MyClass{
public:
static void A()
{
return;
}
};
DWORD __stdcall threadProc(LPVOID lParam)
{
MyClass* pRP = (MyClass*)lParam;
pRP->A();
return 0;
}
void CzhuruDlg::InjectFunc(DWORD dwProcessId, LPVOID pFun, DWORD boolParam, DWORD *Param, DWORD ParamSize )
{
// TODO: 在此添加控件通知处理程序代码
//定义线程体的大小
const DWORD dwThreadSize = 4096;
DWORD dwWriteBytes;
MyClass* pRemoteParam;
HANDLE hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
if (!hTargetProcess) return;
void* pRemoteThread = VirtualAllocEx(hTargetProcess, 0, dwThreadSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!pRemoteThread) return;
//将线程体拷贝到宿主进程中
if (!WriteProcessMemory(hTargetProcess,pRemoteThread, pFun, dwThreadSize, 0)) return;
if(boolParam)
{
pRemoteParam = (MyClass*)VirtualAllocEx(hTargetProcess , 0, ParamSize, MEM_COMMIT, PAGE_READWRITE);
if (!pRemoteParam) return;
//将线程参数拷贝到宿主进程地址空间中
if (!WriteProcessMemory(hTargetProcess ,pRemoteParam, Param, ParamSize, 0)) return;
}
//在宿主进程中创建线程
HANDLE hRemoteThread;
if(boolParam)
{
hRemoteThread = CreateRemoteThread(
hTargetProcess, NULL, 0, (DWORD (__stdcall *)(void *))pRemoteThread,
pRemoteParam, 0, &dwWriteBytes);
}
else
{
hRemoteThread = CreateRemoteThread(
hTargetProcess, NULL, 0, (DWORD (__stdcall *)(void *))pRemoteThread,
NULL, 0, &dwWriteBytes);
}
if(WaitForSingleObject(hRemoteThread,INFINITE) != WAIT_OBJECT_0)
{
CString StrTmp;
StrTmp.Format("%d",GetLastError());
MessageBox(StrTmp);
}
CloseHandle(hRemoteThread);
CloseHandle(hTargetProcess);
VirtualFreeEx(hTargetProcess,pRemoteThread,4096,MEM_RELEASE);
if(boolParam) VirtualFreeEx(hTargetProcess,pRemoteParam,ParamSize,MEM_RELEASE);
return;
}
void CzhuruDlg::OnBnClickedButton1()
{
DWORD dwProcessId = 0;
HWND hdesktop=::GetDesktopWindow();
HWND hCurWnd = ::GetWindow(hdesktop,GW_CHILD);
while(hCurWnd )
{
::GetWindowText(hCurWnd,str,300);
if (strstr(str,"记事本"))
{
//MessageBox(str,NULL,NULL);
GetWindowThreadProcessId(hCurWnd,&dwProcessId);
break;
}
hCurWnd = ::GetWindow(hCurWnd,GW_HWNDNEXT);
}
if(dwProcessId)
{
MyClass myc;
DWORD ParamSum = sizeof(MyClass);
InjectFunc(dwProcessId,threadProc, 1, (DWORD *)&myc, ParamSum);
}
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)