Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WIN32_DCOM
- #define UNICODE
- #include <iostream>
- using namespace std;
- #include <comdef.h>
- #include <Wbemidl.h>
- #pragma comment(lib, "wbemuuid.lib")
- #pragma comment(lib, "credui.lib")
- #pragma comment(lib, "comsuppw.lib")
- #include <wincred.h>
- #include <strsafe.h>
- int __cdecl main(int argc, char **argv)
- {
- HRESULT hres;
- // Step 1: --------------------------------------------------
- // Initialize COM. ------------------------------------------
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres))
- {
- cout << "Failed to initialize COM library. Error code = 0x"
- << hex << hres << endl;
- return 1; // Program has failed.
- }
- // Step 2: --------------------------------------------------
- // Set general COM security levels --------------------------
- hres = CoInitializeSecurity(
- NULL,
- -1, // COM authentication
- NULL, // Authentication services
- NULL, // Reserved
- RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
- RPC_C_IMP_LEVEL_IDENTIFY, // Default Impersonation
- NULL, // Authentication info
- EOAC_NONE, // Additional capabilities
- NULL // Reserved
- );
- if (FAILED(hres))
- {
- cout << "Failed to initialize security. Error code = 0x"
- << hex << hres << endl;
- CoUninitialize();
- return 1; // Program has failed.
- }
- // Step 3: ---------------------------------------------------
- // Obtain the initial locator to WMI -------------------------
- IWbemLocator *pLoc = NULL;
- hres = CoCreateInstance(
- CLSID_WbemLocator,
- 0,
- CLSCTX_INPROC_SERVER,
- IID_IWbemLocator, (LPVOID *)&pLoc);
- if (FAILED(hres))
- {
- cout << "Failed to create IWbemLocator object."
- << " Err code = 0x"
- << hex << hres << endl;
- CoUninitialize();
- return 1; // Program has failed.
- }
- // Step 4: -----------------------------------------------------
- // Connect to WMI through the IWbemLocator::ConnectServer method
- // Get the user name and password for the remote computer
- wchar_t pszName[CREDUI_MAX_USERNAME_LENGTH + 1] = L"\\administrator";
- wchar_t pszPwd[CREDUI_MAX_PASSWORD_LENGTH + 1] = L"Password";
- wchar_t pszDomain[CREDUI_MAX_USERNAME_LENGTH + 1];
- wchar_t pszUserName[CREDUI_MAX_USERNAME_LENGTH + 1];
- // wchar_t pszAuthority[CREDUI_MAX_USERNAME_LENGTH + 1];
- //mbstowcs(pszName, "\\administrator", CREDUI_MAX_USERNAME_LENGTH + 1);
- //mbstowcs(pszPwd, "Vembu123", CREDUI_MAX_USERNAME_LENGTH + 1);
- // Connect to the remote root\cimv2 namespace
- // and obtain pointer pSvc to make IWbemServices calls.
- //---------------------------------------------------------
- cout << "Printing" << endl;
- IWbemServices *pSvc = NULL;
- hres = pLoc->ConnectServer(
- _bstr_t(L"\\\\machine\\root\\cimv2"),
- _bstr_t(pszName), // User name
- _bstr_t(pszPwd), // User password
- NULL, // Locale
- NULL, // Security flags
- NULL,// Authority
- NULL, // Context object
- &pSvc // IWbemServices proxy
- );
- if (FAILED(hres))
- {
- cout << "Could not connect. Error code = 0x"
- << hex << hres << endl;
- pLoc->Release();
- CoUninitialize();
- return 1; // Program has failed.
- }
- cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
- // step 5: --------------------------------------------------
- // Create COAUTHIDENTITY that can be used for setting security on proxy
- COAUTHIDENTITY *userAcct = NULL;
- COAUTHIDENTITY authIdent;
- cout << "!useToken" << endl;
- memset(&authIdent, 0, sizeof(COAUTHIDENTITY));
- authIdent.PasswordLength = wcslen(pszPwd);
- authIdent.Password = (USHORT*)pszPwd;
- LPWSTR slash = wcschr(pszName, L'\\');
- if (slash == NULL)
- {
- cout << "Could not create Auth identity. No domain specified\n";
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return 1; // Program has failed.
- }
- StringCchCopy(pszUserName, CREDUI_MAX_USERNAME_LENGTH + 1, slash + 1);
- authIdent.User = (USHORT*)pszUserName;
- authIdent.UserLength = wcslen(pszUserName);
- StringCchCopyN(pszDomain, CREDUI_MAX_USERNAME_LENGTH + 1, pszName, slash - pszName);
- authIdent.Domain = (USHORT*)pszDomain;
- authIdent.DomainLength = slash - pszName;
- authIdent.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
- cout << "pszName " << pszName << endl;
- cout << "pszPwd " << pszPwd << endl;
- cout << "pszDomain " << pszDomain << endl;
- cout << "pszUserName " << pszUserName << endl;
- userAcct = &authIdent;
- // Step 6: --------------------------------------------------
- // Set security levels on a WMI connection ------------------
- hres = CoSetProxyBlanket(
- pSvc, // Indicates the proxy to set
- RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx
- RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx
- COLE_DEFAULT_PRINCIPAL, // Server principal name
- RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
- RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
- userAcct, // client identity
- EOAC_NONE // proxy capabilities
- );
- if (FAILED(hres))
- {
- cout << "Could not set proxy blanket. Error code = 0x"
- << hex << hres << endl;
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return 1; // Program has failed.
- }
- // Step 7: --------------------------------------------------
- // Use the IWbemServices pointer to make requests of WMI ----
- // For example, get the name of the operating system
- IEnumWbemClassObject* pEnumerator = NULL;
- hres = pSvc->ExecQuery(
- bstr_t("WQL"),
- bstr_t("Select * from Win32_OperatingSystem"),
- WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
- NULL,
- &pEnumerator);
- if (FAILED(hres))
- {
- cout << "Query for operating system name failed."
- << " Error code = 0x"
- << hex << hres << endl;
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return 1; // Program has failed.
- }
- // Step 8: -------------------------------------------------
- // Secure the enumerator proxy
- hres = CoSetProxyBlanket(
- pEnumerator, // Indicates the proxy to set
- RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx
- RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx
- COLE_DEFAULT_PRINCIPAL, // Server principal name
- RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
- RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
- userAcct, // client identity
- EOAC_NONE // proxy capabilities
- );
- if (FAILED(hres))
- {
- cout << "Could not set proxy blanket on enumerator. Error code = 0x"
- << hex << hres << endl;
- pEnumerator->Release();
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return 1; // Program has failed.
- }
- // When you have finished using the credentials,
- // erase them from memory.
- SecureZeroMemory(pszName, sizeof(pszName));
- SecureZeroMemory(pszPwd, sizeof(pszPwd));
- SecureZeroMemory(pszUserName, sizeof(pszUserName));
- SecureZeroMemory(pszDomain, sizeof(pszDomain));
- // Step 9: -------------------------------------------------
- // Get the data from the query in step 7 -------------------
- IWbemClassObject *pclsObj = NULL;
- ULONG uReturn = 0;
- while (pEnumerator)
- {
- HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
- &pclsObj, &uReturn);
- if (0 == uReturn)
- {
- break;
- }
- VARIANT vtProp;
- // Get the value of the Name property
- hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
- wcout << " OS Name : " << vtProp.bstrVal << endl;
- // Get the value of the FreePhysicalMemory property
- hr = pclsObj->Get(L"FreePhysicalMemory",
- 0, &vtProp, 0, 0);
- wcout << " Free physical memory (in kilobytes): "
- << vtProp.uintVal << endl;
- VariantClear(&vtProp);
- pclsObj->Release();
- pclsObj = NULL;
- }
- // Cleanup
- // ========
- pSvc->Release();
- pLoc->Release();
- pEnumerator->Release();
- if (pclsObj)
- {
- pclsObj->Release();
- }
- CoUninitialize();
- return 0; // Program successfully completed.
- }
Add Comment
Please, Sign In to add comment