Powershell执行策略remotesigned矛盾?

时间:2018-08-01 20:28:06

标签: powershell security executionpolicy

请查看以下URL:URL

现在,它对已下载的脚本说以下内容:

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

我刚刚从technet画廊(PS2EXE)下载了一个脚本,并且可以运行包含的测试脚本,而无需使用Unblock_file cmdlet。到底是怎么回事?我是不是误解了微软在告诉我什么,或者这是一个小故障?

1 个答案:

答案 0 :(得分:3)

help unblock-file

  

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

文件“远程”或“来自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 并且仍然可以运行脚本 { {1}}是RemoteSigned,那么您发生了一些奇怪的事情。

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

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

相关问题