2011年4月28日星期四

Streams in NTFS

使用Windows 7后发现,从浏览器下载的文件的属性多了些东西,例如:
"This file came from another computer and might be blocked to help protect this computer"
IE,Google Chrome,Firefox都支持这个功能。这个属性不会随着位置的移动和拷贝而丢失。"Unblock"之后的文件与原文件的各种哈希校验也一致。How Windows managed to track such kind of identity?

这个特性来自于NTFS的Streams概念。存放着数据之外的数据。打开一个命令行,执行以下命令:

echo Hello, Streams >test:stream
more <test:stream
第1条命令会创建一个test文件,文件的大小为0. 第2条命令会显示结果“Hello, Streams”。重定向符冒号后边的即是流的名称了,每个文件可以有多个。上边提到的识别功能即是利用NTFS Stream这种“数据之外的数据”做到的。

使用链接[1]里提供的工具,可以查看到一个文件包含的流信息。用那个工具检查从浏览器里下载的文件,有这样的结果:

F:\>streams breathe-life.jpg

Streams v1.56 - Enumerate alternate NTFS data streams
Copyright (C) 1999-2007 Mark Russinovich
Sysinternals - www.sysinternals.com

F:\breathe-life.jpg:
   :Zone.Identifier:$DATA       24

F:\>more <breathe-life.jpg:Zone.Identifier
[ZoneTransfer]
ZoneId=3
从链接[2]我们可以知道流的命名规范为:filename:stream name:stream type,所以呢,Zone.Identifier就是流的名字了,$DATA即流的类型。那么,如果清空掉这个流的信息会怎么样呢?其实就相当于“Unblock”操作了。

F:\>set /pvar=<nul >breathe-life.jpg:Zone.Identifier
在链接[3]里边有一个写流的例子,用C也可以的,就写了一个。
#include <stdio.h>

int main()
{
    FILE *fout;

    fout = fopen("test:stream", "wb");
    fprintf(fout, "Hello Stream Through Pure C.\n");
    fclose(fout);
    return 0;
}

链接[5]是比较全的一篇介绍,Data Streams是NTFS的特性,所以,当文件从NTFS向FAT转移时,流的信息会丢失。链接[6]来自维基,对Streams的背景进行介绍,在文件系统里边,除我们看到的常规文件属性信息外,还有一种叫做fork的信息,fork信息最大的不同是它没有大小限制,甚至可以比正常数据更大,Streams即是NTFS里的fork。


链接:
[1]. Streams v1.56,http://technet.microsoft.com/en-us/sysinternals/bb897440.aspx
[2]. File Streams,http://msdn.microsoft.com/en-us/library/aa364404(VS.85).aspx
[3]. Using Streams,http://msdn.microsoft.com/en-us/library/bb540537(v=VS.85).aspx
[4]. http://superuser.com/questions/38476/this-file-came-from-another-computer-how-can-i-unblock-all-the-files-in-a
[5]. Practical Guide to Alternative Data Streams in NTFS,http://www.irongeek.com/i.php?page=security/altds
[6]. Fork (file system),http://en.wikipedia.org/wiki/Fork_(filesystem)

没有评论:

发表评论