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/base64.rb
Views: 11780
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Encoder6Rank = GoodRanking78BASE64_BYTES = [9'A'.ord...'Z'.ord,10'a'.ord...'z'.ord,11'0'.ord...'9'.ord12].map(&:to_a).flatten + '+/='.bytes1314def initialize15super(16'Name' => 'Base64 Command Encoder',17'Description' => %q{18This encoder uses base64 encoding to avoid bad characters.19},20'Author' => 'Spencer McIntyre',21'Arch' => ARCH_CMD,22'Platform' => %w[bsd bsdi linux osx solaris unix],23'EncoderType' => Msf::Encoder::Type::CmdPosixBase64)2425register_advanced_options(26[27OptString.new('Base64Decoder', [ false, 'The binary to use for base64 decoding', '', %w[base64 base64-long base64-short openssl] ])28],29self.class30)31end3233#34# Encodes the payload35#36def encode_block(state, buf)37return buf if (buf.bytes & state.badchars.bytes).empty?3839raise EncodingError if (state.badchars.bytes & BASE64_BYTES).any?40raise EncodingError if state.badchars.include?('-')4142ifs_encode_spaces = state.badchars.include?(' ')43raise EncodingError if ifs_encode_spaces && (state.badchars.bytes & '${}'.bytes).any?4445base64_buf = Base64.strict_encode64(buf)46case datastore['Base64Decoder']47when 'base64'48raise EncodingError if (state.badchars.bytes & '(|)'.bytes).any?4950base64_decoder = '(base64 --decode || base64 -d)'51when 'base64-long'52base64_decoder = 'base64 --decode'53when 'base64-short'54base64_decoder = 'base64 -d'55when 'openssl'56base64_decoder = 'openssl enc -base64 -d'57else58# find a decoder at runtime if we can use the necessary characters59if (state.badchars.bytes & '(|)>/&'.bytes).empty?60base64_decoder = '((command -v base64 >/dev/null && (base64 --decode || base64 -d)) || (command -v openssl >/dev/null && openssl enc -base64 -d))'61elsif (state.badchars.bytes & '(|)'.bytes).empty?62base64_decoder = '(base64 --decode || base64 -d)'63else64base64_decoder = 'openssl enc -base64 -d'65end66end6768if (state.badchars.bytes & '|'.bytes).empty?69buf = "echo #{base64_buf}|#{base64_decoder}|sh"70elsif (state.badchars.bytes & '<()'.bytes).empty?71buf = "sh < <(#{base64_decoder} < <(echo #{base64_buf}))"72elsif (state.badchars.bytes & '<`\''.bytes).empty?73buf = "sh<<<`#{base64_decoder}<<<'#{base64_buf}'`"74else75raise EncodingError76end7778buf = buf.gsub(/ +/, '${IFS}') if ifs_encode_spaces79buf80end81end828384