2014-07-22 15:27:45 +00:00
|
|
|
##
|
2014-12-11 22:34:10 +00:00
|
|
|
# This module requires Metasploit: http://metasploit.com/download
|
2014-07-22 15:27:45 +00:00
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
|
2016-03-07 08:56:58 +00:00
|
|
|
class Metasploit < Msf::Encoder
|
2014-07-22 15:27:45 +00:00
|
|
|
|
2014-10-07 15:24:32 +00:00
|
|
|
Rank = GoodRanking
|
2014-07-22 15:27:45 +00:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
super(
|
|
|
|
'Name' => 'Echo Command Encoder',
|
|
|
|
'Description' => %q{
|
|
|
|
This encoder uses echo and backlash escapes to avoid commonly restricted characters.
|
|
|
|
},
|
|
|
|
'Author' => 'hdm',
|
|
|
|
'Arch' => ARCH_CMD,
|
|
|
|
'Platform' => 'unix',
|
2014-10-06 23:42:21 +00:00
|
|
|
'EncoderType' => Msf::Encoder::Type::CmdUnixEcho)
|
2014-07-22 15:27:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Encodes the payload
|
|
|
|
#
|
|
|
|
def encode_block(state, buf)
|
|
|
|
# Skip encoding for empty badchars
|
|
|
|
if state.badchars.length == 0
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
|
|
|
|
if state.badchars.include?("-")
|
2015-05-18 20:33:01 +00:00
|
|
|
raise EncodingError
|
2014-07-22 15:27:45 +00:00
|
|
|
else
|
|
|
|
# Without an escape character we can't escape anything, so echo
|
|
|
|
# won't work.
|
|
|
|
if state.badchars.include?("\\")
|
2015-05-18 20:33:01 +00:00
|
|
|
raise EncodingError
|
2014-07-22 15:27:45 +00:00
|
|
|
else
|
|
|
|
buf = encode_block_bash_echo(state,buf)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Uses bash's echo -ne command to hex encode the command string
|
|
|
|
#
|
|
|
|
def encode_block_bash_echo(state, buf)
|
|
|
|
|
|
|
|
hex = ''
|
|
|
|
|
|
|
|
# Can we use single quotes to enclose the echo arguments?
|
|
|
|
if state.badchars.include?("'")
|
|
|
|
hex = buf.unpack('C*').collect { |c| "\\\\\\x%.2x" % c }.join
|
|
|
|
else
|
|
|
|
hex = "'" + buf.unpack('C*').collect { |c| "\\x%.2x" % c }.join + "'"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Are pipe characters restricted?
|
|
|
|
if state.badchars.include?("|")
|
|
|
|
# How about backticks?
|
|
|
|
if state.badchars.include?("`")
|
|
|
|
# Last ditch effort, dollar paren
|
|
|
|
if state.badchars.include?("$") or state.badchars.include?("(")
|
2015-05-18 20:33:01 +00:00
|
|
|
raise EncodingError
|
2014-07-22 15:27:45 +00:00
|
|
|
else
|
|
|
|
buf = "$(/bin/echo -ne #{hex})"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
buf = "`/bin/echo -ne #{hex}`"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
buf = "/bin/echo -ne #{hex}|sh"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove spaces from the command string
|
|
|
|
if state.badchars.include?(" ")
|
|
|
|
buf.gsub!(/\s/, '${IFS}')
|
|
|
|
end
|
|
|
|
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|