ProcessPage.cpp
上传用户:shouhua
上传日期:2014-12-06
资源大小:5685k
文件大小:11k
源码类别:

杀毒

开发平台:

Visual C++

  1. // ProcessPage.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "AntiEvilTools.h"
  5. #include "ProcessPage.h"
  6. #include "ProcessModule.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. MyList   *Gogogo1;
  13. typedef  struct _PROCESS_INF
  14. {
  15. ULONG   pid;
  16. ULONG   ThreadId;
  17. ULONG   eprocess;
  18. }PROCESS_INF,*PPROCESS_INF;
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CProcessPage property page
  21. IMPLEMENT_DYNCREATE(CProcessPage, CPropertyPage)
  22. typedef
  23. NTSTATUS
  24. (__stdcall*XXXZwOpenProcess )(
  25.   OUT PHANDLE  ProcessHandle,
  26.   IN ACCESS_MASK  DesiredAccess,
  27.   IN POBJECT_ATTRIBUTES  ObjectAttributes,
  28.   IN PCLIENT_ID  ClientId
  29.   );
  30. XXXZwOpenProcess NtOpenProcess=NULL;
  31. typedef
  32. NTSTATUS
  33. (__stdcall *XXXNtCreateJobObject) (
  34.    OUT PHANDLE JobHandle,
  35.    IN ACCESS_MASK DesiredAccess,
  36.    IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
  37.    );
  38. XXXNtCreateJobObject  NtCreateJobObject=NULL;
  39. typedef
  40. NTSTATUS
  41. (__stdcall *XXXNtAssignProcessToJobObject)(
  42.    IN HANDLE JobHandle,
  43.    IN HANDLE ProcessHandle
  44.    );
  45. XXXNtAssignProcessToJobObject NtAssignProcessToJobObject=NULL;
  46. typedef
  47. NTSTATUS
  48. (__stdcall *XXXZwTerminateProcess)(
  49.    IN HANDLE  ProcessHandle,
  50.    IN NTSTATUS  ExitStatus
  51.    );
  52. XXXZwTerminateProcess  ZwTerminateProcess=NULL;
  53. typedef
  54. NTSTATUS
  55. (__stdcall *XXXNtTerminateJobObject)(
  56.  IN HANDLE JobHandle,
  57.  IN NTSTATUS ExitStatus
  58.  );
  59. XXXNtTerminateJobObject  NtTerminateJobObject=NULL;
  60. typedef
  61. NTSTATUS
  62. (__stdcall *XXXNtDuplicateObject) (
  63.    IN HANDLE SourceProcessHandle,
  64.    IN HANDLE SourceHandle,
  65.    IN HANDLE TargetProcessHandle OPTIONAL,
  66.    OUT PHANDLE TargetHandle OPTIONAL,
  67.    IN ACCESS_MASK DesiredAccess,
  68.    IN ULONG HandleAttributes,
  69.    IN ULONG Options
  70.    );
  71. XXXNtDuplicateObject  ZwDuplicateObject=NULL;
  72. typedef
  73. NTSTATUS (__stdcall *XXXZwQueryInformationProcess)(
  74.    HANDLE ProcessHandle,
  75.    ULONG ProcessInformationClass,
  76.    PVOID ProcessInformation,
  77.    ULONG ProcessInformationLength,
  78.    PULONG ReturnLength
  79.    );
  80. XXXZwQueryInformationProcess   ZwQueryInformationProcess=NULL;
  81. typedef
  82. NTSTATUS 
  83. (__stdcall *XXXZwClose)(
  84. IN HANDLE  Handle
  85. );
  86. XXXZwClose  ZwClose=NULL;
  87. HANDLE
  88. CProcessPage::SDM_OpenProcess (
  89.  DWORD ProcessId
  90.  )                  
  91. {
  92. HANDLE      hProcessToRet;
  93. CLIENT_ID     cid;
  94.     OBJECT_ATTRIBUTES     attr;
  95.     attr.Length = sizeof(OBJECT_ATTRIBUTES);
  96.     attr.RootDirectory = 0;
  97.     attr.ObjectName = 0;
  98.     attr.Attributes = 0;
  99.     attr.SecurityDescriptor = 0;
  100. ULONG   OldPID=0;
  101.     attr.SecurityQualityOfService = 0;
  102. NTSTATUS  st;
  103. cid.UniqueProcess = (HANDLE)(ProcessId+1);
  104.     cid.UniqueThread = 0;
  105.     st=NtOpenProcess(&hProcessToRet, PROCESS_ALL_ACCESS, &attr, &cid);
  106. if(!NT_SUCCESS(st))
  107. {
  108. return   hProcessToRet;
  109. }
  110. return   hProcessToRet;
  111. }
  112. CProcessPage::CProcessPage() : CPropertyPage(CProcessPage::IDD)
  113. {
  114. //{{AFX_DATA_INIT(CProcessPage)
  115. hDevice=NULL;
  116. IsHide=false;
  117. // NOTE: the ClassWizard will add member initialization here
  118. //}}AFX_DATA_INIT
  119. }
  120. CProcessPage::~CProcessPage()
  121. {
  122. }
  123. void CProcessPage::DoDataExchange(CDataExchange* pDX)
  124. {
  125. CPropertyPage::DoDataExchange(pDX);
  126. //{{AFX_DATA_MAP(CProcessPage)
  127. DDX_Control(pDX, IDC_LIST_PROCESS, m_ctrlProcess);
  128. //}}AFX_DATA_MAP
  129. }
  130. BEGIN_MESSAGE_MAP(CProcessPage, CPropertyPage)
  131. //{{AFX_MSG_MAP(CProcessPage)
  132. ON_NOTIFY(NM_RCLICK, IDC_LIST_PROCESS, OnRclickProcessList)
  133. ON_COMMAND(ID_MENUITEM_PROCESS_REFRESH, OnMenuitemProcessRefresh)
  134. ON_COMMAND(ID_PROCESS_MODULE, OnProcessModule)
  135. ON_COMMAND(IDC_PROCESS_KILL, OnProcessKill)
  136. ON_COMMAND(ID_MENUITEM_SUPERKILL_PROCESS, OnMenuitemSuperkillProcess)
  137. ON_COMMAND(ID_MENUITEM_HIDE, OnMenuitemHide)
  138. //}}AFX_MSG_MAP
  139. END_MESSAGE_MAP()
  140. /////////////////////////////////////////////////////////////////////////////
  141. // CProcessPage message handlers
  142. BOOL CProcessPage::OnInitDialog()
  143. {
  144. CDialog::OnInitDialog();
  145. HANDLE hProcess = GetCurrentProcess();
  146. LUID luid;
  147. LookupPrivilegeValue(NULL, "SeDebugPrivilege", &luid);
  148. HANDLE hToken;
  149.     if(!OpenProcessToken(hProcess,  TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
  150. MessageBox("OpenProcessToken");
  151. LUID_AND_ATTRIBUTES laa;
  152. laa.Luid = luid;
  153. laa.Attributes = SE_PRIVILEGE_ENABLED;
  154. TOKEN_PRIVILEGES token;
  155. memcpy(token.Privileges, &laa, sizeof(laa));
  156. token.PrivilegeCount = 1;
  157. if(!AdjustTokenPrivileges(hToken, FALSE, &token, NULL, NULL, 0))
  158. {
  159. MessageBox("AdjustTokenPrivileges");
  160. }
  161. m_pImage = new CImageList();
  162. NtOpenProcess = (XXXZwOpenProcess)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwOpenProcess");
  163. NtCreateJobObject=(XXXNtCreateJobObject)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwCreateJobObject");
  164. NtAssignProcessToJobObject=(XXXNtAssignProcessToJobObject)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwAssignProcessToJobObject");
  165. NtTerminateJobObject=(XXXNtTerminateJobObject)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwTerminateJobObject");
  166. ZwTerminateProcess=(XXXZwTerminateProcess)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwTerminateProcess");
  167. ASSERT(m_pImage != NULL);    // serious allocation failure checking
  168. m_ctrlProcess.SetImageList(m_pImage, LVSIL_SMALL);
  169. m_ctrlProcess.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  170. Gogogo1->InitSystemImageLists(m_ctrlProcess.m_hWnd);
  171. int ret=m_ctrlProcess.InsertColumn(0,"进程映象名称",LVCFMT_RIGHT,120,0);
  172. m_ctrlProcess.InsertColumn(1,"进程ID号",LVCFMT_LEFT,80,1);
  173. m_ctrlProcess.InsertColumn(2,"父进程ID号",LVCFMT_LEFT,80,2);
  174. m_ctrlProcess.InsertColumn(3,"进程EPROCESS",LVCFMT_LEFT,120,3);
  175. m_ctrlProcess.InsertColumn(4,"进程路径",LVCFMT_LEFT,300,4);
  176. //m_ctrlProcess.SetTextBkColor(RGB(171,174,235));
  177. m_ctrlProcess.SetTextColor(RGB(255,0,235));
  178. m_ctrlProcess.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  179. GetProcess();
  180. return   true;
  181. }
  182. void CProcessPage::GetProcess()
  183. {
  184. char lastPid[30] = {0};
  185. m_ctrlProcess.GetItemText(m_ctrlProcess.GetNextItem(-1, LVNI_SELECTED), 1, lastPid, 30);
  186. HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  187. if(hSnap == (HANDLE)-1)
  188. return ;
  189. PROCESSENTRY32 info;
  190. info.dwSize = sizeof(info);
  191. if(!Process32First(hSnap, &info))
  192. return ;
  193. m_ctrlProcess.DeleteAllItems();
  194. char strTemp[1024];
  195. int index = 0;
  196. ULONG  eprocess=0;
  197. CString  str;
  198. if(hDevice==NULL)
  199. {
  200. MessageBox("failed");
  201. }
  202. do
  203. {
  204. m_ctrlProcess.InsertItem(index, info.szExeFile);
  205. itoa(info.th32ProcessID, strTemp, 10);
  206. m_ctrlProcess.SetItemText(index, 1, strTemp);
  207. itoa(info.th32ParentProcessID, strTemp, 10);
  208. m_ctrlProcess.SetItemText(index, 2, strTemp);
  209. eprocess=GetEprocessById(info.th32ProcessID);
  210. str.Format("0x%08x",eprocess);
  211. m_ctrlProcess.SetItemText(index, 3, str);
  212. GetExeFilePath(info.szExeFile, strTemp, info.th32ProcessID);
  213. m_ctrlProcess.SetItemText(index, 4, strTemp);
  214. m_ctrlProcess.SetItem(index, 0, LVIF_TEXT | LVIF_IMAGE,info.szExeFile, 
  215. Gogogo1->GetFileIcon(strTemp), 0, 0, 0);
  216. m_ctrlProcess.SetItemData(index,eprocess);
  217. index++;
  218. }while(Process32Next(hSnap, &info));
  219. CloseHandle(hSnap);
  220. return ;
  221. }
  222. bool CProcessPage::GetExeFilePath(char pExeFileName[], char buffer[], DWORD pid)
  223. {
  224. bool flag = true;
  225. memset(buffer, 0, strlen(buffer));
  226. int result = (int)FindExecutable(pExeFileName, 0, buffer);
  227. if(result <= 32)
  228. {
  229. memset(buffer, 0, strlen(buffer));
  230. HANDLE hProcess;
  231. hProcess = OpenProcess(PROCESS_VM_READ|PROCESS_QUERY_INFORMATION, FALSE, pid);
  232. if(hProcess != NULL)
  233. {
  234. if(GetModuleFileNameEx(hProcess, 0, buffer, 1024) == 0)
  235. flag = false;
  236. }
  237. else
  238. {
  239. flag = false;
  240. }
  241. CloseHandle(hProcess);
  242. }
  243. if(!flag)
  244. {
  245. strcpy(buffer, "不知道");
  246. }
  247. return flag;
  248. }
  249. void CProcessPage::OnRclickProcessList(NMHDR* pNMHDR, LRESULT* pResult) 
  250. {
  251. iSelected = m_ctrlProcess.GetNextItem(-1, LVNI_SELECTED);
  252. POINT point;
  253. ::GetCursorPos(&point);
  254. CMenu menu;
  255. menu.LoadMenu(IDR_MENU_PROCESS);
  256. menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
  257. *pResult = 0;
  258. }
  259. void CProcessPage::OnMenuitemProcessRefresh() 
  260. {
  261. // TODO: Add your command handler code here
  262. GetProcess();
  263. }
  264. void CProcessPage::OnProcessModule() 
  265. {
  266. // TODO: Add your command handler code here
  267. char strPid[20] = {0};
  268. m_ctrlProcess.GetItemText(iSelected, 1, strPid, 20);
  269. int pid = atoi(strPid);
  270. CProcessModule* pModule = new CProcessModule;
  271. pModule->Create(IDD_DIALOG_PROCESS_MODULE, GetDesktopWindow());
  272. pModule->SetPID(pid);
  273. pModule->Refresh();
  274. pModule->ShowWindow(SW_SHOW);
  275. }
  276. void CProcessPage::OnProcessKill() 
  277. {
  278. // TODO: Add your command handler code here
  279. if(iSelected < 0 || iSelected >= m_ctrlProcess.GetItemCount())
  280. return;
  281. char strPID[30] = {0};
  282. m_ctrlProcess.GetItemText(iSelected, 0, strPID, 30);
  283. CString s;
  284. s.Format("确定要关闭%s吗?", strPID);
  285. if(MessageBox(s,"提示", MB_OKCANCEL) != IDOK)
  286. return;
  287. m_ctrlProcess.GetItemText(iSelected, 1, strPID, 30);
  288. int pid = atoi(strPID);
  289. TerminateProcess(pid);
  290. GetProcess();
  291. }
  292. void CProcessPage::TerminateProcess(ULONG pid)
  293. {
  294. HANDLE hJob,handle;
  295. handle=SDM_OpenProcess(pid);
  296. OBJECT_ATTRIBUTES     attr;
  297. attr.Length = sizeof(OBJECT_ATTRIBUTES);
  298. NtCreateJobObject(&hJob,JOB_OBJECT_ALL_ACCESS,&attr);
  299. NtAssignProcessToJobObject(&hJob,handle);
  300. NtTerminateJobObject(&hJob,STATUS_SUCCESS);
  301. ZwTerminateProcess(handle,STATUS_SUCCESS);
  302. }
  303. void CProcessPage::SetHandle(HANDLE handle)
  304. {
  305.     hDevice=handle;
  306. }
  307. ULONG CProcessPage::GetEprocessById(ULONG pid)
  308. {
  309.     ULONG   bytesReturned,Pid;
  310.     Pid=pid;
  311. ULONG   eprocess;
  312. DeviceIoControl(hDevice,(DWORD)IOCTL_MT_GETEPROCESS,&Pid,sizeof(pid),&eprocess,sizeof(ULONG),&bytesReturned,NULL);
  313. return  eprocess;
  314. }
  315. void CProcessPage::OnMenuitemSuperkillProcess() 
  316. {
  317. // TODO: Add your command handler code here
  318. if(iSelected < 0 || iSelected >= m_ctrlProcess.GetItemCount())
  319. return;
  320. char strPID[30] = {0};
  321. m_ctrlProcess.GetItemText(iSelected, 0, strPID, 30);
  322. CString s;
  323. ULONG  bytesReturned;
  324. PROCESS_INF   ProcessInfo={0};
  325. s.Format("确定要关闭%s吗?", strPID);
  326. if(MessageBox(s,"提示", MB_OKCANCEL) != IDOK)
  327. return;
  328.     ProcessInfo.eprocess=m_ctrlProcess.GetItemData(iSelected);
  329.     m_ctrlProcess.GetItemText(iSelected, 1, strPID, 30);
  330. ULONG pid = atoi(strPID);
  331. ProcessInfo.pid=pid;
  332. THREADENTRY32    ThreadEntry32={0};
  333. HANDLE   hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
  334. ThreadEntry32.dwSize=sizeof(THREADENTRY32);
  335. if(Thread32First(hSnapshot,&ThreadEntry32))
  336. {
  337. do
  338. {
  339. if(ThreadEntry32.th32OwnerProcessID ==pid)
  340. {
  341.                 ProcessInfo.ThreadId=ThreadEntry32.th32ThreadID;
  342. break;
  343. }
  344. }
  345. while(Thread32Next(hSnapshot,&ThreadEntry32));
  346. }
  347. CloseHandle(hSnapshot);
  348. DeviceIoControl(hDevice,(DWORD)IOCTL_MT_SUPERKILL,&ProcessInfo,sizeof(ProcessInfo),NULL,0,&bytesReturned,NULL);
  349. Sleep(100);
  350. GetProcess();
  351. }
  352. void CProcessPage::OnMenuitemHide() 
  353. {
  354. // TODO: Add your command handler code here
  355. MessageBox("ok");
  356. if(IsHide)
  357. {
  358. SetDlgItemText(32798,"恢复自身");
  359. IsHide=false;
  360. }
  361. else
  362. {
  363. SetDlgItemText(32798,"隐藏自身");
  364. IsHide=true;
  365. }
  366. }