0

请看以下网址:网址

现在它说以下关于下载的脚本:

“运行从 Internet 下载且未签名的脚本,如果脚本未阻止,例如使用 Unblock-File cmdlet。”

我刚刚从 technet 库 (PS2EXE) 下载了一个脚本,我可以在不使用 Unblock_file cmdlet 的情况下运行包含的测试脚本。到底是怎么回事?我误解了微软告诉我的内容还是这是一个小故障?

4

1 回答 1

3

help unblock-file

在内部,Unblock-File cmdlet 删除 Zone.Identifier 备用数据流,其值为“3”,表示它是从 Internet 下载的。

文件是“远程”或“来自互联网”的想法是本地计算机文件系统上的数据,必须由下载文件的工具放置在那里,在下载过程中它不包含在文件中。

如果你通过 Internet Explorer 下载了一个文件,可能是 FireFox、Invoke-WebRequest,这些都会添加它。如果您使用其他内容下载,该工具可能不会添加此备用流。

看看它的行为:

# Show folder is empty
PS C:\temp\> Get-ChildItem


# Make a test script which prints Hello World, and run it
PS C:\temp\> "'Hello World'" | Set-Content -Path .\test.ps1
PS C:\temp\> .\test.ps1
Hello World


# Show the file exists
PS C:\temp\> Get-ChildItem

    Directory: C:\temp\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       01/08/2018     22:07             15 test.ps1


# Add the Zone Identifier alternate data stream
PS C:\temp\> "[ZoneTransfer]`nZoneId=3" | Set-Content -Path 'test.ps1' -Stream 'Zone.Identifier'


# Show that it doesn't appear in a normal directory listing:
PS C:\temp\> Get-ChildItem

    Directory: C:\temp\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       01/08/2018     22:08             15 test.ps1



# Show how it blocks the file from running
PS C:\temp\> .\test.ps1
.\test.ps1 : File C:\temp\test.ps1 cannot be loaded. The file C:\temp\test.ps1 is not digitally signed. You cannot
run this script on the current system. For more information about running scripts and setting execution policy, see
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\test.ps1
+ ~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess


# Show file content
PS C:\temp\> Get-Content -Path .\test.ps1
'Hello World'


# Show alternate data stream content
PS C:\temp\> Get-Content -Path .\test.ps1 -Stream 'Zone.Identifier'
[ZoneTransfer]
ZoneId=3


# Unblock-File removes this alternate stream
PS C:\temp\> Unblock-File .\test.ps1


# Script runs again
PS C:\temp\> .\test.ps1
Hello World

所以主要的问题是,如果你运行Get-Content file.ps1:Zone.Identifier并看到 ZoneId 是3 并且仍然可以运行脚本,并且 Get-ExecutionPolicy是 RemoteSigned,那么你有一些奇怪的事情发生了。

但我的猜测是下载工具没有添加这个数据,所以文件看起来就像一个本地创建的。

注意。RemoteSigned 并非旨在成为一项安全功能,它旨在成为“在阅读脚本并故意选择运行它们之前帮助防止意外运行脚本”检查,例如“您确定吗?” 框,不像密码提示。

于 2018-08-01T21:14:52.873 回答