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/echo.rb
Views: 11779
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Encoder6Rank = GoodRanking78def initialize9super(10'Name' => 'Echo Command Encoder',11'Description' => %q{12This encoder uses echo and backlash escapes to avoid commonly restricted characters.13},14'Author' => 'hdm',15'Arch' => ARCH_CMD,16'Platform' => %w[ linux unix ],17'EncoderType' => Msf::Encoder::Type::CmdPosixEcho)18end192021#22# Encodes the payload23#24def encode_block(state, buf)25# Skip encoding for empty badchars26if state.badchars.length == 027return buf28end2930if state.badchars.include?("-")31raise EncodingError32else33# Without an escape character we can't escape anything, so echo34# won't work.35if state.badchars.include?("\\")36raise EncodingError37else38buf = encode_block_bash_echo(state,buf)39end40end4142return buf43end4445#46# Uses bash's echo -ne command to hex encode the command string47#48def encode_block_bash_echo(state, buf)4950hex = ''5152# Can we use single quotes to enclose the echo arguments?53if state.badchars.include?("'")54hex = buf.unpack('C*').collect { |c| "\\\\\\x%.2x" % c }.join55else56hex = "'" + buf.unpack('C*').collect { |c| "\\x%.2x" % c }.join + "'"57end5859# Are pipe characters restricted?60if state.badchars.include?("|")61# How about backticks?62if state.badchars.include?("`")63# Last ditch effort, dollar paren64if state.badchars.include?("$") or state.badchars.include?("(")65raise EncodingError66else67buf = "$(/bin/echo -ne #{hex})"68end69else70buf = "`/bin/echo -ne #{hex}`"71end72else73buf = "/bin/echo -ne #{hex}|sh"74end7576# Remove spaces from the command string77if state.badchars.include?(" ")78buf.gsub!(/\s/, '${IFS}')79end8081return buf82end83end848586