Skip to content

Instantly share code, notes, and snippets.

@bioshazard
Created December 21, 2018 04:57
Show Gist options
  • Save bioshazard/0fa3d92a9305d99d1cfe1e9544e8771c to your computer and use it in GitHub Desktop.
Save bioshazard/0fa3d92a9305d99d1cfe1e9544e8771c to your computer and use it in GitHub Desktop.
import hug, threading, subprocess, uuid
"""
# Requires,
yum install -y rh-python36-python rh-python36-python-pip
/opt/rh/rh-python36/root/bin/pip3 install hug
# Run with,
/opt/rh/rh-python36/root/bin/hug -f straceinject.py -ho 127.0.0.1 -p 1337
"""
uuidPidMap = { }
def doStrace(uniqId, pid, filename):
cmd = "/usr/bin/strace -T -t -f -v -s4096 -p%d -o /tmp/straceinject_%s.k" % (pid, filename)
proc = subprocess.Popen(cmd.split(" "), stderr=subprocess.PIPE)
# Keep track of the PID to kill it later when we are done tracing
uuidPidMap[uniqId] = proc
@hug.get('/strace/inject/{pid}/{filename}')
def inject(pid: int, filename: str):
# Generate unique identifier
uniqId = str(uuid.uuid4())
# Start a thread to run strace
t = threading.Thread(target=doStrace, args=(uniqId, pid, filename,))
t.start()
return { "uuid": uniqId }
@hug.get('/strace/kill/{targetId}')
def kill(targetId: str):
print(uuidPidMap)
procToKill = uuidPidMap.pop(targetId, None)
if procToKill:
procToKill.kill()
# Clean up zombie process
procToKill.communicate()
return { "msg": "Killed trace %s" % targetId }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment