【问题标题】:Running a self decrypting base64 powershelll script locally with powershell -file /path/to/ps1使用 powershell -file /path/to/ps1 在本地运行自解密 base64 powershell 脚本
【发布时间】:2020-02-03 18:33:40
【问题描述】:

我想将我的 powershell 脚本保留在 base64 中的本地服务器上,但是当从 schtasks 运行或在本地使用 powershell -file /path/to/ps1 运行时,它们会自行解码。这可能吗??

我试过了:

函数解码 { $data = 'base 64 script'

[系统.文本.编码] ::ASCII.GetString([System.Convert]::FromBase64String($data))}

解码

这不起作用。有什么想法吗?

【问题讨论】:

  • 出于好奇,这是什么驱动?我按照建议使用了一次-EncodedCommand,但这是因为我正在生成我的代码。

标签: powershell base64


【解决方案1】:

我认为这种情况至少有两种选择。一种选择是使用-EncodedCommand 参数将base64 编码的命令发送到Powershell.exe。第二个选项是创建您的解码脚本以接受另一个包含 base64 编码命令作为参数值的脚本。


选项 1:传递编码命令

这假定您的 base64 编码命令是使用 UTF-16LE 字符编码 (Unicode) 格式化的 PowerShell 命令的字符串版本。我们还假设您有一个名为 Encoded.ps1 的脚本,其中包含您的 base64 编码命令。满足先决条件后,您可以执行以下操作:

Powershell.exe -EncodedCommand (Get-Content Encoded.ps1)

选项 2:针对编码脚本运行解码脚本

在这种情况下,unicode 要求无关紧要(如果您愿意,可以使用 ANSI)。您只需要知道您的原始命令字符串编码,就可以正确解码它。我们将假设 ASCII 字符集。我们还假设 Encoded.ps1 包含您的 base64 编码命令。

首先,让我们创建名为 Decode.ps1 的解码脚本。

# Decode.ps1
param([string]$FilePath)

$64EncodedData = Get-Content $FilePath  
$DecodedData = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($64EncodedData))

& ([scriptblock]::Create($DecodedData))

其次,我们运行Powershell.exe命令解码Encoded.ps1并执行解码后的命令。

Powershell.exe -File Decoded.ps1 -FilePath Encoded.ps1

上面的代码不是为了显示解码后的命令的内容,而是执行解码后的命令。 $FilePath 是您的 Encoded.ps1 文件的路径,其中包含来自 ASCII 编码字符集的 base64 编码字符串。您可以在Decode.ps1 文件中更改为适用于您的情况的任何编码。 $DecodedData 包含原始命令字符串。最后,创建一个包含$DecodedData 的脚本块,然后使用调用运算符& 调用。

【讨论】:

  • 感谢 AdminOfThings,非常有帮助。我会把你的答案标记为正确的!非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 2021-06-30
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多