将文件创建为 "file.ext:file.ext" 的真正作用是什么?

What does creating a file as "file.ext:file.ext" really do?

我最近开始研究隐写术,我在网上找到了一个教程,其中为了将新的文本文件隐藏在另一个文件中,教程提供者使用了类似于以下命令的内容:

    notepad.exe file.ext:textfile.txt

我发现命令行中的 : 很奇怪:记事本将其识别为文件的有效索引,并且原始文件 (file.ext) 现在增加了 space在磁盘上包含来自新文本文件的数据。由于我在学术生活中知之甚少并且很少使用cmd,所以我想知道这里发生了什么。这是什么功能?它的预期用途是什么?这是 windows cmd 独有的还是在 UNIX 上有等效项?

TL;DR:

您正在查看的是 NTFS 文件系统 (FS) 中文件的备用数据流 (ADS)。

详情:

在较旧的操作系统 (OS) 中,文件系统 (FS) 中的一个条目代表一组数据,这意味着文件只是一个文件。较新的 OSes 具有现代 FS,它允许一个条目代表一组或多组数据。在 NTFS 中,这些被称为流,而在其他 OSes 中,这些通常被称为分叉。对于此解释,这两个术语是同义词。

在今天的 FS 中,每个文件至少有 1 个流。第一个流没有名称,类型为 $DATA。第一个流有时称为主要流、默认流或匿名流。第一个之后的所有 ADS 都将具有名称和类型。默认和最常见的流类型是 $DATA.

流的全名格式如下:

<filename>:<stream name>:<stream type>

用法:

在Windows中(因为你提到了notepad.exe所以关注那里),ADS有很多用途。人们最常与之交互(甚至没有意识到)的 ADS 是 Zone.Identifier,它被添加到由 Internet Explorer 和其他一些浏览器下载的文件中。这个额外的,这个额外的数据流被 OS 用作 'potentially unsafe to run' 的标志。同样,MS Office 应用程序将使用相同的流在打开可能包含恶意宏的文档时警告用户。在所有这些情况下,用户都会收到警告,但不会阻止其打开危险文件。

枚举工具(示例,并不详尽):

dir /r 来自 cmd.exe

Streams.exe 来自 SysInternals

Get-Item 来自 powershell.exe

演示:创建、查看、读取、删除

c:\temp> dir /r ads_test*
File Not Found
c:\temp> echo this is normal text>ads_test.txt
c:\temp> dir /r ads_test*
04/11/2019  01:11 AM                21 ads_test.txt
c:\temp> echo this is text for an ADS>ads_test.txt:myHiddenAds
c:\temp> dir /r ads_test*
04/11/2019  01:12 AM                21 ads_test.txt
                                    25 ads_test.txt:myHiddenAds:$DATA
c:\temp> dir ads_test*
04/11/2019  01:12 AM                21 ads_test.txt
c:\temp> more < ads_test.txt
this is normal text
c:\temp> more < ads_test.txt:myHiddenAds
this is text for an ADS
c:\temp> type nul 2>ads_test.txt:myHiddenAds
c:\temp> dir /r ads_test*
04/11/2019  01:20 AM                21 ads_test.txt
                                     0 ads_test.txt:myHiddenAds:$DATA
c:\temp> echo this is yet another ADS>ads_test.txt:CashMeOutside
c:\temp> dir /r ads_test*
04/11/2019  01:24 AM                21 ads_test.txt
                                    25 ads_test.txt:CashMeOutside:$DATA
                                     0 ads_test.txt:myHiddenAds:$DATA
c:\temp> powershell.exe -c "& {get-item -path 'c:\temp\ads_test.txt' -stream * | ft -property FileName,Stream,Length}"
FileName             Stream        Length
--------             ------        ------
C:\temp\ads_test.txt :$DATA            21
C:\temp\ads_test.txt CashMeOutside     25
C:\temp\ads_test.txt myHiddenAds        0
c:\temp> powershell.exe -c "& {remove-item -path 'c:\temp\ads_test.txt' -stream myHiddenAds}"
c:\temp> powershell.exe -c "& {get-item -path 'c:\temp\ads_test.txt' -stream * | ft -property FileName,Stream,Length}"
FileName             Stream        Length
--------             ------        ------
C:\temp\ads_test.txt :$DATA            21
C:\temp\ads_test.txt CashMeOutside     25

其他用途:

虽然不常见,但目录也可以有 ADS。对于目录,没有默认数据流,但有默认目录流。目录是流类型 $INDEX_ALLOCATION。 $INDEX_ALLOCATION 类型(目录流)的默认流名称是 $I30。尽管目录没有默认数据流,但它们可以有命名数据流。

问题:

近年来,由于ADS被不法分子使用和滥用来编写隐藏数据、存储病毒和保持持久性,因此名声不佳。即使在今天,与 ADS 相比,许多现代病毒扫描程序也能更有效地检测来自主流的威胁。 Microsoft Defender、高级威胁防护和 SmartScreen 可以像从主流中一样有效地检测 ADS 威胁。

Demo2 - 不良行为者如何使用 ADS 的无害示例

C:\temp> echo asdf > \?\c:\temp\COM1.txt
C:\temp> type c:\windows\system32\calc.exe> \?\c:\temp\COM1.txt:TotallyNotMalware.exe
C:\temp> wmic process call create "\?\c:\temp\COM1.txt:TotallyNotMalware.exe"
C:\temp> dir /r
04/11/2019  01:30 AM                21 ads_test.txt
                                    25 ads_test.txt:CashMeOutside:$DATA
04/11/2019  02:45 AM                 7 COM1.txt

C:\temp> rem Notice above that the ADS doesn't show - This is because "COM1" is a system reserved name, and many internal and 3rd party programs deal with it wrong.

补充读物:

Miocrosoft - Windows protocols

Winitor - NTFS Alternate Data Streams

Enigma0x3 - Using alternate date streams to persist on a compormised machine