Convert PowerShell script into non-readable format

情到浓时终转凉″ 提交于 2019-11-30 07:36:17

You can convert the script to Base64 encoding, so that it's not immediately readable. To convert a PowerShell script file to a Base64 String, run the following command:

$Base64 = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes('c:\path\to\script file.ps1'));

To launch the Base64-encoded script, you can call PowerShell.exe as follows:

powershell.exe -EncodedCommand <Base64String>

For example, the following command:

powershell.exe -EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgAC0ATwBiAGoAZQBjAHQAIAAiAEgAZQBsAGwAbwAsACAAdwBvAHIAbABkACEAIgA7AA==

Will return the following results:

Hello, world!

I tried the solution proposed by @TrevorSullivan, but it gave me error

The term '????' is not recognized as the name of a cmdlet, function,
script file or operable program...

As I found out later there was a problem with bad encoding. I found somewhere another approach and when I combined those two, I got working PS command:

$Base64 = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes([System.IO.File]::ReadAllText("script.ps1")))

Then I can redirect the result to file:

$Base64 > base64Script.txt

from where I just copy the encoded command and paste it here instead of <Base64String>:

powershell.exe -EncodedCommand <Base64String>

and it works without any problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!