Advertisement
virtualminds

Untitled

Dec 19th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1.     def inject(self, dll=None, apc=False):
  2.         """Cuckoo DLL injection.
  3.        @param dll: Cuckoo DLL path.
  4.        @param apc: APC use.
  5.        """
  6.         if self.pid == 0:
  7.             log.warning("No valid pid specified, injection aborted")
  8.             return False
  9.  
  10.         if not self.is_alive():
  11.             log.warning("The process with pid %s is not alive, "
  12.                         "injection aborted", self.pid)
  13.             return False
  14.  
  15.         if not dll:
  16.             dll = "cuckoomon.dll"
  17.  
  18.         dll = randomize_dll(os.path.join("dll", dll))
  19.  
  20.         if not dll or not os.path.exists(dll):
  21.             log.warning("No valid DLL specified to be injected in process "
  22.                         "with pid %d, injection aborted", self.pid)
  23.             return False
  24.  
  25.         arg = KERNEL32.VirtualAllocEx(self.h_process,
  26.                                       None,
  27.                                       len(dll) + 1,
  28.                                       MEM_RESERVE | MEM_COMMIT,
  29.                                       PAGE_READWRITE)
  30.  
  31.         if not arg:
  32.             log.error("VirtualAllocEx failed when injecting process with "
  33.                       "pid %d, injection aborted (Error: %s)",
  34.                       self.pid, get_error_string(KERNEL32.GetLastError()))
  35.             return False
  36.  
  37.         bytes_written = c_int(0)
  38.         if not KERNEL32.WriteProcessMemory(self.h_process,
  39.                                            arg,
  40.                                            dll + "\x00",
  41.                                            len(dll) + 1,
  42.                                            byref(bytes_written)):
  43.             log.error("WriteProcessMemory failed when injecting process with "
  44.                       "pid %d, injection aborted (Error: %s)",
  45.                       self.pid, get_error_string(KERNEL32.GetLastError()))
  46.             return False
  47.  
  48.         kernel32_handle = KERNEL32.GetModuleHandleA("kernel32.dll")
  49.         load_library = KERNEL32.GetProcAddress(kernel32_handle, "LoadLibraryA")
  50.  
  51.         config_path = os.path.join(os.getenv("TEMP"), "%s.ini" % self.pid)
  52.         with open(config_path, "w") as config:
  53.             cfg = Config("analysis.conf")
  54.  
  55.             # The first time we come up with a random startup-time.
  56.             if Process.first_process:
  57.                 # This adds 1 up to 30 times of 20 minutes to the startup
  58.                 # time of the process, therefore bypassing anti-vm checks
  59.                 # which check whether the VM has only been up for <10 minutes.
  60.                 Process.startup_time = random.randint(1, 30) * 20 * 60 * 1000
  61.  
  62.             config.write("host-ip={0}\n".format(cfg.ip))
  63.             config.write("host-port={0}\n".format(cfg.port))
  64.             config.write("pipe={0}\n".format(PIPE))
  65.             config.write("results={0}\n".format(PATHS["root"]))
  66.             config.write("analyzer={0}\n".format(os.getcwd()))
  67.             config.write("first-process={0}\n".format(Process.first_process))
  68.             config.write("startup-time={0}\n".format(Process.startup_time))
  69.  
  70.             Process.first_process = False
  71.  
  72.         if apc or self.suspended:
  73.             log.info("Using QueueUserAPC injection")
  74.             if not self.h_thread:
  75.                 log.info("No valid thread handle specified for injecting "
  76.                          "process with pid %d, injection aborted", self.pid)
  77.                 return False
  78.  
  79.             if not KERNEL32.QueueUserAPC(load_library, self.h_thread, arg):
  80.                 log.error("QueueUserAPC failed when injecting process with "
  81.                           "pid %d (Error: %s)",
  82.                           self.pid, get_error_string(KERNEL32.GetLastError()))
  83.                 return False
  84.             log.info("Successfully injected process with pid %d" % self.pid)
  85.         else:
  86.             event_name = "CuckooEvent%d" % self.pid
  87.             self.event_handle = KERNEL32.CreateEventA(None,
  88.                                                       False,
  89.                                                       False,
  90.                                                       event_name)
  91.             if not self.event_handle:
  92.                 log.warning("Unable to create notify event..")
  93.                 return False
  94.  
  95.             log.info("Using CreateRemoteThread injection")
  96.             new_thread_id = c_ulong(0)
  97.             thread_handle = KERNEL32.CreateRemoteThread(self.h_process,
  98.                                                         None,
  99.                                                         0,
  100.                                                         load_library,
  101.                                                         arg,
  102.                                                         0,
  103.                                                         byref(new_thread_id))
  104.             if not thread_handle:
  105.                 log.error("CreateRemoteThread failed when injecting process "
  106.                           "with pid %d (Error: %s)",
  107.                           self.pid, get_error_string(KERNEL32.GetLastError()))
  108.                 KERNEL32.CloseHandle(self.event_handle)
  109.                 self.event_handle = None
  110.                 return False
  111.             else:
  112.                 KERNEL32.CloseHandle(thread_handle)
  113.  
  114.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement