Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding > Coding Tutorials
You last visited: Today at 05:41

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



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

Discussion on Wie erlange ich die PID eines Prozesses für z.B. WriteProcessMemory within the Coding Tutorials forum part of the General Coding category.

Reply
 
Old   #1
 
link's Avatar
 
elite*gold: 1
Join Date: Jul 2005
Posts: 553
Received Thanks: 451
Wie erlange ich die PID eines Prozesses für z.B. WriteProcessMemory

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
link is offline  
Thanks
5 Users
Old 10/30/2008, 08:50   #2
 
verT!c4L's Avatar
 
elite*gold: 0
Join Date: Aug 2005
Posts: 1,245
Received Thanks: 60
STICKY!
Danke Link!
verT!c4L is offline  
Old 10/30/2008, 14:07   #3
 
elite*gold: 20
Join Date: Sep 2006
Posts: 1,100
Received Thanks: 184
Die Method ist ja mal Platz sparend und ich schreib immer ne Inject struct mit call zu LoadLibraryA in ne Code Cave *gg
Bot_interesierter is offline  
Old 10/30/2008, 14:15   #4
 
mr.rattlz's Avatar
 
elite*gold: 0
Join Date: Aug 2005
Posts: 896
Received Thanks: 334
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
mr.rattlz is offline  
Old 11/06/2008, 18:17   #5
 
elite*gold: 0
Join Date: Nov 2008
Posts: 9
Received Thanks: 3
danke, habe etwas neues daraus gelernt
Hygeru is offline  
Reply


Similar Threads Similar Threads
Probleme Visual Basic unter Vista64Bit mit ReadProcessMemory und WriteProcessMemory
04/24/2012 - .NET Languages - 2 Replies
Hallo Ihrs Ich hoffe ihr könnt mir vllt vieleicht weiterhelfen, ich befasse mich schon seit einigen Tagen mit ReadProcessMemory und WriteProcessMemory aber irgendwie will das ganze nicht unter Vista 64 Bit nicht funktionieren. Nachfolgender Code tut zb den Puktestand in Solitaire auslesen und in einer Textbox anzeigen. (Ist ja nur ein Beispiel) unter XP funktioniert das ganze ohne Probleme, aber unter Vista bekomme ich immer den Wert 0 zurück. Mir ist klar das die Speicheradresse unter Vista...
help on Writeprocessmemory
10/07/2009 - General Coding - 8 Replies
#include <Windows.h> #include <iostream> using namespace std; #define EngineUpgrade_Addr 0x2F7EDDC8 #define EngineUpgrade_Value 1000 const SIZE_T EngineUpgrade_Size = sizeof(EngineUpgrade_Value); SIZE_T EngineUpgrade_Sent = 0;
Locking WriteProcessMemory with a TickBox
06/11/2007 - General Coding - 5 Replies
This is the code for one of the buttons in my app: Private Sub Command8_Click&#40;&#41; If Hooked = True Then WriteProcessMemory ProcessHandle, &580316, 386, 2, 0& End If End Sub The problem is the memory address that I write to resets itself after a while and I have to click the button again, is there anyway to lock the memory address that I write to? Or just have the code refresh itself at a set interval? I would like to achieve this with a tick box if possible



All times are GMT +2. The time now is 05:41.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.