Powershell .NET Compiler - Metasploit


This page contains detailed information about how to use the post/windows/manage/powershell/build_net_code metasploit module. For list of all metasploit modules, visit the Metasploit Module Library.

Module Overview


Name: Powershell .NET Compiler
Module: post/windows/manage/powershell/build_net_code
Source code: modules/post/windows/manage/powershell/build_net_code.rb
Disclosure date: 2012-08-14
Last modification time: 2021-10-06 13:43:31 +0000
Supported architecture(s): -
Supported platform(s): Windows
Target service / protocol: -
Target network port(s): -
List of CVEs: -

This module will build a .NET source file using powershell. The compiler builds the executable or library in memory and produces a binary. After compilation the PowerShell session can also sign the executable if provided a path the a .pfx formatted certificate. Compiler options and a list of assemblies required can be configured in the datastore.

Module Ranking and Traits


Module Ranking:

  • excellent: The exploit will never crash the service. This is the case for SQL Injection, CMD execution, RFI, LFI, etc. No typical memory corruption exploits should be given this ranking unless there are extraordinary circumstances. More information about ranking can be found here.

Basic Usage


There are two ways to execute this post module.

From the Meterpreter prompt

The first is by using the "run" command at the Meterpreter prompt. It allows you to run the post module against that specific session:

meterpreter > run post/windows/manage/powershell/build_net_code

From the msf prompt

The second is by using the "use" command at the msf prompt. You will have to figure out which session ID to set manually. To list all session IDs, you can use the "sessions" command.

msf > use post/windows/manage/powershell/build_net_code
msf post(build_net_code) > show options
    ... show and set options ...
msf post(build_net_code) > set SESSION session-id
msf post(build_net_code) > exploit

If you wish to run the post against all sessions from framework, here is how:

1 - Create the following resource script:


framework.sessions.each_pair do |sid, session|
  run_single("use post/windows/manage/powershell/build_net_code")
  run_single("set SESSION #{sid}")
  run_single("run")
end

2 - At the msf prompt, execute the above resource script:

msf > resource path-to-resource-script

Required Options


  • SESSION: The session to run this module on.

  • SOURCE_FILE: Path to source code

Knowledge Base


Example Session


/tmp/hello.cs contains the following:

using System;

public class Hello
{
   public static void Main()
   {
      Console.WriteLine("Hello, World!");
   }
}

To build and run the code:

msf exploit(handler) > use post/windows/manage/powershell/build_net_code
msf post(build_net_code) > set SESSION -1
SESSION => -1
msf post(build_net_code) > show options

Module options (post/windows/manage/powershell/build_net_code):

   Name           Current Setting                                            Required  Description
   ----           ---------------                                            --------  -----------
   ASSEMBLIES     mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll  no        Any assemblies outside the defaults
   CODE_PROVIDER  Microsoft.CSharp.CSharpCodeProvider                        yes       Code provider to use
   COMPILER_OPTS  /optimize                                                  no        Options to pass to compiler
   OUTPUT_TARGET                                                             no        Name and path of the generated binary, default random, omit extension
   RUN_BINARY     false                                                      no        Execute the generated binary
   SESSION        -1                                                         yes       The session to run this module on.
   SOURCE_FILE                                                               yes       Path to source code

msf post(build_net_code) > set SOURCE_FILE /tmp/hello.cs
SOURCE_FILE => /tmp/hello.cs
msf post(build_net_code) > run

[*] Building remote code.
[+] File C:\cygwin64\tmp\aNwCFmmLzlYvPWw.exe found, 3584kb
[+] Finished!
[*] Post module execution completed
msf post(build_net_code) > sessions -i -1
[*] Starting interaction with 1...

meterpreter > shell
Process 4840 created.
Channel 7 created.
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

E:\metasploit-framework>C:\cygwin64\tmp\aNwCFmmLzlYvPWw.exe
C:\cygwin64\tmp\aNwCFmmLzlYvPWw.exe
Hello, World!

You can also run the code automatically:

msf exploit(handler) > use post/windows/manage/powershell/build_net_code
msf post(build_net_code) > set SOURCE_FILE /tmp/hello.cs
SOURCE_FILE => /tmp/hello.cs
msf post(build_net_code) > set RUN_BINARY true
RUN_BINARY => true
msf post(build_net_code) > set SESSION -1
SESSION => -1
msf post(build_net_code) > run

[*] Building remote code.
[+] File C:\cygwin64\tmp\QuEQSEifJOe.exe found, 3584kb
[+] Hello, World!

[+] Finished!
[*] Post module execution completed

Go back to menu.

Msfconsole Usage


Here is how the windows/manage/powershell/build_net_code post exploitation module looks in the msfconsole:

msf6 > use post/windows/manage/powershell/build_net_code

msf6 post(windows/manage/powershell/build_net_code) > show info

       Name: Powershell .NET Compiler
     Module: post/windows/manage/powershell/build_net_code
   Platform: Windows
       Arch: 
       Rank: Excellent
  Disclosed: 2012-08-14

Provided by:
  RageLtMan <rageltman@sempervictus>

Compatible session types:
  Meterpreter

Basic options:
  Name           Current Setting                                            Required  Description
  ----           ---------------                                            --------  -----------
  ASSEMBLIES     mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll  no        Any assemblies outside the defaults
  CODE_PROVIDER  Microsoft.CSharp.CSharpCodeProvider                        yes       Code provider to use
  COMPILER_OPTS  /optimize                                                  no        Options to pass to compiler
  OUTPUT_TARGET                                                             no        Name and path of the generated binary, default random, omit extension
  RUN_BINARY     false                                                      no        Execute the generated binary
  SESSION                                                                   yes       The session to run this module on.
  SOURCE_FILE                                                               yes       Path to source code

