{"id":2753,"date":"2013-11-04T07:00:00","date_gmt":"2013-11-04T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/11\/04\/manipulating-the-zone-identifier-to-specify-where-a-file-was-download-from\/"},"modified":"2013-11-04T07:00:00","modified_gmt":"2013-11-04T07:00:00","slug":"manipulating-the-zone-identifier-to-specify-where-a-file-was-download-from","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131104-00\/?p=2753","title":{"rendered":"Manipulating the zone identifier to specify where a file was download from"},"content":{"rendered":"<p>\nWhen you download a file via Internet Explorer,\nthe file is tagged with a little bit of information known as\na <i>zone identifier<\/i> which remembers where the file was\ndownloaded from.\nThis is what tells Explorer to put up the &#8220;Yo, did you really want\nto run this program?&#8221; prompt\nand which is taken into account by applications\nso that they can do things like disable scripting\nand macros when they open the document, just in case the file is\nmalicious.\n<\/p>\n<p>\nToday&#8217;s Little Program is really three Little Programs:\nOne to read the zone identifier, one to set the zone identifier,\nand one to clear it.\n<\/p>\n<pre>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;atlbase.h&gt;\n#include &lt;urlmon.h&gt;\n#include &lt;stdlib.h&gt;\nint __cdecl wmain(int argc, wchar_t **argv)\n{\n if (argc &lt; 2) return 0;\n <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/05\/20\/135841.aspx\">CCoInitialize<\/a> init;\n CComPtr&lt;IZoneIdentifier&gt; spzi;\n spzi.CoCreateInstance(CLSID_PersistentZoneIdentifier);\n DWORD dwZone;\n if (SUCCEEDED(CComQIPtr&lt;IPersistFile&gt;(spzi)\n                   -&gt;Load(argv[1], STGM_READ)) &amp;&amp;\n     SUCCEEDED(spzi-&gt;GetId(&amp;dwZone))) {\n  printf(\"Zone identifier is %d\\n\", dwZone);\n } else {\n  printf(\"Couldn't get zone identifier (perhaps there isn't one)\\n\");\n }\n return 0;\n}\n<\/pre>\n<p>\nThe first program takes a file name on the command line\n(fully-qualified path, please)\nand prints the zone identifier associated with it.\nThe numeric values for the most commonly-encountered zone identifiers are\n<\/p>\n<table STYLE=\"border-collapse: collapse\" BORDER=\"1\" CELLPADDING=\"3\">\n<tr>\n<th>Identifier<\/th>\n<th>Value<\/th>\n<\/tr>\n<tr>\n<td><code>URLZONE_LOCAL_MACHINE<\/code><\/td>\n<td ALIGN=\"right\">0<\/td>\n<\/tr>\n<tr>\n<td><code>URLZONE_INTRANET<\/code><\/td>\n<td ALIGN=\"right\">1<\/td>\n<\/tr>\n<tr>\n<td><code>URLZONE_TRUSTED<\/code><\/td>\n<td ALIGN=\"right\">2<\/td>\n<\/tr>\n<tr>\n<td><code>URLZONE_INTERNET<\/code><\/td>\n<td ALIGN=\"right\">3<\/td>\n<\/tr>\n<tr>\n<td><code>URLZONE_UNTRUSTED<\/code><\/td>\n<td ALIGN=\"right\">4<\/td>\n<\/tr>\n<\/table>\n<p>\nNote also that if you want your application to be sensitive\nto the file zone (so that you can disable features\nfor untrusted documents),\nyou should use the\n<code>IInternet&shy;Security&shy;Manager::Map&shy;Url&shy;To&shy;Zone<\/code> function rather\nthan using only the file zone identifier,\nbecause the effective zone of a file is a combination of the\nfile&#8217;s declared zone as well as its physical location.\n(For example, a file in the Temporary Internet Files directory\nor on an untrusted server should not be given full trust\nregardless of what it claims.\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/ieinternals\/archive\/2011\/03\/23\/understanding-local-machine-zone-lockdown-restricted-this-webpage-from-running-scripts-or-activex-controls.aspx\">\nAdditional reading<\/a>.)\n<\/p>\n<p>\nHere&#8217;s a program that uses\n<code>IInternet&shy;Security&shy;Manager::Map&shy;Url&shy;To&shy;Zone<\/code>\nto determine the effective security zone:\n<\/p>\n<pre>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;atlbase.h&gt;\n#include &lt;urlmon.h&gt;\n#include &lt;stdlib.h&gt;\nint __cdecl wmain(int argc, wchar_t **argv)\n{\n if (argc &lt; 2) return 0;\n CCoInitialize init;\n CComPtr&lt;IInternetSecurityManager&gt; spism;\n spzi.CoCreateInstance(CLSID_InternetSecurityManager);\n DWORD dwZone;\n if (SUCCEEDED(spism-&gt;MapUrlToZone(\n                   argv[1],\n                   &amp;dwZone,\n                   MUTZ_ISFILE | MUTZ_DONT_UNESCAPE))) {\n  printf(\"Zone is %d\\n\", dwZone);\n } else {\n  printf(\"Couldn't get zone\\n\");\n }\n return 0;\n}\n<\/pre>\n<p>\nThe <code>MUTZ_IS&shy;FILE<\/code> flag\nsaves you the hassle of having to prepend\n<code>file:<\/code> in front of the path,\nbut you still have to pass a full path\nbecause the first parameter is a URL, not a path.\n<\/p>\n<p>\nOkay, that was a bit of a digression there.\nLet&#8217;s write another Little Program which changes the zone identifier.\n<\/p>\n<pre>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;atlbase.h&gt;\n#include &lt;urlmon.h&gt;\n#include &lt;stdlib.h&gt;\nint __cdecl wmain(int argc, wchar_t **argv)\n{\n if (argc &lt; 3) return 0;\n CCoInitialize init;\n CComPtr&lt;IZoneIdentifier&gt; spzi;\n spzi.CoCreateInstance(CLSID_PersistentZoneIdentifier);\n spzi-&gt;SetId(_wtol(argv[2]));\n CComQIPtr&lt;IPersistFile&gt;(spzi)-&gt;Save(argv[1], TRUE);\n return 0;\n}\n<\/pre>\n<p>\nThis program takes two parameters:\nA fully-qualified path and a zone (in integer form).\nIt applies the zone to the file,\noverwriting the existing zone if any.\n<\/p>\n<p>\nFinally, here&#8217;s a Little Program to remove the zone information\nfrom the file entirely.\nThis is the equivalent of clicking the\n<i>Unblock<\/i> button in the file property sheet.\n<\/p>\n<pre>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;atlbase.h&gt;\n#include &lt;urlmon.h&gt;\n#include &lt;stdlib.h&gt;\nint __cdecl wmain(int argc, wchar_t **argv)\n{\n if (argc &lt; 2) return 0;\n CCoInitialize init;\n CComPtr&lt;IZoneIdentifier&gt; spzi;\n spzi.CoCreateInstance(CLSID_PersistentZoneIdentifier);\n spzi-&gt;Remove();\n CComQIPtr&lt;IPersistFile&gt;(spzi)-&gt;Save(argv[1], TRUE);\n return 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you download a file via Internet Explorer, the file is tagged with a little bit of information known as a zone identifier which remembers where the file was downloaded from. This is what tells Explorer to put up the &#8220;Yo, did you really want to run this program?&#8221; prompt and which is taken into [&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":[25],"class_list":["post-2753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>When you download a file via Internet Explorer, the file is tagged with a little bit of information known as a zone identifier which remembers where the file was downloaded from. This is what tells Explorer to put up the &#8220;Yo, did you really want to run this program?&#8221; prompt and which is taken into [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2753","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=2753"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2753\/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=2753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}