prescript.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. if __name__ == "__main__":
  2. import ctypes
  3. import os
  4. import sys
  5. import json
  6. import typing
  7. import time
  8. import traceback
  9. import jinja2
  10. import re
  11. if len(sys.argv) != 4:
  12. sys.exit(-1)
  13. lib = ctypes.CDLL("/tmp/sandbox-python/python.so")
  14. module = sys.argv[1]
  15. code = open(module).read()
  16. with open("/tmp/1.txt", "w") as f:
  17. f.write(code)
  18. def sandbox(uid, gid):
  19. lib.DifySeccomp.argtypes = [ctypes.c_uint32, ctypes.c_uint32]
  20. lib.DifySeccomp.restype = None
  21. lib.DifySeccomp(uid, gid)
  22. uid = int(sys.argv[2])
  23. gid = int(sys.argv[3])
  24. if not uid or not gid:
  25. sys.exit(-1)
  26. options = json.loads(sys.argv[4])
  27. sandbox(uid, gid, options.get("enable_network", False))
  28. # setup sys.excepthook
  29. def excepthook(type, value, tb):
  30. sys.stderr.write("".join(traceback.format_exception(type, value, tb)))
  31. sys.stderr.flush()
  32. sys.exit(-1)
  33. sys.excepthook = excepthook
  34. exec(code)