ProcessPage.cpp
上传用户:shouhua
上传日期:2014-12-06
资源大小:5685k
文件大小:11k
- // ProcessPage.cpp : implementation file
- //
- #include "stdafx.h"
- #include "AntiEvilTools.h"
- #include "ProcessPage.h"
- #include "ProcessModule.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- MyList *Gogogo1;
- typedef struct _PROCESS_INF
- {
- ULONG pid;
- ULONG ThreadId;
- ULONG eprocess;
- }PROCESS_INF,*PPROCESS_INF;
- /////////////////////////////////////////////////////////////////////////////
- // CProcessPage property page
- IMPLEMENT_DYNCREATE(CProcessPage, CPropertyPage)
- typedef
- NTSTATUS
- (__stdcall*XXXZwOpenProcess )(
- OUT PHANDLE ProcessHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes,
- IN PCLIENT_ID ClientId
- );
- XXXZwOpenProcess NtOpenProcess=NULL;
- typedef
- NTSTATUS
- (__stdcall *XXXNtCreateJobObject) (
- OUT PHANDLE JobHandle,
- IN ACCESS_MASK DesiredAccess,
- IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL
- );
- XXXNtCreateJobObject NtCreateJobObject=NULL;
- typedef
- NTSTATUS
- (__stdcall *XXXNtAssignProcessToJobObject)(
- IN HANDLE JobHandle,
- IN HANDLE ProcessHandle
- );
- XXXNtAssignProcessToJobObject NtAssignProcessToJobObject=NULL;
- typedef
- NTSTATUS
- (__stdcall *XXXZwTerminateProcess)(
- IN HANDLE ProcessHandle,
- IN NTSTATUS ExitStatus
- );
- XXXZwTerminateProcess ZwTerminateProcess=NULL;
- typedef
- NTSTATUS
- (__stdcall *XXXNtTerminateJobObject)(
- IN HANDLE JobHandle,
- IN NTSTATUS ExitStatus
- );
- XXXNtTerminateJobObject NtTerminateJobObject=NULL;
- typedef
- NTSTATUS
- (__stdcall *XXXNtDuplicateObject) (
- IN HANDLE SourceProcessHandle,
- IN HANDLE SourceHandle,
- IN HANDLE TargetProcessHandle OPTIONAL,
- OUT PHANDLE TargetHandle OPTIONAL,
- IN ACCESS_MASK DesiredAccess,
- IN ULONG HandleAttributes,
- IN ULONG Options
- );
- XXXNtDuplicateObject ZwDuplicateObject=NULL;
- typedef
- NTSTATUS (__stdcall *XXXZwQueryInformationProcess)(
- HANDLE ProcessHandle,
- ULONG ProcessInformationClass,
- PVOID ProcessInformation,
- ULONG ProcessInformationLength,
- PULONG ReturnLength
- );
- XXXZwQueryInformationProcess ZwQueryInformationProcess=NULL;
- typedef
- NTSTATUS
- (__stdcall *XXXZwClose)(
- IN HANDLE Handle
- );
- XXXZwClose ZwClose=NULL;
- HANDLE
- CProcessPage::SDM_OpenProcess (
- DWORD ProcessId
- )
- {
- HANDLE hProcessToRet;
- CLIENT_ID cid;
- OBJECT_ATTRIBUTES attr;
- attr.Length = sizeof(OBJECT_ATTRIBUTES);
- attr.RootDirectory = 0;
- attr.ObjectName = 0;
- attr.Attributes = 0;
- attr.SecurityDescriptor = 0;
- ULONG OldPID=0;
- attr.SecurityQualityOfService = 0;
- NTSTATUS st;
- cid.UniqueProcess = (HANDLE)(ProcessId+1);
- cid.UniqueThread = 0;
- st=NtOpenProcess(&hProcessToRet, PROCESS_ALL_ACCESS, &attr, &cid);
- if(!NT_SUCCESS(st))
- {
- return hProcessToRet;
- }
- return hProcessToRet;
- }
- CProcessPage::CProcessPage() : CPropertyPage(CProcessPage::IDD)
- {
- //{{AFX_DATA_INIT(CProcessPage)
- hDevice=NULL;
- IsHide=false;
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- }
- CProcessPage::~CProcessPage()
- {
- }
- void CProcessPage::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CProcessPage)
- DDX_Control(pDX, IDC_LIST_PROCESS, m_ctrlProcess);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CProcessPage, CPropertyPage)
- //{{AFX_MSG_MAP(CProcessPage)
- ON_NOTIFY(NM_RCLICK, IDC_LIST_PROCESS, OnRclickProcessList)
- ON_COMMAND(ID_MENUITEM_PROCESS_REFRESH, OnMenuitemProcessRefresh)
- ON_COMMAND(ID_PROCESS_MODULE, OnProcessModule)
- ON_COMMAND(IDC_PROCESS_KILL, OnProcessKill)
- ON_COMMAND(ID_MENUITEM_SUPERKILL_PROCESS, OnMenuitemSuperkillProcess)
- ON_COMMAND(ID_MENUITEM_HIDE, OnMenuitemHide)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CProcessPage message handlers
- BOOL CProcessPage::OnInitDialog()
- {
- CDialog::OnInitDialog();
- HANDLE hProcess = GetCurrentProcess();
- LUID luid;
- LookupPrivilegeValue(NULL, "SeDebugPrivilege", &luid);
- HANDLE hToken;
- if(!OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
- MessageBox("OpenProcessToken");
- LUID_AND_ATTRIBUTES laa;
- laa.Luid = luid;
- laa.Attributes = SE_PRIVILEGE_ENABLED;
- TOKEN_PRIVILEGES token;
- memcpy(token.Privileges, &laa, sizeof(laa));
- token.PrivilegeCount = 1;
- if(!AdjustTokenPrivileges(hToken, FALSE, &token, NULL, NULL, 0))
- {
- MessageBox("AdjustTokenPrivileges");
- }
- m_pImage = new CImageList();
- NtOpenProcess = (XXXZwOpenProcess)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwOpenProcess");
- NtCreateJobObject=(XXXNtCreateJobObject)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwCreateJobObject");
- NtAssignProcessToJobObject=(XXXNtAssignProcessToJobObject)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwAssignProcessToJobObject");
- NtTerminateJobObject=(XXXNtTerminateJobObject)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwTerminateJobObject");
- ZwTerminateProcess=(XXXZwTerminateProcess)GetProcAddress( GetModuleHandle("ntdll.dll"), "ZwTerminateProcess");
- ASSERT(m_pImage != NULL); // serious allocation failure checking
- m_ctrlProcess.SetImageList(m_pImage, LVSIL_SMALL);
- m_ctrlProcess.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- Gogogo1->InitSystemImageLists(m_ctrlProcess.m_hWnd);
- int ret=m_ctrlProcess.InsertColumn(0,"进程映象名称",LVCFMT_RIGHT,120,0);
- m_ctrlProcess.InsertColumn(1,"进程ID号",LVCFMT_LEFT,80,1);
- m_ctrlProcess.InsertColumn(2,"父进程ID号",LVCFMT_LEFT,80,2);
- m_ctrlProcess.InsertColumn(3,"进程EPROCESS",LVCFMT_LEFT,120,3);
- m_ctrlProcess.InsertColumn(4,"进程路径",LVCFMT_LEFT,300,4);
- //m_ctrlProcess.SetTextBkColor(RGB(171,174,235));
- m_ctrlProcess.SetTextColor(RGB(255,0,235));
- m_ctrlProcess.SetExtendedStyle(LVS_EX_FULLROWSELECT);
- GetProcess();
- return true;
- }
- void CProcessPage::GetProcess()
- {
- char lastPid[30] = {0};
- m_ctrlProcess.GetItemText(m_ctrlProcess.GetNextItem(-1, LVNI_SELECTED), 1, lastPid, 30);
- HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if(hSnap == (HANDLE)-1)
- return ;
- PROCESSENTRY32 info;
- info.dwSize = sizeof(info);
- if(!Process32First(hSnap, &info))
- return ;
- m_ctrlProcess.DeleteAllItems();
- char strTemp[1024];
- int index = 0;
- ULONG eprocess=0;
- CString str;
- if(hDevice==NULL)
- {
- MessageBox("failed");
- }
- do
- {
- m_ctrlProcess.InsertItem(index, info.szExeFile);
- itoa(info.th32ProcessID, strTemp, 10);
- m_ctrlProcess.SetItemText(index, 1, strTemp);
- itoa(info.th32ParentProcessID, strTemp, 10);
- m_ctrlProcess.SetItemText(index, 2, strTemp);
- eprocess=GetEprocessById(info.th32ProcessID);
- str.Format("0x%08x",eprocess);
- m_ctrlProcess.SetItemText(index, 3, str);
- GetExeFilePath(info.szExeFile, strTemp, info.th32ProcessID);
- m_ctrlProcess.SetItemText(index, 4, strTemp);
- m_ctrlProcess.SetItem(index, 0, LVIF_TEXT | LVIF_IMAGE,info.szExeFile,
- Gogogo1->GetFileIcon(strTemp), 0, 0, 0);
- m_ctrlProcess.SetItemData(index,eprocess);
- index++;
- }while(Process32Next(hSnap, &info));
- CloseHandle(hSnap);
- return ;
- }
- bool CProcessPage::GetExeFilePath(char pExeFileName[], char buffer[], DWORD pid)
- {
- bool flag = true;
- memset(buffer, 0, strlen(buffer));
- int result = (int)FindExecutable(pExeFileName, 0, buffer);
- if(result <= 32)
- {
- memset(buffer, 0, strlen(buffer));
- HANDLE hProcess;
- hProcess = OpenProcess(PROCESS_VM_READ|PROCESS_QUERY_INFORMATION, FALSE, pid);
- if(hProcess != NULL)
- {
- if(GetModuleFileNameEx(hProcess, 0, buffer, 1024) == 0)
- flag = false;
- }
- else
- {
- flag = false;
- }
- CloseHandle(hProcess);
- }
- if(!flag)
- {
- strcpy(buffer, "不知道");
- }
- return flag;
- }
- void CProcessPage::OnRclickProcessList(NMHDR* pNMHDR, LRESULT* pResult)
- {
- iSelected = m_ctrlProcess.GetNextItem(-1, LVNI_SELECTED);
- POINT point;
- ::GetCursorPos(&point);
- CMenu menu;
- menu.LoadMenu(IDR_MENU_PROCESS);
- menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
- *pResult = 0;
- }
- void CProcessPage::OnMenuitemProcessRefresh()
- {
- // TODO: Add your command handler code here
- GetProcess();
- }
- void CProcessPage::OnProcessModule()
- {
- // TODO: Add your command handler code here
- char strPid[20] = {0};
- m_ctrlProcess.GetItemText(iSelected, 1, strPid, 20);
- int pid = atoi(strPid);
- CProcessModule* pModule = new CProcessModule;
- pModule->Create(IDD_DIALOG_PROCESS_MODULE, GetDesktopWindow());
- pModule->SetPID(pid);
- pModule->Refresh();
- pModule->ShowWindow(SW_SHOW);
- }
- void CProcessPage::OnProcessKill()
- {
- // TODO: Add your command handler code here
- if(iSelected < 0 || iSelected >= m_ctrlProcess.GetItemCount())
- return;
- char strPID[30] = {0};
- m_ctrlProcess.GetItemText(iSelected, 0, strPID, 30);
- CString s;
- s.Format("确定要关闭%s吗?", strPID);
- if(MessageBox(s,"提示", MB_OKCANCEL) != IDOK)
- return;
- m_ctrlProcess.GetItemText(iSelected, 1, strPID, 30);
- int pid = atoi(strPID);
- TerminateProcess(pid);
- GetProcess();
- }
- void CProcessPage::TerminateProcess(ULONG pid)
- {
- HANDLE hJob,handle;
- handle=SDM_OpenProcess(pid);
- OBJECT_ATTRIBUTES attr;
- attr.Length = sizeof(OBJECT_ATTRIBUTES);
- NtCreateJobObject(&hJob,JOB_OBJECT_ALL_ACCESS,&attr);
- NtAssignProcessToJobObject(&hJob,handle);
- NtTerminateJobObject(&hJob,STATUS_SUCCESS);
- ZwTerminateProcess(handle,STATUS_SUCCESS);
- }
- void CProcessPage::SetHandle(HANDLE handle)
- {
- hDevice=handle;
- }
- ULONG CProcessPage::GetEprocessById(ULONG pid)
- {
- ULONG bytesReturned,Pid;
- Pid=pid;
- ULONG eprocess;
- DeviceIoControl(hDevice,(DWORD)IOCTL_MT_GETEPROCESS,&Pid,sizeof(pid),&eprocess,sizeof(ULONG),&bytesReturned,NULL);
- return eprocess;
- }
- void CProcessPage::OnMenuitemSuperkillProcess()
- {
- // TODO: Add your command handler code here
- if(iSelected < 0 || iSelected >= m_ctrlProcess.GetItemCount())
- return;
- char strPID[30] = {0};
- m_ctrlProcess.GetItemText(iSelected, 0, strPID, 30);
- CString s;
- ULONG bytesReturned;
- PROCESS_INF ProcessInfo={0};
- s.Format("确定要关闭%s吗?", strPID);
- if(MessageBox(s,"提示", MB_OKCANCEL) != IDOK)
- return;
- ProcessInfo.eprocess=m_ctrlProcess.GetItemData(iSelected);
- m_ctrlProcess.GetItemText(iSelected, 1, strPID, 30);
- ULONG pid = atoi(strPID);
- ProcessInfo.pid=pid;
- THREADENTRY32 ThreadEntry32={0};
- HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
- ThreadEntry32.dwSize=sizeof(THREADENTRY32);
- if(Thread32First(hSnapshot,&ThreadEntry32))
- {
- do
- {
- if(ThreadEntry32.th32OwnerProcessID ==pid)
- {
- ProcessInfo.ThreadId=ThreadEntry32.th32ThreadID;
- break;
- }
- }
- while(Thread32Next(hSnapshot,&ThreadEntry32));
- }
- CloseHandle(hSnapshot);
- DeviceIoControl(hDevice,(DWORD)IOCTL_MT_SUPERKILL,&ProcessInfo,sizeof(ProcessInfo),NULL,0,&bytesReturned,NULL);
- Sleep(100);
- GetProcess();
- }
- void CProcessPage::OnMenuitemHide()
- {
- // TODO: Add your command handler code here
- MessageBox("ok");
- if(IsHide)
- {
- SetDlgItemText(32798,"恢复自身");
- IsHide=false;
- }
- else
- {
- SetDlgItemText(32798,"隐藏自身");
- IsHide=true;
- }
- }