Advertisement
tunz

attach.py

Dec 12th, 2013
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # tunz
  2. # cwhan.tunz |at| gmail.com
  3. # 2013. 11. 27
  4.  
  5. import os
  6. import sys
  7.  
  8. if os.getuid() != 0:
  9.         print "[-] You are not root"
  10.         sys.exit(-1)
  11.  
  12. if len(sys.argv) < 2:
  13.         print "python attach.py [binary name] [script]"
  14.  
  15. output = os.popen('ps aux | grep '+sys.argv[1]).read()
  16.  
  17. outputs = output.split('\n')
  18. lastline = ""
  19. i=len(outputs)-2
  20. while i>= 0:
  21.         try:
  22.                 pid = outputs[i].split(None)[1]
  23.         except:
  24.                 i -= 1
  25.                 continue
  26.         if int(pid) < os.getpid():
  27.                 lastline = outputs[i]
  28.                 break
  29.         i -= 1
  30.  
  31. pid = lastline.split(None)[1]
  32.  
  33. print "[*] Attach to "+ ' '.join(lastline.split(None)[-2:]) +" ("+pid+")"
  34.  
  35. if len(sys.argv) == 3:
  36.         print "gdb -q --pid="+pid+" -x "+sys.argv[2]
  37.         os.system("gdb -q --pid="+pid+" -x "+sys.argv[2])
  38. else:
  39.         print "gdb -q --pid="+pid
  40.         os.system("gdb -q --pid="+pid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement