Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 tasks done
H4de5-7 opened this issue Apr 25, 2024 · 1 comment
Open
5 tasks done

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

H4de5-7 opened this issue Apr 25, 2024 · 1 comment
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.

Comments

@H4de5-7
Copy link

H4de5-7 commented Apr 25, 2024

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

@H4de5-7 H4de5-7 added the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 25, 2024
@rhubarb-geek-nz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.
Projects
None yet
Development

No branches or pull requests

2 participants