{"id":5893,"date":"2012-12-07T07:00:00","date_gmt":"2012-12-07T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/12\/07\/it-rather-involved-being-on-the-other-side-of-this-airtight-hatchway-writing-to-the-application-directory\/"},"modified":"2012-12-07T07:00:00","modified_gmt":"2012-12-07T07:00:00","slug":"it-rather-involved-being-on-the-other-side-of-this-airtight-hatchway-writing-to-the-application-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20121207-00\/?p=5893","title":{"rendered":"It rather involved being on the other side of this airtight hatchway: Writing to the application directory"},"content":{"rendered":"<p>\nWe received a security vulnerability report that went roughly like this:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nThere is a security vulnerability in the X&nbsp;component.\nIt loads <code>shell32.dll<\/code> from the current directory,\nthereby making it vulnerable to a current directory attack.\nHere is a sample program that illustrates the problem.\nCopy a rogue <code>shell32.dll<\/code> into the current directory\nand run the program.\nObserve that the rogue\n<code>shell32.dll<\/code> is loaded instead of the system one.\n<\/p><\/blockquote>\n<p>\nIf you actually followed the instructions,\nwhat you saw depended on your definition of &#8220;run the program.&#8221;\nLet&#8217;s assume that the program has been placed in the directory\n<code>C:\\sample\\sample.exe<\/code>.\n<\/p>\n<ol>\n<li>Setting the current directory to the application directory.\n<pre>\ncd \/d C:\\sample\ncopy \\\\rogue\\server\\shell32.dll\nc:\\sample\\sample.exe\n<\/pre>\n<p>In this case, the attack succeeds.<\/p>\n<li>Setting the current directory to an unrelated directory.\n<pre>\ncd \/d %USERPROFILE%\ncopy \\\\rogue\\server\\shell32.dll\nc:\\sample\\sample.exe\n<\/pre>\n<p>In this case, the attack fails.<\/p>\n<li>Running the application from Explorer.\n<pre>\ncopy \\\\rogue\\server\\shell32.dll C:\\sample\n<i>double-click sample.exe in Explorer<\/i>\n<\/pre>\n<p>In this case, the attack succeeds.\n<\/ol>\n<p>\nLet&#8217;s look at case&nbsp;3 first.\nIn case&nbsp;3, what is the current directory?\nWhen you launch a program from Explorer,\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/11\/10\/10088566.aspx\">\nthe current directory is set to the directory of the thing\nyou double-clicked<\/a>.\nTherefore, case&nbsp;3 is identical to case&nbsp;1.\nThat&#8217;s one less case to have to study.\n<\/p>\n<p>\nWe also see that the attack\nis not strictly a current directory attack,\nbecause the attack failed in case&nbsp;2\neven though a rogue <code>shell32.dll<\/code> was in the current directory.\n<\/p>\n<p>\nWhat we&#8217;re actually seeing is an <i>application directory<\/i> attack.\n<\/p>\n<p>\nRecall that the application directory is searched ahead of the\nsystem directory.\nTherefore, you can override a file in the system directory\nby putting it in your application directory.\nThis is part of the\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/06\/20\/10176772.aspx\">\n<i>directory as a bundle<\/i> principle<\/a>.\nIf you packaged a DLL with your application,\nthen presumably that&#8217;s the one you want,\neven if a future version of Windows decides to create a DLL of the same name.\n<\/p>\n<p>\nThe vulnerability report sort of acknowledged that this was\nan application directory attack rather than a current directory\nattack when they explained why this is a serious problem:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nBy placing a rogue copy of <code>shell32.dll<\/code> in the\n<code>C:\\Program Files\\Microsoft Office\\Office12<\/code> directory,\nan attacker can inject arbitrary code into all Office applications.\n<\/p><\/blockquote>\n<p>\nIf the attack were really a current directory attack,\nthe attacker would have put a rogue copy of\n<code>shell32.dll<\/code> in the directory containing your\nExcel spreadsheet, not the directory containing\n<code>EXCEL.EXE<\/code>.\n<\/p>\n<p>\nAnd that&#8217;s where you reach the airtight hatchway:\nNormal users do not have write permission into the\n<code>C:\\Program Files\\Microsoft Office\\Office12<\/code> directory.\nYou need administrator privileges to create files there.\nAnd if you have administrator privileges,\nthen you already pwn the machine.\nIt&#8217;s not really a vulnerability that you can do anything you want\nonce you pwn the machine.\n<\/p>\n<p>\nOf course, this non-vulnerability does expose a security\nissue you need to bear in mind when you run your own programs:\nYour application&#8217;s directory is its airtight hatchway.\nMake sure you control who you let in!\nIf you leave your application directory world-writeable,\nthen you&#8217;ve effectively left your airtight hatchway unlocked.\nThis is one reason why the Microsoft Logo guidelines\nrecommend (require?) that programs be\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2012\/03\/07\/10278793.aspx\">\ninstalled into the\nProgram Files directory<\/a>:\nThe default security descriptor for subdirectories of Program Files\ndoes not grant write permission to normal users.\nIt&#8217;s secure by default.\n<\/p>\n<p>\nThere are many variations of this type of vulnerability report,\nand they nearly always are mischaracterized as a current directory attack.\nThey usually go like this:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nThere is a DLL planting vulnerability in LITWARE.EXE.\nPlace a rogue DLL named SHELL32.DLL in the same directory as LITWARE.EXE.\nWhen LITWARE.EXE is run, the rogue DLL is loaded from the current directory,\nresulting in code injection.\n<\/p><\/blockquote>\n<p>\nThe person who submits the report has confused the application directory\nwith the current directory, probably because they never considered that\nthe two might be different.\n<\/p>\n<pre>\nC:\\&gt; mkdir C:\\test\nC:\\&gt; cd C:\\test\nC:\\test&gt; copy \\\\trusted\\server\\LITWARE.EXE\nC:\\test&gt; copy \\\\rogue\\server\\SHELL32.DLL\nC:\\test&gt; LITWARE\n-- observe that the rogue DLL is loaded\n-- proof of current directory attack\n<\/pre>\n<p>\nThey never tried this:\n<\/p>\n<pre>\nC:\\&gt; mkdir C:\\test\nC:\\&gt; cd C:\\test\nC:\\test&gt; copy \\\\trusted\\server\\LITWARE.EXE\nC:\\&gt; mkdir C:\\test2\nC:\\&gt; cd C:\\test2\nC:\\test2&gt; copy \\\\rogue\\server\\SHELL32.DLL\nC:\\test2&gt; ..\\test\\LITWARE\n-- observe that the rogue DLL is not loaded\n<\/pre>\n<p>\nThat second experiment shows that the attack is not a current\ndirectory attack at all.\nIt&#8217;s an application directory attack.\n<\/p>\n<p>\nEach time one of these reports comes in, we have to perform\nthe same evaluation to confirm that it really is an application\ndirectory attack and not a current directory attack.\n(This means, among other things, repeating the test on every\nversion of Windows, and every version of LitWare, and every\ncombination of the two, just to make sure all the possibilities\nhave been covered.\nThe odds are strong that it will all turn into a false alarm,\nbut who knows. Maybe there&#8217;s something about the interaction between\nLitWare 5.2 SP2\nand Windows&nbsp;XP SP3 that triggers a new code path that\ndoes indeed try to load <code>shell32.dll<\/code> from the\ncurrent directory.\nAnd it&#8217;s that specific combination of circumstances the person\nwas trying to report, but did a bad job of expressing.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We received a security vulnerability report that went roughly like this: There is a security vulnerability in the X&nbsp;component. It loads shell32.dll from the current directory, thereby making it vulnerable to a current directory attack. Here is a sample program that illustrates the problem. Copy a rogue shell32.dll into the current directory and run the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-5893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>We received a security vulnerability report that went roughly like this: There is a security vulnerability in the X&nbsp;component. It loads shell32.dll from the current directory, thereby making it vulnerable to a current directory attack. Here is a sample program that illustrates the problem. Copy a rogue shell32.dll into the current directory and run the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=5893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=5893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=5893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=5893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}