Window Stream Proof
Window Stream Proof
#include <iostream>
#include <windows.h>
#include <vcruntime.h>
#include <tlhelp32.h>
#include <uxtheme.h>
#include <dwmapi.h>
#include <winternl.h>
#include <thread>
#include <vector>
#include <array>
#include <WinInet.h>
#include <urlmon.h>
#include <chrono>
#include <algorithm>
inline void xGuard(bool cond, const char* file, int line, int code) { if (!cond)
{ xEpicFail(file, line, code); } }
inline void xGuard(HRESULT code, const char* file, int line) { if FAILED(code)
{ xEpicFail(file, line, code); } }
//
===================================================================================
====
struct HANDLE_traits {
static void Close(HANDLE& Value) throw() { if (xIsHandleValid(Value))
{ CloseHandle(Value); Value = nullptr; } }
};
//
===================================================================================
====
XGUARD_WIN(LookupPrivilegeValueW(nullptr, PrivilegeName,
&Privilege.Privileges[0].Luid));
XScopedHandle Token;
XGUARD_WIN(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, &Token));
PROCESSENTRY32W Entry;
ZeroMemory(&Entry, sizeof(Entry));
Entry.dwSize = sizeof(Entry);
XGUARD_WIN(Process32FirstW(Snapshot.Handle, &Entry));
do
{
if (_wcsicmp(Entry.szExeFile, name) == 0)
{
Pid = Entry.th32ProcessID;
break;
}
} while (Process32NextW(Snapshot.Handle, &Entry));
return Pid;
}
MEMORY_BASIC_INFORMATION MemInfo;
ZeroMemory(&MemInfo, sizeof(MemInfo));
std::vector<uint8_t> Buffer;
std::vector<uint8_t>::iterator Iter;
for (;;)
{
auto QuerySize = VirtualQueryEx(Process, Ptr, &MemInfo,
sizeof(MemInfo));
if (QuerySize != sizeof(MemInfo))
{
DWORD Err = GetLastError();
XGUARD_CODE(ERROR_INVALID_PARAMETER == Err, Err);
break;
}
SIZE_T NumBytes = 0;
XGUARD_WIN(ReadProcessMemory(Process, MemInfo.BaseAddress,
&Buffer[0], MemInfo.RegionSize, &NumBytes));
Ptr += MemInfo.RegionSize;
}
return Result;
}
DWORD Prot = 0;
XGUARD_WIN(VirtualProtectEx(Process, Addr, OrigBytes.size(),
PAGE_EXECUTE_READWRITE, &Prot));
SIZE_T IoSize = 0;
XGUARD_WIN(WriteProcessMemory(Process, (PVOID)((UINT64)Addr +
(UINT64)Offset), &PatchBytes[0], PatchBytes.size(), &IoSize));
DWORD Prot2 = 0;
XGUARD_WIN(VirtualProtectEx(Process, Addr, OrigBytes.size(), Prot, &Prot2));
//printf("xPatchMemory DONE\n");
}
XGUARD(OrigBytes.size());
XGUARD(PatchBytes.size());
DWORD ProcId = 0;
XScopedHandle Process;
if (ProcName)
{
ProcId = xGetPidByName(ProcName);
XGUARD_WIN(ProcId);
xAdjustPrivilege(TEXT("SeDebugPrivilege"), TRUE);
if (PatchAddr)
{
xProtectwrite_virtual(*Process, OrigBytes, PatchBytes, PatchAddr,
PatchOffset);
Result = PatchAddr;
}
else
{
XScopedHandle Snap(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,
ProcId));
XGUARD_WIN(Snap);
MODULEENTRY32 Entry;
ZeroMemory(&Entry, sizeof(Entry));
Entry.dwSize = sizeof(Entry);
XGUARD_WIN(Module32First(*Snap, &Entry));
do
{
if (Entry.th32ProcessID == ProcId)
{
PVOID Addr = xFindPattern(*Process, Entry.modBaseAddr,
OrigBytes);
if (Addr)
{
xProtectwrite_virtual(*Process, OrigBytes,
PatchBytes, Addr, PatchOffset);
Result = Addr;
break;
}
}
} while (Module32Next(*Snap, &Entry));
}
return Result;
}
//
===================================================================================
====
size_t SpaceCount = 0;
for (size_t i = 0; i < Len; ++i)
{
if (Str[i] == ' ') SpaceCount++;
}
std::vector<uint8_t> Vec;
Vec.resize(NumBytes);
return Vec;
}