2007-02-18 00:10:39 +00:00
|
|
|
##
|
2010-04-30 08:40:19 +00:00
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
2007-02-18 00:10:39 +00:00
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
2012-02-21 01:40:50 +00:00
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
2007-02-18 00:10:39 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
|
2006-01-15 22:43:44 +00:00
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
|
2008-10-02 05:23:59 +00:00
|
|
|
class Metasploit3 < Msf::Encoder
|
2006-01-15 22:43:44 +00:00
|
|
|
|
2009-12-03 08:39:22 +00:00
|
|
|
# Has some issues, but overall it's pretty good
|
|
|
|
Rank = GoodRanking
|
|
|
|
|
2006-01-15 22:43:44 +00:00
|
|
|
def initialize
|
|
|
|
super(
|
|
|
|
'Name' => 'Generic Shell Variable Substitution Command Encoder',
|
|
|
|
'Description' => %q{
|
|
|
|
This encoder uses standard Bourne shell variable substitution
|
|
|
|
tricks to avoid commonly restricted characters.
|
|
|
|
},
|
|
|
|
'Author' => 'hdm',
|
|
|
|
'Arch' => ARCH_CMD)
|
|
|
|
end
|
|
|
|
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-15 22:43:44 +00:00
|
|
|
#
|
|
|
|
# Encodes the payload
|
|
|
|
#
|
|
|
|
def encode_block(state, buf)
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-08-17 17:42:39 +00:00
|
|
|
# Skip encoding for empty badchars
|
|
|
|
if(state.badchars.length == 0)
|
|
|
|
return buf
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-03 06:35:13 +00:00
|
|
|
if (state.badchars.include?("-"))
|
|
|
|
# Then neither of the others will work. Get rid of spaces and hope
|
|
|
|
# for the best. This obviously won't work if the command already
|
|
|
|
# has other badchars in it, in which case we're basically screwed.
|
|
|
|
if (state.badchars.include?(" "))
|
|
|
|
buf.gsub!(/\s/, '${IFS}')
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Without an escape character we can't escape anything, so echo
|
|
|
|
# won't work. Try perl.
|
|
|
|
if (state.badchars.include?("\\"))
|
|
|
|
buf = encode_block_perl(state,buf)
|
|
|
|
else
|
|
|
|
buf = encode_block_bash_echo(state,buf)
|
|
|
|
end
|
2006-01-15 22:43:44 +00:00
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-15 22:43:44 +00:00
|
|
|
return buf
|
|
|
|
end
|
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
#
|
|
|
|
# Uses the perl command to hex encode the command string
|
|
|
|
#
|
|
|
|
def encode_block_perl(state, buf)
|
|
|
|
|
2010-04-30 08:40:19 +00:00
|
|
|
hex = buf.unpack("H*")
|
2006-01-16 02:32:30 +00:00
|
|
|
cmd = 'perl -e '
|
|
|
|
qot = ',-:.=+!@#$%^&'
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
# Find a quoting character to use
|
2009-07-03 06:35:13 +00:00
|
|
|
state.badchars.unpack('C*') { |c| qot.delete(c.chr) }
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
# Throw an error if we ran out of quotes
|
|
|
|
raise RuntimeError if qot.length == 0
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
sep = qot[0].chr
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
# Convert spaces to IFS...
|
|
|
|
if (state.badchars.include?(" "))
|
|
|
|
cmd.gsub!(/\s/, '${IFS}')
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
# Can we use single quotes to enclose the command string?
|
|
|
|
if (state.badchars.include?("'"))
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
if (state.badchars.match(/\(|\)/))
|
|
|
|
|
|
|
|
# No paranthesis...
|
|
|
|
raise RuntimeError
|
|
|
|
end
|
|
|
|
|
2009-07-03 06:35:13 +00:00
|
|
|
cmd << "system\\(pack\\(qq#{sep}H\\*#{sep},qq#{sep}#{hex}#{sep}\\)\\)"
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
else
|
|
|
|
if (state.badchars.match(/\(|\)/))
|
|
|
|
if (state.badchars.include?(" "))
|
|
|
|
# No spaces allowed, no paranthesis, give up...
|
|
|
|
raise RuntimeError
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-03 06:35:13 +00:00
|
|
|
cmd << "'system pack qq#{sep}H*#{sep},qq#{sep}#{hex}#{sep}'"
|
2006-01-16 02:32:30 +00:00
|
|
|
else
|
2009-07-03 06:35:13 +00:00
|
|
|
cmd << "'system(pack(qq#{sep}H*#{sep},qq#{sep}#{hex}#{sep}))'"
|
2006-01-16 02:32:30 +00:00
|
|
|
end
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
return cmd
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
#
|
|
|
|
# Uses bash's echo -ne command to hex encode the command string
|
|
|
|
#
|
|
|
|
def encode_block_bash_echo(state, buf)
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
hex = ''
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
# 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
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
# Are pipe characters restricted?
|
|
|
|
if (state.badchars.include?("|"))
|
|
|
|
# How about backticks?
|
|
|
|
if (state.badchars.include?("`"))
|
2009-07-03 06:35:13 +00:00
|
|
|
# Last ditch effort, dollar paren
|
2010-04-30 08:40:19 +00:00
|
|
|
if (state.badchars.include?("$") or state.badchars.include?("("))
|
2009-07-03 06:35:13 +00:00
|
|
|
raise RuntimeError
|
|
|
|
else
|
2009-08-04 19:27:50 +00:00
|
|
|
buf = "$(/bin/echo -ne #{hex})"
|
2009-07-03 06:35:13 +00:00
|
|
|
end
|
2006-01-16 02:32:30 +00:00
|
|
|
else
|
2009-08-04 19:27:50 +00:00
|
|
|
buf = "`/bin/echo -ne #{hex}`"
|
2006-01-16 02:32:30 +00:00
|
|
|
end
|
|
|
|
else
|
2009-08-04 19:27:50 +00:00
|
|
|
buf = "/bin/echo -ne #{hex}|sh"
|
2006-01-16 02:32:30 +00:00
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
# Remove spaces from the command string
|
|
|
|
if (state.badchars.include?(" "))
|
|
|
|
buf.gsub!(/\s/, '${IFS}')
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2006-01-16 02:32:30 +00:00
|
|
|
return buf
|
2010-04-30 08:40:19 +00:00
|
|
|
end
|
2006-01-16 02:32:30 +00:00
|
|
|
|
2009-07-03 06:35:13 +00:00
|
|
|
end
|