Elevating COM objects from .Net

September 10, 2011 at 9:43 PMJoshua Harley

While I was working on one of my personal projects I needed to do some administrative tasks from a program launched as a normal user. Since I try to follow best practice to the best of my ability I knew I had to write an external module that could elevate to handle the administrative tasks required.

After doing quite a bit of research I came across two possible methods

  1. Create a new external program with <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> in the embedded manifest and pass all of the information needed through the command line.
  2. Create a COM object and elevate the object when using it. Information would be passed through COM object calls in real time and allows the caller to handle problems.

Obviously because I like a challenge and I'm a sadist, I decided on option number two, creating and elevating a COM object. Following the directions generously provided by Christoph Wille I was able to successfully create and register a .Net COM object that was able to be elevated. Unfortunately after elevating the object I was unable to invoke any methods and kept getting an odd exception (I think it was 0x80070005, Access Denied, I didn't keep notes for it – so it may be I'm mixing them up).

Honestly, I didn't get the .Net method 100% working, no matter how I ran the commands the methods with the [ComRegisterFunction] and [ComUnregisterFunction] never executed, so I had to finish registration by hand (without those extra registry entries, the COM object won't elevate). As part of my troubleshooting and because of the actions I needed to take when elevated I switched from a .Net component to an ATL component. This simplified development since I could incorporate the registry entries directly into the .rgu file.

Thinking I've solved all of the problems I wrote the ATL COM component, coded it to the best of my ability, set up the .Net calling code and tried it out. Guess what... 0x80070005 Access Denied. At this point I was going insane, everything I tried and everything I did was being denied when it was elevated. If I launched the object under the normal user I was able to interact with it. Elevate it? BOOM access denied. *sigh*

Continuing to research and try to find the problem, I eventually read the small nugget of information about Over-The-Shoulder elevation. Having been on this page many, many times trying to find the information I need, I felt quite stupid when I realized the information I needed was right there the whole time.

For such servers, COM computes a security descriptor that allows only SELF, SYSTEM, and Builtin\Administrators to makes COM calls into the server. This arrangement will not work in OTS scenarios. Instead, the server must call CoInitializeSecurity, either explicitly or implicitly, and specify an ACL that includes the INTERACTIVE group SID and SYSTEM.

Totally makes sense right? Well, to break it down simpler, the default security on the COM object is such that only SYSTEM and Administrators have access to the COM object when elevated, and even though you just gave it permission, your limited user process can't access it. Turns out to properly allow a limited process access to the elevated COM object you need to grant Local Activation to the INTERACTIVE SID.

After using the Component Services snap-in (dcomcnfg) and manually granting the right permissions and confirming that it worked I looked for a way to make the change programmatically, and what do you know, there's an example right there on that same MSDN article!

Below is the code I use to set up the proper security for the COM object (grants local activation to INTERACTIVE and SYSTEM, grants local and remote activation to the Built-in Administrators and SELF SIDS) and is executed through the DllRegisterServer function that ATL calls when registration is to occur. The registry entries required for elevation are handled by ATL when it processes the .rgu file.

STDAPI DllRegisterServer(void) {
  // (0x3 = Local Access, 0x7 = Local + Remote Access)
  // See http://msdn.microsoft.com/en-us/library/ms693364(VS.85).aspx
  static const wchar_t comSDDL[] =
      L"O:BAG:BAD:(A;;0x3;;;IU)(A;;0x3;;;SY)(A;;0x7;;;BA)(A;;0x7;;;PS)";
  bool perUser = false;
  ULONG securityDescriptorSize = 0;
  SECURITY_DESCRIPTOR* securityDescriptor = NULL;

  // Determine if the registration is per user.
  ATL::AtlGetPerUserRegistration(&perUser);

  // registers object, typelib and all interfaces in typelib
  HRESULT hr = _AtlModule.DllRegisterServer();

  // Only set up the elevation moniker if it is a system-wide install.
  // (Elevation doesn't work on per-user COM)
  if (SUCCEEDED(hr) && !perUser) {
    hr = E_FAIL;
    if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(comSDDL, SDDL_REVISION_1, (PSECURITY_DESCRIPTOR*)&securityDescriptor, &securityDescriptorSize))
      return E_FAIL;

    ATL::CRegKey rootAppId;
    ATL::CRegKey appId;
    if (ERROR_SUCCESS == rootAppId.Open(HKEY_CLASSES_ROOT, L"AppID", KEY_READ | KEY_WOW64_32KEY) &&
        ERROR_SUCCESS == appId.Open(rootAppId, _AtlModule.GetAppIdT(), KEY_WRITE | KEY_WOW64_32KEY) &&
        ERROR_SUCCESS == appId.SetBinaryValue(L"AccessPermission", securityDescriptor, securityDescriptorSize)) {

        hr = S_OK;
    }
    LocalFree(securityDescriptor);
  }
  return hr;
}

Posted in: Programming

Tags: , ,

Comments (4) -

Testing the IP address rewrite.

Testing commenting via cloudflare (without fixing rewrite).

