Milton's profilePlace4U..Pace4U....PhotosBlogListsMore Tools Help

Place4U..Pace4U....

  • Send a private message
  • Tell a friend
  • Add to your network

CoSetProxyBlanket, COM client impersonation to IUnknown level granularity.

 

Unable to make a VS2005 COM API call from a web service because CoInitializeSecurity is already called by the .NET framework? Thats where CoSetProxyBlanket comes.

STDMETHODIMP CVSHelper::SetGuestParam(BSTR ServerName, BSTR VMName, BSTR ParamName, BSTR ParamValue, BSTR* ErrorDesc)
{
    HRESULT hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);

    REFCLSID classID = _uuidof(VMVirtualServer);

    IVMVirtualServer* pIVS = NULL;
    
    if (ServerName == NULL){
        hr = CoCreateInstance(classID, 
                          NULL, 
                          CLSCTX_ALL, 
                          IID_IVMVirtualServer, 
                          (LPVOID*)&pIVS);
        if (S_OK != hr)
            return hr;
    }
    else{

        CComBSTR    serverName(ServerName);
        MULTI_QI    multiQI = { &IID_IVMVirtualServer, NULL, NOERROR };
        COSERVERINFO  serverInfo = { 0, serverName, NULL, 0 };
        hr = CoCreateInstanceEx(classID, 
                            NULL, 
                            CLSCTX_ALL, 
                            &serverInfo, 
                            1, 
                            &multiQI);

        if (SUCCEEDED(hr))
            pIVS = (IVMVirtualServer*)(multiQI.pItf);
        else
            return hr;
    }

    hr = CUtils::SetProxyBlanket(pIVS);
    if FAILED( hr ) 
        return hr;

    IVMVirtualMachine* pIVM = NULL; 
    IVMGuestOS* pIVMGOS = NULL;

    _bstr_t bstrVMName(VMName);
    hr = pIVS->FindVirtualMachine(bstrVMName, &pIVM);
    if (FAILED(hr));
    else
    {
        
        hr = CUtils::SetProxyBlanket(pIVM);
        if FAILED( hr ) 
            return hr;

        hr = pIVM->get_GuestOS(&pIVMGOS);
        hr = CUtils::SetProxyBlanket(pIVMGOS);
        if FAILED( hr ) 
            return hr;
                
        _bstr_t bstrName(ParamName);
        _bstr_t bstrValue(ParamValue);
        pIVMGOS->SetParameter(bstrName, bstrValue);

    }

    if ( NULL != pIVS ){
        pIVS->Release();
        pIVS = NULL;
    }

    if ( NULL != pIVM ){
        pIVM->Release();
        pIVM = NULL;
    }

    if ( NULL != pIVMGOS ){
        pIVMGOS->Release();
        pIVMGOS = NULL;
    }

    return S_OK;
}
HRESULT CUtils::SetProxyBlanket(IUnknown* proxy)
{

   return ::CoSetProxyBlanket(proxy,
                                   RPC_C_AUTHN_DEFAULT,
                                   RPC_C_AUTHZ_DEFAULT,
                                   COLE_DEFAULT_PRINCIPAL,
                                   RPC_C_AUTHN_LEVEL_DEFAULT,
                                   RPC_C_IMP_LEVEL_IMPERSONATE,
                                   COLE_DEFAULT_AUTHINFO,
                                   EOAC_DEFAULT);
}

Milton KB (Milton K Baby)

Talking about the i'm campaign

Quote  

 

  

Check if an active directory domain user belongs to a given Group

 

public static bool CheckIfUserinGroup(string userName, string groupName)
{
    string path = "";
    if (username.IndexOf('\\') > 0)
        username = userName.Split('\\')[1];

    DirectoryEntry entry = new DirectoryEntry(null, null, null, AuthenticationTypes.Secure);
    try
    {
        DirectorySearcher search = new DirectorySearcher(entry);

        search.Filter = "(SAMAccountName=" + userName + ")";
        search.PropertiesToLoad.Add("cn");
        SearchResult result = search.FindOne();

        if (null == result)
            return false;
        path = result.Path;
    }
    catch (Exception)
    {
        return false;
    }

    DirectoryEntry entry1 = new DirectoryEntry(path);
    DirectorySearcher search1 = new DirectorySearcher(entry1);

    search1.PropertiesToLoad.Add("memberOf");

    try
    {
        SearchResultCollection src = search1.FindAll();
        foreach (SearchResult sr in src)
        {
            PropertyValueCollection pvc = sr.GetDirectoryEntry().Properties["memberOf"];
            object[] values = (object[])pvc.Value;
            foreach (object value in values)
            {
                if (value.ToString().Contains(groupName))
                    return true;
            }
        }

    }
    catch (Exception)
    {
    }
    return false;
}

Cheers Milton KB  (Milton K Baby)

LINQ

 
A simple SELECT query in LINQ to select a set of items from an object collection. This sample code fetches all available NT services in a remote machine and filters the collection for a specific service.
 
ServiceController [] scs = ServiceController.GetServices("machinename.ursubdomain.urdomain.com");

var crypto = (from service in scs where service.ServiceName == "CryptSvc" select service).ElementAt(0);

string name = crypto.ServiceName;

string status = crypto.Status.ToString();

 

MSDN sample collection: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

 
Cheers
Milton KB
 

Powershell FAQs

Q: Struggling with error ".ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about signing" for more details." while executing .ps1 powershell script file command prompt using powershell.exe?

A: Type C:\>Powershell.exe Set-ExecutionPolicy RemoteSigned  and press enter. Then execute your .ps1 file. - C:\>powershell.exe helloworld.ps1

 

Q: While executing helloworld.ps1 script returns immediately without waiting for me to see the output printed. Is there anything like getch() or cin ?

A: Yes, Powershell.exe –noexit &"c:\My scripts\helloworld.ps1"

 

Q: How to disable Network Card/NIC using powershell.

A:

$adapters=Get-wmiobject win32_NetworkAdapter | where {$_.AdapterType -like 'Ethernet*'}; 

foreach ($adpter in $adapters)

{

 $adpter.Disable();

}

$adapters=Get-wmiobject win32_NetworkAdapter | where {$_.PhysicalAdapter -eq 'true'};

foreach ($adpter in $adapters) { $adpter.Name };

 

Cheers

Milton KB

 
Hey Thanks for coming by !! Let me know you were here... See you again !!
Please wait...
Sorry, the comment you entered is too long. Please shorten it.
You didn't enter anything. Please try again.
Sorry, we can't add your comment right now. Please try again later.
To add a comment, you need permission from your parent. Ask for permission
Your parent has turned off comments.
Sorry, we can't delete your comment right now. Please try again later.
You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
Complete the security check below to finish leaving your comment.
The characters you type in the security check must match the characters in the picture or audio.

Milton

ATL Internals
ATL Internals : Working with ATL 8
Microsoft Windows Internals, Fourth Edition
Rootkits
Photo 1 of 4