| MSDN Home > MSDN Library > .NET Development > Visual Studio .NET > Articles and Columns > Security Articles |
Lars Bergstrom July 2002 Summary: Members of the local Administrators group have privileges to perform any action on a machine. This causes both a risk to the user performing actions in that environment and to the software that they develop. This article explains how to productively develop software while logged on with non-administrative privileges. (14 printed pages) Applies to:
ContentsIntroduction IntroductionSecurity is important. No one argues with this and everyone spends a lot of time thinking about security issues, security bugs, and malicious users. However, very few people are willing make the effort to eradicate the largest single reason that e-mail viruses and cracks in general are so dangerous: everyone logs in as a user who is a member of the local Administrators group, and most services run as Administrators. The principle of "least privilege" states that running with the minimal set of rights needed to perform an action minimizes the damage done when something bad happens, whether it is a corrupt attachment in an e-mail received from Outlook, or a service that has a security risk. By running programs without Administrative privileges whenever possible, you ensure a more secure environment. Currently available software often requires elevated privileges in order to run correctly. To end this situation, developers must take the first step and stop running as administrators. Then, if we all consistently log in, develop, and test applications as non-administrative users, the software we produce is more likely to be executable without artificial requirements of elevated privileges. Until developers fix the software they are writing and shipping, users will never be able to run in a secure environment, too! Group membership and permissions for the login accountTasks such as global registration, installation of new software applications, and reconfiguring devices require administrative privileges. Unless a full-time administrator manages your machine, you will need to keep one administrative account to do these tasks. From now on, it is assumed that this user account is named 'Administrator' and is on the local machine, as opposed to a domain account. However, the user could equally well be named 'root' as long as it is a member of the local machine's Administrators group. It is also assumed that you are working in a Windows domain, and your primary login is an account in that domain of the form DOMAIN\username; however, this is not a requirement and both accounts could be on the local machine. Changing your account statusAfter confirming that you still have access to an administrative account, go to the User Manager, remove your user account from the Administrators group, and then add it to the Users group and any other less-privileged groups that are appropriate. To add your user account to the Users group, you must first load the Microsoft Management Console (MMC) and the User Management snap-in. Note In most network setups, the domain's Domain Users group is already a member of the local Users group, so your domain account will already be in the local Users group. Using the Microsoft Management Console
You are now ready to add your account to the Users group. Adding your account to less-privileged groups
Note If you develop and debug local Web application using Windows XP Professional, use the following procedure to add the Log on as Batch Job privilege to your account. Logging on as a batch job
Ensuring access to files and sharesWhile you were logged on as a member of the local Administrators group, you might have created directories for your personal use. You might have also created network shares that you wanted your user account to access, but those network shares were created with access granted only to the local Administrators group. You should ensure that the directories you have created on the local drive outside of your profile directory are still available to you and are owned by your user account. To verify ownership of a private file or directory
To verify access to file shares
Performing administrative tasksWhen you log in, you will notice that many of the UI widgets no longer work. Double-clicking the clock in the taskbar, for example, no longer displays the calendar, because the calendar is able to change the date, and that functionality is only available to users with elevated privileges. Running an application as another userWindows XP includes facilities to easily run an application as another user without logging out and back in as that user. Note, however, that these applications will terminate when you end your primary interactive logon session. You can do this in one of three ways. Running a shortcut with different credentials
If you are always going to run an application as a different user, you can also allow Windows to automatically prompt you. Running an application command line with different credentials
Runas.exe will prompt for a password and execute whatever command line you passed in a new login session. This is convenient for running programs like regedit, iexplore, or even just opening a new command window, all of whose child processes will be running as that user as well. Because it is a new login, it does not inherit handles, environment variables, or the current working directory from its point of invocation; if you want to run programs that are not on the other user's path, you need to provide a full path to them. Prompting for different username automatically
Running specialized administrative tools with different credentialsThe next logical question is: How do you run the tools that are normally launched from the control panel? Should you right-click My Computer and select Manage, or should you go through other UI widgets that do not have intuitive shortcuts or obvious command line equivalents? Fortunately, all of the administrative tools are available either by running the control panel applets or directly through the Microsoft Management Console (MMC). The following tables list the common administrative tools (control panel applets and Microsoft Management Console files) and how they can be launched (either from a command prompt or by starting the control.exe or mmc.exe applications directly with the RunAs command). All of them are stored in the Windows System32 folder, though some are available exclusively on either the Windows Server 2003 family or Windows XP. Those entries are called out in the table below.
1 Uses the Windows Server 2003 family of operating systems
1 Uses the Windows Server 2003 family of operating systems Networking credentialsWhen a window or logon session is open as the administrator, the default network credentials are not that of the domain account that is normally used to log on. Because most networks expose critical resources such as file shares only to domain accounts, it is necessary to establish alternative network credentials. There are two ways to do this. You can open a new command window and then set up alternative default credentials for use over the network. Another option is to allow individual servers or shares to be accessed with another username and password. When you are running a command prompt, it can be helpful to change your environment to clearly indicate that this is a window with administrative privileges. Many people use one of the following methods:
Opening a new command window with alternative network credentials
Opening a new command window for accessing individual server shares
Other forms of the net use command allow you to either map a network drive to a persistent letter or provide the domain credentials for authenticating access requests coming from this machine. The following table lists some net use commands. After you have completed these commands, applications can be installed from the remote shares, as they would normally have been.
Installing softwareInstalling software when you do not have administrative privileges can be complicated. For example, inserting an installation disc launches setup as the user who is currently logged in, who probably does not have permissions to install the application. To avoid this, you can:
ActiveX controlsMany Web sites especially those on corporate intranets often require administrative access to install their ActiveX controls, and do not support restricted user installation. The easiest way to launch Internet Explorer as Administrator is to do the following:
The Windows Installer packageIf it is a Windows Installer package (.MSI file), then the MSIEXEC.exe application is helpful to install or uninstall applications.
Running programs that assume administrative accessUnfortunately, many programs are not Windows Logo compliant, and many of those programs that are compliant seem to be less so than others. The Windows Logo program states that an application needs to function reasonably under a less-privileged account, whenever possible. Occasionally, a program requires write access for user accounts to its Program Files installation directory or even a key in HKLM\Software. If you can easily determine the directory, files, or registry keys access to which access is required, you can edit them to help the program run. The following procedures explain how to do this. Editing registry permissions
Editing file system permissionsFor files or directories, the cacls.exe program is useful for resetting permissions from the command line. Help for using Cacls.exe usage is available by two different methods:
Example: running the cacls.exe program to add permissions for a user to a directoryRun cacls.exe with the flags to recursively add a grant of the write privilege runas /user:Administrator "cacls.exe directory /t /e /g domain\username:w"
As a last resort, you can use runas.exe to launch the program as the local Administrator user; however, do so with care after all, if the program is not well-behaved enough to degrade gracefully in a less-privileged environment, how much should it be trusted to behave safely in a highly-privileged environment? Unfortunately, many programs were not tested to be run as a user other than the one who installed it, much less in a less-privileged account; you might find that you need to run the program as the local Administrator in order to get work done. This is still a better environment than running all software and logging in as a member of the local Administrators group, but you should try to contact the company who developed the software about their bug so that they can fix it in their next release. The only way end users will ever be able to run in a safer login environment and still be as productive as they are today is for every developer to fix their software. Developing softwareVisual Studio .NET is Windows Logo compliant, so you would expect that it runs as a restricted user. However, even though the vast majority of all functionality works in constrained permissions, there are a few tasks that are slightly more difficult in this model, either because of operating system limitations or the fundamental nature of the tasks being performed. The following sections provide simple workarounds for all of the difficult tasks in a development process; in future versions of the integrated development environment (IDE) we hope to build this functionality directly into the shell and reduce the requirements wherever possible. We are working hard in future versions of both the operating system and development tools to make it easier to perform these actions in equivalent ways that do not require administrative privileges. Doing post-build steps (registration)In classic Visual C++, it is regsvr32.exe. In the Visual Basic .NET and the Visual C# .NET world, it is registering assemblies for interop. Whatever the project system forcing it, there are many tools that require write access to HKLM\Software\Classes in order to register COM components and type libraries. If your application is currently performing registration steps that require write access to secured portions of the registry, you should first determine if you really need to do be doing them. Remember that:
Unfortunately, many of the tools and operating system APIs allow registration only to HKLM\Software\Classes; type library registration is a prime example of this. In these cases, the most feasible options are either to use runas /user:administrator cmd.exe, change directory to the output directory, and then run the registration steps by hand, or to eliminate the dependency altogether. Unfortunately, elimination of the dependency on global registration is complicated enough to be worthy of its own article. An alternative is to create custom manifest files to circumvent the registration steps altogether, but that too is beyond the scope of this article and more information is available at ms-help://MS.VSCC/MS.MSDNVS/sbscs/sidebysideref_03ol.htm. The most uncomplicated way to work around the global registration limitations is to perform registration steps with your administrative account. The following table shows common post-build registration tasks and their registration. Do them the first time and you should be able to build and run as normal afterward. Remember, though, that you might need to re-do the registration step any time you switch between Debug and Release configurations, and when you make changes to your application that impact the items being registered.
Debugging normal applicationsWhen you log on as a non-administrator, there is one feature you will lose: Windows does not allow debugging of applications that are running as a different user unless you have the special user right allowing it (SeDebugPrivilege). By default, only members of the Administrators group have it. You will still be able to debug applications that you have launched from the development environment or which are already running as your non-administrative user. If you need to debug services or other programs that run in other security contexts, you will need to either grant yourself that OS privilege or use RunAs to launch Visual Studio .NET as the local Administrator user. That said, there are many methods to launch your application as your own user, both enabling debugging and helping ensure that your application correctly runs in a privilege-constrained environment. Native and managed appsWherever possible, simply launch the application as your non-administrative user and everything will work. By default, Start from the Debug menu on normal executables works, as does attaching (Attach to Process from the Debug menu) to any of the applications you have launched yourself on that machine. You must be a member of the Debugger Users group in order to attach to any process with the default Visual Studio .NET mechanisms. Membership in this group does not give you access to debug other users' programs, but simply allows you to use the service that attaches the debugger to the process being debugged. Remote machines (native/managed)There are two acceptable ways to do remote debugging: through the Machine Debug Manager service or msvcmon.exe. The machine debug manager is a service that recognizes your network credentials, understands who you are, checks to see if you are a member of the target machine's Debugger Users group, and then allows you to connect to your own applications if you are. The Machine Debug Manager is also the only way to debug remote managed applications, including Web applications. You will not be able to connect to other users' applications on remote machines simply by being in the Debugger Users group. For native programs running on a remote machine, you would need the SeDebugPrivilege user right on the target machine as well. For managed applications on a remote machine, you are required to be a member of the Administrators group; not even the operating system privilege is sufficient to allow access. As an alternative method for debugging native applications, the msvcmon.exe program allows a way to connect over TCP/IP to managed applications on a remote machine. In this scenario, login credentials from the debugging machine are not replicated on the remote machine. For example, if msvcmon.exe is launched on the remote machine as that machine's local Administrator, but with the "-anyuser" flag, then anyone can connect to the service and attach to and debug any native application on that machine, even if they didn't originally have rights to do so. Creating Web applicationsThere are two options for creating new Web applications. The first is to add the user account to which you are logged to the VS Developers groups, allowing that account to write anywhere in the IIS WebRoot. The alternative is to create the directory as that user and then add a Virtual Root. In either case, after you have completed one of those options, you can create the new application from the Visual Studio .NET Create New Project dialog as the restricted user. To add an IIS Virtual Root
Debugging Web ApplicationsThere are two conditions for debugging of Web applications: running ASP.NET as your restricted login account and directory permissions. For the set of directories you will need to add read or write access to, refer to the following table.
Note %INSTALLROOT% is of the form D:\WINDOWS\Microsoft.NET\Framework\v1.0.3705 and %WINDIR% is of the form D:\WINDOWS. By default, with a restricted user login, you cannot debug Web applications because the user running the debugger does not have the privilege to debug other users' programs (SeDebugPrivilege), but the Web server started up ASP.NET as the NETWORK_SERVICE account. If you do not want to grant this login the SeDebugPrivilege, you need to change the account that ASP.NET is running as. On Windows XP Professional, edit machine.config as shown below and put in your username and password in clear text. This has the potential disadvantage of requiring all ASP.NET applications on the machine to run as your user account, but is the best method for IIS 5, and allows you to debug and build Web apps the same way you did in the past. Security Note The machine.config file must be on an NTFS drive and have a restrictive ACL set if you do this, or anyone who logs on to the machine will be able to see your password. Even if you do, all users in the Administrators group will be able to read it, regardless of the ACL set. Enabling ASP.NET to run as another user on Windows XP ProfessionalAs an Administrator, edit the attributes of the file"%INSTALLROOT%\Config\machine.config" 'on the processModel tag, as shown: <processModel
enable="true"
userName="DOMAIN\username"
password="MyPswd2"
…
/ >
Note %INSTALLROOT% is of the form D:\WINDOWS\Microsoft.NET\Framework\v1.0.3705 Enabling ASP.NET to run as another user on Windows Server 2003With Windows Server 2003 and IIS 6, there is a new feature named application pools. Each pool can be configured to run as a different user, provided that user has membership in the IIS_WPG group. Virtual roots can be added to an application pool, and the debugger will then be able to attach to it if the pool is running as the same user that launched the debugger. This mechanism provides an easy way to set up an alternate execution environment, safely protect user credentials, and set up additional virtual roots. Adding and Configuring an Application Pool
Setting a Virtual Root to run in an Application Pool
Installing deployment project outputInstalling the output from a deployment project has the same restrictions as installing an application from the network, as previously mentioned with the RunAs.exe and MSIEXEC tools. Install it as a normal application, for all users, or it might be difficult to launch or to debug. ConclusionThe cost of developing secure applications and having a secure environment in which to work is a little bit of inconvenience when you need to perform a task that requires a higher level of privilege. However, the rewards are both a safer work environment and safer programs. There is no excuse not to do it. Remember that the lower the set of privileges your application requires when it runs, the less damage it can do when it is compromised. It is a simple fact that your application will ship with at least one security vulnerability it is up to you how much it should ruin your users' machines and your company's image if someone else finds it and exploits it. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Contact Us | E-Mail this Page | MSDN Flash Newsletter | Legal |
| © 2003 Microsoft Corporation. All rights reserved. Terms of Use Privacy Statement Accessibility |