Skip to content

Instantly share code, notes, and snippets.

@jasonmc
Created May 11, 2026 20:54
Show Gist options
  • Select an option

  • Save jasonmc/8d8e73a7634754c2fe2d82a1fc8c433b to your computer and use it in GitHub Desktop.

Select an option

Save jasonmc/8d8e73a7634754c2fe2d82a1fc8c433b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import marshal
import sys
WATCHED = {"compile", "code.__new__"}
def audit_hook(event, args):
if event in WATCHED:
raise RuntimeError(f"blocked {event}")
# Simulate attacker-produced code bytes created before the hook/process boundary.
def payload():
print("ATTACKER CODE RAN")
blob = marshal.dumps(payload.__code__)
sys.addaudithook(audit_hook)
code = marshal.loads(blob) # Python 3.10+ emits marshal.loads, not code.__new__
exec(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment