예제 #1
0
파일: pydbgx.py 프로젝트: z3r0zh0u/pydbgx
class DebugClient:
    """IDebugClient Wrapper"""
    
    def __init__(self):
        """DebugClient initialization"""

        self.__debug_create()


    def __debug_create(self):
        """create IDebugClient"""
        
        self.__idebug_client = POINTER(DbgEng.IDebugClient)()
        hr = DebugCreate(byref(DbgEng.IDebugClient._iid_), byref(self.__idebug_client))
        if S_OK != hr:
            raise Exception('DebugCreate() fail.')
        else:
            logger.debug('[D] DebugClient: ' + str(self.__idebug_client))

    def query_interface(self, interface):
        """IDebugClient::QueryInterface method"""

        return self.__idebug_client.QueryInterface(interface)


    def get_indentity(self):
        """IDebugClient::GetIdentity method"""

        buffer_size = 0x100
        identity_size = c_ulong(0)
        hr = self.__idebug_client._IDebugClient__com_GetIdentity(None, buffer_size, byref(identity_size))
        if S_OK != hr:
            raise Exception('GetIdentity() fail.')

        buffer_size = identity_size.value + 1
        buffer = create_string_buffer(buffer_size)
        hr = self.__idebug_client._IDebugClient__com_GetIdentity(buffer, buffer_size, byref(identity_size))
        if S_OK != hr:
            raise Exception('GetIdentity() fail.')
        
        return buffer.value

    def set_event_callbacks(self, event_callbacks):
        """IDebugClient::SetEventCallbacks method"""

        hr = self.__idebug_client.SetEventCallbacks(event_callbacks)
        if S_OK != hr:
            raise Exception('SetEventCallbacks() fail.')

    def get_event_callbacks(self):
        """IDebugClient::GetEventCallbacks method"""

        return self.__idebug_client.GetEventCallbacks()

    def set_output_callbacks(self, output_callbacks):
        """IDebugClient::SetOutputCallbacks method"""

        hr = self.__idebug_client.SetOutputCallbacks(output_callbacks)
        if S_OK != hr:
            raise Exception('SetOutputCallbacks() fail.')

    def get_output_callbacks(self):
        """IDebugClient::GetOutputCallbacks method"""

        return self.__idebug_client.GetOutputCallbacks()

    def get_running_process_ids(self):
        """IDebugClient::GetRunningProcessSystemIds method"""

        server = 0
        count = 256
        ids = (c_ulong * count)()
        actual_count = c_ulong(0)
        hr = self.__idebug_client._IDebugClient__com_GetRunningProcessSystemIds(server, ids, count, byref(actual_count))
        if S_OK != hr:
            raise Exception('GetRunningProcessSystemIds() fail.')

        return ids, actual_count.value

    def get_running_process_desc(self, sysid):
        """IDebugClient::GetRunningProcessDescription method"""

        try:
            server = 0
            flags  = DbgEng.DEBUG_PROC_DESC_NO_PATHS
            exename_size = 0x100
            exename = create_string_buffer(exename_size)
            actual_exename_size = c_ulong(0)
            description_size = 0x100
            description = create_string_buffer(description_size)
            actual_description_size = c_ulong(0)
                
            hr = self.__idebug_client._IDebugClient__com_GetRunningProcessDescription(
                server, sysid, flags,
                exename, exename_size, byref(actual_exename_size),
                description, description_size, byref(actual_description_size))
                
            if S_OK != hr:
                if S_FALSE == hr:
                    exename_size = actual_exename_size.value + 1
                    exename = create_string_buffer(exename_size)
                    description_size = actual_description_size.value + 1
                    description = create_string_buffer(description_size)
                        
                    hr = self.__idebug_client._IDebugClient__com_GetRunningProcessDescription(
                        server, sysid, flags,
                        exename, exename_size, byref(actual_exename_size),
                        description, description_size, byref(actual_description_size))
                    if S_OK != hr:
                        raise Exception('GetRunningProcessDescription() fail.')
                else:
                    raise Exception('GetRunningProcessDescription() fail.')
                    
        except COMError, msg:
            print 'No enough privilege to retrieve process information.'

        return exename.value, description.value