Description:
  This module will build a .NET source file using powershell. The 
  compiler builds the executable or library in memory and produces a 
  binary. After compilation the PowerShell session can also sign the 
  executable if provided a path the a .pfx formatted certificate. 
  Compiler options and a list of assemblies required can be configured 
  in the datastore.

Module Options


This is a complete list of options available in the windows/manage/powershell/build_net_code post exploitation module:

msf6 post(windows/manage/powershell/build_net_code) > show options

Module options (post/windows/manage/powershell/build_net_code):

   Name           Current Setting                                            Required  Description
   ----           ---------------                                            --------  -----------
   ASSEMBLIES     mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll  no        Any assemblies outside the defaults
   CODE_PROVIDER  Microsoft.CSharp.CSharpCodeProvider                        yes       Code provider to use
   COMPILER_OPTS  /optimize                                                  no        Options to pass to compiler
   OUTPUT_TARGET                                                             no        Name and path of the generated binary, default random, omit extension
   RUN_BINARY     false                                                      no        Execute the generated binary
   SESSION                                                                   yes       The session to run this module on.
   SOURCE_FILE                                                               yes       Path to source code

Advanced Options


Here is a complete list of advanced options supported by the windows/manage/powershell/build_net_code post exploitation module:

msf6 post(windows/manage/powershell/build_net_code) > show advanced

Module advanced options (post/windows/manage/powershell/build_net_code):

   Name                                    Current Setting  Required  Description
   ----                                    ---------------  --------  -----------
   CERT_PATH                                                no        Path on compiler host to .pfx fomatted certificate for signing
   NET_CLR_VER                             4.0              no        Minimum NET CLR version required to compile
   Powershell::Post::dry_run               false            yes       Return encoded output to caller
   Powershell::Post::force_wow64           false            yes       Force WOW64 execution
   Powershell::Post::log_output            false            yes       Write output to log file
   Powershell::Post::timeout               15               yes       Powershell execution timeout, set < 0 to run async without termination
   Powershell::encode_final_payload        false            yes       Encode final payload for -EncodedCommand
   Powershell::encode_inner_payload        false            yes       Encode inner payload for -EncodedCommand
   Powershell::exec_in_place               false            yes       Produce PSH without executable wrapper
   Powershell::exec_rc4                    false            yes       Encrypt PSH with RC4
   Powershell::method                      reflection       yes       Payload delivery method (Accepted: net, reflection, old, msil)
   Powershell::no_equals                   false            yes       Pad base64 until no "=" remains
   Powershell::noninteractive              true             yes       Execute powershell without interaction
   Powershell::persist                     false            yes       Run the payload in a loop
   Powershell::prepend_protections_bypass  true             yes       Prepend AMSI/SBL bypass
   Powershell::prepend_sleep                                no        Prepend seconds of sleep
   Powershell::remove_comspec              false            yes       Produce script calling powershell directly
   Powershell::strip_comments              true             yes       Strip comments
   Powershell::strip_whitespace            false            yes       Strip whitespace
   Powershell::sub_funcs                   false            yes       Substitute function names
   Powershell::sub_vars                    true             yes       Substitute variable names
   Powershell::wrap_double_quotes          true             yes       Wraps the -Command argument in single quotes
   VERBOSE                                 false            no        Enable detailed status messages
   WORKSPACE                                                no        Specify the workspace for this module

Post Actions


This is a list of all post exploitation actions which the windows/manage/powershell/build_net_code module can do:

msf6 post(windows/manage/powershell/build_net_code) > show actions

Post actions:

   Name  Description
   ----  -----------

Evasion Options


Here is the full list of possible evasion options supported by the windows/manage/powershell/build_net_code post exploitation module in order to evade defenses (e.g. Antivirus, EDR, Firewall, NIDS etc.):

msf6 post(windows/manage/powershell/build_net_code) > show evasion

Module evasion options:

   Name  Current Setting  Required  Description
   ----  ---------------  --------  -----------

Go back to menu.

Error Messages


This module may fail with the following error messages:

Check for the possible causes from the code snippets below found in the module source code. This can often times help in identifying the root cause of the problem.

Incompatible Environment


Here is a relevant code snippet related to the "Incompatible Environment" error message:

47:	  end
48:	
49:	  def run
50:	    # Make sure we meet the requirements before running the script
51:	    unless session.type == "meterpreter" || have_powershell?
52:	      print_error "Incompatible Environment"
53:	      return 0
54:	    end
55:	
56:	    # Havent figured this one out yet, but we need a PID owned by a user, can't steal tokens either
57:	    if client.sys.config.is_system?

Cannot run as system


Here is a relevant code snippet related to the "Cannot run as system" error message:

53:	      return 0
54:	    end
55:	
56:	    # Havent figured this one out yet, but we need a PID owned by a user, can't steal tokens either
57:	    if client.sys.config.is_system?
58:	      print_error "Cannot run as system"
59:	      return 0
60:	    end
61:	
62:	    # End of file marker
63:	    eof = Rex::Text.rand_text_alpha(8)

Go back to menu.


Go back to menu.

See Also


Check also the following modules related to this module:

Authors


RageLtMan <rageltman[at]sempervictus>

Version


This page has been produced using Metasploit Framework version 6.1.24-dev. For more modules, visit the Metasploit Module Library.

Go back to menu.