I wrote an application using Qt under Windows 7. The application starts up with normal user privileges, but I want to gain the Administrator privileges because I want to modify the registry to auto-start the application.
How could I do this?
Take a look at the MSDN sample: UAC self-elevation (CSUACSelfElevation)
Also, Wikipedia actually has a pretty good reference including information on the ShellExecuteEx() "runas" verb and application manifest for elevation requests.
You should embedd correct manifest to your exe:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb756929.aspx.
I know, you said using win32 API, but consider this standard and recommended way.
You do not need administrator privileges to autostart your application.
Simply write the appropriate keys to:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
which does not require administrator privileges (unlike the same path under HKEY_LOCAL_MACHINE).
to auto-startup the application, you don't need admin rights! Instead of adding the registry key under HKLM (where you need admin rights), use HKCU and you're fine.
Note that privileges cannot be changed while the program is running. It has to be killed and restarted with the correct privileges.
To make an application auto start in HKLM, the administrative privilege is needed. But the privilege of an application could not change while running.
So I made a new small application whose task is only write the registry to make the main application auto start.
The main application could call this application with ShellExecuteEx to require administrative privlege.
Related
Well I noticed that on Windows 7, sometimes even when you are an administrator, you can't do a lot of things, probably it's some sort of bug, my application I check if an user is administrator before start the program because my program creates file in folders that are protected default like the root folder ( C: ), and if you aren't an administrator on Windows 7, you can only create folders there.
So if I right click in my application and go "Run as Administrator", it just works fine.
Is there a way to make my application run as administrator automatically? I would like to be able to make a line of code like: ActivateAdministrator(); and be available for the code completely, because I change attributes, create files with ifstream.
You could add a manifest to your executable - http://msdn.microsoft.com/en-us/library/bb756929.aspx
If the user is running on a system with the UAC switched on, and are not an administrator, a manifest which contains requestedExecutionLevel
level="requireAdministrator" will produce a prompt for the Administrator password before your application can run with administrative privileges. (requiring administrator privileges means that an incorrect password or no password will stop it from running altogether)
If they are an administrator with the UAC switched on, then that same manifest will cause a Yes/No prompt to ask whether your application should be granted administrative privileges.
Of course, the real issue is that whatever your application is doing which requires administrative privileges needs to be examined.
Most of the time the privilege is simply not required for normal user-level applications. This is an application design issue really - what is your application doing which requires admin privileges? is it really necessary? e.g. If you're modifying files, then why are those files in a protected area on the file system instead of in the user's profile space?
You might find the Windows Dev Center article on Priviliges helpful, specifically Enabling and Disabling Privileges in C++ .
Although this is in C#, it might be easier for you I don't know. What I did was to Detect if running as Administrator with or without elevated privileges?, and if not rerun the current process while requesting administrative access (which if the UAC is enabled, would do a popup to the current user and ask if it is ok for the program to run with administrative privleges).
Then some simple (but C# code) looks like:
// UAC is a class from the previous link on SO
if (UAC.IsCurrentProcessElevated())
{
string currentProcess = Assembly.GetEntryAssembly().Location;
string arguments = string.Join(" ", this._Args.ToArray());
ProcessStartInfo startInfo = new ProcessStartInfo(currentProcess, arguments);
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
Process.Start(startInfo);
}
The un-elevated process would quite, with a new one started that requested administrative privileges.
I need the ability in C++ code to logoff the console user when call from an administrator process or if it is called by that user and maybe a windows service in the future. The issue I am running into is that ExitWindowsEx will only logoff the user that calls it. If an administrator process calls a logoff the console user is unaffected. The only application that I know of that can do this is psShutdown.exe, except that psShutdown can only be run by the administrator. Does anyone know in C++ how to identify the console user and then do a logoff against that user? What is the magic inside psShutdown? I'm looking for something that works in WindowsXP and up. Also I cannot use WMI because some machines are running windows Embeded and do not have WMI.
(The reason psShutdown.exe must be run as administrator is because it installs a service and a normal user does not have the rights to do this.)
Use WTSGetActiveConsoleSessionId to identify the console session, but to go further you are going to need some permissions, you would have to call WTSQueryUserToken (You need to run as SYSTEM to do this) to get a token handle and then CreateProcessAsUser or impersonate and call ExitWindowsEx, or if you are not in a service, call WTSLogoffSession. (I tried calling WTSLogoffSession on my XP box and it did not work, probably because the terminal server service is not running on this system)
I have never actually done this, but it seems like it should be possible with the combination of WTSGetActiveConsoleSessionId() and WTSLogoffSession()
I'm doing some crazy impersonation stuff to get around UAC dialogs in Windows 7 so the user does not have to interact with the UI (I have the admin creds of course).
I have a process running as the Administrator and elevated past UAC. The issue that I'm facing is that when I make a call to WNetAddConnection2, within this process, I am not getting a new mapped net drive. The function returns ERROR_SUCCESS but no net drive is visible. We have another method of adding network drives using 'subst' but this, again, returns successful does does not add a net drive. I have tried to use the default user (which is the Administrator because of process's security context) and I have tried using specific user credentials. I can map the drive just fine through Explorer.
Of course the same functionality works fine in XP/2003. I haven't got around to testing on Vista because of issues with impersonation that are limiting my ability to spin up the process. Are there unique Windows 7 limits on this function? MSDN does not glean any that I can find.
Any help would be greatly appreciated!
The issue was that the process was running as Administrator. Impersonation will not work because WNetAddConnection2 evaluates on processes user. You must start a separate process to accomplish this.
Okay, I have a plugin for IE that when installed needs to (with the user's permission) restart IE.
To do this I have a DLL that is invoked by the installer. And it works, but the problem is that when IE is restarted on Vista, it is restarted with the administrator privileges of the installer, which is a problem for a number of reasons.
I'm using CoCreateInstance to start IE, so that I get an instance of the IWebBrowser2 interface in order to perform some actions on it.
So my question is, is it possible to call CoCreateInstance from an application that is running with Administrator privileges, in such a way that the resulting COM object instance inherits the base user privileges rather than the administrator privileges of the calling application?
Okay, I found the solution from here:
http://social.msdn.microsoft.com/Forums/cs-CZ/ieextensiondevelopment/thread/78a2bc18-1920-4e58-af7e-48dbcebe7643
From my installer DLL I need to launch a new thread, and impersonate the current user on that thread, and then set a low integrity level, and create the COM instance with the CLSCTX_ENABLE_CLOAKING context.
I have written a Windows service that allows me to remotely run and stop applications. These applications are run using CreateProcess, and this works for me because most of them only perform backend processing. Recently, I need to run applications that present GUI to the current log in user. How do I code in C++ to allow my service to locate the currently active desktop and run the GUI on it?
Roger Lipscombe's answer, to use WTSEnumerateSessions to find the right desktop, then CreateProcessAsUser to start the application on that desktop (you pass it the handle of the desktop as part of the STARTUPINFO structure) is correct.
However, I would strongly recommend against doing this. In some environments, such as Terminal Server hosts with many active users, determining which desktop is the 'active' one isn't easy, and may not even be possible.
But most importantly, if an application will suddenly appear on a user's desktop, this may very well occur at a bad time (either because the user simply isn't expecting it, or because you're trying to launch the app when the session isn't quite initialized yet, in the process of shutting down, or whatever).
A more conventional approach would be to put a shortcut to a small client app for your service in the global startup group. This app will then launch along with every user session, and can be used start other apps (if so desired) without any juggling of user credentials, sessions and/or desktops.
Also, this shortcut can be moved/disabled by administrators as desired, which will make deployment of your application much easier, since it doesn't deviate from the standards used by other Windows apps...
The short answer is "You don't", as opening a GUI program running under another user context is a security vulnerability commonly known as a Shatter Attack.
Take a look at this MSDN article: Interactive Services. It gives some options for a service to interact with a user.
In short you have these options:
Display a dialog box in the user's session using the WTSSendMessage function.
Create a separate hidden GUI application and use the CreateProcessAsUser function to run the application within the context of the interactive user. Design the GUI application to communicate with the service through some method of interprocess communication (IPC), for example, named pipes. The service communicates with the GUI application to tell it when to display the GUI. The application communicates the results of the user interaction back to the service so that the service can take the appropriate action. Note that IPC can expose your service interfaces over the network unless you use an appropriate access control list (ACL).
If this service runs on a multiuser system, add the application to the following key so that it is run in each session: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. If the application uses named pipes for IPC, the server can distinguish between multiple user processes by giving each pipe a unique name based on the session ID.
WTSEnumerateSessions and CreateProcessAsUser.
Several people suggested WTSEnumerateSessions and CreateProcessAsUser. I wonder why no one suggested WTSGetActiveConsoleSessionId, since you said you only want to target one logged in user.
Several people sure are right to suggest CreateProcessAsUser though. If you call plain old CreateProcess the way you said, then the application's GUI will run with your service's privileges instead of the user's privileges.
That problems Session 0 , Interactive Services ,
Windows Service Allow Service To Interact With Desktop
on Windows 7 or Windows Vista
You can read this article
http://www.codeproject.com/KB/vista-security/SubvertingVistaUAC.aspx
I try explained here it's working on Windows 7
On Win2K, XP and Win2K3 the console user is logged on in Session 0, the same session the services live in. If a service is configured as interactive, it'll be able to show the UI on the user's desktop.
However, on Vista, no user can be logged on in Session 0. Showing UI from a service there is a bit trickier. You need to enumerate the active sessions using WTSEnumerateSessions API, find the console session and create the process as that user. Of course, you need also a token or user credentials to be able to do that. You can read more details about this process here.
I think as long as you have only one user logged in, it will automatically display on that user's desktop.
Anyway, be very careful when having a service start an exe.
If the write access to the folder with the exe is not restricted, any user can replace that exe with any other program, which will then be run with sytem rights. Take for example cmd.exe (available on all windows sytems). The next time the service tries to start your exe, you get a command shell with system rights...
If you launch a GUI from your service it will show up on the currently active desktop.
But only if you adjusted the service permissions: You need to allow it to interact with the desktop.
Important Services cannot directly interact with a user as of Windows Vista. Therefore, the techniques mentioned in the section titled Using an Interactive Service should not be used in new code.
This is taken from : http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx