#--*-- coding:UTF-8 --*--
from ctypes import *
from mis_definiciones_debugger_final import *

kernel32=windll.kernel32

class debugger():
      def __init__(self):
          self.h_process=None
          self.pid=None
          self.debugger_active=False

      def load(self,path_to_exe):
          creation_flags=DEBUG_PROCESS
          startupinfo=STARTUPINFO()
          process_information=PROCESS_INFORMATION()

          startupinfo.dwFlags=0x1
          startupinfo.wShowWindows=0x0
          startupinfo.cb=sizeof(startupinfo)

          if kernel32.CreateProcessA(path_to_exe,None,None,None,None,creation_flags,None,None,byref(startupinfo),byref(process_information)):
             print u"[*] ¡Hemos ejecutado el proceso!"
             print "[*] PID: %d"%process_information.dwProcessId
          else:
               print "[*] Error: 0x%08x." % kernel32.GetLastError()
          print u"[*] Hemos asociado el proceso con éxito"
          print "[*] PID: %d"% process_information.dwProcessId
          self.h_process=self.open_process(process_information.dwProcessId)

      def open_process(self,pid):
          h_process=kernel32.OpenProcess(PROCESS_ALL_ACCESS,pid,False)
          return h_process

      def attach(self,pid):
          self.h_process=self.open_process(pid)
          if kernel32.DebugActiveProcess(pid):
             self.debugger_active=True
             self.pid=int(pid)
             self.run()
          else:
               print "[*] imposible asociar el proceso"

      def run(self):
          while self.debugger_active==True:
               self.get_debug_event()

      def get_debug_event(self):
           debug_event=DEBUG_EVENT()
           continue_status=DBG_CONTINUE
           if kernel32.WaitForDebugEvent(byref(debug_event),INFINITE):
              raw_input("Pulse una tecla para continuar...")
              self.debugger_active=False
              kernel32.ContinueDebugEvent(debug_event.dwProcessId,debug_event.dwThreadId,continue_status)

      def detach(self):
           if kernel32.DebugActiveProcessStop(self.pid):
              print "[*] depurado terminado. salida..."
              return True
           else:
                print "Ha ocurrido un error"
                return False