<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx</link><description>Sample code to demonstrate one way to start a program as the [non-elevated] desktop user from an elevated app.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9703028</link><pubDate>Sat, 06 Jun 2009 22:28:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9703028</guid><dc:creator>Jeremy Bettis </dc:creator><description>&lt;P&gt;What about ShellExecute? &amp;nbsp;Won't that run as the shell's user?&lt;/P&gt;
&lt;DIV class=ajmReply&gt;
&lt;P&gt;&lt;EM&gt;[Aaron Margosis]&amp;nbsp; Not normally, no.&amp;nbsp; The immediate process that gets launched will run with your elevated token.&amp;nbsp; If it is a single instance program (like Explorer) that is already running, then you'll usually get something running in that initial process, but other than that, no.&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9703164</link><pubDate>Sun, 07 Jun 2009 02:29:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9703164</guid><dc:creator>AndyC</dc:creator><description>&lt;P&gt;And that's *still* buggy. Off the top of my head:&lt;/P&gt;
&lt;P&gt;1) Doesn't work if the app is launched directly through Terminal Services, because then the shell isn't running.&lt;/P&gt;
&lt;P&gt;2) Doesn't work in complex chains, e.g. User A launches a command prompt via runas as User B, then that launches your app and does an OTS elevation to User C. Your code then runs the unelevated code as User A, not User B as it should.&lt;/P&gt;
&lt;P&gt;The only 100% safe way of doing this is to use a bootstrap model. You start by launching an unelevated "bootstrap" app, that starts your elevated process which does the admin work and once it has completed, the bootstrapper launches whatever process you want, which is guaranteed to be in the correct user context.&lt;/P&gt;
&lt;DIV class=ajmReply&gt;
&lt;P&gt;&lt;EM&gt;[Aaron Margosis]&amp;nbsp; That depends on your definition of "buggy".&amp;nbsp; I was careful to say "run the app as the non-elevated desktop user" -- if there is no non-elevated desktop, then there's no correct answer.&amp;nbsp; The bootstrap model won't work in that case either.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Re #1 and terminal services:&amp;nbsp; I can't repro this -- when I try to configure the "start a program" option in mstsc, it has no effect (going from a Vista SP1 to another one).&amp;nbsp; I tried a bunch of different ways, but no luck.&amp;nbsp; The shell just comes up each time.&amp;nbsp; Can you specify an app that requires elevation?&amp;nbsp; What happens when you do?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Re #2 and "not User B &lt;STRONG&gt;as it should&lt;/STRONG&gt;." (emphasis mine.)&amp;nbsp; First of all, this seems an odd edge case -- under what non-contrived scenarios are you going to have a chain of three independent security contexts?&amp;nbsp; And why do you assert that the non-elevated app should run as User B and not as User A?&amp;nbsp; How do you know that the&amp;nbsp;User B context is not elevated too?&amp;nbsp; Is that an elevated command prompt?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;The bootstrap model is another model that can be appropriate in some scenarios.&amp;nbsp; However, it is not "100% safe".&amp;nbsp; That model builds in an assumption that the bootstrap app was started in a "safe" non-elevated context.&amp;nbsp; What if the user started the bootstrap app with elevation or &lt;/EM&gt;&lt;EM&gt;from an elevated CMD?&amp;nbsp; Then everything runs elevated.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Second, the bootstrap model is more complex.&amp;nbsp; If the bootstrap app needs to do more than simply wait for the elevated process to exit, then you need to build interprocess communication between the two processes so that the elevated app can tell the bootstrap app "now do XYZ."&amp;nbsp; It's not impossible, of course, but it's much more complex than my sample.&amp;nbsp; (And of course, complexity increases risk of bugs, and those can easily be security issues in a scenario like this.)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Third, sometimes the bootstrap model is simply not appropriate.&amp;nbsp; Say you've built an app that legitimately requires admin rights (my favorite example is &lt;A href="http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx" target=_blank&gt;Process Monitor&lt;/A&gt;).&amp;nbsp;&amp;nbsp;The app&amp;nbsp;has a feature or two that is rarely used but needs to launch a browser -- like Procmon's "search online" feature.&amp;nbsp; In order to use the bootstrap model, Procmon would&lt;/EM&gt; &lt;STRONG&gt;always&lt;/STRONG&gt; &lt;EM&gt;need to be started non-elevated, and would need to keep that non-elevated bootstrap app around as long as Procmon were running,&lt;/EM&gt; just in case &lt;EM&gt;the "search online" feature got invoked.&amp;nbsp; That makes no sense to me at all.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;[Added moments later]&lt;/STRONG&gt;&amp;nbsp; There actually&lt;/EM&gt; &lt;STRONG&gt;is&lt;/STRONG&gt; &lt;EM&gt;a bug in here -- the code that enables SeIncreaseQuotaPrivilege should not do so on the process token -- it should do so in the context of the thread and then revert:&amp;nbsp; ImpersonateSelf, OpenThreadToken, manipulate token, perform operations, RevertToSelf.&amp;nbsp; Fixing that is left as an exercise for the reader :)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;[One more late comment]&lt;/STRONG&gt;&amp;nbsp; In your complex chain scenario, if the "non-elevated" app you want to start is IE7, it will refuse to run as User B.&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9707950</link><pubDate>Mon, 08 Jun 2009 14:53:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9707950</guid><dc:creator>Bob</dc:creator><description>&lt;p&gt;Why MS doesn't provide a nw API that returns the token that was used when launching the program that causes the evelation prompt?&lt;/p&gt;
</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9712557</link><pubDate>Tue, 09 Jun 2009 06:34:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9712557</guid><dc:creator>AndyC</dc:creator><description>&lt;P&gt;#1 requires a real Terminal Server, I believe. I don't think it's supported on client versions. Though it's possible similar behaviours exist when using the Desktop Sharing APIs, so tools like Meeting Space or Remote Assistance may break too.&lt;/P&gt;
&lt;P&gt;#2 I've mostly seen that sort of complex setup when dealing with domains where security was a big issue that were XP oriented, so that multiple user accounts were used to avoid running with a privileged token at all times. Obviously, to some extent, UAC helps to make that sort of thing unnecessary.&lt;/P&gt;
&lt;P&gt;100% safe was perhaps badly worded, it's the only way to ensure what is the usual intent in such scenarios, i.e. to launch in the context of whomever started the application. Had it been an elevated context in the first place, it wouldn't have gone through UAC to change identity again, so that's not really an issue. Ultimately if the end-user has gone out of there way to launch your app such that everything it does is elevated, you ought to let them do so, they might know something you don't.&lt;/P&gt;
&lt;P&gt;As to apps like Process Explorer, they're mostly an edge case too, this sort of question mostly comes up around application installers and for them the bootstrap model is always the best way to go. &lt;/P&gt;
&lt;DIV class=ajmReply&gt;
&lt;P&gt;&lt;EM&gt;[Aaron Margosis]&amp;nbsp; It sounds to me that you're extrapolating your own experiences to a much broader universe that you can authoritatively speak on behalf of.&amp;nbsp; Your "usual intent" may be minority.&amp;nbsp; I'd disagree that users always understand all the implications of their actions, or that any model is "always" the best one for all scenarios.&amp;nbsp; I'll reiterate the browser example, which I doubt is uncommon -- many app installers display a readme using the default web browser, and other elevated apps may also display HTML that way.&amp;nbsp; Since the bootstrapper cannot guarantee that it will always be launched as the desktop user -- not only as a non-admin, but as the desktop user -- that scenario breaks pretty badly.&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;That said, I don't really think the bootstrap model is that much more complicated, especially if you use COM elevation rather than spawning an entirely different executable to create an elevated context. And "it's more effort to do right" is a poor excuse when it comes to security, don't you think?&lt;/P&gt;
&lt;DIV class=ajmReply&gt;
&lt;P&gt;&lt;EM&gt;[Aaron Margosis]&amp;nbsp; Doing COM elevation correctly without exposing trivially exploitable EoP is not simple.&amp;nbsp; Those communication paths have to be carefully threat-modeled and implemented.&amp;nbsp; And "It's more effort to do right" does not reflect what I said at all.&amp;nbsp; If it's the best approach, the extra effort is often worth it.&amp;nbsp; Shoehorning that model where it doesn't belong only adds effort and increases risk without providing value.&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9834564</link><pubDate>Wed, 15 Jul 2009 22:05:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9834564</guid><dc:creator>Roy Folkker</dc:creator><description>&lt;P&gt;Having worked on installers (and bootstraps) extensively, I have to say that using the bootstrap as your non-elevated point for a split level execution is not only a bad idea, but very impractical. &amp;nbsp;In the case of my current work, my bootstrapper actually performs the execution of the MSI package through API calls. &amp;nbsp;As well, it can perform patches, and launch executables through a series of prerequisites. &amp;nbsp;Assuming I choose to have it run as invoked, or as user, that means that every prerequisite will require UAC confirmation (retrieving the token from the process will not work since MSP and MSI function calls do not allow me to elevate them), and once an MSI or MSP package hits a required elevation action, my executable will be elevated, since it is not a parent process, it is the current process, rendering all other considerations moot.&lt;/P&gt;
&lt;P&gt;With all of that aside, there is the ever popular issue of Microsoft key words for UAC. &amp;nbsp;If you name an executable setup, update, or some variations there-in, congratulations, you are elevated. &amp;nbsp;Again, rendering the bootstrap as a non-elevated launch point as moot.&lt;/P&gt;
&lt;DIV class=ajmReply&gt;
&lt;P&gt;&lt;EM&gt;[Aaron Margosis]&amp;nbsp; Installer detection is only for "legacy" apps.&amp;nbsp; If the app has a Vista-aware manifest (i.e., specifies requestedElevationLevel), it overrides installer detection, so you can have an app called setup.exe that doesn't prompt for elevation.&amp;nbsp; By default, Visual Studio 2008 adds a Vista-aware manifest to apps you build.&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;So, either way, from an installer stand-point, *most* if not all situations will have a default shell. &amp;nbsp;If you are installing software via TS to a terminal server, and succeeding you deserve an award (or to be taken out back for doing something that scary). &amp;nbsp;So, the concept of creating your non-admin tool processes as user through an Admin process seem far more beneficial (especially considering in most situations you *want* to be as admin in an install process, with very rare non-admin addendums (e.g. launching an external read-me, launching a Support page, and such).&lt;/P&gt;
&lt;P&gt;Anyways, thank you for the information here. &amp;nbsp;Again, Mr. Margosis you have provided information that has been unbelievably useful and just incomplete enough to force me to make sure I know what I am doing:P.&lt;/P&gt;
&lt;DIV class=ajmReply&gt;
&lt;P&gt;&lt;EM&gt;[Aaron Margosis]&amp;nbsp; Incomplete?&amp;nbsp; Even with the code sample?&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9834732</link><pubDate>Thu, 16 Jul 2009 01:40:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9834732</guid><dc:creator>Roy Folkker</dc:creator><description>&lt;p&gt;Still on 2005, so I haven't seen the difference in manifests yet.&lt;/p&gt;
&lt;p&gt;As for your other statement, sorry, hadn't started working with it, just took your previous statement at face value:).&lt;/p&gt;
&lt;p&gt;-&lt;/p&gt;
&lt;p&gt;[Added moments later] &amp;nbsp;There actually is a bug in here -- the code that enables SeIncreaseQuotaPrivilege should not do so on the process token -- it should do so in the context of the thread and then revert: &amp;nbsp;ImpersonateSelf, OpenThreadToken, manipulate token, perform operations, RevertToSelf. &amp;nbsp;Fixing that is left as an exercise for the reader :)&lt;/p&gt;</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9837113</link><pubDate>Fri, 17 Jul 2009 13:05:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9837113</guid><dc:creator>ilmcuts</dc:creator><description>&lt;p&gt;Alternatively you can use the IShellDispatch2 interface:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://brandonlive.com/2008/04/27/getting-the-shell-to-run-an-application-for-you-part-2-how/"&gt;http://brandonlive.com/2008/04/27/getting-the-shell-to-run-an-application-for-you-part-2-how/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It still needs the Explorer shell to be running, but if you're willing to live with that then this approach seems much easier. And you don't even need to special case Vista/Win7 as the code should work on XP as well.&lt;/p&gt;</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9887056</link><pubDate>Thu, 27 Aug 2009 19:00:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9887056</guid><dc:creator>Boon Hong</dc:creator><description>&lt;p&gt;In my case, I am creating a flash application as the interface to execute some programs and installers from the disc. &lt;/p&gt;
&lt;p&gt;Since I need to execute the installers in elevated mode, I have to amend the default manifest in the flash application to launch in elevated mode too. But by doing this, I will cause all programs launched from flash to be in elevated mode. This is a potential security leakage.&lt;/p&gt;
&lt;p&gt;It will be good if Microsoft can provide a command line utility to downgrade the elevated mode that can be executed from batch files.&lt;/p&gt;
&lt;p&gt;It will even be better if Microsoft is willing to fix this security loophole by having all child process to be launched in non-elevated mode by default just like all applications are launched in non-elevated mode by default.&lt;/p&gt;</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9920771</link><pubDate>Wed, 11 Nov 2009 13:10:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9920771</guid><dc:creator>Bill Zitomer</dc:creator><description>&lt;P&gt;This is great material, thanks for posting. The problem I have, this method works for me for running an exe with a manifest that does not require elevation. But it appears that if the manifest is set to require elevation, then CreateProcessWithTokenW returns 740 (elevation required) even though I'm trying to run it non-elevated.&lt;/P&gt;
&lt;P&gt;I've tried a number of variations. I was previously using CreateProcessAsUser to lower the integrity, which works fine, except the process still runs as the admin. &lt;/P&gt;
&lt;P&gt;I'm about to try the IShellDesktop2 interface to see if it might work any better. &lt;/P&gt;
&lt;DIV class=ajmReply&gt;
&lt;P&gt;&lt;EM&gt;[Aaron Margosis]&amp;nbsp; The only way I know of to override the "requireAdministrator" manifest is to&amp;nbsp;apply the RunAsInvoker application compatibility shim.&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9921145</link><pubDate>Thu, 12 Nov 2009 03:19:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9921145</guid><dc:creator>Bill Zitomer</dc:creator><description>&lt;p&gt;Yeah that's not going to work in this case because I need to distribute. What I have is a custom install process which needs to read a cookie left by our web site. The cookie resides in the low cache, which the installer cannot access. What I did is re-run the installer as a low integrity process and use InternetGetCookie, which works fine if the user is an admin, but for standard user it fails because it's still running as admin, whereas the cookie left by IE was running as the user. I ended up making a new exe with runAsInvoker in the manifest and I think this will work. I will need to embed it, extract at run time, etc. All of this to do what used to be one line of code!&lt;/p&gt;
</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9921178</link><pubDate>Thu, 12 Nov 2009 04:40:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9921178</guid><dc:creator>Bill Zitomer</dc:creator><description>&lt;p&gt;ha! I can do this much more simply with impersonation. &lt;/p&gt;
</description></item><item><title>re: FAQ: How do I start a program as the desktop user from an elevated app?</title><link>http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx#9921989</link><pubDate>Fri, 13 Nov 2009 15:21:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9921989</guid><dc:creator>Suresh</dc:creator><description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a setup application which will be launched from a device driver co-installer dll. My requirement is the setup application has to launch an URL in protected mode. I followed the article given in&lt;/P&gt;
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/bb250462%28VS.85%29.aspx" rel=nofollow target=_new&gt;http://msdn.microsoft.com/en-us/library/bb250462%28VS.85%29.aspx&lt;/A&gt;. But it doesn't help much.&lt;/P&gt;
&lt;P&gt;I also downloaded the sample application given in this article and tried. But IE is still launching with protected mode off.&lt;/P&gt;
&lt;P&gt;As driver installation happens using admin privileges the setup application is launching with admin privileges and IE also launching with admin privileges hence protected mode is off. Is there a way how we can launch IE with Protected mode ON in this situation?&lt;/P&gt;
&lt;P&gt;Help!.&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; Regards&lt;/P&gt;
&lt;P&gt;Suresh&lt;/P&gt;
&lt;DIV class=ajmReply&gt;
&lt;P&gt;&lt;EM&gt;[Aaron Margosis]&amp;nbsp; Why does it matter whether Protected Mode is on or not?&amp;nbsp; Note that PM will be off for the Trusted Sites and Local Computer zones, as well as for the Intranet zone (for IE8).&lt;/EM&gt;&lt;/P&gt;&lt;/DIV&gt;</description></item></channel></rss>