Wie erlange ich die PID eines Prozesses für z.B. WriteProcessMemory

10/29/2008 23:45 link#1
An die, die noch nicht wissen, wie sie ein Handle eines Prozesses erlangen, folgt hier ein kleiner Beispielquelltext [In MASM, damit es auch jeder versteht (aufgrund des vereinfachten Dialektes), und C].

Beispielprogramm:
Code:
.386
.model flat, stdcall
option casemap :none

include windows.inc
include kernel32.inc
include user32.inc
includelib kernel32.lib
includelib user32.lib

.data
    szMsgTitle db "Process ID", 0
    szFormat db "PID über Exenamen : %04d", 13, 10, "PID über ein Fenster: %04d", 0
    szExe db "notepad.exe", 0
    szClass db "Notepad", 0
    szTitle db "Unbenannt - Editor", 0

.data?
    dwWindowThreadProcessId dd ?
    pBuffer db 60h dup (?)
    pe32 PROCESSENTRY32 <?>

.code
GetPID proc pExe:DWORD
    local hProc:DWORD ;ebp - 4 push ebp mov ebp, esp add esp, -4
    invoke CreateToolhelp32Snapshot, TH32CS_SNAPPROCESS, 0
    cmp eax, INVALID_HANDLE_VALUE
    je @@end
    mov [hProc], eax
    mov [pe32.dwSize], size PROCESSENTRY32
    invoke Process32First, [hProc], offset pe32
    test eax, eax
    jz @@e
@@l:
    invoke lstrcmpi, offset pe32.szExeFile, pExe
    test eax, eax
    jnz @f
    mov eax, [pe32.th32ProcessID]
    jmp @@e
@@:
    invoke Process32Next, [hProc], offset pe32
    test eax, eax
    jnz @@l
@@e:
    push eax
    invoke CloseHandle, hProc
    pop eax
    ret;n 4, mov esp, ebp pop ebp
GetPID endp

main:
    invoke FindWindow, offset szClass, offset szTitle
    test eax, eax
    mov [dwWindowThreadProcessId], 0
    jz @f
    invoke GetWindowThreadProcessId, eax, offset dwWindowThreadProcessId
@@:
    invoke GetPID, offset szExe
    push [dwWindowThreadProcessId]
    push eax
    push offset szFormat
    push offset pBuffer
    call wsprintf
    add esp, 16
    invoke MessageBox, 0, offset pBuffer, offset szMsgTitle, 0
    invoke ExitProcess, 0
    end main
Assemblieren mit (Im MASM32-Ordner):
Code:
ml.exe /Dmasm /c /coff /I..\include\ src.asm
link.exe /SUBSYSTEM:WINDOWS /LIBPATH:..\lib\ src.obj
In C:
Code:
DWORD GetPID(char *pName) {
	HANDLE hProc;
	PROCESSENTRY32 pe32;
	hProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if(hProc == INVALID_HANDLE_VALUE) {
		return 0;
	}
	pe32.dwSize = sizeof(PROCESSENTRY32);
	if(Process32First(hProc, &pe32)) {
		do {
			if(!strnicmp(pName, pe32.szExeFile, strlen(pName))) {
				CloseHandle(hProc);
				return pe32.th32ProcessID;
			}
		} while(Process32Next(hProc, &pe32));
	}
	CloseHandle(hProc);
	return 0;
}
(#include <windows.h> und #include <tlhelp32.h> nicht vergessen.)

Um ein Handle zu erhalten, müsste man dann folgende Instruktion implementieren (In C):
Code:
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, GetPID("notepad.exe"));
Und mit dem Handle kann man dann herumfummeln, indem man auf Read-/WriteProcessMemory zurückgreift, oder man kann zum Beispiel eine Dll wie folgt injecten:
Code:
BOOL InjectDll(HANDLE hProc, char *pName) {
	void *lpAlloc;
	PTHREAD_START_ROUTINE pThread;
	lpAlloc = VirtualAllocEx(hProc, NULL, strlen(pName), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
	pThread = (PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
	WriteProcessMemory(hProc, (LPVOID)lpAlloc, pName, strlen(pName), NULL);
	if(!CreateRemoteThread(hProc, NULL, 0, pThread, lpAlloc, 0, NULL)) {
		return FALSE;
	}
	return TRUE;
}
In vereinfachtem ASM:
Code:
InjectDll proc hProc:DWORD, pName:DWORD
.data
   szKernelDll db "kernel32.dll", 0
   szLoadLibrary db "LoadLibraryA", 0
.code
   push ebx
   push edi
   invoke GetModuleHandle, offset szKernelDll
   invoke GetProcAddress, eax, offset szLoadLibrary
   mov ebx, eax
   invoke lstrlen, [pName]
   mov edi, eax
   invoke VirtualAllocEx, [hProc], NULL, eax, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE
   push eax
   invoke WriteProcessMemory, [hProc], eax, [pName], edi, NULL
   pop eax
   invoke CreateRemoteThread, [hProc], NULL, 0, ebx, eax, 0, NULL
   test eax, eax
   jz @f
   mov eax, TRUE
   jmp @@e
@@:
   mov eax, FALSE
@@e:
   pop edi
   pop ebx
   retn 8
InjectDll endp
Vermischen kann man den Dreck natürlich auch, aber leider kenne ich mich mit Inline ASM nicht aus :P
10/30/2008 08:50 verT!c4L#2
STICKY! :D
Danke Link!
10/30/2008 14:07 Bot_interesierter#3
Die Method ist ja mal Platz sparend und ich schreib immer ne Inject struct mit call zu LoadLibraryA in ne Code Cave *gg
10/30/2008 14:15 mr.rattlz#4
Quote:
Originally Posted by Bot_interesierter View Post
Die Method ist ja mal Platz sparend und ich schreib immer ne Inject struct mit call zu LoadLibraryA in ne Code Cave *gg
Ich habe sie mal vor einiger Zeit entweder in einer Anleitung gefunden, oder aus einer Library extrahiert, bin mir da nicht mehr so sicher, aber ich war ebenfalls begeistert von der Einfachheit ;)
11/06/2008 18:17 Hygeru#5
danke, habe etwas neues daraus gelernt