如何在 Powershell 中将文件复制到 NTFS 备用数据流中?

问题描述 投票:0回答:1

我正在尝试为我在网络安全课程的教科书中看到的示例创建概念证明。我设置了所需的文件并尝试了它,但我只收到错误。

这是例子:

C:\> type C:\windows\system32\notepad.exe > c:\windows\system32\calc.exe:notepad.exe

这就是我所做的:

C:\> type c:\hello.exe > c:\README.txt:hello.exe

其中

hello.exe
是一个打印
hello world
的可执行文件,
README.txt
是一个文本文件,其中包含一些随机文本。

我得到的错误是:

out-file: The given path's format is not supported.

当第一个参数和重定向运算符之间有空格时,并且

Get-Content : A positional parameter cannot be found that accepts argument 'C:\README.txt:hello.exe'.

没有空间时。

我尝试将

type
替换为
cat
,添加和删除运算符之间的空格,在尝试导入数据之前创建备用流以及不同的文件类型。另外,切题的问题是,我无法让
::$DATA
处理 NTFS 文件。它只是给出了
cat : Cannot find path 'C:\hello.exe::' because it does not exist.

powershell windows-10 ntfs alternate-data-stream
1个回答
2
投票

在PowerShell中

type
cat
gc
都只是别名
Get-Content
,默认情况下与文本一起使用,所以显然你不能将它们用于二进制数据(在cmd中)
type
也只适用于文本)。要处理二进制文件,您必须使用
-Encoding Byte
-AsByteStream
告诉
Get-Content
不要将文件作为文本处理

此外,您不能使用重定向来保存输出,因为

>
只是
Out-File
的别名,它不支持流。您必须使用
-Stream
 中的 
Set-Content

选项指定要存储的流
Get-Content -Encoding Byte "C:\windows\system32\notepad.exe" | `
    Set-Content -Encoding Byte -Stream Notepad .\README.txt

事实上,PowerShell 中的许多 cmdlet,如

Get-Item
Get-Content
Remove-Item
Set-Content
Clear-Content
Add-Content
》都有
-Stream
在流上操作的选项
。不幸的是
Out-File
没有这个选项

另请参阅与备用数据流交互

© www.soinside.com 2019 - 2024. All rights reserved.