windows下遍历进程和杀进程

windows下遍历进程有多种方式:

进程快照:CreateToolhelp32Snapshot;

进程状态API:PSAPI;


在psapi中主要使用到的方法有:

EnumProcesses——枚举进程;

EnumProcessModules——枚举进程内模块;

GetModuleFileNameEx——获取模块名;

通过这3个方法就可以遍历进程以及进程内各个模块;

其中基本数据结构QString、QList是基于Qt的,如果用的不是Qt库,换成C++对应STL标准库List、String的即可;

[cpp]  view plain copy
  1. //Win32Api:  
  2. void AdjustPrivilege()  
  3. {  
  4.     HANDLE hToken;  
  5.     if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))  
  6.     {  
  7.         TOKEN_PRIVILEGES tp;  
  8.         tp.PrivilegeCount = 1;  
  9.         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
  10.         if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid))  
  11.         {  
  12.             AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);  
  13.         }  
  14.         CloseHandle(hToken);  
  15.     }  
  16. }  
  17.   
  18. //根据进程所在的路径查询进程,并返回进程的ID列表  
  19. QList<DWORD> CheckProcess(const QString &processPath)  
  20. {  
  21.     AdjustPrivilege();  
  22.     QList<DWORD> pIDList;  
  23.     DWORD dwProcessId[1024];  
  24.     DWORD bytesRet;  
  25.     if (EnumProcesses(dwProcessId, sizeof(dwProcessId), &bytesRet))  
  26.     {  
  27.         HANDLE hProcess = NULL;  
  28.         HMODULE hModus[1024];  
  29.         DWORD bytesModuRet;  
  30.         TCHAR szModuleName[MAX_PATH];  
  31.         QStringList tempPathList = processPath.toLower()  
  32.                 .split(QRegExp("[/\\\\]"), QString::SkipEmptyParts);  
  33.         QString processPathWinStd;//转成windows标准的路径  
  34.         int listLength = tempPathList.length();  
  35.         for (int idx = 0; idx < listLength; ++idx)  
  36.         {  
  37.             if (idx != 0)  
  38.                 processPathWinStd.push_back("\\");  
  39.             processPathWinStd.push_back(tempPathList[idx]);  
  40.         }  
  41.         int ProcessNum = (bytesRet/sizeof(DWORD));  
  42.         for (int i = 0, j = 0; i < ProcessNum; ++i)  
  43.         {  
  44.             hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwProcessId[i]);  
  45.             if (hProcess)  
  46.             {  
  47.                 //Do not call CloseHandle on any of the handles returned by this function.  
  48.                 //The information comes from a snapshot, so there are no resources to be freed.  
  49.                 if (EnumProcessModules(hProcess, hModus, sizeof(hModus), &bytesModuRet))  
  50.                 {  
  51.                     int ModuleNum = (bytesRet/sizeof(DWORD));  
  52.                     for (j = 0; j < ModuleNum; ++j)  
  53.                     {  
  54.                         if (GetModuleFileNameEx(hProcess, hModus[j], szModuleName, sizeof(szModuleName))  
  55.                          && processPathWinStd == QString::fromWCharArray(szModuleName, _tcslen(szModuleName)).toLower())  
  56.                         {  
  57.                             pIDList.push_back(dwProcessId[i]);  
  58.                         }  
  59.                     }  
  60.                 }  
  61.                 CloseHandle(hProcess);  
  62.             }  
  63.         }  
  64.     }  
  65.     return pIDList;  
  66. }  
注意其中的EnumProcessModules中的hModus句柄不能使用CloseHandle(因为这些句柄来自一个快照snapshot,不是实际的资源):

Do not call CloseHandle on any of the handles returned by this function.
The information comes from a snapshot, so there are no resources to be freed.


windows下杀死进程也有两种方式:

TerminateProcess——系统API;

NtTerminateProcess——ntdll.dll中未公开导出方法;

TerminateProcess实际上也是调用NtTerminateProcess实现具体功能的;

正常情况下,调用这两个任何一个都可以终结或者杀死一个进程(夸用户杀进程需要先提权,提权见“windows提权方法”),但是在有些情况下有些应用程序为了防止自己被杀,会hook系统的TerminateProcess方法,导致TerminateProcess失效;所以用NtTerminateProcess可以使杀死进程的成功率更高一些;

[cpp]  view plain copy
  1. void Terminate(const QString &processPath)  
  2. {  
  3.     AdjustPrivilege();  
  4.     //================TerminateProcess================//遍历进程列表与进程模块,匹配路径后强杀  
  5.     DWORD dwProcessId[1024];  
  6.     DWORD bytesRet;  
  7.     if (EnumProcesses(dwProcessId, sizeof(dwProcessId), &bytesRet))  
  8.     {  
  9.         HANDLE hProcess = NULL;  
  10.         HMODULE hModus[1024];  
  11.         DWORD bytesModuRet;  
  12.         TCHAR szModuleName[MAX_PATH];  
  13.         QStringList tempPathList = processPath.toLower()  
  14.                 .split(QRegExp("[/\\\\]"), QString::SkipEmptyParts);  
  15.         QString processPathWinStd;//转成windows标准的路径  
  16.         int listLength = tempPathList.length();  
  17.         for (int idx = 0; idx < listLength; ++idx)  
  18.         {  
  19.             if (idx != 0)  
  20.                 processPathWinStd.push_back("\\");  
  21.             processPathWinStd.push_back(tempPathList[idx]);  
  22.         }  
  23.         int ProcessNum = (bytesRet/sizeof(DWORD));  
  24.         for (int i = 0, j = 0; i < ProcessNum; ++i)  
  25.         {  
  26.             hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwProcessId[i]);  
  27.             if (hProcess)  
  28.             {  
  29.                 //Do not call CloseHandle on any of the handles returned by this function.  
  30.                 //The information comes from a snapshot, so there are no resources to be freed.  
  31.                 if (EnumProcessModules(hProcess, hModus, sizeof(hModus), &bytesModuRet))  
  32.                 {  
  33.                     int ModuleNum = (bytesRet/sizeof(DWORD));  
  34.                     for (j = 0; j < ModuleNum; ++j)  
  35.                     {  
  36.                         if (GetModuleFileNameEx(hProcess, hModus[j], szModuleName, sizeof(szModuleName))  
  37.                          && processPathWinStd == QString::fromWCharArray(szModuleName, _tcslen(szModuleName)).toLower())  
  38.                         {  
  39.                             TerminateProcess(hProcess, 4);  
  40.                         }  
  41.                     }  
  42.                 }  
  43.                 CloseHandle(hProcess);  
  44.             }  
  45.         }  
  46.     }  
  47. }  
  48.   
  49. void Terminate(const DWORD &pID)  
  50. {  
  51.     AdjustPrivilege();  
  52.     //================TerminateProcess================//根据进程ID强杀  
  53.     HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pID);  
  54.     if (hProcess)  
  55.     {  
  56.         TerminateProcess(hProcess, 4);  
  57.         CloseHandle(hProcess);  
  58.     }  
  59. }  

ntdll.dll导出方式:

