我听我同学说,windows上运行Powershell恶意脚本很难检测,所以我猜这里windows禁止运行该脚本?

报错就是无法运行该.psl脚本

PS xxxxxxxxxxxxxxxxxxxxxxxx> ./xx/xx.ps1
./xx/xx.ps1 : 无法加载文件 
X:\XXXXXX\xxxxx.ps1,因为在此系统上禁止运行脚本。
有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 
中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ ./xx/xx.ps1
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

我参考了本文:
https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

提取重要信息,感觉就是这个表格:

组策略执行策略
允许单个命令,但不允许脚本,阻止运行 .ps1xml .psm1 所有脚本文件Restricted
允许所有脚本Unrestricted
允许本地脚本和远程签名的脚本RemoteSigned
仅允许签名脚本AllSigned

点开一个 powershell 脚本,看下本机的执行策略:

PS XXXXX> get-executionpolicy
Restricted

当然,基本上都是 Restricted 才不会让 powershell 执行

咱要修改执行策略,需要开启一个管理员模式的Powershell
在这里插入图片描述

输入:

set-executionpolicy remotesigned

在这里插入图片描述
输入Y即可

如果返回的是:

PS XX> set-executionpolicy remotesigned
set-executionpolicy : 对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell”的
访问被拒绝。 要更改默认(LocalMachine)作用域的执行策略,请使用“以管理员身份运行”选项启动 Windows PowerShell。要更改当
前用户的执行策略,请运行 "Set-ExecutionPolicy -Scope CurrentUser"。
所在位置 行:1 字符: 1
+ set-executionpolicy remotesigned
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

在这里插入图片描述
那就是没成功启动管理员模式,重新试一下

最后

必须说一下,在进行完最后的 psl 脚本之后,为了安全起见,一定要在将这个模式改成 Restricted

set-executionpolicy restricted
> get-executionpolicy
Restricted

这就OK了

有参考自:
https://www.jianshu.com/p/4eaad2163567

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