Path: blob/master/modules/encoders/php/hex.rb
19813 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Encoder6Rank = GreatRanking78def initialize9super(10'Name' => 'PHP Hex Encoder',11'Description' => %q{12This encoder returns a hex string encapsulated in13eval(hex2bin()), increasing the size by a bit more than14a factor two.15},16'Author' => 'Julien Voisin',17'License' => BSD_LICENSE,18'Arch' => ARCH_PHP)19register_options(20[21OptBool.new('Compress', [ true, 'Compress the payload with zlib', false ]) # Disabled by default as it relies on having php compiled with zlib, which might not be available on come exotic setups.22]23)24end2526def encode_block(state, buf)27# Have to have these for the decoder stub, so if they're not available,28# there's nothing we can do here.29%w[e v a l h e x 2 b i n ( ) ;].uniq.each do |c|30raise BadcharError if state.badchars.include?(c)31end3233if datastore['Compress']34%w[g z u n c o m p r e s s].uniq.each do |c|35raise BadcharError if state.badchars.include?(c)36end37end3839# Modern versions of PHP choke on unquoted literal strings.40quote = "'"41if state.badchars.include?("'")42raise BadcharError.new, "The #{name} encoder failed to encode the decoder stub without bad characters." if state.badchars.include?('"')4344quote = '"'45end4647if datastore['Compress']48buf = Zlib::Deflate.deflate(buf)49end5051hex = buf.unpack1('H*')5253state.badchars.each_byte do |byte|54# Last ditch effort, if any of the normal characters used by hex55# are badchars, try to replace them with something that will become56# the appropriate thing on the other side.57next unless hex.include?(byte.chr)5859%w[c h r ( ) .].uniq.each do |c|60raise BadcharError if state.badchars.include?(c)61end62hex.gsub!(byte.chr, "#{quote}.chr(#{byte}).#{quote}")63end6465if datastore['Compress']66return 'eval(gzuncompress(hex2bin(' + quote + hex + quote + ')));'67else68return 'eval(hex2bin(' + quote + hex + quote + '));'69end70end71end727374