[cpp]  view plain copy
  1. const unsigned long SE_DEBUG_PRIVILEGE = 0x14;  
  2. typedef int (__stdcall *fRtlAdjustPrivilege)(ULONGBOOLEANBOOLEANPBOOLEAN);  
  3. typedef DWORD (__stdcall *fNtTerminateProcess)(HANDLEUINT);  
  4. HMODULE hNtDll = NULL;  
  5. fRtlAdjustPrivilege funcAdjustPrivilege = NULL;  
  6. fNtTerminateProcess funcTerminateProcess = NULL;  
  7.   
  8. //================  
  9. //NtDll方式:  
  10. //================  
  11. bool NtInit()  
  12. {  
  13.     hNtDll = LoadLibrary(_T("ntdll.dll"));  
  14.     if (!hNtDll)  
  15.         return false;  
  16.     funcAdjustPrivilege =  
  17.             (fRtlAdjustPrivilege)GetProcAddress(hNtDll, "RtlAdjustPrivilege");  
  18.     funcTerminateProcess =  
  19.             (fNtTerminateProcess)GetProcAddress(hNtDll, "NtTerminateProcess");  
  20.     return true;  
  21. }  
  22.   
  23. void NtFree()  
  24. {  
  25.     if (hNtDll)  
  26.         FreeLibrary(hNtDll);  
  27. }  
  28.   
  29. void NtAdjustPrivilege()  
  30. {  
  31.     if (funcAdjustPrivilege)  
  32.     {  
  33.         BOOLEAN oldStatus;  
  34.         funcAdjustPrivilege(SE_DEBUG_PRIVILEGE, truefalse, &oldStatus);  
  35.     }  
  36. }  
  37.   
  38. void NtTerminate(const QString &processPath)  
  39. {  
  40.     NtAdjustPrivilege();  
  41.     //================TerminateProcess================//遍历进程列表与进程模块,匹配路径后强杀  
  42.     if (funcTerminateProcess)  
  43.     {  
  44.         DWORD dwProcessId[1024];  
  45.         DWORD bytesRet;  
  46.         if (EnumProcesses(dwProcessId, sizeof(dwProcessId), &bytesRet))  
  47.         {  
  48.             HANDLE hProcess = NULL;  
  49.             HMODULE hModus[1024];  
  50.             DWORD bytesModuRet;  
  51.             TCHAR szModuleName[MAX_PATH];  
  52.             QStringList tempPathList = processPath.toLower()  
  53.                     .split(QRegExp("[/\\\\]"), QString::SkipEmptyParts);  
  54.             QString processPathWinStd;//转成windows标准的路径  
  55.             int listLength = tempPathList.length();  
  56.             for (int idx = 0; idx < listLength; ++idx)  
  57.             {  
  58.                 if (idx != 0)  
  59.                     processPathWinStd.push_back("\\");  
  60.                 processPathWinStd.push_back(tempPathList[idx]);  
  61.             }  
  62.             int ProcessNum = (bytesRet/sizeof(DWORD));  
  63.             for (int i = 0, j = 0; i < ProcessNum; ++i)  
  64.             {  
  65.                 hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwProcessId[i]);  
  66.                 if (hProcess)  
  67.                 {  
  68.                     //Do not call CloseHandle on any of the handles returned by this function.  
  69.                     //The information comes from a snapshot, so there are no resources to be freed.  
  70.                     if (EnumProcessModules(hProcess, hModus, sizeof(hModus), &bytesModuRet))  
  71.                     {  
  72.                         int ModuleNum = (bytesRet/sizeof(DWORD));  
  73.                         for (j = 0; j < ModuleNum; ++j)  
  74.                         {  
  75.                             if (GetModuleFileNameEx(hProcess, hModus[j], szModuleName, sizeof(szModuleName))  
  76.                              && processPathWinStd == QString::fromWCharArray(szModuleName, _tcslen(szModuleName)).toLower())  
  77.                             {  
  78.                                 funcTerminateProcess(hProcess, 4);  
  79.                             }  
  80.                         }  
  81.                     }  
  82.                     CloseHandle(hProcess);  
  83.                 }  
  84.             }  
  85.         }  
  86.     }  
  87. }  
  88.   
  89. void NtTerminate(const DWORD &pID)  
  90. {  
  91.     NtAdjustPrivilege();  
  92.     //================TerminateProcess================//根据进程ID强杀  
  93.     if (funcTerminateProcess)  
  94.     {  
  95.         HANDLE hProcess = NULL;  
  96.         hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pID);  
  97.         if (hProcess)  
  98.         {  
  99.             funcTerminateProcess(hProcess, 4);  
  100.             CloseHandle(hProcess);  
  101.         }  
  102.     }  
  103. }  

转载于:https://www.cnblogs.com/csdndreamer/p/5490687.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值