PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
43.55k stars 7.06k forks source link

Incorrect output when calling PowerShell's Invoke function in C# #21533

Open H4de5-7 opened 3 weeks ago

H4de5-7 commented 3 weeks ago

Prerequisites

Steps to reproduce

I used Nuget to download and use the official Microsoft.PowerShell.SDK (7.1.0) and System.Management.Automation (7.1.0). The purpose is to convert a piece of obfuscated code to origianl string. However, this code will truncate part of the execution results. But when I use PowerShell7 and PowerShell5 to call the invoke method in powershell environment$PowerShell = [powershell]::Create();[void]$PowerShell.AddScript(......);$PowerShell.Invoke(), I can get the correct results. I suspect there are bugs in the officially provided SDK and Autoumation packages for C#.

using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace ClassLibrary2
{
    public class Class1
    {
        static void Main(string[] args)
        {
            PowerShell psInstance = PowerShell.Create();
            string script = "(NeW-oBjeCt  MAnaGEment.AUtOmATiOn.pScReDeNtiAL ' ', ( '76492d1116743f0423413b16050a5345MgB8AHIAdgA3AFYATgBoAFMAUwBzAEMATgBRAFEAUgB2AGcASwA2AE4AagBlAEEAPQA9AHwANwBjADAAMgBkADcAYwAyADQANgA5ADUANwBlAGMANwBiAGEANQAwADMANwAzAGMAOQBlADkANwBjAGQAMQBlAGUAZAA3AGIAMQBjADQAYwBhAGMAMQAzADYANgBjADQAYwBjADkAOABmAGUAMABhADgANQBmADAANwBkADQAYQBjAGMAZgBjAGIAMAAyAGUANgBkADQAZAAyAGEAMgBiADkAYwA4ADAAZQA4AGUAYwAwAGMAYwBiAGUAZQA3ADgAZABmADYAYgAwADUAMAA0ADIAYgBjAGYANwA1ADcANwA5ADgAYgAwADAAZgA1ADUAMQAyADMANgBlAGYAOABjADkANwA0ADgANgAzADkANAA5ADQAOQBkAGMAMgAwADcANQA1AGMAMQAwADUANgA2ADEANgBlADYANQA2AGMAMQA='| CoNvertTO-SEcuREStrInG -KEY  (216..185))).getNEtWorkCrEdEntIAL().PAsSWord";
            psInstance.AddScript(script);
            Collection<PSObject> psOutput;
            psOutput = psInstance.Invoke();
        }
    }
}

Expected behavior

hello123456789hi123456789go123456789

Actual behavior

hello123456789hi123456789go12345

Error details

No response

Environment data

Microsoft Windows 10.0.19045

C# 
.Net Core 7.0

PowerShell5 
Name                           Value
----                           -----
PSVersion                      5.1.19041.4291
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.4291
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PowerShell7
Name                           Value
----                           -----
PSVersion                      7.5.0-preview.2
PSEdition                      Core
GitCommitId                    7.5.0-preview.2
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

rhubarb-geek-nz commented 3 weeks ago

Hi,

I had success I believe by using an up to date version of the SDK. The SDK should match the version of .NET you are running

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.2" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.3.12" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.19" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.1.0" />
  </ItemGroup>

</Project>

I was able to confirm your desired result on net8.0, net7.0 and net6.0. I was unable to run the net5.0 because I don't have the runtime installed.

C:\overhere> dotnet run --configuration Release --framework net8.0
hello123456789hi123456789go123456789