Skip to content

How to use Powershell in an exploit

sinn3r edited this page Jul 29, 2014 · 16 revisions

Powershell is a scripting language developed by Microsoft. It provides API access to almost everything in a Windows platform, less detectable by countermeasures, easy to learn, therefore it is incredibly powerful for penetration testing during post exploitation, or exploit development for payload execution. Take Metasploit's windows/smb/psexec_psh.rb module for example: it mimics the psexec utility from SysInternals, the payload is encoded in Base64 and executed from the command line, which allows it to be less detectable by antivirus. There's only less than 30 lines of code in psexec_psh.rb (excluding the metadata that describes what the module is about), because most of the work is done by the Powershell mixin, nothing is easier than that.

Requirements

To use the Powershell mixin, make sure you meet these requirements:

  • The target machine supports Powershell. Vista or newer should support it.
  • You must have permission to execute powershell.exe
  • You must be able to supply system command arguments.
  • You must set up a command execution type attack in order to execute powershell.exe

Usage

  1. To add the Powershell to your module, first you need to require it:
require 'msf/core/exploit/powershell'
  1. And then include the mixin within the scope of the Metasploit3 class (or maybe Metasploit4 for some)
include Msf::Exploit::Powershell
  1. Use the cmd_psh_payload method to generate the Powershell payload.
cmd_psh_payload(payload.encoded)

The actual output of cmd_psh_payload is a system command, which would look like the following format (as a one-liner):

%COMSPEC% /B /C start powershell.exe -Command $si = New-Object
System.Diagnostics.ProcessStartInfo;$si.FileName = 'powershell.exe';
$si.Arguments = ' -EncodedCommand [BASE64 PAYLOAD] ';
$si.UseShellExecute = $false;
$si.RedirectStandardOutput = $true;$si.WindowStyle = 'Hidden';
$si.CreateNoWindow = $True;
$p = [System.Diagnostics.Process]::Start($si);

Reference

https://github.com/rapid7/metasploit-framework/blob/master/lib/msf/core/exploit/powershell.rb

Metasploit Wiki Pages


Clone this wiki locally