{"id":1120,"date":"2011-01-01T23:51:50","date_gmt":"2011-01-02T07:51:50","guid":{"rendered":"http:\/\/www.curlybrace.com\/words\/?p=1120"},"modified":"2011-01-01T23:51:50","modified_gmt":"2011-01-02T07:51:50","slug":"alternate-data-streams","status":"publish","type":"post","link":"https:\/\/www.curlybrace.com\/words\/2011\/01\/alternate-data-streams\/","title":{"rendered":"Alternate Data Streams (Metadata) on Files in NTFS"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Alternate Data Streams (ADS) allow arbitrary metadata to be associated with files and directories on Windows NTFS.  Alternate data streams are the Windows implementation of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fork_%28filesystem%29\">forks<\/a>.  The apparent size of the file will be unchanged, and most applications and users are unaware of their existence.  If a file is moved, any alternate data stream will move along with it, as long as the destination is on an NTFS drive.<\/p>\n<p>The command line can access alternate data streams using redirection operators.  Streams are specified on the command line as <i>filename<\/i><b>:<\/b><i>stream name<\/i>.<\/p>\n<h3>Creating an Alternate Data Stream<\/h3>\n<p>As an example, a string is written into an ADS named <tt>hidden<\/tt>, which is associated with file <tt>test.txt<\/tt>:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>echo Hidden text > test.txt:hidden<\/pre>\n<\/blockquote>\n<p>The file appears to be empty, though as detailed below, the metadata is intact and associated with the file:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>dir test.txt\r\n\r\n06\/24\/2010  01:33 PM                 0 test.txt<\/pre>\n<\/blockquote>\n<h3>Viewing an Alternate Data Stream<\/h3>\n<p>The metadata can be viewed by redirecting from it to <tt>more<\/tt>:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>more < test.txt:hidden\r\nHidden text<\/pre>\n<\/blockquote>\n<p>The name and content of the ADS can be anything (see 'Details' below for restrictions):<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>echo Arbitrary string > test.txt:arbitraryName\r\n\r\nC:\\test>more < test.txt:arbitraryName\r\nArbitrary string<\/pre>\n<\/blockquote>\n<h3>Listing Files With Alternate Data Streams<\/h3>\n<p>On Windows Vista and later, a list of alternate data streams can be obtained using <tt>DIR \/R<\/tt>:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>dir test.txt \/R\r\n\r\n06\/24\/2010  01:33 PM                 0 test.txt\r\n                                    38 test.txt:arbitraryName:$DATA\r\n                                    28 test.txt:hidden:$DATA<\/pre>\n<\/blockquote>\n<p>On earlier operating systems, the SysInternals utility <a href=\"http:\/\/technet.microsoft.com\/en-us\/sysinternals\/bb897440\">Streams<\/a> can be used:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>c:\\tools\\SysInternals\\streams.exe test.txt\r\n\r\nStreams v1.56 - Enumerate alternate NTFS data streams\r\nCopyright (C) 1999-2007 Mark Russinovich\r\nSysinternals - www.sysinternals.com\r\n\r\nC:\\test\\test.txt:\r\n   :arbitraryName:$DATA 38\r\n          :hidden:$DATA 28<\/pre>\n<\/blockquote>\n<h3>Alternate Data Streams on Directories<\/h3>\n<p>Metadata can be added to directories the same way it's added to files:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>mkdir test2\r\n\r\nC:\\test>echo ADS on a directory > test2:someText\r\n\r\nC:\\test>dir \/r\r\n\r\n06\/25\/2010  11:27 PM    &lt;DIR&gt;          .\r\n06\/25\/2010  11:27 PM    &lt;DIR&gt;          ..\r\n06\/25\/2010  11:27 PM    &lt;DIR&gt;          test2\r\n                                    42 test2:someText:$DATA\r\n\r\nC:\\test>more < test2:someText\r\nADS on a directory<\/pre>\n<\/blockquote>\n<h2>Details<\/h2>\n<h3>Stream Naming<\/h3>\n<p>To be more accurate, streams are specified as <i>filename<\/i><b>:<\/b><i>stream name<\/i><b>:<\/b><i>stream type<\/i>.  It appears that the only stream type accessible from the command line is $DATA, which is why it's optional.  All of the stream types are listed in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa362667%28v=VS.85%29.aspx\">WIN32_STREAM_ID structure documentation<\/a>.  The default data stream is unnamed, so <i>filename<\/i>::$DATA will contain the file's data:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>echo This is the file > file.txt\r\n\r\nC:\\test>echo This is the stream > file.txt:stream\r\n\r\nC:\\test>more < file.txt::$DATA\r\nThis is the file\r\n\r\nC:\\test>more < file.txt:stream:$DATA\r\nThis is the stream<\/pre>\n<\/blockquote>\n<p>Stream names are generally held to the same requirements as any filename.  One interesting difference is that stream names can contain characters whose integer representations are in the range from 1 through 31.  Refer to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa365247%28v=VS.85%29.aspx\">Naming Files, Paths, and Namespaces<\/a> (MSDN) for details.<\/p>\n<p>Note that when using streams with files having a single letter name, the filename should be prefixed with a period and backslash.  The reason for this is Windows drive names.  For example, does \"<tt>echo hello > c:test<\/tt>\" refer to a stream named <tt>test<\/tt> on file <tt>c<\/tt>, or does it refer to a file <tt>test<\/tt> on drive <tt>c<\/tt>?<\/p>\n<h3>Executing Streams<\/h3>\n<p>As of Windows Vista, it is no longer possible to execute directly from an alternate data stream.  On Windows XP and earlier, the Start command was used, similar to <tt>start somefile.ext:hiddenExecutable<\/tt>.<\/p>\n<h3>Editing with Notepad<\/h3>\n<p>Notepad can be used to create and edit alternate data streams.  The File Open dialog doesn't recognize stream syntax, however, so the file must be created and opened using command line parameters.  Notepad will insist on appending <tt>.txt<\/tt> to the stream name.<\/p>\n<h3>Programmatic Access<\/h3>\n<p>Microsoft provides a <a href=\"http:\/\/support.microsoft.com\/kb\/105763\">sample program<\/a> in C++, demonstrating how to open and write to an alternate data stream.<\/p>\n<h2>Real-World Applications<\/h2>\n<h3>Downloaded Executables<\/h3>\n<p><a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2007\/08\/27\/4580767.aspx?PageIndex=4\">Since Windows XP SP2<\/a>, when a file is downloaded from the Internet and executed (assuming a zone-aware browser), this warning is displayed:<\/p>\n<blockquote><p><a href=\"http:\/\/www.curlybrace.com\/words\/wp-content\/uploads\/2010\/06\/UAC_Example_s.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.curlybrace.com\/words\/wp-content\/uploads\/2010\/06\/UAC_Example_s.png\" alt=\"\" title=\"UAC_Example_s\" width=\"320\" height=\"192\" class=\"aligncenter size-full wp-image-1127\" srcset=\"https:\/\/www.curlybrace.com\/words\/wp-content\/uploads\/2010\/06\/UAC_Example_s.png 320w, https:\/\/www.curlybrace.com\/words\/wp-content\/uploads\/2010\/06\/UAC_Example_s-300x180.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><\/p><\/blockquote>\n<p>Windows displays this warning because the web browser tagged the executable with a alternate data stream named <tt>Zone.Identifier<\/tt>:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>dir \/r setup.exe\r\n\r\n06\/25\/2010  12:10 PM           680,467 setup.exe\r\n                                    26 setup.exe:Zone.Identifier:$DATA<\/pre>\n<\/blockquote>\n<p>By redirecting this stream to <tt>more<\/tt>, we can see its contents:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\test>more < setup.exe:Zone.Identifier\r\n[ZoneTransfer]\r\nZoneId=3<\/pre>\n<\/blockquote>\n<p>The <a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2007\/03\/07\/how-does-the-remotesigned-execution-policy-work.aspx\">PowerShell blog<\/a> has more information on zone identifiers.<\/p>\n<h3>Viruses<\/h3>\n<p>The W2K.Stream virus <a href=\"http:\/\/www.symantec.com\/security_response\/writeup.jsp?docid=2000-121416-2928-99\">used alternate data streams<\/a>.<\/p>\n<h2>Additional Resources<\/h2>\n<ul>\n<li \/><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms810604.aspx\"><del>A Programmer's Perspective on NTFS 2000 Part 1: Stream and Hard Link<\/del><\/a> (MSDN, article removed)\n<li \/><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms810500.aspx\"><del>A Programmer's Perspective on NTFS 2000 Part 2: Encryption, Sparseness, and Reparse Points<\/del><\/a> (MSDN, article removed)\n<li \/><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa364404%28v=VS.85%29.aspx\">File Streams<\/a> (MSDN)\n<li \/><a href=\"http:\/\/www.codeproject.com\/KB\/shell\/csadsdetectorarticle.aspx\">Visual browsing of alternative data-streams in Windows Explorer<\/a> (CodeProject)\n<li \/><a href=\"http:\/\/www.alex-ionescu.com\/NTFS%20Alternate%20Data%20Streams.pdf\">NTFS Alternate Data Streams<\/a> (Alex Ionescu)\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Alternate Data Streams (ADS) allow arbitrary metadata to be associated with files and directories on Windows NTFS. Alternate data streams are the Windows implementation of forks. The apparent size of the file will be unchanged, and most applications and &hellip; <a href=\"https:\/\/www.curlybrace.com\/words\/2011\/01\/alternate-data-streams\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[261,193,15,283],"tags":[],"class_list":["post-1120","post","type-post","status-publish","format-standard","hentry","category-cplusplus","category-scripting","category-technology","category-windows-technology"],"_links":{"self":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/1120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/comments?post=1120"}],"version-history":[{"count":71,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/1120\/revisions"}],"predecessor-version":[{"id":1521,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/1120\/revisions\/1521"}],"wp:attachment":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/media?parent=1120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/categories?post=1120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/tags?post=1120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}