[RESOLVED] Get path of a user's desktop if running as admin
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] Get path of a user's desktop if running as admin

  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    [RESOLVED] Get path of a user's desktop if running as admin

    Say, my process requires elevation and it was run from a Standard User account (right-clicked and then Run As Administrator). How do I get a path to the desktop of that Standard user from such process?

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,286

    Re: Get path of a user's desktop if running as admin

    What have you tried?

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Get path of a user's desktop if running as admin

    Quote Originally Posted by Arjay View Post
    What have you tried?
    Everything possible in the book (that I know of). All of my attempts simply gave me an admin account info. Unfortunately there's no way, that I know of, to lower your own privileges (I even tried spawning a separate process, that unfortunately also ran elevated).

    I thought maybe there's something in a logon token that can point to the actual user where the process was started from? (I don't know this subject that well.)

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,286

    Re: Get path of a user's desktop if running as admin

    If you know the name of the original account, you can derive it from the admin account.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: Get path of a user's desktop if running as admin

    Very intresting issue. For some reason my gut feeling say that it's impossible to get that information though. I get the feeling that the original user is (kind of) not part of the process to start the application when you do run as.

    I hope I'm wrong and this thread is definitely added to my subscriptions.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Get path of a user's desktop if running as admin

    Quote Originally Posted by Arjay View Post
    If you know the name of the original account, you can derive it from the admin account.
    Well, yeah... but how do I get it? (I guess I'll keep digging.)

  7. #7
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,286

    Re: Get path of a user's desktop if running as admin

    Quote Originally Posted by ahmd View Post
    Well, yeah... but how do I get it? (I guess I'll keep digging.)
    SHGetSpecialFolderPath will get you the current account path. Replace the current user name with the original and you've got it.

  8. #8
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Get path of a user's desktop if running as admin

    You could use "runas /env" to run as Administrator but keep the environment of the current user. I mean I've read in a magazine that in Windows7 you also could choose that option when right-clicking on an executable in the Explorer.

    In any case that should give you access to all environment variables of the current user (maybe even access to the user registry) what should give you the user account somehow. Type 'set' in a command window to see all user environment variables.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,971

    Re: Get path of a user's desktop if running as admin

    Well, yeah... but how do I get it? (I guess I'll keep digging.)
    See the sample. You should dig into registry in case you discover process owner sid differs from active user sid. Or in case you've made sure owner sid is built-in admin sid (well known sid).
    Attached Files Attached Files
    Last edited by Igor Vartanov; August 25th, 2010 at 11:28 AM.
    Best regards,
    Igor

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,971

    Re: Get path of a user's desktop if running as admin

    Funny thing, Vista really differs from XP in this Run as Administrator thing:

    XP
    Code:
    E:\Temp\564>564.exe
    [Proc.Owner][SID] = S-1-5-21-1659004503-1326574676-839522115-500
    [Proc.Owner][ACT] = IVARTANOV\Administrator
    [Proc.User][SID] = S-1-5-21-1659004503-1326574676-839522115-500
    [Proc.User][ACT] = IVARTANOV\Administrator
    [Session][SID] = S-1-5-21-1659004503-1326574676-839522115-1003
    [Session][DSK] = C:\Documents and Settings\Igor\Desktop
    [Session][ACT] = IVARTANOV\Igor
    Vista
    Code:
    D:\Temp\564>564.exe
    [Proc.Owner][SID] = S-1-5-32-544
    [Proc.Owner][ACT] = BUILTIN\Administrators
    [Proc.User][SID] = S-1-5-21-3531474516-4049177484-105061100-1000
    [Proc.User][ACT] = IVARTANOV\Igor
    [Session][SID] = S-1-5-21-3531474516-4049177484-105061100-1000
    [Session][DSK] = C:\Users\Igor\Desktop
    [Session][ACT] = IVARTANOV\Igor
    Best regards,
    Igor

  11. #11
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Get path of a user's desktop if running as admin

    Quote Originally Posted by Igor Vartanov View Post
    See the sample. You should dig into registry in case you discover process owner sid differs from active user sid. Or in case you've made sure owner sid is built-in admin sid (well known sid).
    Nice, Igor! I take my hat off for you....

    I didn't think about digging into the registry but it works like a charm. (Tested on both XP and Windows 7. Assuming Vista to work as well.) The [Session][DSK] in his example above gives the answer to my question.

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,971

    Re: Get path of a user's desktop if running as admin

    Registry is very powerful thing if you really know what to look for.
    I even tried spawning a separate process, that unfortunately also ran elevated
    Yes, that was another approach you could use. I can recall we already discussed the sample of running exe as interactive user (though it was related to services). Nothing changed since then: you find the active session user, find the process he owns, borrow the process token and run your own process with the token.

    Looking into registry is a real shortcut for the described procedure, I believe. With one 'but'... The shell folder registry things remain unchanged since Windows NT, but you know, there's no any guarantee MS won't change it in future.

    Besides, I'd like to warn about another point which might be not very clear: the approach I sampled works only with interactive user. It's just because the user's hive is already loaded in by session start routine. Therefore, it's not possible to look for similar things with any user you wish until his profile gets loaded in somehow.
    Best regards,
    Igor

  13. #13
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Get path of a user's desktop if running as admin

    Quote Originally Posted by Igor Vartanov View Post
    The shell folder registry things remain unchanged since Windows NT, but you know, there's no any guarantee MS won't change it in future.
    Hmm, so you're saying it's undocumented, hah? Is it just happened to be that MS was calling those registry keys for the Shell with a text representation of user SIDs?

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,971

    Re: Get path of a user's desktop if running as admin

    I'm saying that Explorer\Shell Folders key remains unchanged since Windows NT. At least for 14 years, as I started with Windows NT4.0. And sorry, my English seems not good enough to comprehend your last question.
    Best regards,
    Igor

  15. #15
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Get path of a user's desktop if running as admin

    Quote Originally Posted by Igor Vartanov View Post
    my English seems not good enough to comprehend your last question.
    I'm sorry.

    You know when you call ConvertSidToStringSid to convert a SID from SID structure to a human readable string... So do they [at Microsoft] name registry keys with those text versions of user token SIDs? Wouldn't that be a security breach since any user account can read and interpret those?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center