1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <windows.h>
#include <Tlhelp32.h>
#include <iostream>
#include <cstdlib>
// Queries the ProcessId of a certain process
DWORD GetPIDForProcess (char* process) {
BOOL working = 0;
PROCESSENTRY32 lppe = {0};
DWORD targetPid = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hSnapshot) {
lppe.dwSize = sizeof(lppe);
working=Process32First(hSnapshot, &lppe);
while(working) {
if(_stricmp(lppe.szExeFile, process) == 0) {
targetPid=lppe.th32ProcessID;
break;
}
working = Process32Next(hSnapshot, &lppe);
}
}
CloseHandle(hSnapshot);
return targetPid;
}
// Enables to open other processes
void EnableDebugPriv() {
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
return;
}
if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue)) {
CloseHandle(hToken);
return;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if(!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL)) {
CloseHandle(hToken);
}
}
// Gets the base of a dll
DWORD GetDLLBase(char* DllName, DWORD tPid) {
HANDLE snapMod;
MODULEENTRY32 me32;
if(tPid == 0) {
return 0;
}
snapMod = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, tPid);
me32.dwSize = sizeof(MODULEENTRY32);
if (Module32First(snapMod, &me32)) {
do {
if (strcmp(DllName, me32.szModule) == 0) {
CloseHandle(snapMod);
return (DWORD) me32.modBaseAddr;
}
} while(Module32Next(snapMod, &me32));
}
CloseHandle(snapMod);
return 0;
}
|