首页 > 代码库 > ring3下利用WMI监视进程创建(vc版)
ring3下利用WMI监视进程创建(vc版)
[cpp] view plain copy
- #include "stdafx.h"
- #define _WIN32_DCOM
- #include <iostream>
- using namespace std;
- #include <comdef.h>
- #include <Wbemidl.h>
- # pragma comment(lib, "wbemuuid.lib")
- int main(int argc, char **argv)
- {
- HRESULT hres;
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres))
- {
- cout << "Failed to initialize COM library. "
- << "Error code = 0x"
- << hex << hres << endl;
- return 1;
- }
- IWbemLocator *pLoc = 0;
- HRESULT hr;
- hr = CoCreateInstance(CLSID_WbemLocator, 0,
- CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
- if (FAILED(hr))
- {
- cout << "Failed to create IWbemLocator object. Err code = 0x"
- << hex << hr << endl;
- return hr; // Program has failed.
- }
- IWbemServices *pSvc = 0;
- bstr_t strNetworkResource("ROOT\\CIMV2");
- hr = pLoc->ConnectServer(
- strNetworkResource,
- NULL, NULL, 0, NULL, 0, 0, &pSvc);
- if (FAILED(hr))
- {
- cout << "Could not connect. Error code = 0x"
- << hex << hr << endl;
- pLoc->Release();
- CoUninitialize();
- return hr; // Program has failed.
- }
- cout << "Connected to WMI" << endl;
- // Set the proxy so that impersonation of the client occurs.
- hr = CoSetProxyBlanket(pSvc,
- RPC_C_AUTHN_WINNT,
- RPC_C_AUTHZ_NONE,
- NULL,
- RPC_C_AUTHN_LEVEL_CALL,
- RPC_C_IMP_LEVEL_IMPERSONATE,
- NULL,
- EOAC_NONE
- );
- if (FAILED(hr))
- {
- cout << "Could not set proxy blanket. Error code = 0x"
- << hex << hr << endl;
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return hr;
- }
- bstr_t strLang("WQL");
- //监视taskmgr.exe进程创建
- bstr_t strQuery("SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA ‘Win32_Process‘ AND TargetInstance.Name = ‘taskmgr.exe‘");
- IEnumWbemClassObject* pResult = NULL;
- hr = pSvc->ExecNotificationQuery(strLang, strQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pResult);
- if(SUCCEEDED(hr))
- {
- do{
- IWbemClassObject* pObject = NULL;
- ULONG lCnt = 0;
- hr = pResult->Next(WBEM_INFINITE, 1, &pObject, &lCnt);
- if(SUCCEEDED(hr) && pObject)
- {
- cout<<"taskmgr.exe进程已创建"<<endl;
- break; //退出
- }
- }while(true);
- }
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- CoUninitialize();
- return 0; // Program successfully completed.
- }
http://blog.csdn.net/zwfgdlc/article/details/6613605
ring3下利用WMI监视进程创建(vc版)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。