0

Hello i want load a assembly from byte array and i found this:

Loading a .NET assembly from a byte array

It only loads .NET byte array.

And here is my code i changed it a little bit:

$ByteArray = (Invoke-WebRequest "https://DOTNETEXEfile.exe").Content

# Base64
 
$Base64String = [System.Convert]::ToBase64String($ByteArray);
$PsEBytes = [System.Convert]::FromBase64String($Base64String)

# Run EXE in memory

$assembly = [System.Reflection.Assembly]::Load($PsEBytes)
# Get the static method that is the executable's entry point.
# Note: 
#   * Assumes 'Program' as the class name, 
#     and a static method named 'Main' as the entry point.
#   * Should there be several classes by that name, the *first* 
#     - public or non-public - type returned is used.
#     If you know the desired type's namespace, use, e.g.
#     $assembly.GetType('MyNameSpace.Program').GetMethod(...)
$entryPointMethod = 
 $assembly.GetTypes().Where({ $_.Name -eq 'Program' }, 'First').
   GetMethod('Main', [Reflection.BindingFlags] 'Static, Public, NonPublic')

# Now you can call the entry point.
# This example passes two arguments, 'foo' and 'bar'
$entryPointMethod.Invoke($null, (, [string[]] ('foo', 'bar')))

But we have a problem, It works when i put my link of a c# project that has a msgbox and its .NET, its works fine but when i put a form app exe (.NET) it gives me that error:

PS C:\Users\sadettin\Desktop> C:\Users\sadettin\Desktop\PE.ps1
Exception calling "Invoke" with "2" argument(s): "parameter number mismatch."
At C:\Users\sadettin\Desktop\PE.ps1:25 char:1
+ $entryPointMethod.Invoke($null, (, [string[]] ('foo', 'bar')))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : TargetParameterCountException

but when i replace $entryPointMethod.Invoke($null, (, [string[]] ('foo', 'bar'))) with $entryPointMethod.Invoke($null, $null) it works

Also when i put a exe (its not .NET) it doesn't work so is there have a way to execute all exe types?

NoobCoding
  • 43
  • 4
  • Does your WinForm application accept arguments (which isn't common)? If not, that would explain the error. And the solution indeed fundamentally only works for _.NET_-based applications. – mklement0 Nov 01 '22 at 22:49
  • How can i make it non spesifik i want work it for as much as i can exe file types. i mean what should i do into code? How should i changeit? "$entryPointMethod.Invoke($null, (, [string[]] ('foo', 'bar')))" – NoobCoding Nov 01 '22 at 23:10
  • @mklement0 I want to work on as many exe files as I can – NoobCoding Nov 01 '22 at 23:13
  • If no one posts an answer to your [related question](https://stackoverflow.com/q/74175072/45375), my pragmatic advice still applies: save to a temporary file, execute that, then delete it. – mklement0 Nov 02 '22 at 01:07

0 Answers0