#!/usr/bin/env bash
#
# OSCP Kali setup — stages the common tools into ~/tools and ~/www.
# Run on YOUR Kali box (needs internet). Safe to re-run (idempotent-ish).
#
#   chmod +x setup.sh && ./setup.sh
#
# It does NOT exploit anything — it just downloads/install tools you're
# allowed to use on the exam (enumeration + manual exploitation helpers).
set -u
TOOLS="$HOME/tools"
WWW="$HOME/www"        # a staging web root you serve with: python3 -m http.server 80
mkdir -p "$TOOLS" "$WWW"
log(){ echo -e "\n[+] $*"; }

# --- apt packages (most of what you need ships in Kali repos) ---------------
log "apt packages"
sudo apt-get update -y
sudo apt-get install -y \
  nmap feroxbuster gobuster ffuf nikto seclists \
  netexec impacket-scripts evil-winrm \
  hydra john hashcat \
  smbclient enum4linux ldap-utils snmp onesixtyone \
  bloodhound.py mingw-w64 proxychains4 \
  python3-pip pipx curl wget git jq rlwrap 2>/dev/null

# --- GitHub release binaries (static, drop onto targets) --------------------
dl(){ # dl <url> <dest>
  echo "    fetching $(basename "$2")"
  curl -fsSL "$1" -o "$2" 2>/dev/null && chmod +x "$2" 2>/dev/null || echo "    !! failed: $1 (grab manually)"
}

log "pivoting — chisel & ligolo-ng"
dl "https://github.com/jpillora/chisel/releases/latest/download/chisel_1.10.1_linux_amd64.gz" "$TOOLS/chisel.gz"
gunzip -f "$TOOLS/chisel.gz" 2>/dev/null; chmod +x "$TOOLS/chisel" 2>/dev/null
dl "https://github.com/jpillora/chisel/releases/latest/download/chisel_1.10.1_windows_amd64.gz" "$WWW/chisel.exe.gz"
gunzip -f "$WWW/chisel.exe.gz" 2>/dev/null
# ligolo-ng (check the releases page for current version tags if these 404):
dl "https://github.com/nicocha30/ligolo-ng/releases/download/v0.6.2/ligolo-ng_proxy_0.6.2_linux_amd64.tar.gz" "$TOOLS/ligolo-proxy.tgz"
dl "https://github.com/nicocha30/ligolo-ng/releases/download/v0.6.2/ligolo-ng_agent_0.6.2_windows_amd64.zip" "$WWW/ligolo-agent-win.zip"

log "AD / Windows — kerbrute, mimikatz, PrintSpoofer, GodPotato, winPEAS"
dl "https://github.com/ropnop/kerbrute/releases/latest/download/kerbrute_linux_amd64" "$TOOLS/kerbrute"
dl "https://github.com/itm4n/PrintSpoofer/releases/latest/download/PrintSpoofer64.exe" "$WWW/PrintSpoofer64.exe"
dl "https://github.com/BeichenDream/GodPotato/releases/latest/download/GodPotato-NET4.exe" "$WWW/GodPotato.exe"

log "PEASS-ng (linpeas / winpeas) into \$WWW"
dl "https://github.com/peass-ng/PEASS-ng/releases/latest/download/linpeas.sh" "$WWW/linpeas.sh"
dl "https://github.com/peass-ng/PEASS-ng/releases/latest/download/winPEASx64.exe" "$WWW/winPEASx64.exe"
dl "https://github.com/peass-ng/PEASS-ng/releases/latest/download/winPEAS.bat" "$WWW/winPEAS.bat"

log "pspy (live process/cron watcher)"
dl "https://github.com/DominicBreuker/pspy/releases/latest/download/pspy64" "$WWW/pspy64"

log "Windows privesc scripts (PowerUp, Seatbelt, PrivescCheck) into \$WWW"
dl "https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/master/Privesc/PowerUp.ps1" "$WWW/PowerUp.ps1"
dl "https://raw.githubusercontent.com/itm4n/PrivescCheck/master/PrivescCheck.ps1" "$WWW/PrivescCheck.ps1"

log "git clones (walkthrough references, not for the exam report)"
[ -d "$TOOLS/PayloadsAllTheThings" ] || git clone -q --depth 1 https://github.com/swisskyrepo/PayloadsAllTheThings "$TOOLS/PayloadsAllTheThings"

# --- static helpers often needed on targets ---------------------------------
log "nc.exe + static binaries into \$WWW"
cp /usr/share/windows-resources/binaries/nc.exe "$WWW/" 2>/dev/null || \
  dl "https://github.com/int0x33/nc.exe/raw/master/nc64.exe" "$WWW/nc64.exe"

# --- proxychains: ensure socks5 127.0.0.1 1080 + quiet mode -----------------
log "proxychains config"
sudo sed -i 's/^#quiet_mode/quiet_mode/' /etc/proxychains4.conf 2>/dev/null
grep -q "socks5.*1080" /etc/proxychains4.conf 2>/dev/null || \
  echo "socks5 127.0.0.1 1080" | sudo tee -a /etc/proxychains4.conf >/dev/null

cat <<EOF

[✓] Done.
    ~/tools  -> your Kali-side tools (chisel, kerbrute, ligolo proxy, PayloadsAllTheThings)
    ~/www    -> files to serve to targets:  cd ~/www && python3 -m http.server 80
               (contains linpeas, winpeas, pspy, potatoes, PowerUp, nc.exe, agents)

    Serve SMB instead:  impacket-smbserver share ~/www -smb2support
    Some GitHub version tags change — if a download 404'd above, grab the
    current release manually from that project's Releases page.
EOF
