0

我正在尝试轻松ByPassPowerShells ExecutionPolicy。我意识到一个简单的解决方法是创建runme.ps1andscript.ps1并且runme.ps1我可以调用. 有没有办法把它放在脚本的“标题”中,让它在ing时调用自己?BypassExecutionPolicyscript.ps1BypassExecutionPolicy

运行me.ps1:

PowerShell.exe -ExecutionPolicy Bypass -File "C:\tmp\script.ps1"

脚本.ps1:

Write-Host "Hello World"
PAUSE

我目前正在研究某种“标志”或“tmpfile”逻辑并让脚本自行调用,但我想知道是否有一种已知/更好的方式,甚至可能的方式让它成为我所有脚本中的标题因此最终用户可以在没有提示的情况下“运行 w/powershell”。

欢迎补充详细说明的答案ExecutionPolicy,但让我们专注于这个问题。

讨论ExecutionPolicy应该集中在“Security Stack Exchange”上,相关帖子链接在这里:

https://security.stackexchange.com/questions/118553/whats-the-purpose-of-executionpolicy-settings-in-powershell-if-the-bypass

https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/

但是,重要的是要了解该设置从来都不是安全控制。相反,它旨在防止管理员在脚下开枪。

4

2 回答 2

1

您可以创建某种受信任的启动器(cmd 文件或 exe 文件),它将运行带有--ExecutionPolicy ByPass标志的 powershell。或者,您甚至可以更改操作行为double-click以始终使用 ByPass 策略标志运行 PowerShell。

但是,计算机上的设置可以由系统管理员在MachinePolicy\ExecutionPolicyor中进行强化UserPolicy\ExecutionPolicy,您将无法以正常方式覆盖它。

ExecutionPloicy 配置为 4+1 级别,优先级从高到低:

> Get-ExecutionPolicy -List

MachinePolicy (Group Policy)
   UserPolicy (Group Policy)
      Process (Configured using powershell -ExecutionPolicy flag for new process only)
  CurrentUser (User settings)
 LocalMachine (Computer settings)

当您使用 ByPass 标志运行 PowerShell 时,您实际上设置Process了覆盖CurrentUser和设置的LocalMachine-level ExecutionPolicy,但可以在本地或域组策略管理UserPolicy或级别进行强化。MachinePolicy


更好的方法是使用组策略配置用户策略以允许仅运行AllSignedRemoteSigned脚本,生成New-SelfSignedCertificate -Type CodeSigning100 年的证书,根据计算机使用 GPO 部署它,并使用您部署给用户的每个脚本进行Trusted Publisher签名。Set-AuthenticodeSignature

于 2020-08-22T00:11:13.743 回答
0

TLDR;

PowerShell.exe -ExecutionPolicy Bypass -File "C:\circumvent\industry\standard.ps1" 2>&1>$null

我想分享脚本并能够说“右键单击并运行 w/powershell”,这已经比我说“双击文件”的批处理脚本更糟糕(人们总是得到那个!)。

解决方案来找我是因为我的PAUSE主脚本中有一个读取控制台输出的内容,我注意到在我的主脚本调用之后,我收到了来自“主/父”脚本script.ps1的附加提示。PAUSE这让我意识到,调用子脚本后父脚本能够继续。因此,call nonexistent script and pipe output to null!继续愉快地前进。

示例场景:

重新启动后,以下脚本不会通过“右键单击,使用 PowerShell 运行”运行,我得到了标准的“执行策略提示”:

脚本.ps1

Write-Host "Calling Scripts? No Problem!"
PAUSE

重新启动后以下工作:

PowerShell.exe -ExecutionPolicy Bypass -File "C:\circumvent\industry\standard.ps1" 2>&1>$null
ECHO "This Script Won't Run Without Line 1" 
ECHO "I had fun to try to circumvent an industry standard" 
ECHO "I Learned a lot about PowerShell ExecutionPolicy"
C:\tmp\script.ps1
PAUSE

结果:

This Script Won't Run Without Line 1
I had fun to try to circumvent an industry standard
I Learned a lot about PowerShell ExecutionPolicy
Calling Scripts? No Problem!
Press Enter to continue...:

根据@BACON 的评论进行更新,这确实只有通过“上下文菜单”通过“使用 powershell 运行”才能实现。我尝试将“powershell”设置为默认应用程序.ps1,但它不仅不起作用,而且上下文菜单删除了“使用 powershell 运行”选项!

值得庆幸的是,最终用户将拥有默认设置和/或系统管理员将知道如何解决。

我最初没有测试的东西,但想知道这个解决方案的真正“规避”是尝试PowerShell.exe -ExecutionPolicy Bypass在标题中使用。这导致脚本无法运行,因此必须为其分配一个-File但如果 File 不存在并允许脚本继续执行则无效。

于 2020-08-22T03:24:12.777 回答