ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

任务管理器中右键菜单,“结束进程树”菜单的实现

任务管理器中右键菜单,“结束进程树”菜单的实现 分析了WIN 7“任务管理器”的“结束任务”菜单的实现其实也是简单的结束进程只是多加检测是否是核心进程并检测是否为16位程序并当前系统是否32位的如果是的用相关APIVDMTerminateTaskWOW。如果是64位的系统当前要结束的进程是16位的直接返回ERROR_INVALID_PARAMETER。“任务管理器”内部是查询自已保存的自定义结构的进程信息数组。所以结束进程他也不用实时查询进程信息。都是查询他保存的数组指针。“任务管理器”内部结束进程等操作都有一个参数:CreateTime : LONGLONG, 通过对比这个时间来区分相同PID的不同进程。MyQuerySystemInformation 是 ntQuerySystemInformation 调用我们为了单独实现出来简单一些我直接查询进程信息function MyKillAllChildrenProcess(const dwTaskPid, dwProcessId : DWORD; const pSystemProcesses : PSYSTEM_PROCESS_INFORMATION; LCreateTime : LONGLONG; var StrError : WideString) : Boolean;vardwNewProcessId : DWORD;newCreateTime : LONGLONG;pSystemProcesses2 : PSYSTEM_PROCESS_INFORMATION;beginResult:False;pSystemProcesses2:pSystemProcesses;while True dobeginwith pSystemProcesses2^ dobegin// 父 PID 等于要结束的进程 IDif (InheritedFromUniqueProcessId dwProcessId) and (UniqueProcessId dwProcessId) and (LCreateTime CreateTime.QuadPart) thenbegindwNewProcessId:UniqueProcessId;newCreateTime:CreateTime.QuadPart;MyKillAllChildrenProcess(dwTaskPid, dwNewProcessId, pSystemProcesses, newCreateTime, StrError);if (dwNewProcessId dwTaskPid) thenbeginResult:MyTerminateProcessEx(dwNewProcessId, StrError);MyAddDebugInfo(结束进程树子进程 PID: IntToStr(dwNewProcessId) 操作 C_FailOrSuccess[Result]);end;end;if (NextEntryOffset 0) thenbeginInc(ULONG(pSystemProcesses2), NextEntryOffset);end else Break;end;end;end;function MyRecursiveKillProcess(const dwProcessId : DWORD; LCreateTime : ULONGLONG; var StrError : WideString) : Boolean;varpSystemProcesses, pSystemProcesses2 : PSYSTEM_PROCESS_INFORMATION;dwQueryProcessIsEnd, dwTaskPid : DWORD;beginResult:False;if (not MyQuerySystemInformation(SystemProcessInformation, Pointer(pSystemProcesses))) thenbeginExit;end;StrError:;pSystemProcesses2:pSystemProcesses;//dwTaskPid 是任务管理器PID 用于最后才结束自已dwTaskPid:MyGetCurrentProcessId; // GetCurrentProcessId 的功能内部实现whileTruedobeginif (dwProcessId pSystemProcesses2^.UniqueProcessId) and (LCreateTime pSystemProcesses2^.CreateTime.QuadPartthenbeginMyKillAllChildrenProcess(dwTaskPid, dwProcessId, pSystemProcesses, LCreateTime, StrError);break;end;if (pSystemProcesses2^.NextEntryOffset 0) thenbeginInc(ULONG(pSystemProcesses2), pSystemProcesses2^.NextEntryOffset);end else Break;end;RtlFreeHeap(GetProcessHeap, 0, pSystemProcesses);Result:MyTerminateProcessEX(dwProcessId, StrError); // 各种TerminateProcess 相关实现。MyAddDebugInfo(结束进程树PID: IntToStr(dwProcessId) 操作 C_FailOrSuccess[Result]);end;
RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进