parent_dropbear_map.py 851 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import time
  5. import psutil
  6. from pathlib import Path
  7. want_name = "dropbear"
  8. # Walks up the parent process tree, prints a r-xp line of /proc/pid/maps when
  9. # it finds the wanted name
  10. def main():
  11. try:
  12. for p in psutil.Process().parents():
  13. print(p.pid, file=sys.stderr)
  14. print(p.name(), file=sys.stderr)
  15. print(p.cmdline(), file=sys.stderr)
  16. if want_name in p.name():
  17. with (Path('/proc') / str(p.pid) / "maps").open() as f:
  18. for i, l in enumerate(f, 1):
  19. if ' r-xp ' in l:
  20. print(l.rstrip())
  21. break
  22. return
  23. raise RuntimeError(f"Couldn't find parent {want_name} process")
  24. except Exception as e:
  25. print(psutil.Process().parents())
  26. for p in psutil.Process().parents():
  27. print(p.name())
  28. print(e)
  29. # time.sleep(100)
  30. raise
  31. if __name__ == "__main__":
  32. main()