1) search and obtain a descriptor of the desired process
2) memory allocation in the process and the subsequent recording of the path in the DLL at the address where the memory allocation occurred
3) create a new thread in the virtual space of the process, the handle of which was received.
procedure TForm1.Button1Click(Sender: TObject); var _curr_process:DWORD; p_handle: THANDLE; begin // _curr_process:=GetCurrentProcessId(); p_handle:=OpenProcess(PROCESS_ALL_ACCESS,false,_curr_process); TerminateProcess(p_handle,4); end; var dw:DWORD; // PID procedure drawGroup(h:HWND); var canvas:TBitMap; rect:TRect; x,y:integer; i: Integer; begin // 10 for i := 1 to 10 do begin canvas:=TBitMap.Create(); canvas.Canvas.Handle:=GetDC(h); // randomize; canvas.Canvas.Brush.Color:=rgb(random(255),random(255),random(255)); GetWindowRect(h,rect); x:=random(rect.Right-rect.Left-25); y:=random(rect.Bottom-rect.Top-25); canvas.Canvas.Rectangle(x,y,x+25,y+25); end; end; function func(h:HWND):BOOL; stdcall; var pid:DWORD; begin GetWindowThreadProcessId(h,pid); // , : if(pid=dw) then drawGroup(h); result:=true; end; procedure TForm1.Button2Click(Sender: TObject); begin // // ( ) dw:=GetCurrentProcessId(); EnumWindows(@func,0); // end;
MSG msg; while (GetMessage(&msg,NULL,0,0)) { // Button Click if(msg.hwnd==button && msg.message==WM_LBUTTONUP) { // Getting Process Name GetWindowText(edit_proc,buff,sizeof(buff)); strncpy(_p_name,buff,BUFF); // Getting DLL Name ZeroMemory(buff,sizeof(buff)); GetWindowText(edit_dll,buff,BUFF); strncpy(_dll_name,buff,BUFF); // Start Injection StartInjection(); }
StartInjection ();which is used to search for a process by its name:
int StartInjection() { // Searching of Process PROCESSENTRY32 pe; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(snapshot==INVALID_HANDLE_VALUE) { ShowMessage("SnapShot Failed."); return 0; } pe.dwSize = sizeof(PROCESSENTRY32); int curr = Process32First(snapshot,&pe); while(curr) { CharLowerBuff(pe.szExeFile,lstrlen(pe.szExeFile)); if(strstr(pe.szExeFile,_p_name)) {pid = pe.th32ProcessID; break;} curr = Process32Next(snapshot,&pe); } if(pid==NULL) { ShowMessage("Searching of process failed."); return 0; } bool result = Inject(); if(result==false) { ShowMessage("Injection failed."); return 0; } }
Inject ():
// Injection bool Inject() { if(pid==NULL) return false; // HANDLE process = OpenProcess(PROCESS_ALL_ACCESS,false,pid); if(process==NULL) return false; // "" // DLL LPVOID fp = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"),"LoadLibraryA"); if(fp==NULL) return false; // strlen(_dll_name) // . LPVOID alloc = (LPVOID)VirtualAllocEx(process,0,strlen(_dll_name), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); if(alloc==NULL) return false; // DLL BOOL w = WriteProcessMemory(process,(LPVOID)alloc,_dll_name,strlen(_dll_name),0); if(w==NULL) return false; // "" // DLL. HANDLE thread = CreateRemoteThread(process,0,0,(LPTHREAD_START_ROUTINE)fp,(LPVOID)alloc,0,0); if(thread==NULL) return false; CloseHandle(process); return true; }
Source: https://habr.com/ru/post/168255/
All Articles