// MaverickHandles.exe — quem tem handle privilegiado no processo alvo. // Enumera handles do sistema via NtQuerySystemInformation(SystemExtendedHandleInformation=64) // e resolve os nomes de tipo, procurando quem tem access a Process. // csc /nologo /optimize+ /out:MaverickHandles.exe MaverickHandles.cs // MaverickHandles.exe --target nvstreamer using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; internal static class MaverickHandles { const int SystemExtendedHandleInformation = 64; const uint STATUS_INFO_LENGTH_MISMATCH = 0xC0000004; const uint PROCESS_TERMINATE = 0x0001; const uint PROCESS_VM_WRITE = 0x0020; const uint PROCESS_VM_OPERATION = 0x0008; const uint PROCESS_SUSPEND_RESUME = 0x0800; [DllImport("ntdll.dll")] static extern int NtQuerySystemInformation(int cls, IntPtr info, int len, out int ret); [DllImport("ntdll.dll", SetLastError = true)] static extern int NtQueryObject(IntPtr handle, int cls, IntPtr info, int len, out int ret); [DllImport("kernel32.dll")] static extern IntPtr OpenProcess(uint access, bool inherit, int pid); [DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h); [DllImport("kernel32.dll", SetLastError = true)] static extern bool DuplicateHandle(IntPtr srcProc, IntPtr src, IntPtr dstProc, out IntPtr dst, uint access, bool inherit, uint options); [DllImport("kernel32.dll")] static extern IntPtr GetCurrentProcess(); [StructLayout(LayoutKind.Sequential)] struct SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX { public IntPtr Object; public IntPtr UniqueProcessId; public IntPtr HandleValue; public uint GrantedAccess; public ushort CreatorBackTraceIndex; public ushort ObjectTypeIndex; public uint HandleAttributes; public uint Reserved; } static byte[] QueryHandles(out int count) { int size = 1024 * 1024; count = 0; for (int i = 0; i < 12; i++) { IntPtr buf = Marshal.AllocHGlobal(size); int ret; int st = NtQuerySystemInformation(SystemExtendedHandleInformation, buf, size, out ret); if (st == 0) { count = Marshal.ReadInt32(buf); var data = new byte[ret]; Marshal.Copy(buf, data, 0, ret); Marshal.FreeHGlobal(buf); return data; } Marshal.FreeHGlobal(buf); if ((uint)st != STATUS_INFO_LENGTH_MISMATCH) break; size *= 2; } return null; } static string TypeNameOf(ushort typeIndex) { return "type#" + typeIndex; } static void Main(string[] args) { string target = "nvstreamer"; for (int i = 0; i < args.Length; i++) if (args[i] == "--target" && i + 1 < args.Length) target = args[++i]; Console.WriteLine("=== MaverickHandles ==="); Console.WriteLine("user=" + Environment.UserName + " session=" + Process.GetCurrentProcess().SessionId); Process[] procs = Process.GetProcessesByName(target); if (procs.Length == 0) { Console.WriteLine(target + ": nao esta rodando"); return; } var wanted = new Dictionary(); foreach (Process p in procs) { wanted[p.Id] = p.ProcessName; Console.WriteLine("alvo: " + p.ProcessName + " PID " + p.Id); } int count; byte[] data = QueryHandles(out count); if (data == null) { Console.WriteLine("NtQuerySystemInformation falhou."); return; } Console.WriteLine("handles no sistema: " + count); int entrySize = Marshal.SizeOf(typeof(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX)); int offset = IntPtr.Size; // ponteiro para o array, depois do contador var accessByPid = new Dictionary(); int matched = 0; for (int i = 0; i < count; i++) { int pos = offset + i * entrySize; if (pos + entrySize > data.Length) break; IntPtr hObj = (IntPtr)BitConverter.ToInt64(data, pos); IntPtr hPid = (IntPtr)BitConverter.ToInt64(data, pos + IntPtr.Size); IntPtr hVal = (IntPtr)BitConverter.ToInt64(data, pos + IntPtr.Size * 2); byte[] accBytes = new byte[4]; Buffer.BlockCopy(data, pos + IntPtr.Size * 3, accBytes, 0, 4); uint access = BitConverter.ToUInt32(accBytes, 0); int pid; try { pid = (int)hPid.ToInt64(); } catch { continue; } if (!wanted.ContainsKey(pid)) continue; if (i == 0) Console.WriteLine("primeira entrada PID=" + pid); // abre o processo dono do handle e duplica o handle para ver o tipo IntPtr ownerHandle = OpenProcess(0x1000 /*QUERY_LIMITED*/, false, pid); if (ownerHandle == IntPtr.Zero) continue; try { IntPtr dup; if (DuplicateHandle(ownerHandle, hVal, GetCurrentProcess(), out dup, 0, false, 0x0002 /*SAME_ACCESS*/)) { // o handle duplicado só existe se o dono tinha acesso completo; isso já é a prova CloseHandle(dup); accessByPid[pid] = access; matched++; string flags = ""; if ((access & PROCESS_TERMINATE) != 0) flags += "TERMINATE "; if ((access & PROCESS_VM_WRITE) != 0) flags += "VM_WRITE "; if ((access & PROCESS_VM_OPERATION) != 0) flags += "VM_OPERATION "; if ((access & PROCESS_SUSPEND_RESUME) != 0) flags += "SUSPEND_RESUME "; Console.WriteLine(string.Format(" PID {0,-6} handle 0x{1:X} access 0x{2:X8} {3}", pid, hVal.ToInt64(), access, flags.Length > 0 ? flags : "(sem direitos de acao)")); } } finally { CloseHandle(ownerHandle); } } Console.WriteLine("\nhandles que concedem acao no alvo: " + matched); Console.WriteLine("nenhum = o filtro cobre todos; algum com TERMINATE = existe processo branco."); } }