KahLeon
KahLeon

Reputation: 29

How to run a large base64 encoded file via powershell

I have a powershell.ps1 script that I performed base64 encoding upon as below

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

Now I have stored this output to base64.txt file.

I tried to launch this script as below via CMD,

powershell.exe -EncodedCommand (Base64String)

But I ended up in the below error

Cannot process the command because the value specified with -EncodedCommand is not properly encoded. The value must be Base64 encoded.

I realized that the CMD is not taking the entire (Base64String). The full length of my (Base64String) is 11,133 characters. But CMD is accepting only 8160 characters.

Is there any way or workaround to run this base64 encoding?

Thanks in advance.

Upvotes: 1

Views: 7343

Answers (1)

gmelodie
gmelodie

Reputation: 442

This worked for me (myscript.ps1 contains the base64 encoded command):

powershell -encodedcommand (Get-Content 'myscript.ps1' -Raw)

Which is very similar to what you would do in Bash:

$ powershell -encodedcommand `cat myscript.ps1`

Obs: Addressing some comments, this is sometimes indeed needed. My particular use case was to do a reverse shell while dodging an AV on a windows machine that was detecting my plaintext shell code.

Upvotes: 2

Related Questions