Testing commenting via cloudflare and new router.

Testing it yet one more time.

Dollars to begin the net log, at the beginning of Google Adsense, but gradually the visitors growths, Maintain a lot more revenue to programs for your web site.

My web-site - best all inclusive island vacations ( Kristie - http://www.Itsarchitektura.cz/wiki/index.php/Basic_Strategies_For_Figuring_Out_How_To_Vacation_Similar_To_A_Pro )

Fixed a low cost for the midsummer journey in advance of your own personal reduction. It is simple overspend while you are during the middle summer season substantial therefore you not necessarily considering.

Look into my web blog -  http://europeansummer.co.uk - http://europeansummer.co.uk

Moze byc bolalo sytuacje w calej wyszukiwarkach. Swe rankingi jak równiez swoja wlasna slawe w celu uzytkowników zagadnienia dyspozycyjnosci. Jednostka ludzka zostanie zwiekszona, wówczas gdy uzyjesz glównie text.

My web page ...  tutaj pozycjonowanie - http://www.szkoleniaseo.pl

qweqweqwe  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  asdaasfasfasf
adasfafascxvzcxv  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  asdadad
gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  ascadvsdbsdbd
zxcz  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  asdads
adasfsdg  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  xcbxbxcb
cbcbnvbm  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  ryruurt
4uruyjfg  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  dghkgkjhl
678utfhg  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  234tdh
2354etyh  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  iuyt5
vdfgrht  gaming laptops under 500 - http://www.nusoundband.com/members/claudelip/activity/82936/  546ygh

Certainly, as a crucial web page and informative posts I'll give it back hyperlink - Bookmark this page? Regards

my website; best affiliate marketing ( Audrey - http://enspirer.info/demo/elgg-1.7.1/pg/groups/19361/expert-strategies-for-enhanced-affiliate-marketing-success/ )

Hi there I like your location

my webpage -  reseller hosting windows - http://kasayahangpinoy.com/WendellPj

Thanks for sharing this well put together website.

my page ::  what are the best lawn mowers - http://lawnmowersreviews2013.info

thank you for your assist!

Here is my web site: private krankenversicherung testsieger ::  Amie - http://belca.islu.ru/blog/index.php?postid=68002  ::

To start creating revenue along with your weblog, initially use Google Adsense but gradually as your site visitors increases, Hold adding an increasing number of income making applications to your web page.

Have a look at my website -  reisen günstiger buchen - http://www.redditors.net/members/josephine/activity/2807

Hello, what entice you to post an report. This article was super fascinating, particularly because I was seeking for ideas around the topic last Thursday.

Feel free to visit my blog ::  web marketing search engine optimization - http://www.nbs.go.tz/index.php?option=com_blog&view=comments&pid=37340&Itemid=0

I am only a couple of articles about members of the same material, but you appear up to know only a little a lot more about that than me.

my page;  marketing Affiliate - http://www.hyperionholdings.co.za/acquire-your-online-advertising-one-stage-further-using-type-assistance.html

My friend and I've argued, just about to sign a similar! Now I admit that I was right. lol! identified as the music of a bad back: D

my homepage ...  seo hizmetleri - http://myvacationgallery.com/groups/your-go-to-steer-for-search-engine-optimization/

Wow, you got an remarkable blog here.

my web page ::  private krankenkasse tarife - http://www.kendoitaly.org/index.php?do=/blog/20730/consider-these-best-health-care-insurance-tips-these-days/

Excellent report, I just deal it with my buddy of Taiwan. I Stumble UP your blog location, you'll determine an increment of site visitors inside 24 hours for targeted people. Cheers

Check out my site; krankenkasse freiberufler ( Carissa - http://speakerscircuit.com/members/bettybeav/activity/530447 )

Hello. I'm a couple of the other posts and wanted to thank you

my site ... affiliate marketing online [ Hayden - http://victimsofslaveryandtorture.com/professional-affiliate-marketing-techniques-which-can-be-confident-succeed ]

fascinating material thanx:)

Also visit my web page ::  versicherungspflicht krankenversicherung - http://seaporttattoos.com/war_junkies/groups/medical-health-insurance-secrets-and-techniques-the-advantages-dont-would-love-you-to-learn/

Thanks! pretty beneficial post!

Review my homepage - travel agents ( Ashli - http://www.Familylinkmobile.com/index.php?option=com_blog&view=comments&pid=150703&Itemid=0 )

Howdy what entice you to spot an post. This article was very occupying, particularly since I was searching for thoughts on this subject final Thursday.

my blog ::  gesetzliche versicherung - http://mathieu.ro/edu/article.php?id=12423

Loaded by way of loan provider or if your credit-based card enterprise. Most likely, your salaryday loan's RATE OF INTEREST will be higher. Consult the particular pay day advance loan's monthly interest will be, before you make a decision to be able to access any money.

Here is my homepage;  paydayyloanssuk.blog.co.uk - http://paydayyloanssuk.blog.co.uk

Add comment

biuquote
Loading