|
Introduction
There is an old rule in Unix environments, that you should never run a desktop environment as root. It makes sense; root is the super user that has complete control of everything in the operating system, and from a security perspective it makes no sense to expose such privileges as you work in the system unless you absolutely have to.
In the Windows world, as far as desktop environments go, there hasn’t been this same “rule of thumb”. Well, more to the point… no one is touting it. Well, this article is to do just that. Although many of us who are security conscience run our systems with least privilege as we do development, most of you do not. Hopefully I can show you a few reasons why developing in a least privileged environment is a good idea, and answer some of the issues people have when they try it, and it fails.
Understanding Least Privilege
If the term “least privilege” seems foreign to you, don’t fret. The term itself is a pretty good definition. Least privilege is a concept in the field of security where basically you give the absolute minimum amount of access rights and privileges to accomplish a task. Think about this concept in reflection of the old wives tale about fishing with dynamite. Perhaps you heard of it? You take a stick of dynamite, light it and throw it in the lake. BOOM! Now go collect the fish that float to the top. Ok, fishing complete. Yet it could have been done with a fishing hook, some line and a little patience. Both can do the job, but the dynamite is excessive… it’s just not the right tool. If something goes wrong, there is a bigger chance you will hurt yourself or others around you than if you used your fishing gear. The same analogy can be used with applications in any environment – they should only need enough privileges to do their job.
When you consider all the security compromises that relate to buffer overflows, privilege elevation, data destruction etc, an application ran in least privilege will do less damage than if it was ran as a super user like root or administrator. The injection of faulty data (malicious or not) makes it way to easy to wreak havoc on the system. As an example, a Trojan executed will spawn itself with the same privileges as the calling process. If your account is part of the local administrator group, a Trojan will now have complete system access which could open you up to virtually everything.
Outside of the issues that relate to application security, there is another good reason to not run as administrator on the local machine, especially if you are doing development. You should be doing your development in an environment as a normal user so your applications will be in the same security sandbox as your intended users. This may seem like a small point, but consider the outcome. If you write your code with full privileges, many conditions that your normal users will have imposed on them will never be seen in a development or QA environment.
I just finished installing an app today that had this problem and did not work for me. It is the whole reason I decided to write this article. No error conditions or alerts of any kind were shown during the normal operation of using the application. Yet every time I tried to click a button it just sat there. Finally I realized that maybe it was trying to read or write something in the registry in which I didn’t have access to. Sure enough, that was the case. It was trying to write to a registry key that I had no rights to. How did I get around this? I used a tool built within Windows XP/2000 to change my user credentials for this application only, so that it could set the required key. Once that was done, I could run the application fine as a normal user. Lets talk about that tool.
The "runas" command
Windows has a neat utility that can run application with other credentials called "runas"[1]. Runas allows a user to run specific tools and programs with different permissions than the user's current logon provides. This can be done through the command line, or through a graphical user interface. Lets give it a try. Here is the scenario. Lets assume you are a normal user without any administrative privileges on Windows XP. You need to check out some settings in HKEY_LOCAL_MACHINE\SECURITY. You do the following:
- Click Start->Run
- Type: regedit
- Expand the HKEY_LOCAL_MACHINE tree node.
- Expand the SECURITY tree node.
- Get prompted with “Error Opening Key” Dialog. Access is denied!
This is expected. As a normal user you shouldn’t be allowed to see this particular key. Now lets try accomplishing the same task using the runas command.
- Click Start->Run
- Type: runas /user:Administrator regedit
- Enter the administrator’s password at the prompt
- Expand the HKEY_LOCAL_MACHINE tree node.
- Expand the SECURITY tree node.
- Note you have access
Bingo. You started the process regedit as administrator on your local desktop running as a normal user. This gives you the flexibility of still being able to run administrative tasks and processes, but on your conditions, when you want it. Yet at the same time, you are restricting the possibilities of malicious code from gaining extra privileges than what is provides as a normal user.
Runas is a pretty good tool for this. But you don’t have to always use it from the command line. You could just as easy create a shortcut on your desktop to regedit, and set it up to run with different credentials. Here is how you could do it on Windows XP.
- Create a new shortcut on your desktop
- Right click and choose Properties
- Select the "Shortcut: Tab
- Click the "Advanced" button
- Select the "Run with different credentials" checkbox
- Click "Ok"
- Click "Apply"
When you double click on the shortcut, it will now prompt you with a graphical dialog on which account’s credentials you wish to launch regedit as. You could also get to this from any shortcut by right clicking and selecting "Run as…" from the popup menu.
Setting up Access Rights for Debugging
As a developer one key feature you will ultimately need is the ability to debug programs. One issue you will have as a normal user is that you cannot attach a debugger to processes that you don’t own. In the real world, I found that sometimes even when it IS your process there are issues in tools like Visual Studio .NET. Further to this, you will want to be able to terminate processes easily, which isn’t permitted as a normal user. These are all issues due to the SeDebugPrivilege flag not being available to you. No worries though, as you can easily increase your privileges for just local (or even remote) debugging by adding yourself to the "Debugger Users" group. This can be accomplished by:
- Click Start->Run
- Type: runas /user:Administrator "mmc compmgmt.msc /s"
- Enter the administrator’s password at the prompt
- When the Computer Management console starts up, expand the “Local Users and Groups” tree node.
- Select the “Users” folder
- Double click on your user name
- Select the “Member of” tab
- Click the “Add” button
- Enter in “Debugger Users” in the field provided (without the quotes)
- Click “Ok”
You will now be a member of the Debugger Users group, and will now have efficient privileges to be able to attach a debugger to any process in the system. Now, I would like to give you a word of caution if you decide to increase your privileges to include debug access. If an attacker can gain access to your account that now has debug privileges, it may be possible to execute malicious code within any running process on the system. There have been recent security proofs [4] in which you can use functions like OpenProcess() and CreateRemoteThread() to insert code in this manner. Even with this potential threat, it is still safer than running as a full administrator. By reducing the attack surface of your account privileges in the system, it is much harder for an attacker to maliciously elevate his (your) privileges in this way, compared to how easy he gets it if he compromises an administrator account.
Tracking down Least Privilege Issues
If any of you take my advice and remove yourself from the administrator’s group, you will probably come across some issues and will have trouble trying to get existing applications you wrote to work as a normal user. This should be expected. It’s not really your fault (Well, it is… but why dwell on it). You probably originally wrote it for Windows 98/ME which didn’t have such finite access control and although it worked fine in W2K/XP, it was because you ran as an administrator with full privileges. You just haven’t dealt with the security permissions and access control lists that you should be in newer Windows environments.
There is a way to help diagnose what may be causing your application to fail as a normal user. If you modify your computer’s logging policy, you can trap many issues your application may have as it dumps it to Event Viewer. Here is how you would configure your security policy to support this:
- Click Start->Run
- Type: runas /user:Administrator “mmc secpol.msc /s”
- Enter the administrator’s password at the prompt
- Expand the “Local Policies” tree node
- Click on the “Audit Policy” folder
- Double click the “Audit privilege use” item
- Check the “Faillure” check box
- Click “Ok”
Now when your application runs, failed privilege attempts will be logged to Event Viewer, and you can begin to figure out what and where your application may be failing.
Another issue that will come up will relate to access privileges to files, directories and the registry. Mark Russinovich and Bryce Cogswell over at SysInternals (http://www.sysinternals.com[^]) wrote two excellent tools that will aid you in diagnosing these types of issues. FileMon and RegMon will monitor access attempts to files and the registry respectively, and will report an access denied (ACCDENIED) if you application does not have the required privileges in the system.
You can use this approach with these tools to quickly find issues in your applications and address them before other users come up to these problems. Remember, if YOU can’t run it as a user, how can you expect them to be able to? Requiring to install as administrator is just not acceptable, unless of course the application REQUIRES administrator access. Don’t get lazy…. do it right. Trust me, you will thank me in the end.
Conclusion
It is easy to be lax with security when it comes to comfort in our development environment. We want to get our job done, without any constraints. Yet doing this exposes us to large risks we really don’t NEED to take. A combination of a normal user account with Debugger User group privileges can give us the same environment to write code as if we were administrator. With addition of the runas command, we can even run administrative tasks in our normal user environment. Hopefully you can see this. It is the old “eat your own dog food” adage. If you yourself run with least privilege, and design your applications in this way, your quality of code will increase, as will the security. Who would have thought such a small change could create such a large benefit?
Of course, you can ignore this article. You could stick your head in the sand and expose yourself to unnecessary risk. You can write applications without thinking about least privilege and access control. Hopefully, I won’t have to run your stuff.
Security is part of our every day life, and we should embrace it. Using secure coding practices is a great start.
References
- http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/winxppro/proddocs/runas.asp[^]
- http://www.jsifaq.com/subf/tip2500/rh2548.htm[^]
- http://www.ntfaq.com/Articles/Index.cfm?ArticleID=25992[^]
- http://www.securityfocus.com/archive/82/312694[^]
History
- 09/03/2003 - Initial Submission
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 32 (Total in Forum: 32) (Refresh) | FirstPrevNext |
|
|
 |
|
|
As a developer, it doesn't really work for me to run with least privelege - I've tried and too many things just don't go well. However, lots of my routine and most dangerous activity could be done with less privelege - for example reading mail and surfing the web.
I've spent an entire day trying to set up a non-admin user I could run with "runas" which would not have access to most of my files: and I just can't make it work. Despite my best efforts, no combination of "deny" as a default file permission and "allow" for just the directories the mail reader ought to have access to seems to work. Wither the files I want remain inacessable, or files I don't want remain acessable. Arrgh!
Without starting a flame war about how broken window's file access security model is, can anyone tell me how ?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Have aclass in my program that uses the function NetFileEnum(). Found out that this requires access rights to the root of the actual File Server, but my users don't have such rights...
So how can I give them access to the class I created, while they are logged in with their own user rights? Say I am prepared to even hard code the user / password for the administrator in my code (ugly I know), or is there another way?
Thanks Alex alex.evans@iinet.net.au
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have XP Home. I'm not a very computer literate guy, but the "Run As" pop up is very annoying and I want it to stop. It pops up four times before any of my programs load. I'm just a home user (Internet, word processing, etc). I don't have passwords set and my wife and kids have different user groups when we turn on the machine. All this started popping up about February 27. I've tried going back, but the computer won't allow it. Even the Dell Tech was stumpt. Just make the "Run As" pop-ups go away without interfering with anything else. It won't allow my Norton System 2004 shortcut to work either. Thanks Bobby G.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
My software has been installed on lots of computers over the last year where the resitry rights have been restricted -- and often even the IT people don't know how to configure the computer to give the user rights to the registry keys we want while maintaining the restrictions they want.
For what it is worth, I've got a bit of code that can set the rights for a key. We use it during our install process so the IT person/user doesn't have to run regedit, but its not perfect -- we've still not found a solution to the built in "restricted user" logins. If I can figure that one out I'll try to put it together in an article.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Greetings,
I have a bit of a problem, step 4:
"When the Computer Management console starts up, expand the “Local Users and Groups” tree node. "
I do not have this node, so what do I do? I'm trying to setup access rights so other users can do debugging using visual studio.
(see http://www.codeproject.com/useritems/runas.asp under title "setting up access rights for debugging")
Bat
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It's because i have Xp Home Edition, and they decided not to included permission settings etc. Does anyone have any ideas of what I could do?
Someone mentioned cacl but it's complicated, and I'm not sure what to use it for or if it will help.
any ideas?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
The problem with XP Home is that it doesn't have the Debugger Users Group at all as all users are part of the "Owners Group".
I wish I knew a solution for you, but I am afraid I have never tried to program under XP Home. You couldn't even do remote debugging except via TCP/IP, but at that point you may as well move your debugger to the other machine that is providing you the ability to do the remote debugging. Unless there is some werid way you could do remote debugging via localhost, but I have never tried this.
Sorry I can't be much help in this case. Only thing I could recommend to you at this point is to move to Pro or W2K/W2K3. Not sure if that is feasible for you or not.
Good luck.
Dana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
.. lots of good info here. Even though I disagree with your fundamental premise that _users_ should never run as administrators - IMHO, security is highly overrated - but I agree that as a developer, I should make sure that my apps will run on systems that have tightened security.
What level of access would you suggest for testing.. Standard User, or Restricted User? I am writing desktop apps for musicians, who tend to want to be in total control of their systems, so it seems to me that Restricted User is a bit too much to worry about.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hey Jim,
Determining what level of access you need for testing comes down to the environment your users would have. With that said, if you used the design philosophy of what least privilege provides, making it work as a restricted user immediately makes it work as a normal user, or even as an administrator.
Make it work in the most stringent environment, and you will make it work on anything MORE open than that standard.
Good luck.
- Dana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It depends on the situation though. Many enterprises do not want employees to even install other programs on their computers for various reasons (license infringement due to pirated software, objectionable content, IT management issues etc.) So the computer tends to be locked down to a very high level. Access may be restricted to parts of the registry etc. by a central policy.
In those situations, it is good to design systems such that it runs with the least required privileges. A business dataentry app does not need to have administrative privileges. But, many applications use things that need administrative privileges. Testing for these inadvertent things will save a lot of headache, if you let the apps run into the field and fail there.
My article on a reference-counted smart pointer that supports polymorphic objects and raw pointers
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks for the insightful article.
Here's a similar one I read on MSDN
Developing Software in Visual Studio .NET with Non-Administrative Privileges http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/tchdevelopingsoftwareinvisualstudionetwithnon-administrativeprivileges.asp[^] Can we have longer URLs please? 
One issue that I have is trying to run EXPLORER.EXE as an administrator, for those times that I need to change permissions or access files that an ordinary user normally doesn't have access to. (I know I can do this using the command line to set permissions etc, but would prefer to use the GUI).
I've tried enabling "Launch folder windows in a separate process", and then using the command line: runas /user:Administrator explorer.exe
I can hear the drive going, but nothing happens. Task Manager still shows a single Explorer.exe process running as my current user.
Any ideas?
Kiliman
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Actually, I just found the answer after reading one of your referenced links:
Running the Windows Explorer Shell in local Administrator Context
Start Task Manager and press the Processes tab. Select Explorer.exe and press End Process and YES. Your desktop will disappear. Select the Programs tab and press New Task. Type:
Runas /user:\administrator explorer.exe
and press OK. Enter the password.
The desktop will return.
When you are finished using this context, log off and a new explorer shell in the original context will start.
NOTE: The Runas service must be started for Runas to function.
This seem rather drastic though, so I'll look around for an "explorer" clone that will help manage permissions.
Thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
The trick to deal with explorer is to understand that with XP/W2K, iexplorer is basically the wrapper to explorer in the new "web browser" style.
So, from the command line try something like:
runas /user:Administrator "%SYSTEMDRIVE%\Program Files\Internet Explorer\iexplore.exe -new c:"
You will have it pop up at the main drive C:, and you can now go to town doing all your administrative file tasks like copying, changing perms etc through the explorer interface.
You could also make a shortcut on your desktop to do the same thing. Simply create the shortcut to iexplorer and use the "Run with different Credentials" checkbox as you would any other way.
There is no reason to drastically kill explorer.exe. Use the iexplore wrapper functionality to accomplish the same access controls.
Good luck!
- Dana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Perfect! Exactly what I was looking for.
BTW: your command line should be iexplore.exe (without the extra r).
Actually, I modified your command line to be
iexplore.exe -e -nohome
which gives me a nice explorer view.
Thanks again!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hey Brian,
Can you clarify your point? I am unsure what you are getting at. Are you saying your apps built with ATL/WTL cannot be ran as a normal user because they can not register components?
- Dana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Actually your article was a good reminder to myself:
Most ATL/WTL apps use HKCR and will NOT run as "user".
They require HKEY_CLASS_ROOT\CLSID and HKEY_CLASS_ROOT\TypeLib.
.reg can be changed to hit HKCU instead of HKCR, but what do you do with typelibs that ATL just wants to place into HKCR?
Brian
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I think the way to deal with this one is to put the registration stuff into the setup program. If you are using the windows installer you get rollback of registration entries as a nice bonus if the install doesn't complete. It is nice to have components that can register themselves (e.g. if a problem occurs in the registry the admin can just use regsvr to fix), but this shouldn't preclude the program running if it is correctly registered, but just doesn't have enough rights to write into HKCR.
It would be nice if MS would fix this problem. And also provide a way to generate the necessary registry entries in the setup project for all the things to be installed (by either scanning the code, or doing a registration and capturing the registry writes).
My .002 Paul.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
This sucks plain and simple.
My program has self-update code to provide automated updates, and install new features and components. May be I sould have done than using MFC. I just realized that group: "users" don't have write access to HKCR.
Plus is seems ATL registers interfaces as needed..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
OK, well lets live with the constraints for a moment and approach this logically.
Absolute security is a myth. What we want to do is reduce the risk as much as we can, while at the same time allowing the user to do their job.
Perhaps you could separate your update code with your main application. The update "daemon" could verify all transactions to the update server through the use of digital certificates to guarantee the authenticity of the update data, as well as the connection itself, reducing the potential harm from network and file attacks.
In this way, we only give enough code the privileges it needs to do its tasks. Installation, registration with the system and file overwriting (in self-updating code) can be done with more privilege, but the actual application itself does not.
This might meet your requirements. Of course without seeing a threat model I am just guessing here.
Good luck!
- Dana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Secure Coding Practices You write, good enough, we need that. Running with Least Privileges in Windows, that's not gonna make me code more secure code, but after half an hour I just throw my computer out the window because all the stuff I cannot do.
I test my applications as a normal user, and check that everything works perfectly good, but there is no way I'm gonna code as a normal user. I do too much stuff, as admin and coder, on my small network, that needs some privileges.
Now I just wait and see if this makes you say that I write more insecure code bacause of that!
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Anders Molin wrote: I do too much stuff, as admin and coder, on my small network, that needs some privileges.
Like what? You dont reconfigure your system every day. 
Anders Molin wrote: Now I just wait and see if this makes you say that I write more insecure code bacause of that!
That is exactly the attitude which prevents (more) secure programming practices: because you dont understand how to use least privileges, you consider it is to difficult|cumbersome to bother with it. 
Learn it.
Shaken, stirred, or strained through a diaper, nothing can make a martini palatable. Roger Wright, Soapbox
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Andreas Saurwein wrote: Like what? You dont reconfigure your system every day.
No, but I do copy files, and change registry keys, that normal users don't have permisions to do. I have to do that, in order to test and develop the software I develop.
Andreas Saurwein wrote: because you dont understand how to use least privileges
Now you are just too plain stupid!
Andreas Saurwein wrote: you consider it is to difficult|cumbersome to bother with it.
What do you know, I know how it is to work as a restricted user, and I know what they can do. I also test my applications as a restricted user, but I don't develop as one.
Andreas Saurwein wrote: Learn it.
Listen you fool, I have learned it, I just don't agree with your black/white attitude. I have worked with security for several years, full time, and I do know what I talk about.
Your problem is that you don't want to understand what I say.
Read carefuly:
I do test all my applications as a restricted user, so that I know it works out there in the real world. I don't log on and code as a restricted user, because it gives me way too many problems in my day to day work.
Is that so hard to understand? And why is my code more insecure than yours because of that?
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hey Anders,
Perhaps I can approach your issues in a more constructive manner.
Anders Molin wrote: I do test all my applications as a restricted user, so that I know it works out there in the real world. I don't log on and code as a restricted user, because it gives me way too many problems in my day to day work.
Is that so hard to understand? And why is my code more insecure than yours because of that?
Developing in an environment of least privilege is not about code quality. Code quality is an added benefit of you yourself taking the effort to run in a more secure environment. It is great you test your application as a normal user. That is one step towards making the user experience easier.
With that said however, your attitude of requiring to run as an administrator "becausing it gives me way to many problems in my day to day work" to run as a normal user is very reflective of your potential attitudes on security as a whole. And I don't criticize you for this personally. Most developers have this attitude because they have better things to do with their time. If you wish to expose yourself to unnecessary risk, I have to believe in a crunch you will sacrifice security in your application over getting a release out. This is human nature when deadlines loom. It takes a strong character to put your foot down and say "No.. I will not sacrifice security to get this out sooner".
From what you say you need to do daily, I am not convinced you need to be an administrator. More to the point, your personal experiences show you have given up because you have had problems. This can easily be overcome with education and experience. You sound like you have had plenty of experience over the "last several years" when it comes to security. Apparently it wasn't a good experience, or I would highly doubt you would have this same position on running with least privilege.
I appreciate your cander, and wish you well. I only hope in time you will realize that security as an after thought will always be trumpted by security by design and use. If you take your experiences (which by your own admission were bad) and think about it from a user's POV, the increasing threats that have elevated over time and the vulnerabilities that we get exposed to on a daily basis, why WOULDN'T you learn to run with least privilege?
To each his own I guess.
Good luck!
- Dana 
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
|
Dana Epp wrote: your attitude of requiring to run as an administrator "becausing it gives me way to many problems in my day to day work" to run as a normal user is very reflective of your potential attitudes on security as a whole.
Dana Epp wrote: I have to believe in a crunch you will sacrifice security in your application over getting a release out.
When you say this it just tells me that you are too stupid or narrow minded. I don't wanna waste much time on you...
With that said, I would necer sacrifice security in my applications, because of a deadline, I have no problem telling the boss that!
You know what, you have no idea of what I do and why I need elevated rights in my everyday work. You just asume that all developers sit and code their stupid GUI-applications as you probably do.
Dana Epp wrote: I only hope in time you will realize that security as an after thought will always be trumpted by security by design and use.
For me security is not an afterthought, why do you think that. Just because I don't wanna program when running as a normal user? If that is the case, you should grow up and stop being so narrow minded.
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
General 
|