Encode long command in unique command

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of asdasdasdsd asdasdasdsd 8 months, 1 week ago.

  • Author
  • #47140
    Profile photo of asdasdasdsd

    I'm trying to encode in base64 a long command in PowerShell, I'm trying to automate this:

    $command = "(New-Object System.Net.WebClient).DownloadFile('http://localhost/update_program.exe','updater.exe'); Start-Process 'updater.exe'"
    $bytes = [Text.Encoding]::Unicode.GetBytes($command)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    echo $encodedCommand

    The problem is that it is impossible to put all this in a command. I tried:

    powershell $command = (New-Object System.Net.WebClient).DownloadFile('http://localhost/update_program.exe','updater.exe'); Start-Process 'updater.exe'";$bytes = [Text.Encoding]::Unicode.GetBytes($command);$encodedCommand = [Convert]::ToBase64String($bytes);echo $encodedCommand;

    But it executes the contents of the string $command as a command and does not show the encoded command. How can I achieve this by using a single statement passed to powershell as in the second code snippet above?

  • #47144
    Profile photo of Dave Wyatt
    Dave Wyatt

    It seems kind of silly to want to pass the unencoded command as an argument to PowerShell.exe just to have it spit out the encoded command. The point of encoding the command is to avoid all the quoting / escaping nonsense that you'd have to do to get the unencoded version working properly. 🙂

    Why not just launch powershell.exe and paste in those commands, rather than trying to do it with arguments to powershell.exe? Then, once that's done, you can use the encoded representation in later calls:

  • #47146
    Profile photo of asdasdasdsd

    It is a program I'm doing, the user enters a link and returns the encoded for powershell command, is what I'm trying to do, getting to the command to display the contents encoded in a single line, the issue is that it is impossible , and try base64 encoding commands and UTF16LE but it became impossible for me.
    This is the plan B, if I can do all this in an already would end command

  • #47197
    Profile photo of Curtis Smith
    Curtis Smith

    You are missing a ", the -command parameter and your curly braces {}

    powershell.exe -command {$command = "(New-Object System.Net.WebClient).DownloadFile('http://localhost/update_program.exe','updater.exe'); Start-Process 'updater.exe'";$bytes = [Text.Encoding]::Unicode.GetBytes($command);$encodedCommand = [Convert]::ToBase64String($bytes);echo $encodedCommand;}


    You are going to have a hard time, however, if the command you are trying to encode has " in it. You will have to have your logic escape the " character before passing it.

    • This reply was modified 8 months, 1 week ago by Profile photo of Curtis Smith Curtis Smith.
  • #47224
    Profile photo of asdasdasdsd

    thanks for the help, but only probe your command returns me the same command uncoded, I have some outdated version of powershell?

  • #47234
    Profile photo of Peter Jurgens
    Peter Jurgens

    Can you give a bit more detail about your intended solution? What language are you writing this "program" in? How would users execute the program? etc...

    In any case I'm not sure I see the reason for executing powershell.exe if you are only looking to return the encoded command, presumably for users to copy and run with "powershell.exe -encodedcommand".

    If that is the end goal, your workflow should simply be accept input, convert input to base64 and output the result. Again, no reason to execute powershell.exe, as the encoding is not language specific and could be done in any language that you have chosen to write your "program" in.

  • #47365
    Profile photo of asdasdasdsd

    Simple , as that made Curtis Smith , only if it returns the command encoded in base64

    • This reply was modified 8 months, 1 week ago by Profile photo of asdasdasdsd asdasdasdsd.

You must be logged in to reply to this topic.