So I am trying to basically loop trough the processes, find the process id of my process (which works), then open a process with that pid (which also works) and then duplicate it with NtDuplicateObject and protect it with NtSetInformationObject.
The problem is that there's always something wrong. First time I tried it it didn't want to duplicate it, fast forward until now and after commenting out the part where I try to close the old handle (which I can't do and the NtDuplicateObject shouldn't do it either) it gives me a handle but I can't use it for writeprocessmemory or anything like that. I will post the function here and the full code in a hastebin link (in case there are confusions in my code that need some stitching)
HANDLE PROTO_HAND::GrabPerfectHandle(const wchar_t *__processName)
{
if (__processName == nullptr)
return reinterpret_cast<HANDLE>(PRH_ERR_BADPARAM);
NTSTATUS __returnError;
SYSTEM_PROCESS_INFORMATION *__systemProcessInfo;
void *__systemInfo;
void *__allocationBuffer;
__allocationBuffer = VirtualAlloc(0, 1024 * 1024, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!__allocationBuffer)
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTALLOC);
__systemProcessInfo = reinterpret_cast<SYSTEM_PROCESS_INFORMATION*>(__allocationBuffer);
if (!NT_SUCCESS(__returnError = NtQuerySystemInformation(SystemProcessInformation, __systemProcessInfo, 1024 * 1024, 0)))
{
if (!VirtualFree(__allocationBuffer, 0, MEM_RELEASE))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTDEALLOC);
return reinterpret_cast<HANDLE>(PRH_ERR_NTQUERYFAIL);
}
while (__systemProcessInfo->NextEntryOffset)
{
if (__systemProcessInfo->ImageName.Buffer != nullptr)
{
if (wcscmp(__systemProcessInfo->ImageName.Buffer, __processName) == 0)
{
HANDLE __basicHandle = OpenProcess(PROCESS_ALL_ACCESS, false, __systemProcessInfo->UniqueProcessId);
HANDLE __perfectHandle{ 0 };
if (!__basicHandle)
{
if (!VirtualFree(__allocationBuffer, 0, MEM_RELEASE))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTDEALLOC);
return reinterpret_cast<HANDLE>(PRH_ERR_OPENPROCFAIL);
}
if (!NT_SUCCESS(NtDuplicateObject(GetCurrentProcess(), __basicHandle, GetCurrentProcess(), &__perfectHandle, PROCESS_ALL_ACCESS, 0, DUPLICATE_SAME_ACCESS)))
{
if (!VirtualFree(__allocationBuffer, 0, MEM_RELEASE))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTDEALLOC);
return reinterpret_cast<HANDLE>(PRH_ERR_DUPHANDFAIL);
}
/*if(!NtClose(__basicHandle))
{
if (!VirtualFree(__allocationBuffer, 0, MEM_RELEASE))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTDEALLOC);
if(!CloseHandle(__basicHandle))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTCLOSEHAND);
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTCLOSEHAND);
}
if(__basicHandle != nullptr)
{
if (!VirtualFree(__allocationBuffer, 0, MEM_RELEASE))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTDEALLOC);
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTCLOSEHAND);
}*/
_OBJECT_HANDLE_FLAG_INFORMATION __objectInformation{ 0 };
__objectInformation.ProtectFromClose = { true };
if (!NT_SUCCESS(NtSetInformationObject(__perfectHandle, ObjectHandleFlagInformation, &__objectInformation, sizeof(_OBJECT_HANDLE_FLAG_INFORMATION))))
{
if (!VirtualFree(__allocationBuffer, 0, MEM_RELEASE))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTDEALLOC);
return reinterpret_cast<HANDLE>(PRH_ERR_PFCFAIL);
}
if (!VirtualFree(__allocationBuffer, 0, MEM_RELEASE))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTDEALLOC);
return __perfectHandle;
}
}
__systemProcessInfo = reinterpret_cast<SYSTEM_PROCESS_INFORMATION*>(reinterpret_cast<BYTE*>(__systemProcessInfo) + __systemProcessInfo->NextEntryOffset);
}
if (!VirtualFree(__allocationBuffer, 0, MEM_RELEASE))
return reinterpret_cast<HANDLE>(PRH_ERR_CANNOTDEALLOC);
return reinterpret_cast<HANDLE>(PRH_ERR_FELLTROUGH);
}