// MaverickFreeze.exe — congela processo thread a thread, sem matar. // Testa, em ordem: TerminateProcess, NtSuspendProcess, SuspendThread por thread, // e leitura/escrita de memoria. Diz exatamente o que o kernel permite. // csc /nologo /optimize+ /out:MaverickFreeze.exe MaverickFreeze.cs // MaverickFreeze.exe --target nvstreamer --freeze // MaverickFreeze.exe --target nvstreamer --resume using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; internal static class MaverickFreeze { const uint PROCESS_TERMINATE = 0x0001; const uint PROCESS_VM_OPERATION = 0x0008; const uint PROCESS_VM_READ = 0x0010; const uint PROCESS_VM_WRITE = 0x0020; const uint PROCESS_SUSPEND_RESUME = 0x0800; const uint THREAD_SUSPEND_RESUME = 0x0002; const uint TH32CS_SNAPTHREAD = 0x00000004; [StructLayout(LayoutKind.Sequential)] struct THREADENTRY32 { public uint dwSize; public uint cntUsage; public uint th32ThreadID; public uint th32OwnerProcessID; public int tpBasePri; public int tpDeltaPri; public uint dwFlags; } [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint pid); [DllImport("kernel32.dll", SetLastError = true)] static extern bool Thread32First(IntPtr snap, ref THREADENTRY32 entry); [DllImport("kernel32.dll", SetLastError = true)] static extern bool Thread32Next(IntPtr snap, ref THREADENTRY32 entry); [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr OpenThread(uint access, bool inherit, uint tid); [DllImport("kernel32.dll", SetLastError = true)] static extern uint SuspendThread(IntPtr h); [DllImport("kernel32.dll", SetLastError = true)] static extern uint ResumeThread(IntPtr h); [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr OpenProcess(uint access, bool inherit, int pid); [DllImport("kernel32.dll", SetLastError = true)] static extern bool TerminateProcess(IntPtr h, uint code); [DllImport("kernel32.dll", SetLastError = true)] static extern bool ReadProcessMemory(IntPtr h, IntPtr addr, byte[] buf, IntPtr size, out IntPtr read); [DllImport("kernel32.dll", SetLastError = true)] static extern bool WriteProcessMemory(IntPtr h, IntPtr addr, byte[] buf, IntPtr size, out IntPtr written); [DllImport("kernel32.dll", SetLastError = true)] static extern bool CloseHandle(IntPtr h); [DllImport("ntdll.dll")] static extern int NtSuspendProcess(IntPtr h); [DllImport("ntdll.dll")] static extern int NtResumeProcess(IntPtr h); [DllImport("kernel32.dll", SetLastError = true)] static extern bool VirtualProtectEx(IntPtr h, IntPtr addr, IntPtr size, uint newProtect, out uint oldProtect); [StructLayout(LayoutKind.Sequential)] struct MEMORY_BASIC_INFORMATION64 { public ulong BaseAddress; public ulong AllocationBase; public uint AllocationProtect; public uint __alignment1; public ulong RegionSize; public uint State; public uint Protect; public uint Type; public uint __alignment2; } [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr VirtualQueryEx(IntPtr h, IntPtr addr, out MEMORY_BASIC_INFORMATION64 info, IntPtr len); /// Primeira regiao MEM_COMMIT com PAGE_EXECUTE*, sem depender de MainModule. static IntPtr FindExecutableRegion(IntPtr h, int pid) { ulong addr = 0x10000; for (int i = 0; i < 20000; i++) { MEMORY_BASIC_INFORMATION64 mbi; if (VirtualQueryEx(h, (IntPtr)(long)addr, out mbi, (IntPtr)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION64))) == IntPtr.Zero) break; if (mbi.RegionSize == 0) break; bool committed = (mbi.State & 0x1000) != 0; // MEM_COMMIT uint p = mbi.Protect & 0xFF; bool exec = p == 0x10 || p == 0x20 || p == 0x40 || p == 0x80; // X / XR / XRW / XWC if (committed && exec) return (IntPtr)(long)mbi.BaseAddress; addr = mbi.BaseAddress + mbi.RegionSize; if (addr > 0x7FFFFFFF0000UL) break; } return IntPtr.Zero; } static List ThreadsOf(int pid) { var list = new List(); IntPtr snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); if (snap == (IntPtr)(-1)) return list; try { var entry = new THREADENTRY32(); entry.dwSize = (uint)Marshal.SizeOf(typeof(THREADENTRY32)); if (Thread32First(snap, ref entry)) { do { if (entry.th32OwnerProcessID == (uint)pid) list.Add(entry.th32ThreadID); entry.dwSize = (uint)Marshal.SizeOf(typeof(THREADENTRY32)); } while (Thread32Next(snap, ref entry)); } } finally { CloseHandle(snap); } return list; } static void Report(string label, bool ok, int err) { Console.WriteLine(string.Format(" {0,-26} {1}{2}", label, ok ? "OK" : "NEGADO", ok ? "" : " err=" + err)); } static void Main(string[] args) { string target = "nvstreamer"; bool freeze = false, resume = false, probeOnly = false; for (int i = 0; i < args.Length; i++) { string a = args[i]; if (a == "--target" && i + 1 < args.Length) target = args[++i]; else if (a == "--freeze") freeze = true; else if (a == "--resume") resume = true; else if (a == "--probe") probeOnly = true; } Process[] list = Process.GetProcessesByName(target); if (list.Length == 0) { Console.WriteLine(target + ": nao esta rodando"); return; } foreach (Process p in list) { Console.WriteLine("== " + target + " PID " + p.Id); IntPtr h = OpenProcess(PROCESS_TERMINATE | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_SUSPEND_RESUME | 0x0400, false, p.Id); if (h == IntPtr.Zero) { Console.WriteLine(" OpenProcess completo NEGADO err=" + Marshal.GetLastWin32Error()); p.Dispose(); continue; } // 1) memoria: leitura e escrita sem efeito (escreve o que leu) // 1) memoria: acha uma regiao commit+executavel sem usar MainModule var buf = new byte[16]; IntPtr got, put; bool readOk = false, writeOk = false, protOk = false; uint oldProt = 0; IntPtr addr = FindExecutableRegion(h, p.Id); if (addr == IntPtr.Zero) { Console.WriteLine(" regiao de memoria: nao localizada"); } else { Console.WriteLine(" regiao alvo: 0x" + addr.ToInt64().ToString("X")); try { readOk = ReadProcessMemory(h, addr, buf, (IntPtr)buf.Length, out got); protOk = VirtualProtectEx(h, addr, (IntPtr)buf.Length, 0x04 /*PAGE_READWRITE*/, out oldProt); writeOk = WriteProcessMemory(h, addr, buf, (IntPtr)buf.Length, out put); if (protOk) { uint ignored; VirtualProtectEx(h, addr, (IntPtr)buf.Length, oldProt, out ignored); } } catch (Exception ex) { Console.WriteLine(" memoria: " + ex.Message); } } Report("ReadProcessMemory", readOk, Marshal.GetLastWin32Error()); Report("VirtualProtectEx", protOk, Marshal.GetLastWin32Error()); Report("WriteProcessMemory", writeOk, Marshal.GetLastWin32Error()); // 2) suspensao por processo int st = NtSuspendProcess(h); Report("NtSuspendProcess", st == 0, st); if (st == 0) NtResumeProcess(h); // 3) suspensao thread a thread List tids = ThreadsOf(p.Id); Console.WriteLine(" threads: " + tids.Count); int suspended = 0, denied = 0, lastErr = 0; var handles = new List(); foreach (uint tid in tids) { IntPtr th = OpenThread(THREAD_SUSPEND_RESUME, false, tid); if (th == IntPtr.Zero) { denied++; lastErr = Marshal.GetLastWin32Error(); continue; } handles.Add(th); if (freeze || probeOnly) { uint prev = SuspendThread(th); if (prev == 0xFFFFFFFF) { denied++; lastErr = Marshal.GetLastWin32Error(); } else suspended++; } } if (freeze || probeOnly) Console.WriteLine(string.Format(" SuspendThread: {0} suspensas, {1} negadas (err={2})", suspended, denied, lastErr)); else Console.WriteLine(string.Format(" OpenThread: {0} handles, {1} negados (err={2})", handles.Count, denied, lastErr)); if (resume) { foreach (IntPtr th in handles) ResumeThread(th); NtResumeProcess(h); Console.WriteLine(" resume aplicado"); } foreach (IntPtr th in handles) CloseHandle(th); CloseHandle(h); p.Dispose(); } } }