Make OpenLibary() and Import safe from dll-hijacking.
Posted: Thu Sep 16, 2010 1:18 am
SetDllDirectory()
http://msdn.microsoft.com/en-us/library ... 85%29.aspx
http://blogs.msdn.com/b/david_leblanc/a ... tacks.aspx
Solution for older OS http://blogs.msdn.com/b/david_leblanc/a ... tacks.aspx
Fred, any chance you could make PureBasic always do a SetDLLDirectory("") at the very start of the program (before any dynamic dll loading takes place).
SetDLLDirectory() is available from Windows XP with SP1 and Server 2003.
SetDLLDirectory("") tells the OS that it should remove the current directory from the dll search path,
this twarts the majority of dll-hijacking/dll-preloading exploits.
The regular search paths are untouched, so the OS will still look in the exe's directory for the dll and then the system folders etc.
For XP "Gold", W2K and older the alternative technique shown in the second blog post is needed.
If these changes are done for the next PureBasic release then PureBasic would be "safe" "out-of-the-box" against the dll-hijacking/dll-preloading attacks.
Obviously there may still be an issue with load-time dll's (aka Import feature) as a Vista dll may not exist on XP and thus such a dll search could still be exploited.
dwmapi.dll is such a dll, I'm not sure how that can be fixed, is there a way to force Import (load-time dlls) to NOT search current directory?
Maybe one of those confusing MSDN pages about manifests has a solution for that?
or is it possible to use SetDLLDirectory("") before the Import *happens* at run time? I believe the kernel actually uses LoadLibary for load-time dlls but it's before PureBasic can do a SetDLLDirectory("") I suspect!
Ideally SetDLLDirectory("") should be done right after kernel32.dll is loaded (where SetDLLDirectory is found) but before any other dlls are loaded.
Anyway, SetDLLDirectory("") and the fix in the second blog post should fix half the issue related to LoadLibary and other related APIs being vulnerable as current directory will not be searched any longer.
And if anyone has an issue with this then make it the default but allow developers to disable it (thus assuming they know what they are doing).
Personally I can't think of ever needing to load a dll from the current directory, the exe directory is the usual place, and if it's a sub folder in the exe folder then I explicitly load them using the full path anyway so...
Oh and for those wishing to individually secure their system across the board there is a "fix" from Microsoft http://support.microsoft.com/kb/2264107 for XP and later.
But obviously we developers can't demand that users apply this fix or expect all of them to be aware of it so the request above is still valid regardless.
Oh and for those that do not know how this exploit works, it's laughably simple (and published all over the web by now).
Trick someone to open what appears to be a benign document of some sort, in fact it could even be a real and clean document, but in the same folder as the document is a dll.
For example if it was a XP system it could be a normal file but which is used by a program that works on both XP and Vista+ (and makes use of dwmapi.dll if available like Firefox might do).
As soon as the program starts it will look for the dll, and it find it in the current directory (where the document is) and load the dll, the actual document is just a red-herring,
the dll that is now loaded can do whatever evil thing it wants to do.
This exploit would never have been possible if Microsoft had not allowed dll search in the current directory. (it's a behavior holdover from the old DOS days actually)
http://www.reuters.com/article/idUS2168761020100825
Acros discovered at least 121 exploits in 41 Microsoft applications, and if you look around on the net you'll see that potentially thousands of other non-microsoft applications are vulnerable as well.
(PS! Make sure that your browser and utorrent and whatever else is updated as most of the more popular apps out there have/has/are fixing the issue. (using SetDLLDirectory("") and the alternative fix for older OS' most likely.)
Here is the report and proof-of-concept on uTorrent's vulnerability http://www.exploit-db.com/exploits/14726/
Creepy how easy it is right?
Basically you could be exploited by simply opening a html file using a older Firefox on XP if an attacker/site manages to sneak in a dwmapi.dll along with with the html, and I doubt many people would think twice about a dll just sitting there along with a html for example. We'll I'll certainly do from now on but...)
Anyway, now that I've given you all a nightmare to dream about I'll continue looking through all my apps and make sure that I use the two solutions mentioned above and make sure to use full paths to any plugin dlls my apps may use etc.
Oh and to depress you all further, here's a "small" list of some apps that was/are vulnerable http://www.corelan.be:8800/index.php/20 ... cial-list/
EDIT: Changed the topic to reflect the (now) thread subject better.
http://msdn.microsoft.com/en-us/library ... 85%29.aspx
http://blogs.msdn.com/b/david_leblanc/a ... tacks.aspx
Solution for older OS http://blogs.msdn.com/b/david_leblanc/a ... tacks.aspx
Fred, any chance you could make PureBasic always do a SetDLLDirectory("") at the very start of the program (before any dynamic dll loading takes place).
SetDLLDirectory() is available from Windows XP with SP1 and Server 2003.
SetDLLDirectory("") tells the OS that it should remove the current directory from the dll search path,
this twarts the majority of dll-hijacking/dll-preloading exploits.
The regular search paths are untouched, so the OS will still look in the exe's directory for the dll and then the system folders etc.
For XP "Gold", W2K and older the alternative technique shown in the second blog post is needed.
If these changes are done for the next PureBasic release then PureBasic would be "safe" "out-of-the-box" against the dll-hijacking/dll-preloading attacks.
Obviously there may still be an issue with load-time dll's (aka Import feature) as a Vista dll may not exist on XP and thus such a dll search could still be exploited.
dwmapi.dll is such a dll, I'm not sure how that can be fixed, is there a way to force Import (load-time dlls) to NOT search current directory?
Maybe one of those confusing MSDN pages about manifests has a solution for that?
or is it possible to use SetDLLDirectory("") before the Import *happens* at run time? I believe the kernel actually uses LoadLibary for load-time dlls but it's before PureBasic can do a SetDLLDirectory("") I suspect!
Ideally SetDLLDirectory("") should be done right after kernel32.dll is loaded (where SetDLLDirectory is found) but before any other dlls are loaded.
Anyway, SetDLLDirectory("") and the fix in the second blog post should fix half the issue related to LoadLibary and other related APIs being vulnerable as current directory will not be searched any longer.
And if anyone has an issue with this then make it the default but allow developers to disable it (thus assuming they know what they are doing).
Personally I can't think of ever needing to load a dll from the current directory, the exe directory is the usual place, and if it's a sub folder in the exe folder then I explicitly load them using the full path anyway so...
Oh and for those wishing to individually secure their system across the board there is a "fix" from Microsoft http://support.microsoft.com/kb/2264107 for XP and later.
But obviously we developers can't demand that users apply this fix or expect all of them to be aware of it so the request above is still valid regardless.
Oh and for those that do not know how this exploit works, it's laughably simple (and published all over the web by now).
Trick someone to open what appears to be a benign document of some sort, in fact it could even be a real and clean document, but in the same folder as the document is a dll.
For example if it was a XP system it could be a normal file but which is used by a program that works on both XP and Vista+ (and makes use of dwmapi.dll if available like Firefox might do).
As soon as the program starts it will look for the dll, and it find it in the current directory (where the document is) and load the dll, the actual document is just a red-herring,
the dll that is now loaded can do whatever evil thing it wants to do.
This exploit would never have been possible if Microsoft had not allowed dll search in the current directory. (it's a behavior holdover from the old DOS days actually)
http://www.reuters.com/article/idUS2168761020100825
Acros discovered at least 121 exploits in 41 Microsoft applications, and if you look around on the net you'll see that potentially thousands of other non-microsoft applications are vulnerable as well.
(PS! Make sure that your browser and utorrent and whatever else is updated as most of the more popular apps out there have/has/are fixing the issue. (using SetDLLDirectory("") and the alternative fix for older OS' most likely.)
Here is the report and proof-of-concept on uTorrent's vulnerability http://www.exploit-db.com/exploits/14726/
Creepy how easy it is right?
Basically you could be exploited by simply opening a html file using a older Firefox on XP if an attacker/site manages to sneak in a dwmapi.dll along with with the html, and I doubt many people would think twice about a dll just sitting there along with a html for example. We'll I'll certainly do from now on but...)
Anyway, now that I've given you all a nightmare to dream about I'll continue looking through all my apps and make sure that I use the two solutions mentioned above and make sure to use full paths to any plugin dlls my apps may use etc.
Oh and to depress you all further, here's a "small" list of some apps that was/are vulnerable http://www.corelan.be:8800/index.php/20 ... cial-list/
EDIT: Changed the topic to reflect the (now) thread subject better.