ProcessInfo.h
上传用户:cjw5120
上传日期:2022-05-11
资源大小:5032k
文件大小:4k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "windows.h"
  3. //#include "comm.h"
  4. #include <vector>
  5. #include <tlhelp32.h>
  6. #include <PSAPI.H>
  7. #pragma comment( lib, "PSAPI.LIB" )
  8. /*
  9.  作者:海啸 lyyer English Name: Jack 
  10.   blog:http://lyyer.blog.sohu.com
  11.   website:http://www.cnGSG.com
  12.   海啸网络安全组织
  13. */
  14. BOOL EnablePrivilege(HANDLE hToken,LPCSTR szPrivName)
  15. {
  16. TOKEN_PRIVILEGES tkp;
  17. LookupPrivilegeValue( NULL,szPrivName,&tkp.Privileges[0].Luid );//修改进程权限
  18. tkp.PrivilegeCount=1;
  19. tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
  20. AdjustTokenPrivileges( hToken,FALSE,&tkp,sizeof tkp,NULL,NULL );//通知系统修改进程权限
  21. return( (GetLastError()==ERROR_SUCCESS) );
  22. }
  23. BOOL GetProcessList(std::vector<PROCESSINFO*> *pProcInfo)
  24. {
  25.     DWORD processid[1024],needed;
  26.     HANDLE hProcess;
  27.     HMODULE hModule;
  28.     char path[MAX_PATH] = "";
  29. char temp[256] = "";
  30.     CString path_convert=path;
  31.     pProcInfo->clear();
  32.     HANDLE handle = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  33.     PROCESSENTRY32 *info = new PROCESSENTRY32;
  34.     info->dwSize=sizeof(PROCESSENTRY32);
  35. int i = 0;
  36.     PROCESSINFO *Proc = new PROCESSINFO;
  37.     if(Process32First(handle,info))
  38.     {
  39. //添加代码 new 更新
  40. Proc = new PROCESSINFO;
  41. memset(Proc, 0,sizeof(PROCESSINFO));
  42. //////////////////////////////////////////////////////////////////////////
  43. Proc->PID      =  info->th32ProcessID;
  44. HANDLE hToken;
  45. lstrcpy(Proc->ProcName,info->szExeFile);
  46. if ( OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken) )
  47. {
  48. if (EnablePrivilege(hToken,SE_DEBUG_NAME))
  49. {
  50. EnumProcesses(processid, sizeof(processid), &needed);
  51. hProcess=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,false,processid[i]);
  52. if (hProcess)
  53. {
  54. EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
  55. GetModuleFileNameEx(hProcess, hModule, path, sizeof(path));
  56. GetShortPathName(path,path,260);
  57. //Proc.ProcPath=path;
  58. lstrcpy(Proc->ProcPath,path);
  59. }
  60. }
  61. }
  62. i++;
  63. pProcInfo->push_back(Proc);
  64. }
  65. while(Process32Next(handle,info)!=FALSE)
  66. {
  67. //添加代码 new 更新
  68. Proc = new PROCESSINFO;
  69. memset(Proc, 0,sizeof(PROCESSINFO));
  70. //////////////////////////////////////////////////////////////////////////
  71. Proc->PID      =  info->th32ProcessID;
  72.         lstrcpy(Proc->ProcName,info->szExeFile);
  73. HANDLE hToken;
  74. if ( OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken) )
  75. {
  76. if (EnablePrivilege(hToken,SE_DEBUG_NAME))
  77. {
  78. EnumProcesses(processid, sizeof(processid), &needed);
  79. hProcess=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,false,processid[i]);
  80. if (hProcess)
  81. {
  82. EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
  83. GetModuleFileNameEx(hProcess, hModule, path, sizeof(path));
  84. GetShortPathName(path,path,260);
  85. lstrcpy(Proc->ProcPath,path);
  86. }
  87. }
  88. }
  89. i++;
  90. pProcInfo->push_back(Proc);
  91. }
  92.     CloseHandle(handle);
  93.     return true;
  94. }
  95. BOOL KillProcess(DWORD pid)
  96. {
  97. //////////////////////////////////////////////////////////////////////////
  98. //匹配进程
  99. //////////////////////////////////////////////////////////////////////////
  100. HANDLE hkernel32;    //被注入进程的句柄
  101.     HANDLE hSnap;
  102.     PROCESSENTRY32 pe; 
  103.     BOOL bNext;
  104.     pe.dwSize = sizeof(pe);
  105.     hSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  106.     bNext=Process32First(hSnap, &pe); 
  107.     while(bNext) 
  108.     {
  109. //if (EnablePrivilege(hSnap,SE_DEBUG_NAME))
  110. //{
  111. if(pe.th32ProcessID=pid)        //--->>
  112. {
  113. hkernel32=OpenProcess(PROCESS_TERMINATE|PROCESS_CREATE_THREAD|PROCESS_VM_WRITE|PROCESS_VM_OPERATION,1,pe.th32ProcessID);
  114. TerminateProcess(hkernel32,0);
  115. break;
  116. }
  117. //}
  118.         bNext=Process32Next(hSnap, &pe); 
  119.     }
  120.     CloseHandle(hSnap);
  121. return true;
  122. }