feat(cli): split lab lifecycle from exploitation, add --url/--path targets, socket readiness probe
This commit is contained in:
+38
-23
@@ -1,15 +1,17 @@
|
||||
import os
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
INIT = ("cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini\n"
|
||||
"chown -R www-data:www-data /out 2>/dev/null || true\n"
|
||||
"exec apache2-foreground")
|
||||
|
||||
INI_KEYS = {"display_errors", "memory_limit", "post_max_size", "output_buffering",
|
||||
"max_file_uploads", "max_input_vars", "session.upload_progress.enabled",
|
||||
"session.save_handler", "session.serialize_handler"}
|
||||
|
||||
|
||||
class LabError(Exception):
|
||||
pass
|
||||
@@ -57,22 +59,39 @@ def run(args, timeout=300, check=True):
|
||||
return proc
|
||||
|
||||
|
||||
def executable(root, name):
|
||||
def executable(argv, name):
|
||||
exe = docker()
|
||||
cmd = [exe, "exec", name] + root
|
||||
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
|
||||
proc = subprocess.run([exe, "exec", name] + argv,
|
||||
capture_output=True, text=True, timeout=120)
|
||||
return proc.stdout
|
||||
|
||||
|
||||
def wait_ready(port, host="127.0.0.1", attempts=60, delay=0.5):
|
||||
url = "http://%s:%d/index.php" % (host, port)
|
||||
def _http_get(host, port, path="/index.php", timeout=1.0):
|
||||
sock = socket.create_connection((host, port), timeout=timeout)
|
||||
try:
|
||||
request = ("GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, host)).encode()
|
||||
sock.sendall(request)
|
||||
data = b""
|
||||
while b"\r\n\r\n" not in data:
|
||||
chunk = sock.recv(1024)
|
||||
if not chunk:
|
||||
break
|
||||
data += chunk
|
||||
finally:
|
||||
sock.close()
|
||||
head, _, body = data.partition(b"\r\n\r\n")
|
||||
return head, body
|
||||
|
||||
|
||||
def wait_ready(port, host="127.0.0.1", attempts=120, delay=0.25):
|
||||
for _ in range(attempts):
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=2) as resp:
|
||||
body = resp.read().decode("latin1").strip()
|
||||
return body
|
||||
except (urllib.error.URLError, OSError):
|
||||
time.sleep(delay)
|
||||
head, body = _http_get(host, port)
|
||||
if b" 200" in head:
|
||||
return body.decode("latin1").strip()
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(delay)
|
||||
return None
|
||||
|
||||
|
||||
@@ -85,7 +104,8 @@ def up(root, image, name, port, net="", host="127.0.0.1"):
|
||||
body = wait_ready(port, host)
|
||||
if body is None:
|
||||
logs = run(["logs", "--tail", "20", name], check=False)
|
||||
raise LabError("target did not become ready on port %d\n%s" % (port, logs.stdout + logs.stderr))
|
||||
raise LabError("target did not answer on %s:%d within %.0fs\n%s"
|
||||
% (host, port, 120 * 0.25, (logs.stdout or "") + (logs.stderr or "")))
|
||||
return body
|
||||
|
||||
|
||||
@@ -94,23 +114,18 @@ def down(name):
|
||||
return True
|
||||
|
||||
|
||||
def status(root, name, lib_path):
|
||||
def status(name, lib_path):
|
||||
rows = []
|
||||
ver = executable(["php", "-v"], name).splitlines()
|
||||
rows.append(("php", ver[0] if ver else "?"))
|
||||
info = executable(["php", "-i"], name)
|
||||
keep = {"display_errors", "memory_limit", "post_max_size", "output_buffering",
|
||||
"max_file_uploads", "max_input_vars", "session.upload_progress.enabled",
|
||||
"session.save_handler", "session.serialize_handler"}
|
||||
for line in info.splitlines():
|
||||
for line in executable(["php", "-i"], name).splitlines():
|
||||
key, _, val = line.partition("=>")
|
||||
key = key.strip()
|
||||
if key in keep:
|
||||
if key in INI_KEYS:
|
||||
rows.append((key, val.split("=>")[0].strip()))
|
||||
sha = executable(["sha256sum", lib_path], name).split()
|
||||
rows.append(("libphp_sha256", sha[0] if sha else "?"))
|
||||
apache = executable(["apache2", "-v"], name).splitlines()
|
||||
rows.append(("apache", apache[0] if apache else "?"))
|
||||
workers = executable(["sh", "-c", "pgrep -c apache2"], name).strip()
|
||||
rows.append(("workers", workers))
|
||||
rows.append(("workers", executable(["sh", "-c", "pgrep -c apache2"], name).strip()))
|
||||
return rows
|
||||
|
||||
Reference in New Issue
Block a user