Skip to content

Commit

Permalink
Land #19377, Add compressinon to php/base64
Browse files Browse the repository at this point in the history
This enables users to set a datastore option in enocoders/php/base64
which will compress the payload using zlib, greatly reducing its size
  • Loading branch information
jheysel-r7 committed Aug 27, 2024
2 parents 3ad24b4 + 573643a commit 49d3826
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion modules/encoders/php/base64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def initialize
'Author' => 'egypt',
'License' => BSD_LICENSE,
'Arch' => ARCH_PHP)
register_options(
[
OptBool.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.
],
self.class)
end

def encode_block(state, buf)
Expand All @@ -26,6 +31,12 @@ def encode_block(state, buf)
raise BadcharError if state.badchars.include?(c)
end

if datastore['Compress']
%w[g z u n c o m p r e s s].uniq.each do |c|
raise BadcharError if state.badchars.include?(c)
end
end

# Modern versions of PHP choke on unquoted literal strings.
quote = "'"
if state.badchars.include?("'")
Expand All @@ -34,6 +45,10 @@ def encode_block(state, buf)
quote = '"'
end

if datastore['Compress']
buf = Zlib::Deflate.deflate(buf)
end

# PHP escapes quotes by default with magic_quotes_gpc, so we use some
# tricks to get around using them.
#
Expand Down Expand Up @@ -98,6 +113,10 @@ def encode_block(state, buf)
# cause a syntax error. Remove any trailing dots.
b64.chomp!('.')

return 'eval(base64_decode(' + quote + b64 + quote + '));'
if datastore['Compress']
return 'eval(gzuncompress(base64_decode(' + quote + b64 + quote + ')));'
else
return 'eval(base64_decode(' + quote + b64 + quote + '));'
end
end
end

0 comments on commit 49d3826

Please sign in to comment.