![]() |
![]() |
|
November 06, 2003Secure Coding: Spawning external processes securely in WindowsToday I thought I would give everyone a good secure coding tip as it relates to executing external processes programmatically under Windows. Recently I was reviewing different ways to do this through .NET for C#, and started digging deeper into the Windows API to follow the execution path of the various calls. Most developers use ShellExecute. It seems harmless enough as in the docs it is explained as a wrapper around the CreateProcess() API call. But thats wrong. It might wrap the CreateProcess() call, but it does so in a way that exposes your code to the potential of tainted data injection, or more to the point misdirection threats. ShellExecute() uses the built in file association data stored in the registry to determine which process will be passed to CreateProcess. So if you feed it *.DOC file and Word is installed, it will execute Word accordingly. The problem with this is that an attacker could modify the association and point it to their malicious program. In doing so, you increase the attack surface of your application as you have untrusted data determining your code execution path. Very bad. Very bad indeed. A more secure way to do this is to call CreateProcess() directly. But I wouldn't even recommend that. What I would recommend would be to use the the CreateProcessAsUser() API. It is similar to CreateProcess() but you can force the new process to run in the security context of the user you truely want to execute as. You can create a security descriptor to tie this down even further, and even use a restricted token to ensure this new process has only the required access to do its task. Remember, all code execution should be ran with least privilege whereever possible. If you would like to learn more on how to do this, check out the CreateRestrictedToken() API. If you need to spawn secondary processes in Windows, forget using ShellExecute(). Instead, use CreateProcessAsUser(). If yer in C#, you will need to P/Invoke this, but thats not all that hard. Posted by SilverStr at November 6, 2003 11:08 AM | TrackBackComments
This is bullshit. If an attacker can modify the association with .doc on your machine. You are already owned. There are dangers with ShellExecute but you don't mention them. Most of the dangers revolve around the attacker setting the value of the lpfile param. Posted by: aj at November 29, 2003 10:15 AMThats a nice assumption, but incorrect in context. A power user, (or even a limited account for that matter in certain cases) can easily modify their own file associations, which means so too can the attacker. You are not OWNED because of this interaction. However, the fact that it can be modified covertly and then executed allows for an attack vector of an unsafe code execution path that the unsuspected user will run. That is the bigger threat... and the one I was trying to point out. You are right in pointing out that the lpfile param is quite dangerous, but I felt I didn't need to muddy up the waters any more since I was already recommending NOT to use it. I notice you don't disagree with the fact using ShellExecute is a bad thing, when there are safer functions like CreateProcessAsUser(). Although we disagree on what is a bigger threat when using ShellExecute, the fact remains that there are safer, and better alternatives. That was the point of my post. To each his own. Thanks for commenting on my blog. Good insight here. Posted by: SilverStr at November 29, 2003 05:40 PMDo you know if it is possible to redirect browser, IE, to URL at another application directory on same IIS server using CreateProcessAsUser? I'm using LogonUser() to authenticate on anonymous aspx page and want to redirect authenticated user to secure page. So far, only works with Server.Transfer with same app directory. Thanks! Posted by: Mark Peacock at December 8, 2003 07:23 AM |
![]() ![]()
My 5 Favorite Books
Writing Secure Code
Secure Programming Cookbook Security Engineering Secure Coding Principles & Practice Inside the Security Mind ![]()
My 5 Favorite Papers
Smashing the Stack
Penetration Studies Covert Channel Analysis of Trusted Systems DoD Trusted Computer System Evaluation Criteria NSA Security Recommendation Guides ![]()
Archives
November 2006
October 2006 September 2006 August 2006 July 2006 June 2006 May 2006 April 2006 March 2006 February 2006 January 2006 December 2005 November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 February 2005 January 2005 December 2004 November 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 July 2003 June 2003 May 2003 April 2003 March 2003 February 2003 January 2003 December 2002 November 2002 October 2002 September 2002 August 2002 July 2002 ![]() |
|