递归火山软件开发平台

标题: 监视进程 [打印本页]

作者: aleax1984    时间: 2025-1-15 11:57
标题: 监视进程
之前记得论坛里有看到监视进程的帖子,进程被创建或退出都能显示,现在怎么都找到,有人看到过的吗

作者: 创世魂    时间: 2025-1-15 12:15
没印象
作者: 承易    时间: 2025-1-18 20:18
  1. import psutil
  2. import time

  3. def get_running_processes():
  4.     """获取当前所有正在运行的进程的PID和名称"""
  5.     processes = {}
  6.     for proc in psutil.process_iter(['pid', 'name']):
  7.         processes[proc.info['pid']] = proc.info['name']
  8.     return processes

  9. def monitor_processes():
  10.     """监视进程的创建和退出"""
  11.     old_processes = get_running_processes()
  12.    
  13.     while True:
  14.         time.sleep(1)  # 每隔1秒检查一次
  15.         new_processes = get_running_processes()
  16.         
  17.         # 检查新创建的进程
  18.         for pid, name in new_processes.items():
  19.             if pid not in old_processes:
  20.                 print(f"新进程创建: PID={pid}, 名称={name}")
  21.         
  22.         # 检查退出的进程
  23.         for pid, name in old_processes.items():
  24.             if pid not in new_processes:
  25.                 print(f"进程退出: PID={pid}, 名称={name}")
  26.         
  27.         # 更新旧的进程列表
  28.         old_processes = new_processes

  29. if __name__ == "__main__":
  30.     monitor_processes()
复制代码

作者: 承易    时间: 2025-1-18 20:25
  1. #include <ntddk.h>

  2. // 进程创建和退出的回调函数
  3. void ProcessNotifyCallback(
  4.     _Inout_ PEPROCESS Process,
  5.     _In_ HANDLE ProcessId,
  6.     _Inout_opt_ PPS_CREATE_NOTIFY_INFO CreateInfo
  7. )
  8. {
  9.     if (CreateInfo != NULL)
  10.     {
  11.         // 进程创建
  12.         DbgPrint("Process created: PID=%d, ImageFileName=%wZ\n", HandleToULong(ProcessId), CreateInfo->ImageFileName);
  13.     }
  14.     else
  15.     {
  16.         // 进程退出
  17.         DbgPrint("Process exited: PID=%d\n", HandleToULong(ProcessId));
  18.     }
  19. }

  20. // 驱动程序卸载函数
  21. void DriverUnload(_In_ PDRIVER_OBJECT DriverObject)
  22. {
  23.     // 取消注册回调函数
  24.     PsSetCreateProcessNotifyRoutineEx(ProcessNotifyCallback, TRUE);
  25.     DbgPrint("Driver unloaded.\n");
  26. }

  27. // 驱动程序入口点
  28. extern "C" NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath)
  29. {
  30.     UNREFERENCED_PARAMETER(RegistryPath);

  31.     // 注册进程创建和退出的回调函数
  32.     NTSTATUS status = PsSetCreateProcessNotifyRoutineEx(ProcessNotifyCallback, FALSE);
  33.     if (!NT_SUCCESS(status))
  34.     {
  35.         DbgPrint("Failed to register process notify callback.\n");
  36.         return status;
  37.     }

  38.     // 设置卸载函数
  39.     DriverObject->DriverUnload = DriverUnload;

  40.     DbgPrint("Driver loaded.\n");

  41.     return STATUS_SUCCESS;
  42. }
复制代码





欢迎光临 递归火山软件开发平台 (https://bbs.voldp.com/) Powered by Discuz! X3.4